halo2_base::gates::flex_gate

Trait GateInstructions

Source
pub trait GateInstructions<F: ScalarField> {
Show 36 methods // Required methods fn pow_of_two(&self) -> &[F]; fn inner_product<QA>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> AssignedValue<F> where QA: Into<QuantumCell<F>>; fn inner_product_left_last<QA>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> (AssignedValue<F>, AssignedValue<F>) where QA: Into<QuantumCell<F>>; fn inner_product_left<QA>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> (AssignedValue<F>, Vec<AssignedValue<F>>) where QA: Into<QuantumCell<F>>; fn inner_product_with_sums<'thread, QA>( &self, ctx: &'thread mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> Box<dyn Iterator<Item = AssignedValue<F>> + 'thread> where QA: Into<QuantumCell<F>>; fn sum_products_with_coeff_and_var( &self, ctx: &mut Context<F>, values: impl IntoIterator<Item = (F, QuantumCell<F>, QuantumCell<F>)>, var: QuantumCell<F>, ) -> AssignedValue<F>; fn select( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, sel: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>; fn or_and( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, c: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>; fn num_to_bits( &self, ctx: &mut Context<F>, a: AssignedValue<F>, range_bits: usize, ) -> Vec<AssignedValue<F>>; fn pow_var( &self, ctx: &mut Context<F>, a: AssignedValue<F>, exp: AssignedValue<F>, max_bits: usize, ) -> AssignedValue<F>; // Provided methods fn add( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn inc( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn sub( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn dec( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn sub_mul( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, c: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn neg( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn mul( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn mul_add( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, c: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn mul_not( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn assert_bit(&self, ctx: &mut Context<F>, x: AssignedValue<F>) { ... } fn div_unsafe( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn assert_is_const( &self, ctx: &mut Context<F>, a: &AssignedValue<F>, constant: &F, ) { ... } fn sum<Q>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = Q>, ) -> AssignedValue<F> where Q: Into<QuantumCell<F>> { ... } fn partial_sums<'thread, Q>( &self, ctx: &'thread mut Context<F>, a: impl IntoIterator<Item = Q>, ) -> Box<dyn Iterator<Item = AssignedValue<F>> + 'thread> where Q: Into<QuantumCell<F>> { ... } fn accumulated_product<QA, QB>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QB>, ) -> Vec<AssignedValue<F>> where QA: Into<QuantumCell<F>>, QB: Into<QuantumCell<F>> { ... } fn or( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn and( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn not( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn bits_to_indicator( &self, ctx: &mut Context<F>, bits: &[AssignedValue<F>], ) -> Vec<AssignedValue<F>> { ... } fn idx_to_indicator( &self, ctx: &mut Context<F>, idx: impl Into<QuantumCell<F>>, len: usize, ) -> Vec<AssignedValue<F>> { ... } fn select_by_indicator<Q>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = Q>, indicator: impl IntoIterator<Item = AssignedValue<F>>, ) -> AssignedValue<F> where Q: Into<QuantumCell<F>> { ... } fn select_from_idx<Q>( &self, ctx: &mut Context<F>, cells: impl IntoIterator<Item = Q>, idx: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> where Q: Into<QuantumCell<F>> { ... } fn select_array_by_indicator<AR, AV>( &self, ctx: &mut Context<F>, array2d: &[AR], indicator: &[AssignedValue<F>], ) -> Vec<AssignedValue<F>> where AR: AsRef<[AV]>, AV: AsRef<AssignedValue<F>> { ... } fn is_zero( &self, ctx: &mut Context<F>, a: AssignedValue<F>, ) -> AssignedValue<F> { ... } fn is_equal( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F> { ... } fn lagrange_and_eval( &self, ctx: &mut Context<F>, coords: &[(AssignedValue<F>, AssignedValue<F>)], x: AssignedValue<F>, ) -> (AssignedValue<F>, AssignedValue<F>) { ... }
}
Expand description

Trait that defines basic arithmetic operations for a gate.

Required Methods§

Source

fn pow_of_two(&self) -> &[F]

Returns a slice of the ScalarField field elements 2^i for i in 0..F::NUM_BITS.

Source

fn inner_product<QA>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> AssignedValue<F>
where QA: Into<QuantumCell<F>>,

Constrains and returns the inner product of <a, b>.

Assumes ‘a’ and ‘b’ are the same length.

Source

fn inner_product_left_last<QA>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> (AssignedValue<F>, AssignedValue<F>)
where QA: Into<QuantumCell<F>>,

Returns the inner product of <a, b> and the last element of a after it has been assigned.

NOT encouraged for general usage. This is a low-level function, where you want to avoid first assigning a and then copying the last element into the correct cell for this computation.

Assumes ‘a’ and ‘b’ are the same length.

Source

fn inner_product_left<QA>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> (AssignedValue<F>, Vec<AssignedValue<F>>)
where QA: Into<QuantumCell<F>>,

Returns (<a,b>, a_assigned). See inner_product for more details.

NOT encouraged for general usage. This is a low-level function, useful for when you want to simultaneously compute an inner product while assigning private witnesses for the first time. This avoids first assigning a and then copying into the correct cells for this computation. We do not return the assignments of a in inner_product as an optimization to avoid the memory allocation of having to collect the vectors.

Assumes ‘a’ and ‘b’ are the same length.

Source

fn inner_product_with_sums<'thread, QA>( &self, ctx: &'thread mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QuantumCell<F>>, ) -> Box<dyn Iterator<Item = AssignedValue<F>> + 'thread>
where QA: Into<QuantumCell<F>>,

Calculates and constrains the inner product.

Returns the assignment trace where output[i] has the running sum sum_{j=0..=i} a[j] * b[j].

Assumes ‘a’ and ‘b’ are the same length.

  • ctx: Context to add the constraints to
  • a: Iterator of QuantumCell values
  • b: Iterator of QuantumCell values to calculate the partial sums of the inner product of a by.
Source

fn sum_products_with_coeff_and_var( &self, ctx: &mut Context<F>, values: impl IntoIterator<Item = (F, QuantumCell<F>, QuantumCell<F>)>, var: QuantumCell<F>, ) -> AssignedValue<F>

Constrains and returns the sum of products of coeff * (a * b) defined in values plus a variable var e.g. x = var + values[0].0 * (values[0].1 * values[0].2) + values[1].0 * (values[1].1 * values[1].2) + ... + values[n].0 * (values[n].1 * values[n].2).

  • ctx: Context to add the constraints to.
  • values: Iterator of tuples (coeff, a, b) where coeff is a field element, a and b are QuantumCell’s.
  • var: QuantumCell that represents the value of a variable added to the sum.
Source

fn select( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, sel: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns sel ? a : b assuming sel is boolean.

Defines a vertical gate of form | 1 - sel | sel | 1 | a | 1 - sel | sel | 1 | b | out |, where out = sel * a + (1 - sel) * b.

Source

fn or_and( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, c: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constains and returns a || (b && c), assuming a, b and c are boolean.

Defines a vertical gate of form | 1 - b c | b | c | 1 | a - 1 | 1 - b c | out | a - 1 | 1 | 1 | a |, where out = a + b * c - a * b * c.

Source

fn num_to_bits( &self, ctx: &mut Context<F>, a: AssignedValue<F>, range_bits: usize, ) -> Vec<AssignedValue<F>>

Constrains and returns little-endian bit vector representation of a.

Assumes range_bits >= bit_length(a).

  • a: QuantumCell of the value to convert
  • range_bits: range of bits needed to represent a
Source

fn pow_var( &self, ctx: &mut Context<F>, a: AssignedValue<F>, exp: AssignedValue<F>, max_bits: usize, ) -> AssignedValue<F>

Constrains and computes aexp where both a, exp are witnesses. The exponent is computed in the native field F.

Constrains that exp has at most max_bits bits.

Provided Methods§

Source

fn add( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a + b * 1 = out.

Defines a vertical gate of form | a | b | 1 | a + b | where (a + b) = out.

Source

fn inc( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns out = a + 1.

Source

fn sub( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a + b * (-1) = out.

Defines a vertical gate of form | a - b | b | 1 | a |, where (a - b) = out.

Source

fn dec( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns out = a - 1.

Source

fn sub_mul( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, c: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a - b * c = out.

Defines a vertical gate of form | a - b * c | b | c | a |, where (a - b * c) = out.

Source

fn neg( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a * (-1) = out.

Defines a vertical gate of form | a | -a | 1 | 0 |, where (-a) = out.

Source

fn mul( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns 0 + a * b = out.

Defines a vertical gate of form | 0 | a | b | a * b |, where (a * b) = out.

Source

fn mul_add( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, c: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a * b + c = out.

Defines a vertical gate of form | c | a | b | a * b + c |, where (a * b + c) = out.

Source

fn mul_not( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns (1 - a) * b = b - a * b.

Defines a vertical gate of form | (1 - a) * b | a | b | b |, where (1 - a) * b = out.

Source

fn assert_bit(&self, ctx: &mut Context<F>, x: AssignedValue<F>)

Constrains that x is boolean (e.g. 0 or 1).

Defines a vertical gate of form | 0 | x | x | x |.

Source

fn div_unsafe( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a / b = out.

Defines a vertical gate of form | 0 | a / b | b | a |, where a / b = out.

Assumes b != 0.

Source

fn assert_is_const( &self, ctx: &mut Context<F>, a: &AssignedValue<F>, constant: &F, )

Constrains that a is equal to constant value.

  • ctx: Context to add the constraints to
  • a: QuantumCell value
  • constant: constant value to constrain a to be equal to
Source

fn sum<Q>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = Q>, ) -> AssignedValue<F>
where Q: Into<QuantumCell<F>>,

Constrains and returns the sum of QuantumCell’s in iterator a.

Source

fn partial_sums<'thread, Q>( &self, ctx: &'thread mut Context<F>, a: impl IntoIterator<Item = Q>, ) -> Box<dyn Iterator<Item = AssignedValue<F>> + 'thread>
where Q: Into<QuantumCell<F>>,

Calculates and constrains the sum of the elements of a.

Returns the assignment trace where output[i] has the running sum sum_{j=0..=i} a[j].

Source

fn accumulated_product<QA, QB>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = QA>, b: impl IntoIterator<Item = QB>, ) -> Vec<AssignedValue<F>>
where QA: Into<QuantumCell<F>>, QB: Into<QuantumCell<F>>,

Calculates and constrains the accumulated product of ‘a’ and ‘b’ i.e. x_i = b_1 * (a_1...a_{i - 1}) + b_2 * (a_2...a_{i - 1}) + ... + b_i

Returns the assignment trace where output[i] is the running accumulated product x_i.

Assumes ‘a’ and ‘b’ are the same length.

  • ctx: Context to add the constraints to
  • a: Iterator of QuantumCell values
  • b: Iterator of QuantumCell values to take the accumulated product of a by
Source

fn or( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a || b, assuming a and b are boolean.

Defines a vertical gate of form | 1 - b | 1 | b | 1 | b | a | 1 - b | out |, where out = a + b - a * b.

Source

fn and( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns a & b, assumeing a and b are boolean.

Defines a vertical gate of form | 0 | a | b | out |, where out = a * b.

Source

fn not( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains and returns !a assumeing a is boolean.

Defines a vertical gate of form | 1 - a | a | 1 | 1 |, where 1 - a = out.

Source

fn bits_to_indicator( &self, ctx: &mut Context<F>, bits: &[AssignedValue<F>], ) -> Vec<AssignedValue<F>>

Constrains and returns an indicator vector from a slice of boolean values, where output[idx] = 1 iff idx = (the number represented by bits in binary little endian), otherwise output[idx] = 0.

  • ctx: Context to add the constraints to
  • bits: slice of QuantumCell’s that contains boolean values
§Assumptions
  • bits is non-empty
Source

fn idx_to_indicator( &self, ctx: &mut Context<F>, idx: impl Into<QuantumCell<F>>, len: usize, ) -> Vec<AssignedValue<F>>

Constrains and returns a Vec indicator of length len, where indicator[i] == 1 if i == idx otherwise 0, if idx >= len then indicator is all zeros.

Assumes len is greater than 0.

  • ctx: Context to add the constraints to
  • idx: QuantumCell index of the indicator vector to be set to 1
  • len: length of the indicator vector
Source

fn select_by_indicator<Q>( &self, ctx: &mut Context<F>, a: impl IntoIterator<Item = Q>, indicator: impl IntoIterator<Item = AssignedValue<F>>, ) -> AssignedValue<F>
where Q: Into<QuantumCell<F>>,

Constrains the inner product of a and indicator and returns a[idx] (e.g. the value of a at idx).

Assumes that a and indicator are non-empty iterators of the same length, the values of indicator are boolean, and that indicator has at most one 1 bit.

  • ctx: Context to add the constraints to
  • a: Iterator of QuantumCell’s that contains field elements
  • indicator: Iterator of AssignedValue’s where indicator[i] == 1 if i == idx, otherwise 0
Source

fn select_from_idx<Q>( &self, ctx: &mut Context<F>, cells: impl IntoIterator<Item = Q>, idx: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>
where Q: Into<QuantumCell<F>>,

Constrains and returns cells[idx] if idx < cells.len(), otherwise return 0.

Assumes that cells and idx are non-empty iterators of the same length.

  • ctx: Context to add the constraints to
  • cells: Iterator of QuantumCells to select from
  • idx: QuantumCell with value idx where idx is the index of the cell to be selected
Source

fn select_array_by_indicator<AR, AV>( &self, ctx: &mut Context<F>, array2d: &[AR], indicator: &[AssignedValue<F>], ) -> Vec<AssignedValue<F>>
where AR: AsRef<[AV]>, AV: AsRef<AssignedValue<F>>,

array2d is an array of fixed length arrays. Assumes:

  • array2d.len() == indicator.len()
  • array2d[i].len() == array2d[j].len() for all i,j.
  • the values of indicator are boolean and that indicator has at most one 1 bit.
  • the lengths of array2d and indicator are the same.

Returns the “dot product” of array2d with indicator as a fixed length (1d) array of length array2d[0].len().

Source

fn is_zero(&self, ctx: &mut Context<F>, a: AssignedValue<F>) -> AssignedValue<F>

Constrains that a cell is equal to 0 and returns 1 if a = 0, otherwise 0.

Defines a vertical gate of form | out | a | inv | 1 | 0 | a | out | 0 |, where out = 1 if a = 0, otherwise out = 0.

Source

fn is_equal( &self, ctx: &mut Context<F>, a: impl Into<QuantumCell<F>>, b: impl Into<QuantumCell<F>>, ) -> AssignedValue<F>

Constrains that the value of two cells are equal: b - a = 0, returns 1 if a = b, otherwise 0.

Source

fn lagrange_and_eval( &self, ctx: &mut Context<F>, coords: &[(AssignedValue<F>, AssignedValue<F>)], x: AssignedValue<F>, ) -> (AssignedValue<F>, AssignedValue<F>)

Performs and constrains Lagrange interpolation on coords and evaluates the resulting polynomial at x.

Given pairs coords[i] = (x_i, y_i), let f be the unique degree len(coords) - 1 polynomial such that f(x_i) = y_i for all i.

Returns: (f(x), Prod_i(x - x_i))

  • ctx: Context to add the constraints to
  • coords: immutable reference to a slice of tuples of AssignedValues representing the points to interpolate over such that coords[i] = (x_i, y_i)
  • x: x-coordinate of the point to evaluate f at
§Assumptions
  • coords is non-empty

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.

Implementors§