Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions .github/workflows/noir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ concurrency:
cancel-in-progress: true

env:
# Keep CI pinned to the same nargo version contributors run locally. Bump
# together with any toolchain change.
# Keep CI pinned to the same nargo + bb versions contributors run locally.
# Bump together; the VK extraction pipeline (vks/) is byte-sensitive to bb.
NARGO_VERSION: 1.0.0-beta.11
BB_VERSION: 0.87.0

jobs:
check-test-info:
Expand All @@ -42,17 +43,73 @@ jobs:

- name: Install noirup
# noirup is the official installer; it lands at ~/.nargo/bin/nargo.
# The install script is pinned to a specific commit (not the moving
# `main` branch) and its SHA256 is checked before execution so a
# silently rewritten upstream cannot subvert this job. Bump the URL
# and the hash together when intentionally tracking new upstream.
env:
NOIRUP_INSTALL_URL: https://raw.githubusercontent.com/noir-lang/noirup/c3bc9922bf7eeafdaba08fb6518776c4ba263a8c/install
NOIRUP_INSTALL_SHA256: 89dde59d8ef9e7794d694abca097fa139fa7395b947cb7b0b7a8d1839c737d8b
run: |
curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | bash
curl -fsSL "$NOIRUP_INSTALL_URL" -o /tmp/noirup-install.sh
echo "$NOIRUP_INSTALL_SHA256 /tmp/noirup-install.sh" | sha256sum -c -
bash /tmp/noirup-install.sh
echo "$HOME/.nargo/bin" >> "$GITHUB_PATH"
working-directory: ${{ github.workspace }}

- name: Pin nargo version
run: noirup -v "$NARGO_VERSION"

- name: Install bbup
# bbup is the official Barretenberg installer; lands at ~/.bb/bb.
# Same pinning + SHA256 verification as noirup above.
env:
BBUP_INSTALL_URL: https://raw.githubusercontent.com/AztecProtocol/aztec-packages/073ea66ad92c53ebbf7be70d28973a68a8628942/barretenberg/bbup/install
BBUP_INSTALL_SHA256: 3e4b7ce2d18e7680b897d23de0328ba6365616b2c7d592a5ebb74a368f364efd
run: |
curl -fsSL "$BBUP_INSTALL_URL" -o /tmp/bbup-install.sh
echo "$BBUP_INSTALL_SHA256 /tmp/bbup-install.sh" | sha256sum -c -
bash /tmp/bbup-install.sh
echo "$HOME/.bb" >> "$GITHUB_PATH"
working-directory: ${{ github.workspace }}
Comment thread
brozorec marked this conversation as resolved.
Resolved

- name: Pin bb version
run: bbup -v "$BB_VERSION"
Comment thread
brozorec marked this conversation as resolved.
Resolved

- name: nargo test
run: nargo test

- name: Diff extracted VKs against committed artifacts
# Re-extracts the VK for each per-operation circuit using the pinned
# bb version and compares against the committed `vks/*.vk.json`.
# Mirrors the constraint-baseline diff: drift means either the
# circuit changed (regenerate and commit in this PR) or the
# toolchain drifted (don't regenerate -- investigate).
run: |
mkdir -p /tmp/vks-current
cp vks/*.vk.json /tmp/vks-current/ 2>/dev/null || true
rm -f vks/*.vk.json
./scripts/extract_vks.sh

# Exclude README.md from the diff; only `.vk.json` artifacts
# should be compared.
if ! diff -q --exclude='*.md' /tmp/vks-current vks > /tmp/vks-diff 2>&1; then
{
echo ""
echo "### ❌ VK artifact drift detected"
echo ""
cat /tmp/vks-diff
echo ""
echo "If this is intentional, regenerate VKs in this PR:"
echo ""
echo '```'
echo "cd packages/tokens/src/confidential/circuits"
echo "./scripts/extract_vks.sh"
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
exit 1
fi

- name: Diff nargo info against checked-in baseline
# Fails the build if the per-primitive constraint counts drift from
# `constraints.baseline`. When the lib genuinely changes, regenerate
Expand Down
1 change: 1 addition & 0 deletions packages/tokens/src/confidential/circuits/Nargo.toml
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# Toolchain: nargo 1.0.0-beta.11
#
| Package | Function | Expression Width | ACIR Opcodes | Brillig Opcodes |
| circuit_register | decompose_hint | N/A | N/A | 30 |
| circuit_register | directive_invert | N/A | N/A | 9 |
| circuit_register | lte_hint | N/A | N/A | 33 |
| circuit_register | main | Bounded { width: 4 } | 33 | 72 |
| gadget_assert_on_curve | main | Bounded { width: 4 } | 2 | 0 |
| gadget_commit | decompose_hint | N/A | N/A | 30 |
| gadget_commit | lte_hint | N/A | N/A | 33 |
Expand Down
8 changes: 8 additions & 0 deletions packages/tokens/src/confidential/circuits/register/Nargo.toml
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 packages/tokens/src/confidential/circuits/register/src/main.nr
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 packages/tokens/src/confidential/circuits/register/src/tests.nr
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 packages/tokens/src/confidential/circuits/scripts/extract_vks.sh
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."
31 changes: 31 additions & 0 deletions packages/tokens/src/confidential/circuits/vks/README.md
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.
Loading
Loading