Skip to content

Commit 0e9b2b0

Browse files
authored
feat: ECCVM Fuzzer (#16503)
A fuzzer for ECCVM that checks that for various sequences of legitimate operations ECCVM can create a correct trace
2 parents ec77233 + e6f0249 commit 0e9b2b0

File tree

6 files changed

+556
-3
lines changed

6 files changed

+556
-3
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# ECCVM Fuzzer
2+
3+
This document describes the ECCVM (Elliptic Curve Circuit Virtual Machine) fuzzer implementation, which provides comprehensive testing of the ECCVM circuit builder and trace checker.
4+
5+
## Overview
6+
7+
The ECCVM fuzzer generates random ECC operations to test the ECCVM circuit builder. It checks the correctness of the trace without constructing the full proof.
8+
9+
10+
## Architecture
11+
12+
### Data Structures
13+
14+
The fuzzer operates on the following data structures:
15+
16+
```cpp
17+
// Operation types for the fuzzer
18+
enum class OpType : uint8_t {
19+
ADD = 0, // Add a point to the accumulator
20+
MUL = 1, // Multiply a point by a scalar and add to accumulator
21+
EQ_AND_RESET = 2, // Check equality and reset accumulator
22+
MERGE = 3, // Merge operation (used internally)
23+
EMPTY_ROW = 4, // Insert empty row for testing
24+
MAX_OP = 5
25+
};
26+
27+
struct FieldVMDataChunk {
28+
std::array<uint8_t, 64> data; // 64 bytes of FieldVM data
29+
};
30+
31+
struct SingleOp {
32+
OpType op_type; // Operation type
33+
uint8_t generator_index; // Generator index and top bit for negating the generator
34+
uint8_t scalar_index; // Scalar index (0-255)
35+
};
36+
37+
struct FuzzerTuple {
38+
FieldVMDataChunk fieldvm_data; // FieldVM data for scalar computation
39+
SingleOp operation; // ECCVM operation
40+
};
41+
```
42+
43+
### Operation Flow
44+
45+
1. **Input Processing**: The fuzzer processes `FuzzerTuple` structures containing FieldVM data and ECC operations.
46+
47+
2. **FieldVM Computation**: Uses FieldVM to precompute scalars from the input data with controlled settings:
48+
- Disabled heavy operations (inversion, square root, batch inversion, power, division)
49+
- Limited to 65536 steps for performance
50+
51+
3. **Point Generation**: Creates base generators and linear combinations for testing.
52+
53+
4. **Operation Execution**: Processes operations through the ECCOpQueue:
54+
- ADD: Adds points to accumulator
55+
- MUL: Multiplies points by scalars and adds to accumulator
56+
- EQ_AND_RESET: Checks equality and resets accumulator
57+
- MERGE: Merges operations
58+
- EMPTY_ROW: Inserts empty rows for testing
59+
60+
5. **Circuit Validation**: Uses `ECCVMTraceChecker::check()` to validate the circuit.
61+
62+
## Conditional Skip Optimization
63+
64+
The fuzzer includes conditional skip optimization that is only enabled in fuzzing builds:
65+
66+
### Fuzzing Builds (`FUZZING` macro defined)
67+
- Skip optimization is enabled for performance
68+
- Relations with `skip` methods are skipped when their skip condition is met
69+
- Currently only `ECCVMSetRelation` has a skip method
70+
71+
### Production Builds (default)
72+
- Skip optimization is disabled for maximum security
73+
- All relations are always accumulated
74+
- Ensures no performance optimizations affect correctness
75+
76+
### Skip Conditions
77+
78+
Currently, only `ECCVMSetRelation` implements skip optimization:
79+
80+
```cpp
81+
template <typename AllEntities> inline static bool skip(const AllEntities& in)
82+
{
83+
// Skip when no non-trivial copy constraints and no transcript operations
84+
return (in.z_perm - in.z_perm_shift).is_zero() &&
85+
in.transcript_mul.is_zero() &&
86+
in.lagrange_last.is_zero();
87+
}
88+
```
89+
90+
## Build Configuration
91+
92+
### Compilation Flags
93+
94+
```bash
95+
# For fuzzing builds (with skip optimization)
96+
clang++ -DFUZZING -fsanitize=fuzzer -O2 -g eccvm.fuzzer.cpp -o eccvm_fuzzer
97+
98+
# For production builds (without skip optimization)
99+
clang++ -O2 -g eccvm.fuzzer.cpp -o eccvm_fuzzer
100+
```
101+
102+
### Function Signature
103+
104+
The fuzzer function signature changes based on build configuration:
105+
106+
```cpp
107+
// Fuzzing builds
108+
bool ECCVMTraceChecker::check(Builder& builder,
109+
numeric::RNG* engine_ptr,
110+
bool disable_fixed_dyadic_trace_size);
111+
112+
// Production builds
113+
bool ECCVMTraceChecker::check(Builder& builder,
114+
numeric::RNG* engine_ptr);
115+
```
116+
117+
## Usage
118+
119+
### Running the Fuzzer
120+
121+
```bash
122+
# Basic fuzzing run
123+
./eccvm_fuzzer
124+
125+
# Run with specific corpus directory
126+
./eccvm_fuzzer corpus/
127+
128+
# Run with custom parameters
129+
./eccvm_fuzzer -max_len=1024 -timeout=30 corpus/
130+
```
131+
132+
### Fuzzer Parameters
133+
134+
- **Input Size**: Must be at least `sizeof(FuzzerTuple)` bytes
135+
- **Number of Operations**: Determined by `Size / sizeof(FuzzerTuple)`
136+
- **FieldVM Data**: 64 bytes per operation for scalar computation
137+
- **Generator Indices**: 0-255 range, with modulo operations for safety
138+
- **Scalar Indices**: 0-255 range, used to select precomputed scalars
139+
140+
## Error Handling
141+
142+
The fuzzer includes comprehensive error handling:
143+
144+
1. **Input Validation**: Checks minimum input size and valid operation count
145+
2. **Bounds Checking**: All array accesses are bounds-checked
146+
3. **Exception Handling**: Catches and reports exceptions without crashing
147+
4. **Circuit Validation**: Reports detailed failure information when circuit check fails
148+
149+
### Error Reporting
150+
151+
When the circuit validation fails, the fuzzer reports:
152+
- Number of operations
153+
- Operation sequence that caused failure
154+
- Generator indices
155+
- Detailed operation information including infinity checks and negation flags
156+
157+
## Performance Considerations
158+
159+
### Fuzzing Builds
160+
- **Skip Optimization**: Reduces computation for inactive relations
161+
- **FieldVM Settings**: Disabled heavy operations for better performance
162+
- **Controlled Data Size**: Limited FieldVM data to prevent excessive computation
163+
164+
### Production Builds
165+
- **Maximum Security**: All relations always accumulated
166+
- **No Performance Optimizations**: Ensures correctness over speed
167+
168+
## Testing Strategy
169+
170+
The fuzzer is designed to test:
171+
172+
1. **Basic Operations**: ADD, MUL, EQ_AND_RESET operations
173+
2. **Edge Cases**: Points at infinity, negation, empty operations
174+
3. **Complex Sequences**: Multiple operations with various combinations
175+
4. **Circuit Correctness**: Validation through ECCVMTraceChecker
176+
5. **Memory Safety**: Proper bounds checking and error handling
177+
178+
## Integration with LibFuzzer
179+
180+
The fuzzer integrates seamlessly with LibFuzzer:
181+
182+
1. **Automatic Discovery**: LibFuzzer automatically detects and uses the fuzzer
183+
2. **Corpus Management**: Works with LibFuzzer's corpus management features
184+
3. **Crash Reporting**: Integrates with LibFuzzer's crash reporting
185+
4. **Performance Metrics**: Provides performance data for optimization
186+
187+
## Troubleshooting
188+
189+
### Common Issues
190+
191+
1. **Compilation Errors**: Ensure all required headers are included
192+
2. **Runtime Errors**: Check that input data is properly aligned
193+
3. **Performance Issues**: Verify skip optimization is enabled in fuzzing builds
194+
4. **Memory Issues**: Check for proper bounds checking and error handling
195+
196+
### Debugging
197+
198+
Enable debug output by setting environment variables:
199+
200+
```bash
201+
export FUZZER_DEBUG=1
202+
./eccvm_fuzzer
203+
```
204+
205+
## Contributing
206+
207+
When modifying the fuzzer:
208+
209+
1. **Test Thoroughly**: Run extensive fuzzing tests after changes
210+
2. **Maintain Security**: Ensure all security considerations are preserved
211+
3. **Update Documentation**: Keep this README up to date
212+
4. **Performance**: Benchmark changes to ensure no performance regression
213+
5. **Skip Optimization**: Only enable skip optimization in fuzzing builds
214+
215+
## References
216+
217+
- [LibFuzzer Documentation](https://llvm.org/docs/LibFuzzer.html)
218+
- [ECCVM Circuit Documentation](../README.md)
219+
- [Barretenberg Fuzzing Guide](../../../docs/fuzzing.md)

0 commit comments

Comments
 (0)