Repository navigation
feat(confidential): register circuit #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d4b538f
feat: noir lib init
brozorec dac2f14
docs: update Architecture with Noir conventions
brozorec 6b130f6
ci: add noir workflow with a baseline
brozorec bcc3d5a
ci(noir): pin LC_ALL=C for stable constraint baseline diff
brozorec 1d560d8
fix(noir): avoid `s_ba` identifier that trips the typos checker
brozorec 3c927b7
fix: address coderabbit review feedback
brozorec 26f4496
feat(noir): in-circuit on-curve validation for public-input points
brozorec 0cfdc85
fix(noir): enforce canonical identity encoding in assert_on_curve
brozorec 8239bfa
chore(noir): gitignore auto-generated Prover.toml + target/
brozorec ab62bf6
fix(docs): shorten inline comment
brozorec da17e87
feat(noir): register circuit
brozorec fba25dc
docs(noir): freeze public-input ordering for register
brozorec c5a8187
ci(noir): vk extraction pipeline for per-operation circuits
brozorec a75d028
ci(noir): use bb fields format for cross-platform vk diff
brozorec ed3ca29
merge: main into ct-noir-circuits
brozorec 1be0ead
ci: rm unnecessary step
brozorec 2e68866
feat(noir/register): add R4 (sk != 0) and R5 (vk != 0) per design doc
brozorec 893e4b1
ci(noir): pin noirup/bbup installers to commit SHA + verify checksum
brozorec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| [workspace] | ||
| members = [ | ||
| "lib", | ||
| "register", | ||
| "gadgets/assert_on_curve", | ||
| "gadgets/commit", | ||
| "gadgets/ecdh", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/tokens/src/confidential/circuits/register/Nargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "circuit_register" | ||
| type = "bin" | ||
| authors = ["OpenZeppelin"] | ||
| compiler_version = ">=0.30.0" | ||
|
|
||
| [dependencies] | ||
| stellar_confidential_lib = { path = "../lib" } |
64 changes: 64 additions & 0 deletions
64
packages/tokens/src/confidential/circuits/register/src/main.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| use stellar_confidential_lib::{H, pvk_from_vk, scalar_mul, vk_from_sk}; | ||
|
|
||
| mod tests; | ||
|
|
||
| // Register circuit -- design doc Section 7.2. | ||
| // | ||
| // Constraints | ||
| // ----------- | ||
| // R1 Y = sk * H spending key well-formed | ||
| // R2 vk = Poseidon2(delta_vk, sk, wrap) wrapper-bound viewing key | ||
| // R3 PVK = vk * H public viewing key matches vk | ||
| // R4 sk != 0 rules out Y = O | ||
| // R5 vk != 0 rules out PVK = O (would | ||
| // collapse every incoming | ||
| // transfer's ECDH) | ||
| // | ||
| // On-curve checks for Y and PVK are not needed: both are bound to in-circuit | ||
| // `scalar_mul` outputs, which are on-curve by construction (Noir's embedded- | ||
| // curve ops cannot produce an off-curve Grumpkin point).See design doc | ||
| // Section 10.8 for the system-wide point-validation doctrine. | ||
| // | ||
| // Public inputs | ||
| // ------------- | ||
| // `Y` and `PVK` each occupy two Field slots (Grumpkin affine coordinates). | ||
| // | ||
| // Idx Param Symbol Source / Note | ||
| // --- ----- ------ ----------------------------------------------- | ||
| // 0 y_x Y.x Prover-supplied spending public key. Written | ||
| // 1 y_y Y.y to `account.spending_key` on success. | ||
| // 2 pvk_x PVK.x Prover-supplied public viewing key. Written | ||
| // 3 pvk_y PVK.y to `account.viewing_public_key` on success. | ||
| // 4 wrap wrap `env.current_contract_address()` - binds the | ||
| // proof to a specific wrapper instance. | ||
| // | ||
| // Private witness | ||
| // --------------- | ||
| // sk Spending secret scalar; never revealed by the proof. | ||
| fn main( | ||
| sk: Field, | ||
| y_x: pub Field, | ||
| y_y: pub Field, | ||
| pvk_x: pub Field, | ||
| pvk_y: pub Field, | ||
| wrap: pub Field, | ||
| ) { | ||
| // R4 | ||
| assert(sk != 0); | ||
|
|
||
| // R1 | ||
| let y_derived = scalar_mul(sk, H); | ||
| assert(y_derived.x == y_x); | ||
| assert(y_derived.y == y_y); | ||
|
|
||
| // R2 | ||
| let vk = vk_from_sk(sk, wrap); | ||
|
|
||
| // R5 | ||
| assert(vk != 0); | ||
|
|
||
| // R3 | ||
| let pvk_derived = pvk_from_vk(vk); | ||
| assert(pvk_derived.x == pvk_x); | ||
| assert(pvk_derived.y == pvk_y); | ||
| } |
65 changes: 65 additions & 0 deletions
65
packages/tokens/src/confidential/circuits/register/src/tests.nr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use crate::main; | ||
| use stellar_confidential_lib::{pvk_from_vk, vk_from_sk}; | ||
|
|
||
| // Canonical fixture inputs (pinned in lib/testdata/*.json and asserted by | ||
| // `fixtures_match_testdata` in lib's tests). Reusing them keeps the SDK | ||
| // contract and the per-circuit happy path tied to the same vectors. | ||
| global SK: Field = 0xdead; | ||
| global WRAP: Field = 0xbeef; | ||
| global Y_X: Field = 0x1b46b003b88a6c34549dc74115f088f4b231a151397526bc10cbf1d15b457646; | ||
| global Y_Y: Field = 0x29116280600c10ead1fdbd9ab4b571896030679bf554d7f1ebf681e5147de21b; | ||
| global PVK_X: Field = 0x01de20bfe21ad660e073b14a106feae4afce4e78a020953ca16b767e0615331b; | ||
| global PVK_Y: Field = 0x12cf407b75fccb5c7721c5fdadc3d1a22b5440e36b08fe52658ab885529e92fd; | ||
|
|
||
| #[test] | ||
| fn matches_fixture() { | ||
| main(SK, Y_X, Y_Y, PVK_X, PVK_Y, WRAP); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| fn rejects_tampered_pvk() { | ||
| // PVK.x +1: R3 sees pvk_derived != (pvk_x, pvk_y). | ||
| main(SK, Y_X, Y_Y, PVK_X + 1, PVK_Y, WRAP); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| fn rejects_wrong_wrap() { | ||
| // Y / PVK match (sk, wrap=0xbeef). A different wrap recomputes vk under | ||
| // that wrap; PVK = vk*H then mismatches under R3. | ||
| main(SK, Y_X, Y_Y, PVK_X, PVK_Y, 0xcafe); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| fn rejects_off_curve_y() { | ||
| // (1, 2) is off-curve. R1 fails because scalar_mul(sk, H) yields an | ||
| // on-curve point that cannot equal (1, 2). | ||
| main(SK, 1, 2, PVK_X, PVK_Y, WRAP); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| fn rejects_identity_y() { | ||
| // Y = (0, 0) is the identity encoding. To pass R1, sk*H would have to | ||
| // equal (0, 0), which means sk = 0 -- but R4 rules that out before R1 | ||
| // can see it. Either way: rejected. | ||
| main(SK, 0, 0, PVK_X, PVK_Y, WRAP); | ||
| } | ||
|
|
||
| #[test(should_fail)] | ||
| fn rejects_sk_zero() { | ||
| // sk = 0 is the exact attack R4 closes: without it the prover could | ||
| // publish Y = O and collapse every ECDH involving the spending key. | ||
| // Y / PVK are fixture values that would otherwise be rejected here | ||
| // because they don't derive from sk = 0 -- R4 fires first. | ||
| main(0, Y_X, Y_Y, PVK_X, PVK_Y, WRAP); | ||
| } | ||
|
|
||
| #[test] | ||
| fn vk_intermediate_matches_lib() { | ||
| // Sanity: confirm the canonical PVK fixture was derived from the same vk | ||
| // the lib computes. If this diverges, lib/testdata is out of sync with | ||
| // the Register fixture above and one of them must be regenerated. | ||
| let vk = vk_from_sk(SK, WRAP); | ||
| let pvk = pvk_from_vk(vk); | ||
| assert(pvk.x == PVK_X); | ||
| assert(pvk.y == PVK_Y); | ||
| } |
52 changes: 52 additions & 0 deletions
52
packages/tokens/src/confidential/circuits/scripts/extract_vks.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/env bash | ||
| # Extracts UltraHonk verification keys for each per-operation circuit and | ||
| # writes them under `vks/<circuit>.vk.json`. The committed artifacts in | ||
| # `vks/` are the integration contract with the verifier (#701); CI re-runs | ||
| # this script and diffs against the committed copy, so any drift either | ||
| # means the circuit changed (regenerate) or the toolchain drifted (don't | ||
| # regenerate -- track down the source). | ||
| # | ||
| # Output format is `fields` (JSON array of hex-encoded Fr elements) -- bb's | ||
| # `bytes` format includes platform-dependent header bytes, so committing | ||
| # them and diffing across macOS/Linux would spuriously fail. `fields` is | ||
| # byte-identical across platforms, parseable by the verifier, and | ||
| # human-readable in code review. | ||
| # | ||
| # Requires the pinned `nargo` and `bb` versions declared in | ||
| # `.github/workflows/noir.yml`. Run from anywhere; the script anchors to the | ||
| # circuits/ root. | ||
| # | ||
| # Add a new circuit by appending its package name to CIRCUITS below. | ||
| set -euo pipefail | ||
|
|
||
| cd "$(dirname "$0")/.." | ||
|
|
||
| CIRCUITS=( | ||
| "register" | ||
| ) | ||
|
|
||
| OUT_DIR="vks" | ||
| TMP_DIR="$(mktemp -d)" | ||
| trap 'rm -rf "$TMP_DIR"' EXIT | ||
|
|
||
| mkdir -p "$OUT_DIR" | ||
|
|
||
| for name in "${CIRCUITS[@]}"; do | ||
| pkg="circuit_${name}" | ||
| bytecode="target/${pkg}.json" | ||
|
|
||
| echo "==> Compiling ${pkg}" | ||
| nargo compile --package "$pkg" | ||
|
|
||
| echo "==> Extracting VK for ${pkg}" | ||
| # `bb write_vk --output_format fields` writes `vk_fields.json` into the | ||
| # directory passed via `-o`. Stage in a per-circuit temp subdir, then | ||
| # move to the canonical `vks/<name>.vk.json` path. | ||
| stage="${TMP_DIR}/${name}" | ||
| mkdir -p "$stage" | ||
| bb write_vk -s ultra_honk -b "$bytecode" -o "$stage" --output_format fields | ||
| mv "${stage}/vk_fields.json" "${OUT_DIR}/${name}.vk.json" | ||
| echo " wrote ${OUT_DIR}/${name}.vk.json ($(wc -c < "${OUT_DIR}/${name}.vk.json") bytes)" | ||
| done | ||
|
|
||
| echo "Done." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Verification keys | ||
|
|
||
| UltraHonk verification keys for the per-operation circuits, one JSON file | ||
| per circuit (`<name>.vk.json`). These are committed artifacts -- the | ||
| integration contract with the verifier (#701). | ||
|
|
||
| **Format:** each file is a JSON array of hex-encoded `Fr` elements, produced | ||
| by `bb write_vk --output_format fields`. Used instead of bb's raw `bytes` | ||
| format because the latter includes platform-dependent header bytes that | ||
| spuriously break cross-platform reproducibility (macOS vs Linux CI). | ||
|
|
||
| Reproducible from the circuit sources with the pinned toolchain: | ||
|
|
||
| | Tool | Version | | ||
| |:-------|:-----------------| | ||
| | nargo | `1.0.0-beta.11` | | ||
| | bb | `0.87.0` | | ||
|
|
||
| Both versions are pinned in `.github/workflows/noir.yml`. CI re-runs the | ||
| extraction and diffs against the files here; any drift fails the build. | ||
|
|
||
| ## Regenerating | ||
|
|
||
| ```bash | ||
| cd packages/tokens/src/confidential/circuits | ||
| ./scripts/extract_vks.sh | ||
| ``` | ||
|
|
||
| If the diff is intentional (the circuit changed), regenerate and commit in | ||
| the same PR. If unintentional (e.g. toolchain bumped without an explicit | ||
| decision), do **not** regenerate -- track down the source first. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.