Skip to content

feat(confidential): Noir transfer circuit - #728

Merged
brozorec merged 16 commits into
mainfrom
feat/confidential-transfer-circuit
May 29, 2026
Merged

brozorec merged 16 commits into
mainfrom
feat/confidential-transfer-circuit

Conversation

@brozorec

@brozorec brozorec commented May 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Implements the confidential transfer circuit (closing issue #706, design doc §7.6):

  • Sender state (T1-T4, T10-T12): key ownership, wrapper-bound vk, opening of C_spend^A, 127-bit range validity (Section 2.6/3.4), deterministic new-balance randomness, new spendable commitment, encrypted balance scalar.
  • Recipient ECDH + transfer commitment (T5-T9, T13): in-circuit recipient ECDH, ephemeral R_e, anti-poisoning binding of r_tx via Poseidon2(δ_tx_blind, S.x, σ) (Section 5.4), transfer commitment, encrypted amount, r_e ≠ 0.
  • Auditor block (T_a1-T_a8): dual-channel ECDH with reused r_e, per-channel SpongeSqueeze_2 (Section 2.5), recipient-auditor (ṽ_aud,r, r̃_aud,r) and sender-auditor (ṽ_aud,s, b̃_aud,s) (Section 8.1).

24 public inputs in design-doc canonical order. Auditor keys (the only proof-less entry, Section 10.8 path 3) are validated on-curve + non-identity in-circuit before consumption by T_a1 / T_a5.

Closes #706.

Test plan

  • nargo test --package circuit_transfer (28 tests pass: happy path, v_tx = v boundary, T4 underflow + range overflow on v and v_tx, wrong sk / opening / wrap, wrong recipient PVK, anti-poisoning rejection of mutated C_tx, every tampered ciphertext, r_e = 0, on-curve + non-identity rejection of both auditor keys)
  • nargo test (all 79 tests pass workspace-wide; no regressions in lib, register, withdraw)
  • nargo info matches constraints.baseline (transfer adds 129 ACIR opcodes)
  • ./scripts/extract_vks.sh reproduces the committed vks/transfer.vk.json byte-for-byte with the pinned bb 0.87.0

Summary by CodeRabbit

  • New Features

    • Added a new transfer circuit implementation.
  • Tests

    • Added comprehensive test suite for the transfer circuit with positive and negative test cases.
  • Chores

    • Updated build configuration and scripts to include the transfer circuit.
    • Generated verification key artifacts for the transfer circuit.

Review Change Stack

brozorec added 12 commits May 22, 2026 12:02
Replaces the per-plaintext encrypt_auditor_tx / encrypt_auditor_bal
helpers with the per-channel Poseidon2 sponge described in design doc
v0.5 -> v0.6 (Section 2.5, Section 8.1). One absorb of
(delta_channel, S.x, sigma) is followed by N squeezes from the same
permutation; rate=3 lets every current call serve all squeezes without
a second permutation. AUDITOR_TX and AUDITOR_BALANCE domain tags are
dropped in favour of AUDITOR_SENDER (delta_aud_s) and AUDITOR_RECIPIENT
(delta_aud_r) at the same numeric slots (10, 11).

Adds the sponge_squeeze_2 gadget for nargo info accounting and pins
the new sponge outputs in testdata/sponge_squeeze_{1,2}.json. The
constraints baseline gains one row; no existing primitive's output
changed.
Implements the spend-side block of the Withdraw circuit from design doc
Section 7.5: owner key ownership (W1), wrapper-bound viewing key (W2),
spendable balance opening (W3), range validity on v, a, and v - a (W4),
deterministic randomness for the new commitment (W5), the refreshed
spendable commitment (W6), and the encrypted balance scalar (W7).

The auditor block (W_a1-W_a5, K_aud_s + r_e + R_e + b_tilde_aud_s) lands
in a follow-up commit so this one stays focused on balance conservation
and key ownership; once that lands the public input signature grows
from 10 to the design doc's frozen 15 fields.

Tests reuse the lib's pinned (sk, wrap, sigma, v, r) fixtures so any
drift in commit / vk_from_sk / encrypt_balance / derive_spend_r is
caught by `withdraw_fixtures_match_lib` before the rest of the harness
runs. Negative coverage: under-funded withdrawal (W4), v or a out of
range, wrong sk (W1), wrong balance opening (W3), wrong wrap (W2/W6/W7
chain), tampered b_tilde (W7), tampered C_spend' (W6). VK extraction
script and constraints baseline updated.
Adds the sender-auditor visibility block to the Withdraw circuit
(design doc Section 7.5, Section 8.1): the ephemeral key R_e = r_e * H
(W_a1), the sender-auditor ECDH shared secret S_{a,s} = r_e * K_{aud,s}
(W_a2), the single-squeeze sender-channel sponge mask m_b (W_a3), the
sender-auditor balance checkpoint ciphertext b_tilde_aud_s = (v - a) +
m_b (W_a4), and the r_e != 0 non-collapse constraint (W_a5).

The public input signature grows from 10 to the design-doc-frozen 15
fields, in canonical order (C_spend, Y, wrap, K_aud_s, a, C_spend',
sigma, b_tilde, R_e, b_tilde_aud_s). K_aud_s carries an explicit
on-curve + non-identity check at the entrypoint because the verifier
doesn't check curve membership and an off-curve K_aud_s would break the
soundness of W_a2 (Section 10.8).

`encrypt_auditor_sender_balance` lands in the lib as the canonical
W_a3+W_a4 composition; the same helper covers RevokeOperator V_a3/V_a5
and SetOperator S_a3/S_a5 when those circuits land. Pinned via testdata
and a lib round-trip test that ties it back to sponge_squeeze_1.

Auditor-side negative coverage: r_e = 0 (W_a5), wrong r_e (W_a1),
off-curve K_aud_s, identity K_aud_s, valid-but-wrong K_aud_s, tampered
b_tilde_aud_s (W_a4). Closes the under-funded-withdrawal + tampered-
ciphertext criteria from issue #705.
VK extracted via `bash scripts/extract_vks.sh` after the Withdraw circuit
landed in the workspace. Same UltraHonk universal-SRS pipeline as the
register VK -- no new trusted-setup contribution required. The committed
JSON is the integration contract with the on-chain verifier (#701); CI
re-runs the extraction script and diffs against this copy.
Aligns W4 with design doc §7.5 / §2.6 / §3.4: the value domain is the
SEP-41 non-negative i128 range [0, 2^127), not [0, 2^128). The circuit
now uses Noir stdlib's `Field::assert_max_bit_size::<127>()` directly
(the 127-bit decomposition spelled out in §2.6) instead of the lib's
u128-cast helper. Doc comments for W4 and the public-input table for
`a` are restated against the tighter bound and reference §3.4 for the
entrypoint-level `a >= 0` check.

Test boundaries retarget to 2^127 (the smallest value W4 must reject),
not 2^128. Withdraw VK regenerated; constraint count drops from 130 to
92 ACIR opcodes (stdlib's assert_max_bit_size is cheaper than the
u128 round-trip).

The lib's `assert_range_128` primitive is now unused by Withdraw but
stays in place for backwards compat -- a separate cleanup can remove
it once no callers reference it.
The test previously fired W3 (commit(v_huge, R) != stale C_SPEND) before
W4 ever ran, so the range check was never the load-bearing assertion.
Recompute C_spend from v_huge so W3 passes and the very first W4 line
(v.assert_max_bit_size::<127>()) becomes the failing constraint.

rejects_a_out_of_range already isolates W4 (V is unchanged, W3 passes,
W4 hits a.assert_max_bit_size::<127>() on a_huge = 2^127) so it is left
as-is.
The design doc added delta_addr = 1 (§2.7), shifting every other tag up
by one. Realign the lib constants, re-pin all Poseidon-derived fixtures
and testdata, regenerate VKs, and fix a stale W_a5 label in the withdraw
test (now W8).
Promote `domain` and its constants from `pub(crate)` to `pub` so
per-circuit crates can spell out `sponge_squeeze_2(domain::AUDITOR_*,
...)` directly -- the lib already documented this as the canonical
caller pattern for the dual-mask sender / recipient channels (Transfer
T_a2/T_a6, OperatorTransfer O_a2/O_a6).
Implements the confidential transfer circuit per design doc §7.6:
sender state (T1-T4, T10-T12), recipient ECDH + transfer commitment
including the anti-poisoning binding (T5-T9, T13), and the dual-auditor
block (T_a1-T_a8). 24 public inputs in the design-doc canonical order.

Tests cover the happy path, the v_tx = v boundary, T4 underflow / range
overflow, anti-poisoning rejection of mutated C_tx, the wrong-recipient
PVK case, every tampered ciphertext, r_e = 0 (T13), and on-curve /
non-identity rejection for both auditor keys.
Adds `transfer` to the extract-VKs pipeline and commits the
UltraHonk verification key produced by the pinned bb 0.87.0
toolchain.
@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cc5771c1-b85d-4d83-ad52-ccd089fc3fc3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title 'feat(confidential): Noir transfer circuit' clearly summarizes the main change—implementing a new confidential transfer circuit in Noir.
Linked Issues check ✅ Passed All constraints (T1–T13, T_a1–T_a8), public inputs (24 fields), tests (28 passing), verification key extraction, and acceptance criteria from issue #706 are fully implemented and documented.
Out of Scope Changes check ✅ Passed All changes directly support the transfer circuit implementation and library promotion for domain constants; no out-of-scope modifications detected beyond issue #706 requirements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The PR description provides a comprehensive summary of changes, references the closed issue #706, includes detailed test plan results, and outlines all major components implemented.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/confidential-transfer-circuit

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

@brozorec brozorec changed the title feat(confidential): Noir circuit -- Transfer feat(confidential): Noir transfer circuit May 26, 2026
@brozorec brozorec self-assigned this May 26, 2026

@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.

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@packages/tokens/src/confidential/circuits/transfer/src/tests.nr`:
- Around line 191-248: The tests use run_main which hardcodes parts of the
public tuple, causing negative tests (e.g., wrong_sk, wrong_wrap, wrong_r_e,
r_e_zero, and the v/v_tx boundary tests) to leave downstream witnesses canonical
and thus mask regressions; modify the tests so the helper function accepts the
public inputs instead of baking them in and for each targeted failing case
recompute all dependent witnesses from the tampered input (for
wrong_sk/wrong_wrap recompute vk, r_new, c_spend_new, b_tilde; for
wrong_r_e/r_e_zero recompute R_E_X/Y, C_tx, v_tilde and both auditor channels
k_aud_*; for v/v_tx boundary tests recompute every downstream value that depends
on the mutated amount) so the single intended assertion is the only constraint
that can fail; locate run_main and main and update tests that call them to
pass-through public tuple values and re-derive witnesses as described.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e043a0b4-c5f7-4b03-b665-7cc665c2c51c

📥 Commits

Reviewing files that changed from the base of the PR and between a1d04af and 2d926e6.

📒 Files selected for processing (8)
  • packages/tokens/src/confidential/circuits/Nargo.toml
  • packages/tokens/src/confidential/circuits/constraints.baseline
  • packages/tokens/src/confidential/circuits/lib/src/lib.nr
  • packages/tokens/src/confidential/circuits/scripts/extract_vks.sh
  • packages/tokens/src/confidential/circuits/transfer/Nargo.toml
  • packages/tokens/src/confidential/circuits/transfer/src/main.nr
  • packages/tokens/src/confidential/circuits/transfer/src/tests.nr
  • packages/tokens/src/confidential/circuits/vks/transfer.vk.json

@codecov

codecov Bot commented May 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.49%. Comparing base (c819794) to head (aa9db54).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #728   +/-   ##
=======================================
  Coverage   96.49%   96.49%           
=======================================
  Files          61       61           
  Lines        6440     6440           
=======================================
  Hits         6214     6214           
  Misses        226      226           

☔ View full report in Codecov by Sentry.
📢 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.

`run_main` now takes the full 24-public-input tuple, instead of baking
Y_X/Y_Y/R_E_X/R_E_Y/SIGMA. Each negative test recomputes every
downstream public that depends on the tampered witness so the targeted
constraint is the only one that can fail -- removing the targeted
assertion would let the proof verify, exposing the regression.

Touches `rejects_v_out_of_range` (T4 v sub-check), `rejects_v_tx_out_of_range`
(T4 v_tx + v_new sub-checks), `rejects_wrong_sk` (T1),
`rejects_wrong_balance_opening` (T3), `rejects_wrong_wrap` (T12;
recomputing both vk-derived publics would make the proof verify since the
circuit has no direct T2 assertion -- T12 is the load-bearing wrap-binding
check left in place), `rejects_r_e_zero` (T13), `rejects_wrong_r_e` (T6).
@brozorec
brozorec requested a review from bidzyyys May 27, 2026 08:39
@brozorec brozorec linked an issue May 27, 2026 that may be closed by this pull request
5 tasks
@brozorec
brozorec changed the base branch from feat/confidential-withdraw-circuit to main May 27, 2026 12:23

@bidzyyys bidzyyys left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

@brozorec
brozorec merged commit 0c9e3fd into main May 29, 2026
9 checks passed
@brozorec
brozorec deleted the feat/confidential-transfer-circuit branch May 29, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Noir circuit: Transfer

2 participants