Stellar
A shielded transfer circuit
The Noir circuit behind Veilum and Shielded Token: spend two notes, create two, reveal nothing.
Overview
One circuit handles both private transfers and withdrawals. It proves the spender owns the input notes, that they sit in the pool's Merkle tree, that the nullifiers are correct, and that value is conserved, while amounts, owners and blindings stay private.
The code
packages/circuits/src/main.nrView on GitHub ↗
struct Note { owner: Field, token: Field, amount: Field, blinding: Field }
fn hash_note(note: Note) -> Field {
poseidon2_hash4(note.owner, note.token, note.amount, note.blinding)
}
fn verify_merkle_path(leaf: Field, siblings: [Field; 20], directions: [bool; 20], root: Field) -> bool {
let mut acc = leaf;
for i in 0..20 {
if directions[i] { acc = merkle_parent(siblings[i], acc); }
else { acc = merkle_parent(acc, siblings[i]); }
}
acc == root
}
fn main(spending_key: Field, in_amounts: [Field; 2], /* … */ mode: pub Field /* … */) {
// Ownership is tied to a public key hash derived from spending key.
let owner_pk = poseidon2_hash2(spending_key, 1);
// …
let expected_nf = hash_nullifier(spending_key, input_commitment);
assert((expected_nf - nullifiers[i]) * should_validate_input == 0);
// …
// mode must be binary: {0,1}
assert(mode * (mode - 1) == 0);
// mode=0 transfer: value is conserved into output notes.
assert((in_amounts[0] + in_amounts[1] - (out_amounts[0] + out_amounts[1])) * (1 - mode) == 0);
// mode=1 unshield: partial/full withdrawal with private change output in lane #0.
assert((in_amounts[0] - (unshield_amount + out_amounts[0])) * mode == 0);
}
How it works
- Notes are Poseidon2 hashes of owner, token, amount and blinding; the owner is a hash of the spending key
- Each input is checked against a depth-20 Merkle path, and its nullifier is derived from the spending key and commitment
- A binary mode selects transfer (two outputs, value conserved) or unshield (one private change note plus a public withdrawal)
- Unused lanes are forced to zero with multiplicative constraints, which keeps the circuit branch-free