Repository navigation
security(circuit-dsl): enforce index bounds in arrayAccess and add arrayAccessOrZero (ADR-0065) - #119
Conversation
…rayAccessOrZero (ADR-0065) Implements ADR-0065 to close silent out-of-bounds-to-zero arrayAccess soundness bug: - CircuitAPIImpl.arrayAccess: validate lifecycle/nulls atomically, reject empty arrays, and emit Gate.LinComb(selectorSum) + assertEqual(selectorSum, oneWire) (1 R1CS constraint, 1 wire) so 0 <= index < arr.length is strictly enforced across CircuitAPI, SignalBuilder, and Mux. - CircuitAPI.arrayAccessOrZero / SignalBuilder.arrayAccessOrZero: add explicit total-function opt-in returning 0 on out-of-bounds indices without an index constraint, with soundness warning Javadoc. - ZkMpf.PathSignals.at: opt into arrayAccessOrZero for independently bounded active reads and valid-masked padding rows, preserving 100% of pinned MPF R1CS SHA-256 digests. - Add ADR-0065 (Proposed), ADR-0010 back-reference, user-guide updates, ArrayAccessSoundnessTest (BN254 & BLS12-381, R1CS & PlonK), and Mux.arrayAccess bounds tests.
|
Initial automated review · Aster Hi @satran004, thanks for the PR! 🙏 This is an automatic first check by an AI reviewer. It does not replace maintainer review and may be wrong. Reviewed commit: No issues found in this first check. Not tested: Code and tests were read only; I did not run the build or tests. |
|
reviewer1 · Changes requested on The built-in lookup constrains the index correctly. Each equality selector is bound by
I reran the Gradle test tasks for |
|
Verdict: CHANGES REQUESTED Reviewer: reviewer2 · Review of ADR-0065 and its implementation (PR #119) at The fix is sound, and I could not break it. Each The one change I need is to the ADR's cost and backend statements, which are only true for R1CS (F1). The other findings are P3. No code change to the relation is requested. F1 [P2] Cost and backend scope are stated only for R1CS; the Halo2 backend is not coveredLocation:
Problem:
Evidence: I wrote a temporary probe in the worktree and deleted it afterwards. It compares the strict lookup with
Soundness holds on Halo2. A forged out-of-range witness is rejected there just as on R1CS and PlonK; see F2. Requested revision:
F2 [P3] The constraint-level evidence for I2 covers only
|
| Suite | Tests | Failures | Errors | Skipped |
|---|---|---|---|---|
zeroj-circuit-dsl |
134 | 0 | 0 | 1 |
zeroj-circuit-lib |
1313 | 0 | 0 | 26 |
zeroj-circuit-annotation-api |
22 | 0 | 0 | 0 |
zeroj-circuit-annotation-processor |
31 | 0 | 0 | 0 |
zeroj-mpf-poseidon |
48 | 0 | 0 | 0 |
zeroj-jmt-poseidon |
15 | 0 | 0 | 0 |
Mutation checks (both reverted afterwards):
- With
assertEqual(selectorSum, oneWire)removed, 8 of 11ArrayAccessSoundnessTestcases fail, and so doesCircuitLibTest.mux_arrayAccess_enforcesBoundsAndRejectsEmpty. The new tests catch the bug. - With
ZkMpf.PathSignals.atswitched to strictarrayAccess, 4 of 48zeroj-mpf-poseidontests fail: the honest padding-row test and three pinned digest/count tests.
Probe: a forged-witness and cost comparison across R1CS, PlonK and Halo2 on BLS12-381 and BN254, with results as in F1 and F2. It was never committed and has been deleted.
Caller census: I confirmed that the only production callers are SignalBuilder, Mux and ZkMpf. The local zeroj-usecases checkout has no arrayAccess callers.
Not run:
- the full
./gradlew build; - PR CI: some jobs were still pending when I checked, and
Test (macos-arm64)fails after 2s, the same runner-configuration failure seen on security(bbs): harden on-chain/off-chain BBS verification (ADR-0064) and upgrade JuLC 0.1.0-pre22 / CCL 0.8.0-pre8 #118.
No files in the PR were changed by this review.
This is design acceptance only. It does not certify the implementation, its tests or its security. ADR-0065 itself records that an external circuit audit is still needed before value-bearing use.
…Comb, Halo2/control tests, ADR-0065 per-backend costs) Addresses all review findings from reviewer1 and reviewer2 on PR #119: - Gate.LinComb: defend terms with List.copyOf in the compact constructor (covers both arrayAccess and fromBinary) and add immutability regression test in ArrayAccessSoundnessTest. - ArrayAccessSoundnessTest: extend direct forged-witness matrix verification to {n, n + 1, p - 1} across R1CS, PlonK, and Halo2 on both BN254 and BLS12-381, add matching arrayAccessOrZero positive control, and assert exact per-backend row deltas (+1 R1CS inlined, +n PlonK, +n Halo2). - ADR-0065: record per-backend costs (R1CS +1 inlined / +2 under pressure mode, PlonK & Halo2 +n, new keys on all backends), add Halo2 to I2/M2/Verification Strategy, add empty-array Compatibility bullet and custom CircuitAPI migration note, and fix ZkMpf citations (differentLeafRoot:131, computeRoot:323, final-cursor padding wording). - Update CircuitAPI.java, ZkMpf.java comment, gadgets.md, and zeroj-circuit-lib/README.md to match.
|
Addressed all review findings from reviewer1 and reviewer2 in commit
Re-ran |
|
Verdict: APPROVED Reviewer: reviewer2 · Round 2 review of I checked the files at the new head, not the author's reply. All four of my findings are resolved in the ADR text, the code and the tests. I re-ran the affected suites and the mutation check at this head. I have no new findings.
F1.
F2.
Halo2 is also checked on honest in-range reads. The new F3.
F4.
reviewer1's points, which I also endorsed:
ValidationRun at
No files in the PR were changed by this review. No P0–P3 findings remain open. The one recorded gate is ADR-0065's external circuit audit before any value-bearing deployment. This is design acceptance only. It does not certify the implementation, its tests or its security. Commits pushed after |
|
Verdict: APPROVED (one non-blocking P3, two nits) Reviewer: reviewer3 · Independent review of ADR-0065 and its implementation at I read reviewer1's and reviewer2's comments, but I re-derived the soundness argument and re-checked the compiler paths myself rather than relying on theirs. Soundness of the relation
F1 [P3, non-blocking] Every soundness test uses an array of constants; add a witness-valued array and the R1CS inlining policiesLocation: Problem:
Evidence: I ran a temporary probe in an isolated worktree, then deleted it; nothing was committed.
So the implementation is correct. The test suite just doesn't pin these paths. Suggested revision (can be a follow-up):
N1 [nit]
|
| Suite | Tests | Failures | Errors | Skipped |
|---|---|---|---|---|
zeroj-circuit-dsl |
135 | 0 | 0 | 1 |
zeroj-circuit-lib |
1313 | 0 | 0 | 26 |
zeroj-circuit-annotation-api |
22 | 0 | 0 | 0 |
zeroj-circuit-annotation-processor |
31 | 0 | 0 | 0 |
zeroj-mpf-poseidon |
48 | 0 | 0 | 0 |
zeroj-jmt-poseidon |
15 | 0 | 0 | 0 |
- Caller census: the production callers of
arrayAccessareSignalBuilder,MuxandZkMpf(now onOrZero). The only otherCircuitAPIimplementations in the repo are the two test-onlyNonOwningCircuitAPIfixtures. No other main-code MUX scans useisEqual(index, constant(i)). - Not run: the full
./gradlew build. - PR CI when I checked:
build, gnark, snarkjs and the BLS/BBS WASM differential job had passed.Build & TestandTest (linux-x86_64)were pending.Test (macos-arm64)fails after 1s, the same runner-configuration problem seen on security(bbs): harden on-chain/off-chain BBS verification (ADR-0064) and upgrade JuLC 0.1.0-pre22 / CCL 0.8.0-pre8 #118.
No files in the PR were changed by this review.
This is design acceptance only. It does not certify the implementation, its tests or its security. ADR-0065's external circuit audit is still required before any value-bearing deployment. Commits pushed after a36e99c2 are not covered by this approval.
Summary
Implements ADR-0065 (
docs/adr/0065-array-access-bounds-soundness.md, statusProposed) to close a circuit-soundness defect whereCircuitAPI.arrayAccess(and its delegatesSignalBuilder.arrayAccessandMux.arrayAccess) silently returned0and satisfied R1CS/PlonK/Halo2 for out-of-bounds indices (index = arr.length,index = p - 1, or empty arrays).Changes
docs/adr/0065-array-access-bounds-soundness.md(Proposed) and an "Amended by ADR-0065" back-reference indocs/adr/0010-java-circuit-dsl.md, plus updateddocs/circuit-dsl-user-guide.md,www/src/content/docs/guides/circuits/circuit-dsl.md,www/src/content/docs/guides/circuits/gadgets.md, andzeroj-circuit-lib/README.md.arrayAccess(zeroj-circuit-dsl,zeroj-circuit-lib):CircuitAPIImpl.arrayAccessvalidates builder lifecycle (requireNotFrozen()), rejectsnullarray/elements/index atomically before emitting gates, rejects empty arrays (IllegalArgumentException), and emitsGate.LinComb(selectorSum)+assertEqual(selectorSum, oneWire).Gate.LinCombdefensively copiestermsviaList.copyOfin its compact constructor so published constraint graphs cannot be mutated post-build (protects botharrayAccessandfromBinary).SignalBuilder.arrayAccessandMux.arrayAccessdelegate directly and inherit the invariant automatically.arrayAccessOrZero):default Variable arrayAccessOrZero(Variable[] arr, Variable index)toCircuitAPI(overridden inCircuitAPIImplwith lifecycle/null guards) andSignalBuilder.arrayAccessOrZerowith explicit soundness warning Javadoc.ZkMpf.PathSignals.at(zeroj-mpf-poseidon/.../circuit/ZkMpf.java) to opt intoarrayAccessOrZero, sinceZkMpfalready independently constrains all active indices to[0, 63](prepareBranchProof:249,computeRoot:323,differentLeafRoot:131,verifyDifferentLeafInsertion:168) and masks inactive padding rows (which evaluate the final cursor, possibly64) withvalid.select. All pinnedZkMpfR1CS constraint counts and canonical SHA-256 digests remain 100% unchanged.Breaking Note
arrayAccesscallers outsideZkMpf):+1intermediate wire (selectorSum),+1Gate.LinComb,+1Gate.AssertEq.+1constraint row whenselectorSumis inlined;+2rows under compiler pressure mode.+nrows/gates (n - 1binary addition gates for then-termLinCombchain plus1equality gate).CircuitAPI.arrayAccess,SignalBuilder.arrayAccess, orMux.arrayAccessmust regenerate circuit-specific proving/verifying keys.arrayAccesswith an empty array previously compiled and returnedconstant(0); it now throwsIllegalArgumentExceptionat circuit definition time (opt intoarrayAccessOrZeroif total empty-array lookup is intended).CircuitAPIimplementations:CircuitBuilderalways instantiatesCircuitAPIImpl, so standard DSL circuits inherit the fix automatically. Downstream customCircuitAPIproviders passed directly intoMux.arrayAccessor otherzeroj-circuit-libgadgets still compile against the unchanged signature and must update theirarrayAccessimplementation and verify OOB rejection before regenerating keys.zeroj-mpf-poseidon: Templates and pinned R1CS SHA-256 digests are unchanged.Verification
ArrayAccessSoundnessTest(zeroj-circuit-dsl): verified on both BLS12-381 and BN254 across WitnessCalculator, R1CS, PlonK, and Halo2:0 .. n - 1(n = 1andn = 4) succeed and satisfy R1CS, PlonK, and Halo2.index ∈ {n, n + 1, p - 1}fails R1CS, PlonK, and Halo2 for strictarrayAccesswhile the identical forged witnesses pass the matchingarrayAccessOrZerocontrol circuit on all three backends.WitnessCalculatorOOB tests (n,n + 1,64,p - 1,p - 12345) rejected even with zero-valued array entries andexpected == 0.Gate.LinComb.terms()immutability guards verified.+1R1CS inlined,+nPlonK,+nHalo2) along with totalarrayAccessOrZerosemantics.CircuitLibTest.mux_arrayAccess_enforcesBoundsAndRejectsEmpty(zeroj-circuit-lib)../gradlew :zeroj-circuit-dsl:test :zeroj-circuit-lib:test :zeroj-circuit-annotation-api:test :zeroj-circuit-annotation-processor:test :zeroj-mpf-poseidon:test :zeroj-jmt-poseidon:test verifyDefaultModuleSurface verifyNoBuiltinProfileDefine— BUILD SUCCESSFUL (0 failures; all pinnedZkMpf&ZkJmtR1CS digests verified unchanged).