Skip to content

Commit acfcc38

Browse files
CopilotSteake
andcommitted
Replace placeholder with comprehensive Groth16 state circuit documentation
Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
1 parent d672a9d commit acfcc38

1 file changed

Lines changed: 91 additions & 3 deletions

File tree

docs/issue-45.md

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,93 @@
1-
# Issue 45
1+
# Issue 45: Groth16 State Circuit Constraints
22

3-
Work in progress by Emulated Coder.
3+
## Background
44

5-
Ref: #45
5+
Groth16 is a widely used zk-SNARK proving system that requires careful circuit design to ensure correctness, security, and efficiency. In our project, the state circuit is responsible for enforcing the validity of state transitions within the zero-knowledge proof. Proper constraints must be implemented to guarantee that only valid transitions are provable.
6+
7+
## Problem Statement
8+
9+
The BitCell system requires robust zero-knowledge proof circuits to ensure privacy and integrity of state transitions. Groth16 state circuit constraints are essential for:
10+
- Verifying Merkle tree state transitions
11+
- Ensuring nullifier uniqueness to prevent double-spending
12+
- Validating commitment derivation for new state elements
13+
- Maintaining consistency between old and new state roots
14+
15+
## Current Implementation Status
16+
17+
**IMPLEMENTED** - The Groth16 state circuit constraints are fully implemented in `crates/bitcell-zkp/src/state_constraints.rs`.
18+
19+
### Key Components
20+
21+
1. **StateCircuit** - Main state transition circuit with R1CS constraints for:
22+
- Merkle tree path verification for old state root
23+
- Nullifier derivation from leaf values
24+
- Commitment computation for new leaf values
25+
- Merkle tree path verification for new state root
26+
27+
2. **NullifierCircuit** - Nullifier set membership verification circuit:
28+
- Verifies whether a nullifier exists in the nullifier set
29+
- Uses Merkle tree membership proofs
30+
- Prevents double-spending attacks
31+
32+
3. **Constraint Implementation**:
33+
- Merkle tree depth: 32 levels
34+
- Uses arkworks library for R1CS constraint synthesis
35+
- Compatible with Groth16 proving system on BN254 curve
36+
- Implements proper public/private input separation
37+
38+
4. **Hash Functions**:
39+
- Simplified hash functions for proof-of-concept
40+
- Note: Production deployment should use Poseidon or other SNARK-friendly hash functions
41+
42+
### Technical Specifications
43+
44+
- **Proving System**: Groth16 (via arkworks-rs)
45+
- **Curve**: BN254 (Bn254)
46+
- **Field**: Fr (scalar field of BN254)
47+
- **Merkle Tree Depth**: 32
48+
- **Public Inputs**:
49+
- StateCircuit: old_root, new_root, nullifier, commitment
50+
- NullifierCircuit: nullifier, set_root, is_member
51+
52+
### Implementation Details
53+
54+
The circuits enforce the following constraints:
55+
56+
**StateCircuit Constraints:**
57+
1. `computed_old_root == old_root` - Verifies the old Merkle tree state
58+
2. `H(leaf) == nullifier` - Ensures proper nullifier derivation
59+
3. `H(new_leaf) == commitment` - Validates new commitment
60+
4. `computed_new_root == new_root` - Verifies the new Merkle tree state
61+
62+
**NullifierCircuit Constraints:**
63+
1. `(computed_root == set_root) == is_member` - Membership verification
64+
65+
### API Methods
66+
67+
Both circuits provide:
68+
- `setup()` - Generate proving and verifying keys
69+
- `prove(&self, pk)` - Generate a proof for the circuit instance
70+
- `verify(vk, proof, public_inputs)` - Verify a proof
71+
- `public_inputs(&self)` - Extract public inputs vector
72+
73+
## Testing
74+
75+
The implementation includes comprehensive tests:
76+
- Constraint satisfiability tests
77+
- End-to-end proof generation and verification
78+
- Merkle tree computation verification
79+
80+
## Future Improvements
81+
82+
- [ ] Replace simplified hash functions with Poseidon hash
83+
- [ ] Add range check constraints for enhanced security
84+
- [ ] Optimize constraint count for faster proving
85+
- [ ] Add batch verification support
86+
- [ ] Implement circuit-specific optimizations
87+
88+
## References
89+
90+
- [Groth16 Paper](https://eprint.iacr.org/2016/260.pdf)
91+
- [Arkworks Documentation](https://arkworks.rs/)
92+
- Implementation: `crates/bitcell-zkp/src/state_constraints.rs`
93+
- [GitHub Issue #45](https://github.com/Steake/BitCell/issues/45)

0 commit comments

Comments
 (0)