Spring
WhitepaperGitHubLaunch app

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

  1. Rebuild the state tree from Deposited, Withdrawn and Ragequit events in leaf order, and take a path to your commitment.
  2. 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.
  3. Compute context = hash_to_field(3, scope, processor, recipient, fee_recipient, relayer_fee_le).
  4. 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

NameFormula
scopehash_to_field(1, program_id, pool_pda)
labelhash_to_field(2, scope, depositor, nonce_le)
withdraw contexthash_to_field(3, scope, processor, recipient, fee_recipient, relayer_fee_le)
ragequit contexthash_to_field(4, scope, depositor, depositor, depositor, 0)
commitmentPoseidon(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.