pub trait API {
Show 30 methods
// Required methods
fn metadata(&self) -> &impl Metadata;
fn append_operation(
&mut self,
op: OpCode,
inputs: Vec<VariableType>,
outputs: Vec<VariableType>,
);
fn allocate_local_variable(&mut self) -> VariableType;
// Provided methods
fn allocate_local_variable_n(&mut self, n: u64) -> Vec<VariableType> { ... }
fn add(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn add_multi(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
xn: &[&dyn Variable],
) -> VariableType { ... }
fn mul_acc(
&mut self,
a: &impl Variable,
b: &impl Variable,
c: &impl Variable,
) -> VariableType { ... }
fn neg(&mut self, x: &impl Variable) -> VariableType { ... }
fn sub(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn sub_multi(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
xn: &[&dyn Variable],
) -> VariableType { ... }
fn mul(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn mul_multi(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
xn: &[&dyn Variable],
) -> VariableType { ... }
fn div_unchecked(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
) -> VariableType { ... }
fn div(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn inverse(&mut self, x: &impl Variable) -> VariableType { ... }
fn variable_to_binary(
&mut self,
x: &impl Variable,
n: u64,
) -> Vec<VariableType> { ... }
fn variable_from_binary(&mut self, b: &[&dyn Variable]) -> VariableType { ... }
fn xor(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn or(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn and(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn select(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
x3: &impl Variable,
) -> VariableType { ... }
fn lookup2(
&mut self,
b0: &impl Variable,
b1: &impl Variable,
y1: &impl Variable,
y2: &impl Variable,
y3: &impl Variable,
y4: &impl Variable,
) -> VariableType { ... }
fn is_zero(&mut self, x: &impl Variable) -> VariableType { ... }
fn cmp(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType { ... }
fn assert_is_equal(&mut self, x1: &impl Variable, x2: &impl Variable) { ... }
fn assert_is_different(&mut self, x1: &impl Variable, x2: &impl Variable) { ... }
fn assert_is_boolean(&mut self, x: &impl Variable) { ... }
fn assert_is_crumb(&mut self, x: &impl Variable) { ... }
fn assert_is_less_or_equal(
&mut self,
v: &impl Variable,
bound: &impl Variable,
) { ... }
fn println(&mut self, message: &impl Variable) { ... }
}Expand description
The main API trait for building arithmetic circuits in zero-knowledge proof systems.
This trait provides a comprehensive set of operations for constructing arithmetic circuits,
including basic arithmetic operations, logical operations, assertions, and utility functions.
All operations work with variables that implement the Variable trait, enabling flexible
circuit construction with different variable types.
Required Methods§
fn metadata(&self) -> &impl Metadata
fn append_operation( &mut self, op: OpCode, inputs: Vec<VariableType>, outputs: Vec<VariableType>, )
fn allocate_local_variable(&mut self) -> VariableType
Provided Methods§
fn allocate_local_variable_n(&mut self, n: u64) -> Vec<VariableType>
Sourcefn add(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn add(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Sourcefn add_multi(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
xn: &[&dyn Variable],
) -> VariableType
fn add_multi( &mut self, x1: &impl Variable, x2: &impl Variable, xn: &[&dyn Variable], ) -> VariableType
Sourcefn mul_acc(
&mut self,
a: &impl Variable,
b: &impl Variable,
c: &impl Variable,
) -> VariableType
fn mul_acc( &mut self, a: &impl Variable, b: &impl Variable, c: &impl Variable, ) -> VariableType
Performs multiply-accumulate operation: res = a + (b * c)
This is an optimized operation that combines multiplication and addition in a single constraint, which can be more efficient than separate operations.
§Arguments
a- The accumulator valueb- First multiplicandc- Second multiplicand
§Returns
A new local variable containing the result a + (b * c)
Sourcefn neg(&mut self, x: &impl Variable) -> VariableType
fn neg(&mut self, x: &impl Variable) -> VariableType
Sourcefn sub(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn sub(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Sourcefn sub_multi(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
xn: &[&dyn Variable],
) -> VariableType
fn sub_multi( &mut self, x1: &impl Variable, x2: &impl Variable, xn: &[&dyn Variable], ) -> VariableType
Sourcefn mul(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn mul(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Sourcefn mul_multi(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
xn: &[&dyn Variable],
) -> VariableType
fn mul_multi( &mut self, x1: &impl Variable, x2: &impl Variable, xn: &[&dyn Variable], ) -> VariableType
Sourcefn div_unchecked(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
) -> VariableType
fn div_unchecked( &mut self, x1: &impl Variable, x2: &impl Variable, ) -> VariableType
Performs unchecked division: res = x1 / x2
This division operation does not enforce that the divisor is non-zero. If both x1 and x2 are zero, the result is defined as 0.
§Arguments
x1- Dividendx2- Divisor
§Returns
A new local variable containing the quotient
§Safety
This operation does not verify that x2 ≠ 0. Use div for checked division.
Sourcefn div(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn div(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Performs checked division: res = x1 / x2
This division operation enforces that the divisor is non-zero. The circuit constraint will fail if x2 equals zero.
§Arguments
x1- Dividendx2- Divisor (must be non-zero)
§Returns
A new local variable containing the quotient
§Panics
The circuit will be unsatisfiable if x2 == 0
Sourcefn inverse(&mut self, x: &impl Variable) -> VariableType
fn inverse(&mut self, x: &impl Variable) -> VariableType
Computes the multiplicative inverse: res = 1 / x
This operation enforces that x is non-zero. The circuit constraint will fail if x equals zero.
§Arguments
x- The variable to invert (must be non-zero)
§Returns
A new local variable containing the multiplicative inverse
§Panics
The circuit will be unsatisfiable if x == 0
Sourcefn variable_to_binary(&mut self, x: &impl Variable, n: u64) -> Vec<VariableType>
fn variable_to_binary(&mut self, x: &impl Variable, n: u64) -> Vec<VariableType>
Converts a variable to its binary representation
Decomposes the input variable into its constituent bits, returning them as a vector of boolean variables.
§Arguments
x- The variable to decomposen- Number of bits to extract (starting from least significant bit)
§Returns
A vector of local variables representing the binary decomposition, where index 0 is the least significant bit
Sourcefn variable_from_binary(&mut self, b: &[&dyn Variable]) -> VariableType
fn variable_from_binary(&mut self, b: &[&dyn Variable]) -> VariableType
Sourcefn xor(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn xor(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Sourcefn or(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn or(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Sourcefn and(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn and(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Sourcefn select(
&mut self,
x1: &impl Variable,
x2: &impl Variable,
x3: &impl Variable,
) -> VariableType
fn select( &mut self, x1: &impl Variable, x2: &impl Variable, x3: &impl Variable, ) -> VariableType
Performs conditional selection: res = x1 ? x2 : x3
If x1 is non-zero (true), returns x2; otherwise returns x3. This is equivalent to a ternary operator in programming languages.
§Arguments
x1- Condition variable (typically 0 or 1)x2- Value to return if condition is truex3- Value to return if condition is false
§Returns
A new local variable containing the selected value
Sourcefn lookup2(
&mut self,
b0: &impl Variable,
b1: &impl Variable,
y1: &impl Variable,
y2: &impl Variable,
y3: &impl Variable,
y4: &impl Variable,
) -> VariableType
fn lookup2( &mut self, b0: &impl Variable, b1: &impl Variable, y1: &impl Variable, y2: &impl Variable, y3: &impl Variable, y4: &impl Variable, ) -> VariableType
Performs a 2-bit lookup table operation
Selects one of four values (y1, y2, y3, y4) based on a 2-bit index formed by concatenating bits b1 and b0 (b1 is MSB, b0 is LSB).
§Arguments
b0- Least significant bit of the indexb1- Most significant bit of the indexy1- Value for index 00 (b1=0, b0=0)y2- Value for index 01 (b1=0, b0=1)y3- Value for index 10 (b1=1, b0=0)y4- Value for index 11 (b1=1, b0=1)
§Returns
The selected value based on the 2-bit index
Sourcefn is_zero(&mut self, x: &impl Variable) -> VariableType
fn is_zero(&mut self, x: &impl Variable) -> VariableType
Sourcefn cmp(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
fn cmp(&mut self, x1: &impl Variable, x2: &impl Variable) -> VariableType
Sourcefn assert_is_equal(&mut self, x1: &impl Variable, x2: &impl Variable)
fn assert_is_equal(&mut self, x1: &impl Variable, x2: &impl Variable)
Sourcefn assert_is_different(&mut self, x1: &impl Variable, x2: &impl Variable)
fn assert_is_different(&mut self, x1: &impl Variable, x2: &impl Variable)
Sourcefn assert_is_boolean(&mut self, x: &impl Variable)
fn assert_is_boolean(&mut self, x: &impl Variable)
Sourcefn assert_is_crumb(&mut self, x: &impl Variable)
fn assert_is_crumb(&mut self, x: &impl Variable)
Sourcefn assert_is_less_or_equal(&mut self, v: &impl Variable, bound: &impl Variable)
fn assert_is_less_or_equal(&mut self, v: &impl Variable, bound: &impl Variable)
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.