Evidence
The leak zoo
Eleven pairs of small contracts (22 in all, 10 pairs on Ledger 8 and 1 on Ledger 9), each a leak a careful developer could ship and its fix: votes, salaries, bids, keys, tags, membership claims, a constructor, shielded and unshielded tokens, and a Ledger 9 event. For every pair Tell reports the leak on the left, and an attack test reads the secret back out of public state. 22 attack tests, and auto-fix tests that apply Tell’s verified patches to the five original leaky contracts and run the attacks again, run in CI.
The secret ballot
criticalGuessable hashEach vote is stored as hash(choice). It looks like a commitment. With four candidates, it is a lookup table.
pragma language_version 0.23; import CompactStandardLibrary; export ledger voters: Set<Bytes<32>>;export ledger ballots: Map<Bytes<32>, Bytes<32>>; witness voterKey(): Bytes<32>;witness choice(): Uint<8>; circuit voterTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "ballot:voter"), sk]);} export pure circuit choiceHash(c: Uint<8>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "ballot:choice"), (c as Field) as Bytes<32>]);} export circuit register(): [] { voters.insert(disclose(voterTag(voterKey())));} export circuit vote(): [] { const tag = disclose(voterTag(voterKey())); assert(voters.member(tag), "not a registered voter"); assert(!ballots.member(tag), "already voted"); const c = choice(); assert(c < 4, "there are four candidates"); ballots.insert(tag, disclose(choiceHash(c)));}
pragma language_version 0.23; import CompactStandardLibrary; export ledger voters: Set<Bytes<32>>;export ledger ballots: Map<Bytes<32>, Bytes<32>>; witness voterKey(): Bytes<32>;witness choice(): Uint<8>;witness ballotSalt(): Bytes<32>; circuit voterTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "ballot:voter"), sk]);} export pure circuit choiceHash(c: Uint<8>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "ballot:choice"), (c as Field) as Bytes<32>]);} export circuit register(): [] { voters.insert(disclose(voterTag(voterKey())));} export circuit vote(): [] { const tag = disclose(voterTag(voterKey())); assert(voters.member(tag), "not a registered voter"); assert(!ballots.member(tag), "already voted"); const c = choice(); assert(c < 4, "there are four candidates"); ballots.insert(tag, disclose(persistentCommit<Uint<8>>(c, ballotSalt())));}
- Tell reports
- choice() is hidden behind a persistentHash anyone can reverse: 2 bits once the contract asserts c < 4, at most 4 tries.
- The attack test
- The test casts twelve votes, then hashes the four choices and matches them against the public ballots map: every vote recovered.
- The fix
- persistentCommit(choice, salt) with a 32-byte salt that only the voter holds. The same attack matches nothing.
The salary record
criticalGuessable hashAn employer files hash(employee id, salary) so staff can prove their pay to a lender later. The id is the public map key, so the salary is the only unknown.
pragma language_version 0.23; import CompactStandardLibrary; export ledger employer: Bytes<32>;export ledger records: Map<Uint<32>, Bytes<32>>; witness employerKey(): Bytes<32>;witness salary(): Uint<32>; constructor(owner: Bytes<32>) { employer = disclose(owner);} export pure circuit keyHash(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "payband:employer"), sk]);} export pure circuit recordOf(employee: Uint<32>, pay: Uint<32>): Bytes<32> { return persistentHash<Vector<3, Bytes<32>>>([pad(32, "payband:record"), (employee as Field) as Bytes<32>, (pay as Field) as Bytes<32>]);} export circuit file(employee: Uint<32>): [] { assert(keyHash(employerKey()) == employer, "only the employer files records"); const pay = salary(); assert(pay > 0, "salary must be positive"); records.insert(disclose(employee), disclose(recordOf(employee, pay)));}
pragma language_version 0.23; import CompactStandardLibrary; export struct Payslip { employee: Uint<32>; pay: Uint<32>; } export ledger employer: Bytes<32>;export ledger records: Map<Uint<32>, Bytes<32>>; witness employerKey(): Bytes<32>;witness salary(): Uint<32>;witness slipSalt(): Bytes<32>; constructor(owner: Bytes<32>) { employer = disclose(owner);} export pure circuit keyHash(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "payband:employer"), sk]);} export circuit file(employee: Uint<32>): [] { assert(keyHash(employerKey()) == employer, "only the employer files records"); const pay = salary(); assert(pay > 0, "salary must be positive"); records.insert(disclose(employee), disclose(persistentCommit<Payslip>(Payslip { employee: employee, pay: pay }, slipSalt())));}
- Tell reports
- salary() is hidden behind a persistentHash anyone can reverse; Tell notices the employee id is published in the same call and does not count it as secret.
- The attack test
- The test brute-forces monthly salaries from ₹50 to ₹1,00,000 in ₹50 steps against the public records: all three salaries recovered in under a second.
- The fix
- A commitment to the whole payslip with a random salt handed to the employee with the slip.
The sealed sale
mediumSecret-dependent branchBids are committed properly, but the contract files each bidder under qualified or belowReserve depending on the hidden amount.
pragma language_version 0.23; import CompactStandardLibrary; export ledger reserve: Uint<64>;export ledger bids: Map<Bytes<32>, Bytes<32>>;export ledger qualified: Set<Bytes<32>>;export ledger belowReserve: Set<Bytes<32>>; witness bidderKey(): Bytes<32>;witness amount(): Uint<64>;witness bidSalt(): Bytes<32>; constructor(r: Uint<64>) { reserve = disclose(r);} circuit bidderTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "sale:bidder"), sk]);} export circuit bid(): [] { const tag = disclose(bidderTag(bidderKey())); assert(!bids.member(tag), "one bid per bidder"); const a = amount(); bids.insert(tag, disclose(persistentCommit<Uint<64>>(a, bidSalt()))); if (disclose(a >= reserve)) { qualified.insert(tag); } else { belowReserve.insert(tag); }}
pragma language_version 0.23; import CompactStandardLibrary; export ledger reserve: Uint<64>;export ledger bids: Map<Bytes<32>, Bytes<32>>; witness bidderKey(): Bytes<32>;witness amount(): Uint<64>;witness bidSalt(): Bytes<32>; constructor(r: Uint<64>) { reserve = disclose(r);} circuit bidderTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "sale:bidder"), sk]);} export circuit bid(): [] { const tag = disclose(bidderTag(bidderKey())); assert(!bids.member(tag), "one bid per bidder"); const a = amount(); assert(a >= reserve, "bids below the reserve are not accepted"); bids.insert(tag, disclose(persistentCommit<Uint<64>>(a, bidSalt())));}
- Tell reports
- Whether qualified is touched depends on amount(). The public transcript names the set that was written.
- The attack test
- The test places five bids and reads the two sets: which bidders met the reserve is public.
- The fix
- Assert the reserve instead of branching on it: every accepted bid leaves an identical trace, low bids never land.
The vault
criticalSecret in the clearThe vault remembers its owner by storing the owner’s secret key.
pragma language_version 0.23; import CompactStandardLibrary; export ledger owner: Bytes<32>;export ledger withdrawals: Counter; witness secretKey(): Bytes<32>; export circuit claim(): [] { assert(owner == default<Bytes<32>>, "already claimed"); owner = disclose(secretKey());} export circuit withdraw(): [] { assert(owner == disclose(secretKey()), "not the owner"); withdrawals.increment(1);}
pragma language_version 0.23; import CompactStandardLibrary; export ledger owner: Bytes<32>;export ledger withdrawals: Counter; witness secretKey(): Bytes<32>; export pure circuit publicKey(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "vault:owner"), sk]);} export circuit claim(): [] { assert(owner == default<Bytes<32>>, "already claimed"); owner = disclose(publicKey(secretKey()));} export circuit withdraw(): [] { assert(owner == publicKey(secretKey()), "not the owner"); withdrawals.increment(1);}
- Tell reports
- secretKey() looks like a secret key, and it is written to owner as a readable value.
- The attack test
- The test reads owner from the ledger, uses it as its own secret key, and withdraws.
- The fix
- Store a hash of the key; prove knowledge of the key in the circuit. The thief’s copy of the hash does not work as a key.
The staff board
mediumSame tag on every actionAn anonymous feedback board. Nobody learns who a member is, but every post stores the same author tag.
pragma language_version 0.23; import CompactStandardLibrary; export ledger members: Set<Bytes<32>>;export ledger posts: Map<Uint<32>, Bytes<32>>;export ledger authors: Map<Uint<32>, Bytes<32>>;export ledger count: Counter; witness memberKey(): Bytes<32>; circuit memberTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "board:member"), sk]);} export circuit join(): [] { members.insert(disclose(memberTag(memberKey())));} export circuit post(text: Bytes<32>): [] { const tag = disclose(memberTag(memberKey())); assert(members.member(tag), "members only"); const n = count.read() as Uint<32>; posts.insert(n, disclose(text)); authors.insert(n, tag); count.increment(1);}
pragma language_version 0.23; import CompactStandardLibrary; export ledger members: MerkleTree<10, Bytes<32>>;export ledger posts: Map<Uint<32>, Bytes<32>>;export ledger tags: Set<Bytes<32>>;export ledger count: Counter; witness memberKey(): Bytes<32>;witness memberPath(leaf: Bytes<32>): MerkleTreePath<10, Bytes<32>>; export pure circuit memberLeaf(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "board:member"), sk]);} export circuit join(leaf: Bytes<32>): [] { members.insert(disclose(leaf));} export circuit post(text: Bytes<32>): [] { const sk = memberKey(); const leaf = memberLeaf(sk); const path = memberPath(leaf); assert(path.leaf == leaf, "path is not for this member"); assert(members.checkRoot(disclose(merkleTreePathRoot<10, Bytes<32>>(path))), "members only"); const n = count.read() as Uint<32>; const oneTime = persistentHash<Vector<3, Bytes<32>>>([pad(32, "board:post"), sk, (n as Field) as Bytes<32>]); tags.insert(disclose(oneTime)); posts.insert(n, disclose(text)); count.increment(1);}
- Tell reports
- Every post stores the same tag derived from memberKey(): one identified post unmasks all of that person’s posts.
- The attack test
- Two members write five posts; grouping the public authors map by tag splits them 3 and 2.
- The fix
- A Merkle membership proof plus a one-time tag hash(key, post number) per post. Five posts, five unrelated tags.
The raffle
criticalGuessable hash, in the constructorThe organiser seals the winning number at deploy time as hash(number). There are 10,000 tickets, and the seal sits in the contract’s initial state.
pragma language_version 0.23; import CompactStandardLibrary; export ledger sealedDraw: Bytes<32>;export ledger drawn: Uint<16>;export ledger open: Boolean; witness winningNumber(): Uint<16>; export pure circuit seal(n: Uint<16>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "raffle:seal"), (n as Field) as Bytes<32>]);} constructor() { const n = winningNumber(); assert(n < 10000, "tickets run from 0 to 9999"); sealedDraw = disclose(seal(n)); open = false;} export circuit reveal(n: Uint<16>): [] { assert(!open, "already drawn"); assert(seal(n) == sealedDraw, "that is not the sealed number"); drawn = disclose(n); open = true;}
pragma language_version 0.23; import CompactStandardLibrary; export ledger sealedDraw: Bytes<32>;export ledger drawn: Uint<16>;export ledger open: Boolean; witness winningNumber(): Uint<16>;witness sealSalt(): Bytes<32>; export pure circuit seal(n: Uint<16>, salt: Bytes<32>): Bytes<32> { return persistentCommit<Uint<16>>(n, salt);} constructor() { const n = winningNumber(); assert(n < 10000, "tickets run from 0 to 9999"); sealedDraw = disclose(seal(n, sealSalt())); open = false;} export circuit reveal(n: Uint<16>, salt: Bytes<32>): [] { assert(!open, "already drawn"); assert(seal(n, salt) == sealedDraw, "that is not the sealed number"); drawn = disclose(n); open = true;}
- Tell reports
- The constructor writes a hash whose only unknown is the winning number, about 14 bits. Tell now traces constructors the same way as circuits.
- The attack test
- The test deploys the raffle and hashes every ticket against the initial state: the winning number is read before a single ticket is sold.
- The fix
- Seal the number in a commitment salted with 32 random bytes from the organiser’s private state. The same search finds nothing; the organiser can still open the draw.
The faucet
mediumShielded mint, public nonceA faucet mints a shielded coin to each caller, deriving the coin nonce from a counter in the public ledger. The token and amount are public too.
pragma language_version 0.23; import CompactStandardLibrary; export ledger nonce: Bytes<32>;export ledger claims: Counter; constructor() { nonce = pad(32, "faucet:nonce:0");} export circuit claim(): [] { const n = evolveNonce(claims.read() as Uint<128>, nonce); nonce = n; claims.increment(1); mintShieldedToken(pad(32, "faucet:token"), 100, n, left<ZswapCoinPublicKey, ContractAddress>(ownPublicKey()));}
pragma language_version 0.23; import CompactStandardLibrary; export ledger claims: Counter; witness nonceSeed(): Bytes<32>; export circuit claim(): [] { const n = disclose(evolveNonce(claims.read() as Uint<128>, nonceSeed())); claims.increment(1); mintShieldedToken(pad(32, "faucet:token"), 100, n, left<ZswapCoinPublicKey, ContractAddress>(ownPublicKey()));}
- Tell reports
- With the nonce, colour and value public, the coin commitment’s only unknown is the recipient: one hash per known wallet key says who received each coin.
- The attack test
- The test mints to four claimers and matches every coin commitment against eight known wallet keys: each recipient is named.
- The fix
- Derive the nonce from a 32-byte seed in each claimer’s private state, as Midnight’s shielded token tutorial does. The commitment then hides who received the coin.
The deposit
mediumPrivate amount, paid in the clearBids are sealed in salted commitments, which is right. Then each bidder locks exactly the bid amount in unshielded tokens.
pragma language_version 0.23; import CompactStandardLibrary; export ledger bids: Map<Bytes<32>, Bytes<32>>; witness bidderKey(): Bytes<32>;witness bidAmount(): Uint<64>;witness bidSalt(): Bytes<32>; export pure circuit bidderTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "deposit:bidder"), sk]);} export circuit bid(): [] { const tag = disclose(bidderTag(bidderKey())); assert(!bids.member(tag), "one bid per bidder"); const amount = bidAmount(); bids.insert(tag, disclose(persistentCommit<Uint<64>>(amount, bidSalt()))); receiveUnshielded(nativeToken(), disclose(amount) as Uint<128>);}
pragma language_version 0.23; import CompactStandardLibrary; export ledger bids: Map<Bytes<32>, Bytes<32>>;export sealed ledger ceiling: Uint<64>; witness bidderKey(): Bytes<32>;witness bidAmount(): Uint<64>;witness bidSalt(): Bytes<32>; constructor(maxBid: Uint<64>) { ceiling = disclose(maxBid);} export pure circuit bidderTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "deposit:bidder"), sk]);} export circuit bid(): [] { const tag = disclose(bidderTag(bidderKey())); assert(!bids.member(tag), "one bid per bidder"); const amount = bidAmount(); assert(amount <= ceiling, "bid above the ceiling"); bids.insert(tag, disclose(persistentCommit<Uint<64>>(amount, bidSalt()))); receiveUnshielded(nativeToken(), ceiling as Uint<128>);}
- Tell reports
- The unshielded amount locked by the circuit is the private bid: token amounts are part of every transaction’s public transcript.
- The attack test
- The test reads every sealed bid straight from the deposits.
- The fix
- Every bidder locks the same public bond, the auction’s ceiling, and the difference is refunded after the bids are opened. The deposits say nothing.
The members’ club
mediumLinkable tagAn “anonymous” claim for members. The key stays private, but a claim proves membership by looking the caller’s own leaf up in the member set.
pragma language_version 0.23; import CompactStandardLibrary; export ledger members: Set<Bytes<32>>;export ledger claims: Counter; witness memberKey(): Bytes<32>; export pure circuit memberLeaf(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "club:member"), sk]);} export circuit join(leaf: Bytes<32>): [] { members.insert(disclose(leaf));} export circuit claim(): [] { assert(members.member(disclose(memberLeaf(memberKey()))), "members only"); claims.increment(1);}
pragma language_version 0.23; import CompactStandardLibrary; export ledger members: MerkleTree<10, Bytes<32>>;export ledger spent: Set<Bytes<32>>;export ledger claims: Counter; witness memberKey(): Bytes<32>;witness memberPath(leaf: Bytes<32>): MerkleTreePath<10, Bytes<32>>; export pure circuit memberLeaf(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "club:member"), sk]);} export circuit join(leaf: Bytes<32>): [] { members.insert(disclose(leaf));} export circuit claim(): [] { const sk = memberKey(); const leaf = memberLeaf(sk); const path = memberPath(leaf); assert(path.leaf == leaf, "path is not for this member"); assert(members.checkRoot(disclose(merkleTreePathRoot<10, Bytes<32>>(path))), "members only"); const nul = disclose(persistentHash<Vector<2, Bytes<32>>>([pad(32, "club:claim"), sk])); assert(!spent.member(nul), "already claimed"); spent.insert(nul); claims.increment(1);}
- Tell reports
- The key of a Set lookup is part of the public transcript: every claim carries the claimant’s leaf, which ties it to their join and to all their other claims.
- The attack test
- The test matches each claim’s transcript to the member who joined with that leaf.
- The fix
- Members join a Merkle tree; a claim proves some leaf is the caller’s without saying which, and spends a one-time nullifier from a different hash domain.
The keyring
criticalPrivate key derivable from public dataEach user registers an account id, hash(seed), and an encryption key. To save a derivation, the private scalar of that key is the same hash.
pragma language_version 0.23; import CompactStandardLibrary; export ledger accounts: Map<Bytes<32>, JubjubPoint>; witness seed(): Bytes<32>; export pure circuit accountOf(s: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "keyring:account"), s]);} export circuit register(): [] { const id = accountOf(seed()); accounts.insert(disclose(id), disclose(ecMulGenerator(degradeToTransient(id))));}
pragma language_version 0.23; import CompactStandardLibrary; export ledger accounts: Map<Bytes<32>, JubjubPoint>; witness seed(): Bytes<32>; export pure circuit accountOf(s: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "keyring:account"), s]);} export pure circuit scalarOf(s: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "keyring:enc-key"), s]);} export circuit register(): [] { const s = seed(); accounts.insert(disclose(accountOf(s)), disclose(ecMulGenerator(degradeToTransient(scalarOf(s)))));}
- Tell reports
- ecMulGenerator(k) is a public key with private key k, and k here is the account id the contract publishes: anyone holds the private key.
- The attack test
- The test reads each account id from the ledger and opens the matching encryption key with it.
- The fix
- Derive the account id and the private scalar from the seed under two different domain separators; the id says nothing about the scalar.
The poll (Ledger 9)
criticalGuessable hash, in an eventThe ledger keeps each vote as a salted commitment. Then the contract emits a receipt event so voters can find their vote, and the receipt is hash(choice).
pragma language_version >= 0.25.0; import CompactStandardLibrary; export ledger ballots: Map<Bytes<32>, Bytes<32>>; witness voterKey(): Bytes<32>;witness choice(): Uint<8>;witness ballotSalt(): Bytes<32>; export pure circuit voterTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "poll:voter"), sk]);} export pure circuit receipt(c: Uint<8>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "poll:receipt"), (c as Field) as Bytes<32>]);} export circuit vote(): [] { const tag = disclose(voterTag(voterKey())); assert(!ballots.member(tag), "already voted"); const c = choice(); assert(c < 4, "four options"); ballots.insert(tag, disclose(persistentCommit<Uint<8>>(c, ballotSalt()))); emit(Misc { name: disclose(receipt(c)), payload: pad(256, "poll:voted") });}
pragma language_version >= 0.25.0; import CompactStandardLibrary; export ledger ballots: Map<Bytes<32>, Bytes<32>>; witness voterKey(): Bytes<32>;witness choice(): Uint<8>;witness ballotSalt(): Bytes<32>; export pure circuit voterTag(sk: Bytes<32>): Bytes<32> { return persistentHash<Vector<2, Bytes<32>>>([pad(32, "poll:voter"), sk]);} export circuit vote(): [] { const tag = disclose(voterTag(voterKey())); assert(!ballots.member(tag), "already voted"); const c = choice(); assert(c < 4, "four options"); const sealedVote = disclose(persistentCommit<Uint<8>>(c, ballotSalt())); ballots.insert(tag, sealedVote); emit(Misc { name: sealedVote, payload: pad(256, "poll:voted") });}
- Tell reports
- Events are part of the public transaction, like ledger writes. Tell reads Compact 0.36 and Ledger 9 events: four hashes recover every vote.
- The attack test
- The test casts votes and matches the four candidate hashes against the emitted receipts.
- The fix
- The receipt carries the same salted commitment the ledger stores; a voter finds it with the salt from their private state.