feat: Add structural guards for proof, VK, and public input validation (ZK-075)#433
Merged
ANAVHEOBA merged 2 commits intoANAVHEOBA:mainfrom May 1, 2026
Conversation
…n (ZK-075) - Added pre-deserialization validation for all proof, VK, and public input components - Validates byte lengths and vector counts before touching cryptographic operations - Added 8 new granular error codes for structural validation failures - Implemented structural guards in both contract (Rust) and SDK (TypeScript) - Added comprehensive test suites with 20+ contract tests and 30+ SDK tests - Malformed payloads now fail fast with explicit errors before expensive operations Contract changes: - validate_proof_structure(): Checks A (64B), B (128B), C (64B) - validate_vk_structure(): Checks all VK points and IC vector (9 points) - validate_public_inputs_structure(): Checks all 8 fields (32B each) SDK changes: - validateProofStructure(): Validates 256-byte proof format - validateVkStructure(): Validates all VK components and IC vector - validatePublicInputsStructure(): Validates byte arrays and hex strings - extractProofComponents(): Safely extracts A, B, C from proof bytes Wave Issue Key: ZK-075
… structural guards Resolved conflicts in: - contracts/privacy_pool/src/crypto/verifier.rs: Combined ZK-074 and ZK-075 validations - contracts/privacy_pool/src/types/errors.rs: Merged error codes, renumbered to avoid conflicts Both validation layers now work together: - ZK-075 structural guards validate byte lengths and vector counts - ZK-074 metadata validation checks circuit IDs and public input counts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements ZK-075: Structural Guards for Proof, VK, and Public Input Shapes. The verifier path now rejects malformed byte lengths, wrong IC counts, and impossible payload shapes BEFORE deserializing elliptic-curve points or touching pairing logic.
Closes #351
Changes Made
Contract Changes (Rust)
Added 8 new granular error codes for structural validation:
VkAlphaG1WrongLength(52) - VK alpha_g1 has wrong byte length (expected 64)VkBetaG2WrongLength(53) - VK beta_g2 has wrong byte length (expected 128)VkGammaG2WrongLength(54) - VK gamma_g2 has wrong byte length (expected 128)VkDeltaG2WrongLength(55) - VK delta_g2 has wrong byte length (expected 128)VkIcVectorWrongLength(56) - VK gamma_abc_g1 vector has wrong length (expected 9)VkIcPointWrongLength(57) - VK gamma_abc_g1 contains a point with wrong byte lengthPublicInputWrongLength(63) - Public input field has wrong byte length (expected 32)Added three structural validation functions in
verifier.rs:validate_proof_structure()validate_vk_structure()validate_public_inputs_structure()Updated
verify_proof()to call all three validation functions BEFORE any cryptographic operations.Added comprehensive test suite (
structural_guards.rs) with 20+ tests covering:SDK Changes (TypeScript)
Created
structural_guards.tsmodule with:Constants:
G1_POINT_BYTE_LENGTH = 64G2_POINT_BYTE_LENGTH = 128FIELD_ELEMENT_BYTE_LENGTH = 32EXPECTED_PUBLIC_INPUT_COUNT = 8EXPECTED_IC_VECTOR_LENGTH = 9GROTH16_PROOF_TOTAL_LENGTH = 256Functions:
validateProofStructure(proof: Uint8Array)WitnessValidationErroron malformed proofvalidateVkStructure(vk: VerifyingKeyStructure)validatePublicInputsStructure(publicInputs: Uint8Array[])validatePublicInputsHexStructure(publicInputs: string[])extractProofComponents(proof: Uint8Array)Created comprehensive test suite (
structural_guards.test.ts) with 30+ tests covering all validation functions.Updated
witness.tsto usevalidateProofStructure()from structural guards module.Documentation
Validation Flow
Before ZK-075
After ZK-075
Benefits
✅ Early Failure: Malformed payloads fail before expensive cryptographic operations
✅ Explicit Errors: Specific error codes identify exactly which component is malformed
✅ Consistent Validation: Same invariants enforced in both contract and SDK
✅ Performance: Structural checks are O(1) vs. O(n) for curve operations
✅ Security: Prevents malformed data from reaching cryptographic code
✅ Debuggability: Clear error messages help developers identify issues quickly
Expected Byte Lengths
Acceptance Criteria
✅ Malformed proof or VK structures fail with explicit pre-verification errors
✅ Contract and SDK tests cover short, long, and count-mismatch payloads
✅ Verifier code is no longer the first place malformed data is discovered
✅ Structural guards run before any elliptic curve deserialization
✅ Error messages clearly identify which component is malformed
Testing
Contract Tests
SDK Tests
Files Changed
Contract Files (4)
contracts/privacy_pool/src/types/errors.rs- Added 8 new error codescontracts/privacy_pool/src/crypto/verifier.rs- Added 3 validation functionscontracts/privacy_pool/src/test/structural_guards.rs- NEW: 20+ testscontracts/privacy_pool/src/test/mod.rs- Added structural_guards moduleSDK Files (3)
sdk/src/structural_guards.ts- NEW: Validation functions and constantssdk/src/structural_guards.test.ts- NEW: 30+ testssdk/src/witness.ts- Updated to use structural guardsDocumentation Files (2)
ZK-075_IMPLEMENTATION_SUMMARY.md- NEW: Complete implementation detailsSTRUCTURAL_GUARDS_QUICK_REF.md- NEW: Quick reference guidePerformance Impact
Structural guards add minimal overhead:
Estimated savings on malformed payload:
Migration Notes
No breaking changes - this is purely additive validation. Existing valid payloads continue to work unchanged.
Wave Issue Key: ZK-075