Skip to content

Derive the withdraw auditor checkpoint pad from the second sponge squeeze (audit N-07) - #792

Merged
brozorec merged 1 commit into
mainfrom
fix/n07-withdraw-checkpoint-pad
Jul 16, 2026
Merged

brozorec merged 1 commit into
mainfrom
fix/n07-withdraw-checkpoint-pad

Conversation

@brozorec

@brozorec brozorec commented Jul 13, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #785 (audit finding N-07).

The collision. encrypt_auditor_sender_balance padded the withdraw balance checkpoint with Poseidon2(AUDITOR_SENDER, [s_x, sigma]) — the first output of the sender-auditor sponge, which is exactly the transfer circuit's amount-pad slot. transfer, set_spender, and revoke_spender all already pad balances/allowances with the second squeeze; withdraw was the lone divergence. Under (r_e, sigma) reuse across a transfer and a later withdraw, anyone who knows the transfer amount (e.g. its recipient) can recover the shared pad from the published v_tilde_aud_s and decrypt the withdrawal's balance checkpoint.

The fix. The helper now returns v_new + sponge_squeeze_2(AUDITOR_SENDER, s_x, sigma)[1], restoring a uniform slot assignment across every circuit: first squeeze = amount masks, second squeeze = balance/allowance masks. The checkpoint pad can no longer coincide with any amount pad, even under nonce reuse. This also makes the clawback decryption recipe in COMPLIANCE.md §5 (which already assumed the uniform two-squeeze form) correct for withdraw checkpoints.

Changes:

  • circuits/lib/src/lib.nr: pad derivation + docstring (which also wrongly listed SetSpender/RevokeSpender as consumers — only withdraw calls this helper)
  • circuits/withdraw/: W_a3 constraint comment; regenerated pinned fixture B_TILDE_AUD_S
  • vks/withdraw.vk.json: regenerated with the pinned toolchain (only VK that changed); constraints.baseline is unchanged (same permutation count)
  • lib/testdata/encrypt_auditor_sender_balance.json: regenerated vector + corrected description — cross-language contract change, downstream SDK/prover implementations of the withdraw checkpoint must switch to the second squeeze
  • docs/DESIGN.md: W_a3 formula updated; §2.5 now states the canonical slot assignment and the normative requirement that a (r_e, sigma) pair be unique per proof, with the residual risk under reuse spelled out

Residual note: two balance checkpoints under a reused (r_e, sigma) still share their (second-slot) pad and leak their difference — inherent to any deterministic pad, hence the uniqueness requirement in §2.5.

Validation: nargo test passes across all circuit packages (153 tests); cargo test -p stellar-tokens (676) unaffected.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Corrected auditor-sender withdrawal checkpoint encryption to use the designated second sponge output.
    • Updated withdrawal verification data and encrypted balance fixtures to match the corrected checkpoint behavior.
  • Documentation

    • Clarified sponge slot usage for withdrawal checkpoints.
    • Added guidance requiring unique proof randomness to prevent mask reuse and potential ciphertext disclosure.
  • Tests

    • Updated encryption, withdrawal, and fixture expectations for the revised checkpoint format.

…eeze

The withdraw balance checkpoint previously padded with the first squeeze
of the sender-auditor sponge, which is the transfer circuit's amount-pad
slot; under (r_e, sigma) reuse a known transfer amount would decrypt a
later withdrawal's balance checkpoint. All circuits now use the second
squeeze for balance/allowance masks. Regenerates the withdraw VK and
fixtures; documents the unique-(r_e, sigma)-per-proof requirement.
@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The sender-auditor balance checkpoint now derives its mask from the second sponge_squeeze_2 output. Withdrawal documentation, verification keys, fixtures, test expectations, and encrypted test vectors were updated accordingly.

Changes

Auditor checkpoint padding

Layer / File(s) Summary
Second-squeeze checkpoint contract
packages/tokens/src/confidential/circuits/lib/src/lib.nr, packages/tokens/src/confidential/docs/DESIGN.md
The sender-auditor balance checkpoint uses the second sponge squeeze, with the first amount slot unused.
Withdrawal circuit artifacts
packages/tokens/src/confidential/circuits/withdraw/src/main.nr, packages/tokens/src/confidential/circuits/withdraw/src/tests.nr, packages/tokens/src/confidential/circuits/vks/withdraw.vk.json
Withdrawal documentation, the pinned auditor fixture, and the verifying key are updated for the revised checkpoint derivation.
Helper and vector validation
packages/tokens/src/confidential/circuits/lib/src/tests.nr, packages/tokens/src/confidential/circuits/lib/testdata/encrypt_auditor_sender_balance.json
Round-trip expectations, fixture assertions, descriptions, references, and encrypted output values use the second squeeze.
Estimated code review effort: 3 (Moderate) ~20 minutes

Possibly related PRs

Suggested reviewers: bidzyyys

Poem

A rabbit hops where sponge pads align,
The second squeeze now marks the sign.
Fixtures bloom and keys renew,
Tests chase the checkpoint through.
Thump, thump—withdrawals run true!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: deriving the withdraw auditor checkpoint pad from the second sponge squeeze.
Description check ✅ Passed The description includes the issue reference, fix summary, changed files, validation, and test/documentation coverage.
Linked Issues check ✅ Passed The PR matches #785 by switching withdraw to sponge_squeeze_2()[1] and updating the VK, fixtures, tests, and docs.
Out of Scope Changes check ✅ Passed The changed files stay within the linked issue scope and cover only the helper, withdraw artifacts, tests, VK, and design docs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/n07-withdraw-checkpoint-pad

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/tokens/src/confidential/circuits/lib/src/lib.nr (1)

288-298: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Update the sponge_squeeze_2 "Used by" list.

encrypt_auditor_sender_balance (Withdraw's W_a3/W_a4) is now a consumer of sponge_squeeze_2, but the doc comment on this function only lists Transfer and SpenderTransfer.

✏️ Proposed doc update
 /// Used by: Transfer (T_a2 recipient-auditor amount + randomness, T_a6
-/// sender-auditor amount + balance) and SpenderTransfer (O_a2, O_a6).
+/// sender-auditor amount + balance), SpenderTransfer (O_a2, O_a6), and
+/// Withdraw (W_a3, via `encrypt_auditor_sender_balance`, second squeeze only).
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/tokens/src/confidential/circuits/lib/src/lib.nr` around lines 288 -
298, Update the doc comment for sponge_squeeze_2 to include
encrypt_auditor_sender_balance as a consumer, noting its use by Withdraw’s
W_a3/W_a4 alongside the existing Transfer and SpenderTransfer references. Do not
alter the function implementation or existing squeeze-order documentation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/tokens/src/confidential/circuits/lib/src/lib.nr`:
- Around line 288-298: Update the doc comment for sponge_squeeze_2 to include
encrypt_auditor_sender_balance as a consumer, noting its use by Withdraw’s
W_a3/W_a4 alongside the existing Transfer and SpenderTransfer references. Do not
alter the function implementation or existing squeeze-order documentation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8e595abd-3f98-436d-b406-be472d162c19

📥 Commits

Reviewing files that changed from the base of the PR and between 1e51389 and e3d02de.

📒 Files selected for processing (7)
  • packages/tokens/src/confidential/circuits/lib/src/lib.nr
  • packages/tokens/src/confidential/circuits/lib/src/tests.nr
  • packages/tokens/src/confidential/circuits/lib/testdata/encrypt_auditor_sender_balance.json
  • packages/tokens/src/confidential/circuits/vks/withdraw.vk.json
  • packages/tokens/src/confidential/circuits/withdraw/src/main.nr
  • packages/tokens/src/confidential/circuits/withdraw/src/tests.nr
  • packages/tokens/src/confidential/docs/DESIGN.md

@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.56%. Comparing base (1e51389) to head (e3d02de).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #792   +/-   ##
=======================================
  Coverage   96.56%   96.56%           
=======================================
  Files          71       71           
  Lines        7346     7346           
=======================================
  Hits         7094     7094           
  Misses        252      252           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@brozorec brozorec self-assigned this Jul 16, 2026
@brozorec
brozorec requested a review from ozgunozerk July 16, 2026 08:39

@ozgunozerk ozgunozerk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let put my understanding here, to confirm whether I got it correct. I'm approving in case I got it right. If my understanding is incorrect, please don't merge, I'll have to review again.

My understanding:

  • info is encrypted as: ciphertext = plaintext + pad -> one-time pad pattern
  • pad is derived using nonce (technical detail, "how" part is not relevant to this PR)
  • anyone who knows the plaintext and the ciphertext, can derive the pad. This means, the receiver of the transfer, can decipher to pad, due to they know they received 100 (plaintext), and ciphertext is already public info
  • so, if the same pad is used elsewhere, considering the ciphertext is public info, they can decipher the plaintext. For example, the receiver can also decipher the remaining balance of the sender, which they shouldn't know ideally...

Pre-fix (before this PR), the same pad was used for 2 different things, potentially creating a private info leak, now different/unique pads are used for different inputs, fixing the problem

@brozorec

Copy link
Copy Markdown
Collaborator Author

Yes, just to precise that it happens only if the same r_e and sigma are used for both, the transfer and the withdrawal.

@brozorec
brozorec merged commit 4242d1e into main Jul 16, 2026
9 checks passed
@brozorec
brozorec deleted the fix/n07-withdraw-checkpoint-pad branch July 16, 2026 13:07
brozorec added a commit that referenced this pull request Jul 17, 2026
Resolve conflicts from the confidential-token changes that landed on main
(#792 withdraw checkpoint pad, #790 typed-payload doc renames). The withdraw
sender-auditor checkpoint now composes both changes: the bound ECDH scalar
(this PR) feeds the second sponge squeeze (#792). Regenerated the withdraw VK
and the B_TILDE_AUD_S fixture (0x288ff488…) from the combined circuit — the
merged value matches neither side. Other four VKs and the constraint baseline
re-extract byte-identical to this branch; register.vk.json is byte-identical
to main. nargo test: 155 green.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Audit N-07] Withdraw auditor checkpoint pad collides with transfer amount pad

2 participants