snarkvm-circuit crate provides circuit equivalents of all console types, enabling zero-knowledge proof generation through constraint system synthesis. It implements the R1CS (Rank-1 Constraint System) framework used by the Marlin proof system.
Architecture
The circuit crate mirrors the structure of the console crate:Constraint System
R1CS Structure
The circuit crate builds an R1CS constraint system where each constraint has the form:A, B, and C are linear combinations of variables:
circuit/environment/src/helpers/linear_combination.rs:36-42
Variable Types
Circuit variables come in three modes:circuit/environment/src/helpers/mode.rs:21-25
Console/Circuit Synchronization
The circuit and console crates must remain synchronized. Every console type has a corresponding circuit type with identical structure and API. When modifying one, always update the other.
Inject and Eject Traits
Circuit types convert to/from console types usingInject and Eject:
circuit/environment/src/traits/inject.rs:19-36, circuit/environment/src/traits/eject.rs:18-38
Constraint Counting
The environment tracks resource usage:circuit/environment/src/helpers/count.rs:26-53
Testing Constraints
Use constraint counting in tests:R1CS Generation
Enforcing Constraints
The environment provides methods to add constraints:circuit/environment/src/environment.rs:64-108
Extracting R1CS
Extract the complete constraint system:circuit/environment/src/environment.rs:183-189, circuit/environment/src/helpers/r1cs.rs:63-71
Environment Scoping
Use scopes to organize constraint generation:circuit/environment/src/environment.rs:60-62, circuit/environment/src/environment.rs:154-163
Resource Limits
Set limits on circuit size:circuit/environment/src/environment.rs:165-175
Example: Circuit Synthesis
Best Practices
- Always test constraint counts - Use
assert_scope!to verify expected resource usage - Minimize constraint generation - Prefer constant operations when possible
- Keep console/circuit synchronized - Same structure, same API, same tests
- Use scopes for organization - Track resource usage per component
- Test satisfaction - Use
is_satisfied()to verify constraint correctness
See Also
- Circuit Types - Field, Boolean, Integer, Group, Scalar types
- Circuit Environment - Environment trait and witness management
- Circuit Program - Request, Response, and program execution types
- Console API - Primitive types that circuits mirror