Skip to content

Centralize ZK constants and refactor withdrawal tests (ZK-002, ZK-003…#332

Merged
ANAVHEOBA merged 1 commit intoANAVHEOBA:mainfrom
xeladev4:zk-centralization-and-withdrawal-refactor
Apr 25, 2026
Merged

Centralize ZK constants and refactor withdrawal tests (ZK-002, ZK-003…#332
ANAVHEOBA merged 1 commit intoANAVHEOBA:mainfrom
xeladev4:zk-centralization-and-withdrawal-refactor

Conversation

@xeladev4
Copy link
Copy Markdown
Contributor

Summary

This PR centralizes ZK protocol constants across Noir circuits and TypeScript SDK, refactors withdrawal circuit tests into a dedicated module, adds circuit-adjacent Merkle root validation, and implements a negative tamper matrix for proof boundary validation.

Changes

ZK-002: Centralize ZK Constants

  • Created sdk/src/zk_constants.ts with centralized constants for:
    • MERKLE_TREE_DEPTH (20)
    • NOTE_SCALAR_BYTE_LENGTH (31)
    • MERKLE_NODE_BYTE_LENGTH (32)
    • ZERO_FIELD_HEX (64 zeros)
    • STELLAR_ZERO_ACCOUNT (GAAAAAAAA...WHF)
    • GROTH16_PROOF_BYTE_LENGTH (256)
    • BN254_FIELD_MODULUS and BN254_FIELD_BYTE_LENGTH
  • Created circuits/lib/src/constants.nr with shared Noir constants
  • Updated all SDK and circuit files to use centralized constants
  • Removed magic numbers from encoding, merkle, witness, note, and proof modules

ZK-003: Refactor Withdrawal Circuit Tests

  • Extracted withdrawal circuit tests from circuits/withdraw/src/main.nr into dedicated circuits/withdraw/src/tests.nr
  • Added mod tests declaration in main.nr to import the test module
  • Kept the public circuit interface stable
  • Improved code organization and maintainability

ZK-034: Circuit-Adjacent Root Validation

  • Added Merkle root validation in sdk/src/merkle.ts:
    • Rejects all-zero roots (empty root check)
    • Rejects non-canonical roots (>= field modulus)
  • Extended WitnessValidationError with MERKLE_ROOT error code
  • Added dedicated test suite in sdk/test/merkle_root_validation.test.ts

ZK-037: Negative Tamper Matrix

  • Implemented negative test matrix in sdk/test/verification_harness.test.ts
  • Tests tampering with one public input at a time:
    • recipient
    • relayer
    • denomination (amount)
    • fee
  • Uses deterministic mock verifier to ensure proof verification fails for tampered inputs
  • Protects the proof boundary against invalid public inputs

Testing

All existing tests should remain green after migration. New test suites added:

  • sdk/test/merkle_root_validation.test.ts - root canonical and non-zero validation
  • Extended sdk/test/verification_harness.test.ts - negative tamper matrix

Fixes

…, ZK-034, ZK-037)

- Create shared constants modules in SDK (zk_constants.ts) and Noir (lib/src/constants.nr)
- Centralize tree depth, field widths, zero-address semantics, amount encoding
- Extract withdrawal circuit tests from main.nr into dedicated tests.nr module
- Add circuit-adjacent Merkle root validation (canonical + non-zero checks)
- Build negative tamper matrix for proof boundary validation
- Update all SDK and circuit files to use centralized constants
- Add MERKLE_ROOT error code for root-specific validation

Fixes ANAVHEOBA#246, ANAVHEOBA#247, ANAVHEOBA#278, ANAVHEOBA#281
@ANAVHEOBA ANAVHEOBA merged commit 4350b46 into ANAVHEOBA:main Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment