Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .github/workflows/assurance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ jobs:
:zeroj-integration-tests:test --tests 'org.zeroj.it.snarkjs.*' \
:zeroj-crypto:test --tests 'org.zeroj.crypto.groth16.R1csSnarkjsCeremonyRoundTripTest' \
--tests 'org.zeroj.crypto.groth16.ZkeyPkStoreImporterTest' \
--tests 'org.zeroj.crypto.plonk.PtauImporterBLS381Test' \
:zeroj-tools:test --tests 'org.zeroj.ceremony.ZkeyContributorInteropTest' \
--tests 'org.zeroj.ceremony.CeremonyCliTest' \
--stacktrace
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ remaining production gates are tracked in
| Circuit definition (DSL, symbolic annotations, gadgets) | `zeroj-circuit-dsl`, `zeroj-circuit-annotation-*`, `zeroj-circuit-lib` ([per-gadget table](zeroj-circuit-lib/README.md)) | **Beta** |
| Groth16 BLS12-381 — pure Java prove + verify | `zeroj-crypto`, `zeroj-verifier-groth16` | **Beta** (production trusted setup requires an external snarkjs MPC ceremony; in-repo setup is dev-only and flag-gated) |
| Groth16 BLS12-381 — on-chain (Julc / Plutus V3) | `zeroj-onchain-julc` | **Beta — testnet only**, not value-bearing; bind `ScriptContext` in real validators (see `Groth16BLS12381TxOutRefBindingVerifier`) |
| PlonK BLS12-381 — pure Java prove + verify, `.ptau`/`.zkey` import | `zeroj-crypto`, `zeroj-verifier-plonk` | **Beta** |
| PlonK BLS12-381 — pure Java prove + verify, `.ptau` SRS import | `zeroj-crypto`, `zeroj-verifier-plonk` | **Experimental** — universal `.ptau` SRS import (`PtauImporterBLS381`) + pure-Java prove/verify; snarkjs PlonK `.zkey` import is unsupported ([ADR-0066](docs/adr/0066-plonk-bls12381-ptau-and-zkey-import.md)) |
| PlonK BLS12-381 — on-chain (Julc / Plutus V3) | `zeroj-onchain-julc` | **Experimental** — full KZG check implemented; labeled testnet trials only |
| BBS (CFRG draft-10) — verification | `zeroj-bbs` | **Beta** (spec is an IRTF draft, not yet an RFC) |
| BBS — issuance / proof generation | `zeroj-bbs` | **Beta with caveat** — offline/isolated only: no provider (pure Java or blst) has an evidenced side-channel contract ([ADR-0062](docs/adr/0062-bls12381-independent-review-and-remediation.md)) |
Expand All @@ -61,7 +61,7 @@ remaining production gates are tracked in
- **Pure Java prover** (recommended) — Groth16 + PlonK for BLS12-381. Zero native dependencies. GraalVM compatible. Allocation-lean flat arithmetic, an `mmap`-able proving key, and a streaming trusted setup keep large circuits (millions of constraints) within commodity memory, no JNI (ADR-0029/0033/0034/0035).
- **blst-accelerated prover backend** — optional, opt-in FFM-bound native MSM (`blst_p1s/p2s_mult_pippenger`); a faster Groth16 proving backend, bit-identical proofs. The MSM's `libblst` is built from source (no third-party wrapper) ([ADR-0029](docs/adr/0029-blst-accelerated-groth16-prover.md)).
- **snarkjs CLI** — external CLI for circom-based circuits
- **snarkjs key import** — import `.zkey` files, prove with the pure Java prover
- **snarkjs key & SRS import** — import Groth16 `.zkey` files and streaming BLS12-381 `.ptau` SRS files, prove with the pure Java prover

### Verify ZK Proofs (Pure Java, Zero Native Deps)
- **Groth16 BLS12-381** — pure Java verification; optional blst-backed native verifier is also available
Expand Down
12 changes: 7 additions & 5 deletions docs/adr/0047-snarkjs-bidirectional-interop-export-and-ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ produced by this ADR's code (see invariant V8).
## Context

ZeroJ already consumed snarkjs artifacts (`SnarkjsJsonCodec`, `SnarkjsPlonkCodec`,
`ZkeyImporterBLS381`, `PlonKZkeyImporterBLS381`, `R1csExporter`) and its verifiers were checked
`ZkeyImporterBLS381`, `PlonKZkeyImporterBLS381` *(later withdrawn by [ADR-0066](0066-plonk-bls12381-ptau-and-zkey-import.md))*, `R1csExporter`) and its verifiers were checked
against snarkjs-generated vectors in the default build. The other direction did not exist: nothing
in the repository could write a ZeroJ Groth16 or PlonK proof or verification key in the format
`snarkjs groth16 verify` / `snarkjs plonk verify` reads, so the claim "ZeroJ proofs are
Expand Down Expand Up @@ -248,10 +248,12 @@ Independent evidence, per AGENTS.md "Testing":

## Known gaps

- **ZeroJ proof under a snarkjs PlonK zkey is not covered.** `PlonKZkeyImporterBLS381` reads the
header, selectors, sigmas and SRS but not the A/B/C wire-map sections, so ZeroJ cannot assign
wires for a snarkjs-arithmetised circuit. Closing this needs an importer extension (its own
R2 change), tracked separately.
- **ZeroJ proof under a snarkjs PlonK zkey is not supported.** `PlonKZkeyImporterBLS381.importZkey`
fails explicitly with `UnsupportedOperationException` ([ADR-0066](0066-plonk-bls12381-ptau-and-zkey-import.md)):
supporting snarkjs 0.7.6 PlonK `.zkey` files requires a full layout rework (sections 3–6 wire
maps, sections 7–11 `Qm/Ql/Qr/Qo/Qc` extended evaluations, and section 13 Fr Lagrange
polynomials). Universal ceremony SRS import works via `PtauImporterBLS381.importPtau(...)` +
`PlonKSetupBLS381.setup(...)`.
- The live suites use one small circuit (`c = a·b`, `nPublic = 1`); the checked-in vectors add
`nPublic = 2` (Groth16) and identity selector commitments (PlonK). Larger circuits and
`nPublic = 0` are not exercised live.
Expand Down
179 changes: 179 additions & 0 deletions docs/adr/0066-plonk-bls12381-ptau-and-zkey-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# ADR-0066: BLS12-381 PlonK `.ptau` Streaming Import, `n + 6` SRS Sizing, and `.zkey` Importer Withdrawal

## Status

Proposed (2026-11-21)

## Risk classification

- **R2:** Untrusted `.ptau` SRS codec/import boundary (`PtauImporterBLS381`), PlonK quotient-split
and SRS sizing (`PlonKProverBLS381`, `PlonKSetupBLS381`, `PlonkSetupCache`), and explicit
withdrawal of the broken `PlonKZkeyImporterBLS381` entry point.

## Context

Three coupled defects broke BLS12-381 PlonK ceremony-SRS interoperability against `snarkjs@0.7.6`:

1. **`PtauImporterBLS381` rejected every valid BLS12-381 `.ptau` file and could not stream large
ceremony files.** Header prime parsing called `readFieldLE(..., FP, ...)` which rejected any
value `>= FP`, unconditionally rejecting `prime == FP`. It also ignored `ceremonyPower`, mapped
infinity sentinels `(0, 0)` / `(0, 0, 0, 0)` in `readG1Mont`/`readG2Mont` (which `toG1`/`toG2`
then converted into off-curve non-infinity `(0, 0)` points), and buffered the entire `.ptau`
stream in memory under a 64 MiB cap (too small for real BLS12-381 `.ptau` files above power ~15).
2. **`PlonKProverBLS381`, `PlonKSetupBLS381`, and `PlonkSetupCache` required `2n` SRS G1 powers
instead of `n + 6`.** Because `t3` was allocated to `tCoeffs.length - 2n = 2n` (with `n - 6`
trailing zero coefficients), a same-power `snarkjs` `.ptau` — which contains `2n - 1` G1 powers
— was rejected even though `deg(t) <= 3n + 5` and `t3` only ever has `n + 6` coefficients.
3. **`PlonKZkeyImporterBLS381` was fundamentally incompatible with `snarkjs@0.7.6` PlonK `.zkey`
files.** Beyond the same prime-parsing bug, it read sections 7–11 in the wrong order
(`Ql, Qr, Qm, Qo, Qc` vs `snarkjs` `Qm, Ql, Qr, Qo, Qc`, each stored as `n` coefficients + `4n`
extended evaluations), misparsed section 13 (`Fr` Lagrange polynomials) as 96-byte G1 points,
required `2n` points in section 14 (snarkjs writes `n + 6`), and skipped sections 3–6 (additions
and A/B/C wire maps). Several documentation pages still advertised `.zkey` import as working.

## Threat model & trust assumptions

- **Untrusted `.ptau` stream:** Callers may import `.ptau` files downloaded from public or untrusted
mirrors. Malicious inputs may contain wrong curve primes, non-canonical coordinates (`>= p`),
identity points, off-curve or small-subgroup points, inconsistent powers of `tau`, malformed or
duplicate sections, trailing bytes, or multi-GB payloads designed to trigger OOM.
- **Content pinning & streaming trade-off (amends [ADR-0022](0022-pure-java-plonk-hardening.md)):**
ADR-0022's original bounded-buffer `.ptau` importer buffered up to 64 MiB in memory and verified
`expectedSha256` before reading headers. Because real BLS12-381 `.ptau` files exceed 64 MiB above
power ~15, this ADR amends that boundary to a single streaming pass: only minimal section framing
and incrementally acquired raw point chunks (`<= 768 KiB` per chunk, tracking received bytes up
to `min(availableG1, maxPoints) * 96 + 384` bytes, capped at `(2^24 + 6) * 96` bytes) are
allocated while `DigestInputStream` hashes the entire stream. Truncated large-power headers fail
with `IOException` without pre-allocating the declared size, and **zero elliptic-curve decoding,
subgroup checks, or pairing checks run before `expectedSha256` matches**, so a substituted file
from a mirror fails on SHA-256 without spending elliptic-curve validation CPU.
- **Ceremony trust:** SRS soundness still depends on at least one honest phase-1 ceremony
participant; `PtauImporterBLS381` verifies algebraic structure and optional SHA-256 pinning, not
ceremony provenance.

## Pinned normative references

- `snarkjs@0.7.6`:
- `src/powersoftau_utils.js` (`writePTauHeader`, `readPTauHeader`: 60-byte Section 1 = `n8`,
raw unsigned LE `q`, `power`, `ceremonyPower`)
- `src/powersoftau_new.js` (Section 2 = `(2^(power+1) - 1) * 96` B, Section 3 = `2^power * 192` B)
- `src/plonk_setup.js` (Sections 7–11 selector layout, Section 13 Lagrange Fr, Section 14 `n + 6` G1)
- `src/plonk_prove.js` (lines 649–684: `deg(T) < 3n + 6` degree assertion and `T3` length `n + 6`)
- `ffjavascript@0.3.1`: `src/wasm_curve.js` (LEM affine zero encoding `0^96` / `0^192`).
- [ADR-0047](0047-snarkjs-bidirectional-interop-export-and-ci.md) (pinned `snarkjs@0.7.6` CI oracle
and `-PrequireSnarkjs`).

## Decision & exact security invariants

### I1 — Streaming `.ptau` parser with exact `snarkjs@0.7.6` framing

`PtauImporterBLS381.importPtau(InputStream, int maxPoints, byte[] expectedSha256)` streams the input
sequentially without buffering the entire file:

- Validates global header (`"ptau"`, `version == 1`, `1 <= numSections <= 64`) and rejects duplicate
section IDs, negative sizes, premature EOF, and trailing bytes after the final section.
- Section 1 (must precede Sections 2 and 3): exact size `60` bytes; `n8 == 48`; raw unsigned LE
`prime == MontFp381.modulus()`; `1 <= power <= 32`; `power <= ceremonyPower <= 32`.
- Section 2 (`tauG1`): exact size `((1L << (power + 1)) - 1) * 96`; reads `min(availableG1, maxPoints)`
raw point records (bounded by `MAX_IMPORT_POINTS = 2^24 + 6`, aligned with PlonK's maximum
supported `2^24` domain plus 6 blinding powers) and streams past remaining bytes.
- Section 3 (`tauG2`): exact size `(1L << power) * 192`; reads the first 2 raw G2 point records
and streams past remaining bytes.
- Remaining sections (4–7, 12–15, etc.) are streamed past while still feeding `SHA-256` when
`expectedSha256 != null`. Full-stream SHA-256 equality is verified **before** decoding points or
running curve, subgroup, and pairing checks.

### I2 — Strict SRS point validation & infinity rejection

- Every loaded coordinate is decoded via `MontFp381.fromMontLimbs(...)` (canonical `< p`, no silent
modular reduction).
- Infinity (`(0, 0)` in G1, `(0, 0, 0, 0)` in G2) is explicitly rejected for all loaded SRS points.
Defensively, `toG1`/`toG2` also map `isInfinity()` to `G1Point.INFINITY`/`G2Point.INFINITY` rather
than constructing off-curve `(0, 0)`.
- Every loaded point is checked for curve membership (`isOnCurve()`) and prime-order subgroup
membership (`isValid()`).
- Anchors `tauG1[0] == G1`, `tauG2[0] == G2`, and pairing progression
`e(tauG1[i], G2) == e(tauG1[i-1], tauG2[1])` are verified for every loaded G1 power.

### I3 — Exact `t3 = n + 6` quotient-split & SRS sizing

- With 11 blinding scalars (`deg(aBlind), deg(bBlind), deg(cBlind) <= n + 1`, `deg(zBlind) <= n + 2`),
the quotient polynomial `t(X)` has degree `<= 3n + 5` (`3n + 6` coefficients).
- `PlonKProverBLS381` sizes `t3` to exact length `n + 6` (indices `2n .. 3n + 5`) and throws
`IllegalStateException` if any coefficient at index `>= 3n + 6` is non-zero (matching
`snarkjs@0.7.6` `plonk_prove.js:649-651`).
- `PlonKSetupBLS381.validateSrs`, `PlonKProverBLS381.validateProverInputs`, and
`PlonkSetupCache` require `srsG1.length >= domainSize + 6` (minimum `domainSize = 8`, where
same-power `.ptau` provides `2n - 1 = 15 >= 14 = n + 6` powers).

### I4 — Explicit withdrawal of `PlonKZkeyImporterBLS381`

- Both `PlonKZkeyImporterBLS381.importZkey(InputStream)` and `importZkey(InputStream, byte[])`
fail explicitly with `UnsupportedOperationException` citing this ADR and directing callers to
`PtauImporterBLS381` + `PlonKSetupBLS381`. Full snarkjs PlonK `.zkey` import requires a separate
format/wire-map rework.

### I5 — Documentation accuracy

- Align `README.md`, `www/src/content/docs/guides/proving/plonk.md`,
[ADR-0047](0047-snarkjs-bidirectional-interop-export-and-ci.md), and `zeroj-crypto/README.md`
to remove false `.zkey` import claims, document `PtauImporterBLS381` + `PlonKSetupBLS381`, and
reconcile PlonK's status as Experimental (with 11 blinding scalars already implemented).

## Alternatives considered

1. **Full snarkjs PlonK `.zkey` rework now:** Requires implementing snarkjs addition gates, wire-map
deserialization (sections 3–6), `writeP4` selector/sigma layout (sections 7–12), and Fr Lagrange
loading (section 13). Deferred because PlonK remains Experimental and universal `.ptau` import +
`PlonKSetupBLS381` already unlocks public-ceremony BLS12-381 PlonK proving.
2. **Keep `2n` SRS requirement and require a `.ptau` one power larger:** Rejected because `t3` has
exact degree `<= n + 5` (`n + 6` coefficients); lowering to `n + 6` eliminates wasted MSM work
on `n - 6` trailing zeros and makes same-power `.ptau` files work natively.

## Compatibility & breaking notes

- `PlonKZkeyImporterBLS381.importZkey(...)` now throws `UnsupportedOperationException` instead of
failing with a misleading `IOException` ("prime is not canonical"). No in-repo production code
called it.
- `PtauImporterBLS381` now requires exact `snarkjs@0.7.6` Section 1 length (60 bytes including
`ceremonyPower`) and exact Section 2/3 sizes matching `power`; synthetic test fixtures must
conform to the `snarkjs` layout.
- `PlonKSetupBLS381`, `PlonKProverBLS381`, and `PlonkSetupCache` now accept SRS lengths in
`[domainSize + 6, 2 * domainSize)` that were previously rejected. Forward compatibility note:
proving keys or setup caches built from fewer than `2 * domainSize` G1 powers will be rejected by
earlier ZeroJ releases that required `2 * domainSize`.
- **Quotient degree check in `PlonKProverBLS381.prove`:** Because `t(X)` is now checked for
`deg(t) <= 3n + 5` (matching `snarkjs@0.7.6` `plonk_prove.js:649-651`), proving with an
unsatisfying witness may now fail fast with `IllegalStateException` when the interpolated
quotient polynomial has non-zero coefficients above `3n + 5`, rather than always emitting an
unverifiable proof. This degree check is a consistency guard, not a complete witness-satisfaction
check; downstream negative tests expecting `verify == false` should tamper with an honest proof
or public input.

## Milestones

| Milestone | Scope | Status |
|---|---|---|
| M1 | Streaming `PtauImporterBLS381` + infinity/canonical/subgroup/SHA-256 enforcement | Implemented |
| M2 | `t3 = n + 6` sizing & degree check across `PlonKProverBLS381`, `PlonKSetupBLS381`, `PlonkSetupCache` | Implemented |
| M3 | Explicit `UnsupportedOperationException` withdrawal of `PlonKZkeyImporterBLS381` | Implemented |
| M4 | Offline positive/negative/70 MiB streaming tests + live same-power `snarkjs@0.7.6` `.ptau -> setup -> prove -> snarkjs plonk verify` CI test | Implemented |
| M5 | Documentation alignment across `README.md`, `plonk.md`, ADR-0047, and `zeroj-crypto/README.md` | Implemented |
| M6 | External cryptographic review before any promotion of PlonK out of Experimental | Open (mandatory gate) |

## Verification & test-vector strategy

- Offline positive tests: synthetic BLS12-381 `.ptau` full + bounded `maxPoints` import, SHA-256
pin pass/mismatch, and a 70 MiB virtual streaming test exceeding the former 64 MiB cap.
- Offline negative tests: null/truncated/wrong-hash/duplicate-section/wrong-prime/trailing-bytes,
infinity in G1/G2, off-curve, small-subgroup `(0, 2)`, non-canonical `>= p`, pairing-inconsistent
SRS power, and `domainSize + 5` short SRS rejection alongside `domainSize + 6` acceptance.
- Live differential CI test (`PtauImporterBLS381Test` in `.github/workflows/assurance.yml` under
`-PrequireSnarkjs`): generates a live same-power `snarkjs@0.7.6` `power=3` BLS12-381 `.ptau`,
imports exact `14` (`8 + 6`) points with SHA-256 pinning, runs `PlonKSetupBLS381` +
`PlonKProverBLS381`, exports snarkjs JSON, and verifies with live `snarkjs plonk verify`.

## Revision history

- **2026-11-21:** Initial `Proposed` ADR for PR-4 (`PtauImporterBLS381` streaming fix, `t3 = n + 6`
SRS sizing, `PlonKZkeyImporterBLS381` withdrawal, and doc alignment).
Loading
Loading