Docs / Build
Integration
How to build the inputs and call the program from TypeScript with Anchor. Field elements are 32-byte big-endian BN254 scalars and must be below the field modulus.
Deposit
import { buildPoseidon } from "circomlibjs"
import { BN } from "@coral-xyz/anchor"
import { PublicKey, SystemProgram } from "@solana/web3.js"
const poseidon = await buildPoseidon()
const nullifier = randomField()
const secret = randomField()
const precommitment = poseidon.F.toObject(poseidon([nullifier, secret])) // bigint
const [pool] = PublicKey.findProgramAddressSync([Buffer.from("pool")], program.programId)
await program.methods
.deposit(new BN(1_000_000_000), Array.from(toBytes32(precommitment)))
.accounts({ depositor: wallet.publicKey, pool, systemProgram: SystemProgram.programId })
.rpc()
Read the Deposited event for your label, value (after the fee), leaf_index and the new root. You need the label and value to spend the note.
Withdraw
- Rebuild the state tree from
Deposited,WithdrawnandRagequitevents in leaf order, and take a path to your commitment. - Rebuild the provider's set from its published list and take a path to your label. Check that its root equals the provider account's
root. - Compute
context = hash_to_field(3, scope, processor, recipient, fee_recipient, relayer_fee_le). - Prove with snarkjs and the circuit's wasm and zkey.
const { proof, publicSignals } = await snarkjs.groth16.fullProve(input, "withdraw.wasm", "withdraw.zkey")
const proofBytes = encodeProof(proof) // -A, B, C big-endian, G2 limbs as (c1, c0): 256 bytes
await program.methods
.withdraw(Array.from(nullifierHash), {
proof: Array.from(proofBytes),
newCommitment: Array.from(newCommitment),
withdrawnValue: new BN(value),
stateRoot: Array.from(stateRoot),
stateDepth: stateDepth,
aspRoot: Array.from(aspRoot),
aspDepth: aspDepth,
relayerFee: new BN(relayerFee),
})
.accounts({ processor, pool, provider, nullifier, recipient, feeRecipient, systemProgram: SystemProgram.programId })
.rpc()
The nullifier account is the PDA ["nullifier", nullifier_hash], and provider is ["asp", provider_authority].
Hashes
| Name | Formula |
|---|---|
| scope | hash_to_field(1, program_id, pool_pda) |
| label | hash_to_field(2, scope, depositor, nonce_le) |
| withdraw context | hash_to_field(3, scope, processor, recipient, fee_recipient, relayer_fee_le) |
| ragequit context | hash_to_field(4, scope, depositor, depositor, depositor, 0) |
| commitment | Poseidon(value, label, precommitment) |
hash_to_field is keccak over the concatenated bytes, reduced into the field. The tag byte keeps the four hashes in separate domains.