DocsCircuits

DocsReference26 of 33 · 4 min read

Circuits

The two circuits behind Liora's proofs, their one public input, and how the program computes it.

Liora has two circuits, written in Circom over the BN254 scalar field and proved with Groth16.

CircuitUsed byConstraintsPublic inputs
paypay28,9791
withdrawwithdraw8,4281

Each has a single public value, the statement. The prover computes it inside the circuit from private inputs. The program computes it again from the instruction, the accounts and the configuration, and verifies the proof against its own result.

H below is circomlib's Poseidon at the arity shown. For a curve point X, ph(X) = H(y, sign), where y and sign are the two parts of the packed point: the y coordinate, and the bit that says which x goes with it.

#The transaction binding

Both circuits start from a SHA-256 digest of everything the transfer does in public. The program hashes these in order:

FieldEncoding
PurposeLiora/authorize/v1 as UTF-8
ActionOne byte: 1 for a payment, 2 for a withdrawal
Program address, configuration address32 bytes each
Auditor key32 bytes, packed
Fee recipient, relayer, destination32 bytes each. For a payment the destination is the relayer
Protocol fee, relayer fee, public amountThree little-endian u64 in lamports. The amount is zero for a payment
The SPP instructionA little-endian u32 length, then the exact bytes forwarded, SPP's own proof included
The accounts of the SPP callA little-endian u16 count, then each 32-byte address in order

The digest is split in two 16-byte halves, hi and lo, each read as a big-endian number.

Because the SPP instruction is inside the digest, the Rings proof is made first and the Liora proof second.

#The owners

Each transfer names 1 to 3 owner keys, the public keys that own the notes being spent. For each, the circuit takes the secret key as a private input, checks that it is non-zero and below the subgroup order, and multiplies the generator by it.

ownerSet = H(ownerCount, ph(owner0), ph(owner1) or 0, ph(owner2) or 0)

Change returns to the first owner. To make sure that owner really holds a note in this transfer, the circuit opens the first note being spent and recomputes its nullifier:

nullifierPk = H(nullifierSecret)
owner       = H(firstOwnerIdentity, nullifierPk)
ownerUtxo   = H(owner, blinding)
utxo        = H(3, treeId, H(0, 0), amount, dataHash, H(0, 0), ownerUtxo)
nullifier   = H(utxo, blinding, nullifierSecret)

firstOwnerIdentity is the Ring's identity hash of the first owner address, which the program computes from the account it checked. The nullifier must equal the first nullifier in the forwarded SPP instruction. The two H(0, 0) are SOL's asset commitment and the Default Ring's ring field.

tx = H(hi, lo, sppPrivateTxHash, ownerSet, firstOwnerIdentity, nullifier)

For the withdraw circuit, tx is the statement.

#The payment statement

The pay circuit adds the recipient. Its private inputs are the recipient's S and V, the auditor's A, the leaf index with its 24 siblings, and the two secrets r and k.

leaf  = H(S.x, S.y, V.x, V.y)
root  = the whitelist path from leaf, by the bits of the index

Q     = rV
R     = rG
P     = S + H(Q.x, Q.y)·G
c1    = kG
c2    = S + kA
tag   = low 8 bits of H(1, Q.x, Q.y)

announcement = H(root, ph(P), ph(R), ph(c1), ph(c2), ph(A), tag)
statement    = H(tx, announcement)

The circuit also checks that S, V and A are in the prime-order subgroup, and that neither P nor c2 is the identity.

The program builds announcement from the announcement in the instruction, the auditor key in the configuration and a root it accepts. The proof therefore holds only if the announced P, R, c1, c2 and tag were all computed from one whitelisted (S, V), under the auditor key in force.

#The proof on the wire

A proof is 128 bytes:

OffsetBytesContent
032-A, a compressed G1 point
3264B, a compressed G2 point
9632C, a compressed G1 point

Each compressed x coordinate is big-endian with the sign flag in bit 7 of its first byte. A is negated before compression, as the verifier's pairing equation takes it. The program decompresses the three points and verifies them through Solana's alt_bn128 syscalls.

#Proving

Proving runs on the sender's device with snarkjs, from two files per circuit: a witness calculator (.wasm) and a proving key (.zkey). The times are on Measured figures.