# Cryptographic Schemes

`PoseidonHash`

Function

Poseidon is a circuit friendly permutation hash function described in the paper GKRRS2019.

Parameter | Setting |
---|---|

S-box | $x→x_{5}$ |

Full rounds | 8 |

Partial rounds | 56 |

Our usage matches that of the halo2 library. Namely using a sponge configuration with addition which defines the function $PoseidonHash:F_{p}×⋯×F_{p}→F_{p}$

## Bulla Commitments

Given an abstract hash function such as `PoseidonHash`

,
we use a variant of the commit-and-reveal scheme to define anonymized
representations of objects on chain. Contracts then operate with these anonymous
representations which we call bullas.

Let $Params∈F_{p}$ represent object parameters, then we can define $Bulla:F_{p}×F_{p}→F_{p}$ $Bulla(Params,b)=PoseidonHash(Params,b)$ where $b∈F_{p}$ is a random blinding factor.

Then the bulla (on chain anonymized representation) can be used in contracts with ZK proofs to construct statements on $Params$.

## Pallas and Vesta

DarkFi uses the elliptic curves Pallas and Vesta that form a 2-cycle. We denote Pallas by $ₚ$ and Vesta by $ᵥ$. Set the following values:

$p=0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001$ $q=0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001$

We now construct the base field for each curve $K_{p}$ and $K_{v}$ as $K_{p}=F_{p}$ and $K_{v}=F_{q}$. Let $f=y_{2}−(x_{2}+5)∈Z[x,y]$ be the Weierstrauss normal form of an elliptic curve. We define $f_{p}=fmodK_{p}$ and $f_{v}=fmodK_{v}$. Then we instantiate Pallas as $E_{p}=V(f_{p})$ and $E_{v}=V(f_{v})$. Now we note the 2-cycle behaviour as

$#V(f_{p})=q$ $#V(f_{v})=p$

An additional projective point at infinity $∞$ is added to the curve.

Let $P_{p}$ be the group of points with $∞$ on $E_{p}$.

Let $P_{v}$ be the group of points with $∞$ on $E_{v}$.

Arithmetic is mainly done in circuits with $F_{p}$ and $E_{p}$.

### Coordinate Extractor for Pallas

Let $P_{p},∞,F_{p}$ be defined as above.

Define $X:P_{p}→F_{p}$ such that $X(∞_{E_{p}})=0$ $X((x,y))=x$ $Y(∞_{E_{p}})=0$ $Y((x,y))=y$

**Note:** There is no $P=(0,y)∈E_{p}$ so $X(P)=0⟹P=∞$.
Likewise there is no $P=(x,0)∈E_{p}$ so $Y(P)=0⟹P=∞$.

### Hashing to $F_{p}$

Define $B_{64}2F_{p}:B_{64}→F_{p}$ as the matching decoding of $F_{p}$ modulo the canonical class in little endian byte format.

Let there by a uniform hash function $h:X→[0,r)$ with $r=p$, and a map $σ:[0,r)→[0,p)$ converting to the canonical representation of the class in $Z/⟨p⟩$.

Let $s=σ∘h$ be the composition of functions, then $s$ has a non-uniform range. However increasing the size of $r$ relative to $p$ diminises the statistical significance of any overlap. For this reason we define the conversion from $B_{64}$ for hash functions.

### PubKey Derivation

Let $G_{N}∈P_{p}$ be the constant `NULLIFIER_K`

defined in
`src/sdk/src/crypto/constants/fixed_bases/nullifier_k.rs`

.
Since the scalar field of $P_{p}$ is prime, all points in the group except
the identity are generators.

We declare the function $Lift_{q}(x):F_{p}→F_{v}$. This map is injective since ${0,p−1}⊂{0,q−1}$.

Define the function $DerivePubKey:F_{p}→P_{p}$ $DerivePubKey(x)=Lift_{q}(x)G_{N}$

### Point Serialization

The maximum value of $F_{p}$ fits within 255 bits, with the last bit of $B_{32}$ being unused. We use this bit to store the sign of the $y$-coordinate.

We compute the sign of $y=Y(P)$ for $P∈P_{p}$ by dividing $F_{p}$ into even and odd sets. Let $sgn(y)=ymod2$.

We define $P_{p}2B_{32}:P_{p}→B_{32}$ as follows. Let $P∈P_{p}$, then

$P_{p}2B_{32}={N2B_{32}(0)N2B_{32}(X(P)+2_{255}sgn(Y(P)) ifP=∞otherwise $

**Security note:** apart from the case when $P=∞$, this function is mostly
constant time. In cases such as key agreement, where constant time decryption
is desirable and $P=∞$ is mostly guaranteed, this provides a strong
approximation.

## Group Hash

Let $GroupHash:B_{∗}×B_{∗}→P_{p}$ be the hash to curve function defined in ZCash Protocol Spec, section 5.4.9.8. The first input element acts as the domain separator to distinguish uses of the group hash for different purposes, while the second input is the actual message.

The main components are:

- An isogeny map $iso_map_{G}:iso-G→G$ which is a group homomorphism from $P_{p}$ to a curve $iso-P_{p}$ with $a_{iso-P_{p}},b_{iso-P_{p}}=0$ which is required by the group hash. See IETF: Simplified SWU for AB == 0.
`hash_to_field`

implementation which maps a byte array to the scalar field $F_{q}$.`map_to_curve_simple_swu(u)`

which maps $u∈F_{q}$ to a curve point $iso-P_{p}$.

Then $GroupHash(D,M)$ is calculated as follows:

Let $DST=D∣∣"-pallas_XMD:BLAKE2b_SSWU_RO_"$

Assert $len(DST)≤255$

Let $(u_{1},u_{2})=hash_to_field(M,DST)$

For $i∈[2]$

Let $Q_{i}=map_to_curve_simple_swu(u_{i})$

Return $iso_map_{P_{p}}(Q_{1}+Q_{2})$

## BLAKE2b Hash Function

BLAKE2 is defined by ANWW2013. Define the BLAKE2b variant as $BLAKE2b_{n}:B_{∗}→B_{n}$

## Homomorphic Pedersen Commitments

Let $GroupHash$ be defined as in Group Hash.

Let $Lift_{q}$ be defined as in Pubkey Derivation.

When instantiating value commitments, we require the homomorphic property.

Define: $G_{V}=GroupHash("z.cash:Orchard-cv","v")$ $G_{B}=GroupHash("z.cash:Orchard-cv","r")$ $PedersenCommit:F_{p}×F_{v}→P_{p}$ $PedersenCommit(v,b)=Lift_{q}(v)G_{V}+bG_{B}$

This scheme is a computationally binding and perfectly hiding commitment scheme.

## Incremental Merkle Tree

Let $ℓ_{M}=32$ be the merkle depth.

The incremental merkle tree is fixed depth of $ℓ_{M}$ used to store $F_{p}$ items. It is an append-only set for which items can be proved to be inside within ZK. The root value is a commitment to the entire tree.

Denote combining two nodes to produce a parent by the operator $⊕:F_{p}×F_{p}→F_{p}$. Denote by $⊕_{b}$ where $b∈Z_{2}$, the function which swaps both arguments before calling $⊕$, that is $⊕_{b}(X_{1},X_{2})={⊕(X_{1},X_{2})⊕(X_{1},X_{2}) ifb=0ifb=1 $

We correspondingly define the types $MerklePos=Z_{2}$ $MerklePath=F_{p}$ and a function to calculate the root given a leaf, its position and the path, $MerkleRoot:MerklePos×MerklePath×F_{p}→F_{p}$ $MerkleRoot(p,Π,B)=⊕_{p_{ℓ}}(…,⊕_{p_{2}}(π_{2},⊕_{p_{1}}(π_{1},B))…)$

## Symmetric Encryption

Let $Sym$ be an *authenticated one-time symmetric encryption scheme*
instantiated as AEAD_CHACHA20_POLY1305 from RFC 7539.
We use a nonce of $N2B_{12}(0)$ with a 32-byte key.

Let $K=B_{32}$ represent keys, $N=B_{∗}$ for plaintext data and $C=B_{∗}$ for ciphertexts.

$Sym.Encrypt:K×N→C$ is the encryption algorithm.

$Sym.Decrypt:K×C→N∪{⊥}$ is the decryption algorithm, such that for any $k∈K$ and $p∈P$, we have $Sym.Decrypt(k,Sym.Encrypt(k,p))=p$ we use $⊥$ to represent the decryption of an invalid ciphertext.

**Security requirement:** $Sym$ must be *one-time* secure. One-time here
means that an honest protocol participant will almost surely encrypt only
one message with a given key; however, the adversary may make many adaptive
chosen ciphertext queries for a given key.

## Key Agreement

Let $F_{p},P_{p},Lift_{q}$ be defined as in the section Pallas and Vesta.

A *key agreement scheme* is a cryptographic protocol in which two parties
agree on a shared secret, each using their *private key* and the other
party's *public key*.

Let $KeyAgree:F_{p}×P_{p}→P_{p}$ be defined as $KeyAgree(x,P)=Lift_{q}(x)P$.

## Key Derivation

Let $BLAKE2b_{n}$ be defined as in the section BLAKE2b Hash Function.

Let $P_{p},P_{p}2B_{32}$ be defined as in the section Pallas and Vesta.

A *Key Derivation Function* is defined for a particular *key agreement scheme*
and *authenticated one-time symmetric encryption scheme*; it takes the
shared secret produced by the key agreement and additional arguments, and
derives a key suitable for the encryption scheme.

$KDF$ takes as input the shared Diffie-Hellman secret $x$ and the
*ephemeral public key* $EPK$. It derives keys for use with
$Sym.Encrypt$.
$KDF:P_{p}×P_{p}→B_{32}$
$KDF(P,EPK)=BLAKE2b_{32}(P_{p}2B_{32}(P)∣∣P_{p}2B_{32}(EPK))$

## In-band Secret Distribution

Let $Sym.Encrypt,Sym.Decrypt$ be defined as in the section Symmetric Encryption.

Let $KeyAgree$ be defined as in the section Key Agreement.

Let $KDF$ be defined as in the section Key Derivation.

Let $F_{p},P_{p},DerivePubKey$ be defined as in the section Pallas and Vesta.

To transmit secrets securely to a recipient *without* requiring an out-of-band
communication channel, we use the key derivation function
together with symmetric encryption.

Denote $AeadEncNote_{n}=(E,C)$ where $E$ is the space of *ephemeral
public keys* and $C$ is the ciphertext space.

See `AeadEncryptedNote`

in `src/sdk/src/crypto/note.rs`

.

### Encryption

We let $P∈P_{p}$ denote the recipient's public key. Let $note∈N=B_{∗}$ denote the plaintext note to be encrypted.

Let $esk∈F_{p}$ be the randomly generated *ephemeral secret key*.

Let $EPK=DerivePubKey(esk)∈P_{p}$

Let $shared_secret=KeyAgree(esk,P)$

Let $k=KDF(shared_secret,EPK)$

Let $c=Sym.Encrypt(k,note)$

Return $c$

### Decryption

We denote the recipient's secret key with $x∈F_{p}$. Let $c∈C=B_{∗}$ denote the ciphertext note to be decrypted.

The recipient receives the *ephemeral public key* $EPK∈P_{p}$ used to decrypt
the ciphertext note $c$.

Let $shared_secret=KeyAgree(x,EPK)$

Let $k=KDF(shared_secret,EPK)$

Let $note=Sym.Decrypt(k,c)$. If $note=⊥$ then return $⊥$, otherwise return $note$.

## Verifiable In-Band Secret Distribution

Let $PoseidonHash$ be defined as in the section PoseidonHash Function.

This scheme is verifiable inside ZK using the Pallas and Vesta curves.

Let $n∈N$. Denote the plaintext space $N_{k}$ and ciphertext $C_{k}$ with $N_{k}=C_{k}=F_{p}$ where $k∈N$.

Denote $ElGamalEncNote_{k}=(E,C_{k})$ where $E$ is the space of *ephemeral
public keys* and $C$ is the ciphertext space.

See `ElGamalEncryptedNote`

in `src/sdk/src/crypto/note.rs`

.

### Encryption

We let $P∈P_{p}$ denote the recipient's public key. Let $n∈N$ denote the plaintext note to be encrypted.

Define $ElGamal.Encrypt:N_{k}×F_{p}×P_{p}→C_{k}×P_{p}$ by $ElGamal.Encrypt(n,P)$ as follows:

Let $esk∈F_{p}$ be the randomly generated *ephemeral secret key*.

Let $EPK=DerivePubKey(esk)∈P_{p}$

Let $shared_secret=KeyAgree(esk,P)$

Let $k=PoseidonHash(X(shared_secret),Y(shared_secret))$

For $i∈[k]$ then compute:

Let $b_{i}=PoseidonHash(k,i)$

Let $c_{i}=note_{i}+b_{i}$

Return $(c,EPK)$ where $c=(c_{i})$

### Decryption

We denote the recipient's secret key with $x∈F_{p}$.
The recipient receives the *ephemeral public key* $EPK∈P_{p}$ used to decrypt
the ciphertext note $c∈C_{k}$.

Define $ElGamal.Decrypt:C_{k}×F_{p}×P_{p}→N_{k}$ by $ElGamal.Decrypt(c,x,EPK)$ as follows:

Let $shared_secret=KeyAgree(x,EPK)$

Let $k=PoseidonHash(X(shared_secret),Y(shared_secret))$

For $i∈[k]$ then compute:

Let $b_{i}=PoseidonHash(k,i)$

Let $n_{i}=c_{i}−b_{i}$

Return $n=(n_{i})$