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
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# ADR-0067: Off-Chain Verifier Envelope Binding, Canonical Codecs, and Legacy BN254 Gating

## Status

Proposed

## Date

2026-11-21

## Risk Classification

**R2** — Security-sensitive validation, serialization/deserialization trust boundaries, cross-backend verifier consistency, and legacy curve isolation.

## Context

Four off-chain trust-boundary gaps existed across ZeroJ's verification orchestration, backend verifiers, envelope codecs, and legacy BN254 surface:

1. **Divergent envelope-to-material binding across off-chain backends:** `PlonkBLS12381Verifier` validated that `envelope.proofSystem()`, `envelope.curve()`, `envelope.circuitId()`, `material.vkHash()`, and `envelope.vkRef()` (`ByHash`) matched `VerificationMaterial` before verifying cryptography, returning `VK_MISMATCH` on any mismatch. However, `VerifierOrchestrator`, `Groth16BLS12381Verifier`, `Groth16BLS12381PureJavaVerifier`, and `BbsZkVerifier` did not enforce these checks. An attacker could supply a valid proof for `circuit-a` inside an envelope claiming `circuit-b` (or referencing a different `ByHash` VK digest) and receive `proofValid = true` from `VerifierOrchestrator` or the Groth16/BBS verifiers. In addition:
- `InMemoryVerificationKeyRegistry.register` indexed `material.vkHash()` without checking that it equaled `SHA-256(material.vkBytes())`.
- `VerifierRegistry` stored backends in an unsynchronized `ArrayList`.
- Groth16 verifiers decoded proof/VK bytes with the platform default charset instead of UTF-8, did not check parsed JSON `protocol`/`curve`, and mapped `CodecException` to `INTERNAL_ERROR` instead of `MALFORMED_ENVELOPE`.
- `BbsZkVerifier` mapped malformed CBOR/public-key `BbsException` failures to `INTERNAL_ERROR` instead of `MALFORMED_ENVELOPE`.
2. **Broken `SnarkjsJsonCodec.toEnvelope` serialization & unchecked PlonK VK JSON:** `SnarkjsJsonCodec.toEnvelope(SnarkjsProof, SnarkjsVerificationKey, ...)` serialized Java record field names (`piA`, `vkAlpha1`, numeric JSON numbers) rather than snarkjs JSON (`pi_a`, `vk_alpha_1`, canonical decimal strings), producing envelopes that failed `SnarkjsJsonCodec.parseProof` and computed `vkRef` hashes over non-snarkjs JSON. Meanwhile `SnarkjsPlonkCodec.toEnvelopeFromJson` never parsed or cross-checked `vkJson` (`protocol`, `curve`, `nPublic`).
3. **Lossy and non-canonical `CborEnvelopeCodec`:** `CborEnvelopeCodec` documented optional keys `8` (`domainTag`), `9` (`metadata`), and `10` (`proofFormat`) but dropped them on encode/decode — stripping `proofFormat` needed by format-sensitive verifiers (e.g., Cardano PlonK profiles and BBS). On decode it used default `CborDecoder.decode(cbor)` without duplicate-key rejection, preallocation bounds, unknown-key rejection, version bounds, minimal `BigInteger` checks, or canonical re-encoding verification.
4. **Incomplete legacy BN254 gating:** `ZkeyImporter.importWtns` lacked both `LegacyCurvePolicy.requireLegacyBn254Enabled()` and binary framing/prime/bounds validation. Legacy BN254 verifiers (`Groth16BN254Verifier`, `PlonkBN254Verifier`) also returned `isAvailable() == true` even when BN254 was disabled, and `LegacyCurvePolicy` did not explicitly warn that legacy BN254 verifiers are unsound against adversarial input.

## Threat Model & Trust Assumptions

- **Untrusted inputs:** `ZkProofEnvelope` (including `version`, `proofSystem`, `curve`, `circuitId`, `proofBytes`, `publicInputs`, `vkRef`, `proofFormat`, `domainTag`, `metadata`), CBOR envelope bytes, snarkjs proof/VK/public JSON strings, and `.wtns` streams.
- **Trusted boundary:** `VerificationMaterial` registered in a trusted `VerificationKeyRegistry` (or passed directly by the caller after binding it to the intended statement/circuit) represents the expected statement and verification key bytes.
- **Threats addressed:**
- Cross-circuit or cross-VK substitution via mismatched `circuitId`, `proofSystem`, `curve`, `ByHash` VK digest, or spoofed `material.vkHash()`.
- Silent cross-backend divergence where PlonK rejects a substituted envelope while Groth16 or BBS accepts it.
- CBOR malleability, duplicate-key smuggling, unknown-key injection, preallocation DoS, and silent stripping of `proofFormat`/`domainTag`/`metadata` on round-trip.
- Accidental exposure or unbounded parsing of legacy BN254 `.wtns` files or BN254 verifiers via `ServiceLoader` or `VerifierRegistry`.
- **Non-goals:** Cryptographic proof validity still does not confer application authorization, replay protection, or `ScriptContext` binding.

## Normative References

- **RFC 8949 §4.2 & §10** — Deterministic Encoding for CBOR (shortest-form integers, definite lengths, no duplicate keys, unsigned bytewise canonical map key ordering, and resource-bounded parsing before object-tree materialization).
- **snarkjs 0.7.6** — Groth16 and PlonK JSON proof / verification key / public input schemas (`pi_a`, `pi_b`, `pi_c`, `vk_alpha_1`, `vk_beta_2`, `vk_gamma_2`, `vk_delta_2`, `vk_alphabeta_12`, `IC`, decimal strings) and binary `.wtns` section format (`magic = "wtns"`, `src/wtns_utils.js:68-71` max version `2`, section 1 header + section 2 witness).
- **CFRG BBS draft-10 (`draft-irtf-cfrg-bbs-signatures-10`)** — ZeroJ's `BbsPresentationCodec` strict canonical CBOR model and dual-suite (`SHA-256` / `SHAKE-256`) `BbsZkVerifier` adapter.
- **ADR-0044** — Module surface isolation (`zeroj-backend-spi`, opt-in backends, no BN254 in default `ServiceLoader` surface).
- **ADR-0062** — BLS12-381 Groth16 pure-Java / `blst` verifier parity and canonical point encoding (`z = 1`).

## Security Invariants

1. **I1 — Uniform envelope/material binding:** `VerifierOrchestrator.verify(envelope, material)` AND each BLS12-381 verifier (`Groth16BLS12381Verifier`, `Groth16BLS12381PureJavaVerifier`, `PlonkBLS12381Verifier`, `BbsZkVerifier`) enforce:
- `envelope != null` (else `MALFORMED_ENVELOPE`), `material != null` (else `VK_MISMATCH`);
- `1 <= envelope.version() <= ZkProofEnvelope.currentVersion()` (else `MALFORMED_ENVELOPE`);
- `material.proofSystemId() == envelope.proofSystem()`, `material.curveId() == envelope.curve()`, and `material.circuitId().equals(envelope.circuitId())` (else `VK_MISMATCH`);
- `actualVkHash = SHA-256(material.vkBytes())`: if `material.vkHash()` is present it must equal `actualVkHash`, and if `envelope.vkRef()` is `ByHash` it must equal `actualVkHash` (else `VK_MISMATCH`).
- Binding MUST NOT assume Groth16 JSON structure for `material.vkBytes()` (BBS uses raw 96-byte G2 public keys) and MUST NOT reject empty `publicInputs` (valid for BBS and 0-public-input circuits).
- For BBS presentations, authenticated disclosures (revealed messages, header, and presentation header) are carried inside the canonical CBOR presentation in `envelope.proofBytes()`, not in the SNARK field-element vector; `BbsZkVerifier` therefore rejects non-empty `envelope.publicInputs()` with `INVALID_PUBLIC_INPUTS`.
2. **I2 — Registry integrity & thread safety:** `InMemoryVerificationKeyRegistry.register(material)` rejects any `material` whose declared `vkHash` disagrees with `SHA-256(material.vkBytes())`. `VerifierRegistry` uses thread-safe `CopyOnWriteArrayList` storage and returns an immutable snapshot from `all()`.
3. **I3 — Backend metadata validation & error classification:** Both BLS12-381 Groth16 verifiers decode JSON explicitly as UTF-8, validate optional `proofFormat` (`"snarkjs-json"` if present), validate parsed JSON `protocol == "groth16"`, `curve == "bls12-381"`, and `IC.size() == nPublic + 1`, and map `CodecException` to `MALFORMED_ENVELOPE` (preserving pure-Java / `blst` `ReasonCode` parity). `BbsZkVerifier` maps malformed CBOR/key `BbsException` errors to `MALFORMED_ENVELOPE` rather than `INTERNAL_ERROR`.
4. **I4 — Verifiable snarkjs JSON round-trip:** `SnarkjsJsonCodec.serializeProofToBytes` and `serializeVkToBytes` (exposed via `toVerificationKeyBytes`) emit canonical snarkjs JSON (`pi_a`, `pi_b`, `pi_c`, `vk_alpha_1`, ..., `IC`, canonical decimal strings) that round-trips through `parseProof` / `parseVerificationKey` and verifies against BLS12-381 Groth16 verifiers. `SnarkjsPlonkCodec.toEnvelopeFromJson` parses and cross-checks `vkJson` (`protocol == "plonk"`, `vk.curve() == proof.curve()`, `vk.nPublic() == inputs.size()`).
5. **I5 — Strict canonical CBOR envelope wire format (v1):** `CborEnvelopeCodec` carries required keys `1..7` and optional keys `8` (`domainTag`, omitted when empty), `9` (`metadata`, omitted when empty, keys sorted in RFC 8949 §4.2.1 unsigned bytewise order of encoded UTF-8 keys), and `10` (`proofFormat`, omitted when absent). Envelopes with none of keys `8..10` populated emit the exact existing 7-key v1 bytes. Encoding rejects malformed UTF-16 (unpaired surrogates) via `CodingErrorAction.REPORT` and writes canonical CBOR directly without relying on signed-byte map re-sorting. Decoding parses sequentially without recursion and enforces before collection allocation:
- `MAX_CBOR_BYTES` bound, definite-length shortest-form CBOR only, and exact remaining-byte checks on declared collection/string lengths;
- single definite-length CBOR map with strictly ascending keys in `{1..10}` (no duplicate, out-of-order, or unknown keys, and no nesting deeper than the envelope's shallow schema);
- `1 <= version <= ZkProofEnvelope.currentVersion()` and strict UTF-8 decoding;
- minimal non-negative `BigInteger.toByteArray()` encodings in `publicInputs`;
- strict omission of empty optional keys (`domainTag == ""`, `metadata == {}`, blank `proofFormat`);
- zero trailing bytes and byte-for-byte canonical round-trip equality (`Arrays.equals(cbor, encode(decoded))`).
Local non-wire fields (`proofUri`, `proofHash`, `createdAt`, `producerId`) are explicitly documented as not serialized in CBOR.
6. **I6 — Legacy BN254 fail-closed isolation:** `ZkeyImporter.importWtns` enforces `LegacyCurvePolicy.requireLegacyBn254Enabled()`, pinned `.wtns` version range `1..2` (matching `snarkjs 0.7.6` `src/wtns_utils.js:68-71`), bounded section framing, exact BN254 scalar prime `FR`, and canonical scalar range `< FR`. `Groth16BN254Verifier` and `PlonkBN254Verifier` report `isAvailable() == LegacyCurvePolicy.legacyBn254Enabled()`, carry explicit Javadoc/policy warnings that they are unsound against adversarial input, and remain absent from all `META-INF/services/org.zeroj.backend.spi.ZkVerifier` files.

## Decision & Alternatives Considered

- **Shared `EnvelopeBinding` helper in `zeroj-backend-spi`:** Placing the shared binding logic in `org.zeroj.backend.spi.EnvelopeBinding` avoids duplicating hash/binding checks across `VerifierOrchestrator`, Groth16, PlonK, and BBS while keeping `zeroj-backend-spi` free of `zeroj-codec` dependencies.
- *Alternative rejected:* Wiring `EnvelopeValidator` centrally. `EnvelopeValidator` currently rejects empty `publicInputs`, which would break BBS presentations and 0-public-input circuits.
- **Keep CBOR envelope version = 1 with optional keys 8–10:** Existing 7-key envelopes (where `domainTag` is empty, `metadata` is empty, and `proofFormat` is absent) remain 100% byte-compatible with v1, while envelopes carrying `domainTag`/`metadata`/`proofFormat` now round-trip losslessly using the exact key numbers already documented in `CborEnvelopeCodec`.
- *Alternative rejected:* Bumping to version 2 or stripping keys 8–10 from Javadoc. Bumping version unnecessarily breaks existing v1 7-key fixtures, while dropping `proofFormat` breaks Cardano PlonK and BBS envelope round-trips.
- **Fix `SnarkjsJsonCodec.toEnvelope` rather than deleting it:** Emitting true snarkjs JSON fixes the broken API in place while keeping binary/source compatibility for callers; `toVerificationKeyBytes(SnarkjsVerificationKey)` lets callers materialize matching `VerificationMaterial` cleanly, while raw-file callers continue to prefer `toEnvelopeFromJson`.

## Compatibility

- **Breaking for callers passing mismatched envelopes/materials:** Callers that previously constructed Groth16 or BBS envelopes with dummy/mismatched `ByHash(new byte[32])` or mismatched `circuitId` now receive `VK_MISMATCH` instead of `proofValid = true` (matching `PlonkBLS12381Verifier`).
- **Breaking for non-canonical CBOR inputs:** `CborEnvelopeCodec.decode` now rejects non-canonical CBOR (non-minimal ints, indefinite lengths, duplicate/unknown/out-of-order keys, padded `BigInteger` bytes) and `encode` now emits keys `8..10` when populated.
- **Breaking for ungated `ZkeyImporter.importWtns`:** Callers of `ZkeyImporter.importWtns` (BN254) must now opt in via `-Dzeroj.allowLegacyBn254=true` or `ZEROJ_ALLOW_LEGACY_BN254=true`, matching `ZkeyImporter.importZkey`.

## Implementation Milestones

- **M1:** Add `EnvelopeBinding` in `zeroj-backend-spi`; harden `VerifierOrchestrator`, `InMemoryVerificationKeyRegistry`, `VerifierRegistry`, `Groth16BLS12381Verifier`, `Groth16BLS12381PureJavaVerifier`, `PlonkBLS12381Verifier` (reuse `EnvelopeBinding`), and `BbsZkVerifier`.
- **M2:** Fix `SnarkjsJsonCodec.toEnvelope` / `serializeProofToBytes` / `serializeVkToBytes` + `toVerificationKeyBytes` and `SnarkjsPlonkCodec.toEnvelopeFromJson` VK validation.
- **M3:** Implement keys 8–10 and strict canonical CBOR validation in `CborEnvelopeCodec`.
- **M4:** Gate and harden `ZkeyImporter.importWtns`, override `isAvailable()` and add unsoundness warnings on `LegacyCurvePolicy` / `Groth16BN254Verifier` / `PlonkBN254Verifier`, add ServiceLoader isolation tests, and update `www/src/content/docs/guides/verifying/off-chain.md`.

## Verification & Test Strategy

- **Positive + negative binding tests** across `VerifierOrchestrator`, `InMemoryVerificationKeyRegistry`, `Groth16BLS12381Verifier` & `Groth16BLS12381PureJavaVerifier` (with exact blst/pure-Java `ReasonCode` parity), and `BbsZkVerifier` (including `ById`, `ByHash`, dual SHA-256/SHAKE-256 suites, malformed CBOR -> `MALFORMED_ENVELOPE`).
- **Round-trip + verification test** proving `SnarkjsJsonCodec.toEnvelope(...)` produces a proof verifiable by BLS12-381 Groth16 verifiers, plus negative `SnarkjsPlonkCodec.toEnvelopeFromJson` VK mismatch tests.
- **Adversarial CBOR tests** rejecting indefinite lengths, non-minimal ints, duplicate keys, unknown keys, out-of-order keys, padded `BigInteger`, bad versions, and empty optional keys, plus round-trip tests preserving `domainTag`, `metadata`, and `proofFormat`.
- **Legacy BN254 tests** confirming `ZkeyImporter.importWtns` rejects disabled policy, wrong prime, bad framing, and non-canonical witness scalars; confirming `Groth16BN254Verifier` / `PlonkBN254Verifier` report `isAvailable() == false` by default and never appear in `META-INF/services/org.zeroj.backend.spi.ZkVerifier`.

## Remaining Production & Audit Gates

- Independent external cryptographic review of all verifier backends, codecs, and on-chain/off-chain integration boundaries remains required prior to production or value-bearing promotion.
- Application-level authorization, replay protection, nullifier tracking, and `ScriptContext` binding remain the application's responsibility outside the generic verifier SPI.
40 changes: 26 additions & 14 deletions www/src/content/docs/guides/verifying/off-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ The `ReasonCode` enum covers both sides:
|---|---|
| `INVALID_PROOF` | A backend: the pairing check failed, or a point was malformed |
| `INVALID_PUBLIC_INPUTS` | A backend: wrong count, or a value outside `[0, r)` |
| `INTERNAL_ERROR` | A backend: an unexpected exception, e.g. unparseable proof bytes |
| `INTERNAL_ERROR` | A backend: an unexpected internal or native-loading error |
| `UNKNOWN_VERIFICATION_KEY` | The orchestrator: the envelope's `vkRef` isn't registered |
| `UNSUPPORTED_PROOF_SYSTEM` | The orchestrator (no backend registered) or a backend given the wrong proof system |
| `VK_MISMATCH`, `UNSUPPORTED_CURVE`, `MALFORMED_ENVELOPE` | Consistency checks in the PlonK and BBS backends, and in your own gate |
| `VK_MISMATCH`, `UNSUPPORTED_CURVE`, `MALFORMED_ENVELOPE` | Envelope-to-material binding and format checks in `VerifierOrchestrator` and all BLS12-381 backends (Groth16, PlonK, BBS), plus your own policy gate |
| `UNKNOWN_CIRCUIT`, `RETIRED_CIRCUIT`, `STALE_STATE_ROOT`, `DUPLICATE_NONCE`, `UNAUTHORIZED_SUBMITTER`, `USED_NULLIFIER` | Your policy layer |

## Choose a backend
Expand All @@ -142,9 +142,10 @@ The PlonK verifier is an experimental, opt-in path. Groth16 on BLS12-381 is the
the current release; don't treat PlonK verification as production evidence.
:::

The legacy BN254 verifiers are not registered with `ServiceLoader` and refuse to run unless you
start the JVM with `-Dzeroj.allowLegacyBn254=true`. BN254 is not a Cardano curve; ignore it
unless you are running old off-chain experiments.
The legacy BN254 verifiers are not registered with `ServiceLoader`, report `isAvailable() == false`
by default, and refuse to run unless you start the JVM with `-Dzeroj.allowLegacyBn254=true`. They
are **unsound against adversarial input** and BN254 is not a Cardano curve; ignore them unless you
are running trusted local off-chain experiments.

## Route proofs with registries

Expand Down Expand Up @@ -201,10 +202,18 @@ The Groth16 backends fail closed on malformed input before any pairing work:
An infinity `IC` entry would leave a public input unbound, so ZeroJ's setup refuses to create
one and every verifier rejects it.

What the Groth16 backends do **not** check: that `envelope.circuitId()` or `envelope.vkRef()`
matches the `VerificationMaterial` you passed. They verify the proof against whatever key you give
them. (The experimental PlonK verifier does compare them and returns `VK_MISMATCH`.) Do that
comparison yourself, as the next section shows.
- **Envelope-to-material binding.** `VerifierOrchestrator` and every BLS12-381 backend (Groth16
pure-Java, Groth16 blst, PlonK, and BBS) verify that `envelope.version()`, `proofSystem()`,
`curve()`, `circuitId()`, `material.vkHash()`, and `ByHash` `vkRef()` match the provided
`VerificationMaterial` (`VK_MISMATCH` on mismatch; `MALFORMED_ENVELOPE` on invalid version,
unsupported `proofFormat`, or unparseable JSON/CBOR). The Groth16 backends also verify the
parsed JSON `protocol` (`"groth16"`) and `curve` (`"bls12-381"`). For BBS, authenticated
disclosures are carried inside the canonical CBOR presentation in `proofBytes()`, so
`BbsZkVerifier` requires `envelope.publicInputs()` to be empty (`INVALID_PUBLIC_INPUTS` otherwise).

Your application gate must still decide *which* `circuitId` and pinned VK hash an endpoint expects
(for example, rejecting a valid proof for `circuit-a` on an endpoint that requires `circuit-b`), as
the next section shows.

## Build a verification service

Expand Down Expand Up @@ -308,11 +317,14 @@ ZkProofEnvelope decoded = CborEnvelopeCodec.decode(cbor); // bounded, typed Cod
byte[] id = CanonicalHash.hash(envelope); // SHA-256 over the core fields
```

The CBOR encoding carries the core fields only: version, proof system, curve, circuit ID, proof
bytes, public inputs, and the VK reference. Optional fields such as `metadata()` (where annotated
circuits put their version) and `proofFormat()` are **not** carried, and `CanonicalHash` ignores
them too. If your policy depends on them, send them another way, or rely on the pinned VK hash,
which already distinguishes circuit versions.
The CBOR encoding is strictly canonical (RFC 8949 §4.2): keys `1..7` carry version, proof system,
curve, circuit ID, proof bytes, minimal non-negative `BigInteger` public inputs, and the VK
reference, while optional keys `8` (`domainTag`), `9` (`metadata`, sorted canonically), and `10`
(`proofFormat`) are included whenever populated and omitted when empty. `CborEnvelopeCodec.decode`
fails closed on duplicate keys, unknown keys, non-minimal integers, indefinite lengths, padded
`BigInteger` bytes, or any non-canonical re-encoding. Local builder metadata (`proofUri`,
`proofHash`, `createdAt`, `producerId`) is not carried in CBOR, and `CanonicalHash` hashes the core
fields (`1..7`) only.

## Write a custom backend

Expand Down
Loading
Loading