|
LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
|
R1CS
Add two linear expressions
Syntax:
Traits: AlwaysSpeculatableImplTrait, Commutative
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
| Operand | Description |
|---|---|
| lhs | R1CS linear expression type |
| rhs | R1CS linear expression type |
| Result | Description |
|---|---|
| sum | R1CS linear expression type |
Defines an R1CS circuit with labeled inputs and constraints
The r1cs.circuit operation defines a named circuit, with explicit inputs and internally declared signals and constraints.
Signals defined with r1cs.def and marked public are considered outputs.
The optional r1cs.input_labels array supplies one explicit wire ID for each input, in block-argument order. When present, all wire IDs must be consecutive after constant one, ordered as public outputs, public inputs, private inputs, then internal signals. Circuits may omit this attribute when the binary exporter will assign input labels.
Example:
Traits: IsolatedFromAbove, NoTerminator, SingleBlock
Interfaces: Symbol
| Attribute | MLIR Type | Description |
|---|---|---|
sym_name | ::mlir::StringAttr | string attribute |
arg_attrs | ::mlir::DictionaryAttr | dictionary of named attribute values |
Create a constant linear value
Syntax:
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
| Attribute | MLIR Type | Description |
|---|---|---|
value | ::r1cs::FeltAttr | Field element represented as an arbitrary precision integer |
| Result | Description |
|---|---|
| out | R1CS linear expression type |
Enforce a * b - c = 0 R1CS constraint
Syntax:
| Operand | Description |
|---|---|
| a | R1CS linear expression type |
| b | R1CS linear expression type |
| c | R1CS linear expression type |
Define a signal with label and optional public visibility
Syntax:
The def op allocates a distinct signal associated with a numeric label. Unused signals remain part of the circuit interface and wire layout, so their declarations must survive common subexpression elimination.
If the optional public keyword is present, the signal is treated as a public output of the circuit. Otherwise, it is considered private/internal.
Example:
Interfaces: MemoryEffectOpInterface (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{MemoryEffects::Allocate on ::mlir::SideEffects::DefaultResource}
| Attribute | MLIR Type | Description |
|---|---|---|
label | ::mlir::IntegerAttr | 32-bit unsigned integer attribute |
pub | ::r1cs::PublicAttr | An attribute to mark a type as public |
| Result | Description |
|---|---|
| out | R1CS signal wire type |
Scale a linear expression by a constant
Syntax:
Traits: AlwaysSpeculatableImplTrait, Commutative
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
| Attribute | MLIR Type | Description |
|---|---|---|
constValue | ::r1cs::FeltAttr | Field element represented as an arbitrary precision integer |
| Operand | Description |
|---|---|
| input | R1CS linear expression type |
| Result | Description |
|---|---|
| scaled | R1CS linear expression type |
Negate a linear expression
Syntax:
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
| Operand | Description |
|---|---|
| input | R1CS linear expression type |
| Result | Description |
|---|---|
| neg | R1CS linear expression type |
Casts a signal to a linear expression
Syntax:
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
| Operand | Description |
|---|---|
| input | R1CS signal wire type |
| Result | Description |
|---|---|
| output | R1CS linear expression type |
Field element represented as an arbitrary precision integer
Syntax:
An exact signed mathematical coefficient, independent of storage width. Binary export reduces coefficients modulo the selected field prime.
| Parameter | C++ type | Description |
|---|---|---|
| value | llzk::DynamicAPIntValue | signed coefficient |
An attribute to mark a type as public
Syntax: #r1cs.pub
Examples:
R1CS linear expression type
Syntax: !r1cs.linear
R1CS signal wire type
Syntax: !r1cs.signal
R1CS constants and multipliers use exact signed mathematical integers. Their assembly has no integer-width annotation: for example, r1cs.mul_const x, -1 : !r1cs.linear. Binary export reduces each coefficient modulo the selected prime. The --r1cs-prime decimal option does not require a bit width; the exporter derives the fixed byte width required by the R1CS format.
R1CS lowering rejects live cast.tofelt and cast.toindex operations and potentially failing assertions in constrain functions. An assert cast that can fail must be preserved even when its result is unused (current lowering does not encode its range check). Valid constant assertions can be folded by canonicalization before lowering (the full R1CS pipeline includes this). Unused infallible casts, including boolean-to-felt casts and cast.toindex assertions from fields whose entire canonical range fits in the index safe range, such as BabyBear, are accepted even without prior cleanup.