Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
chore(acir)!: rename term_multiplication to `push_multiplication_te…
Browse files Browse the repository at this point in the history
…rm` (#122)

chore(acir)!: rename `term_addition` to `push_addition_term`
  • Loading branch information
TomAFrench authored Feb 28, 2023
1 parent 9f002c7 commit d389385
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
21 changes: 14 additions & 7 deletions acir/src/native_types/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,17 @@ impl Expression {
let mul_term_coeff = read_field_element::<FIELD_ELEMENT_NUM_BYTES, _>(&mut reader)?;
let mul_term_lhs = read_u32(&mut reader)?;
let mul_term_rhs = read_u32(&mut reader)?;
expr.term_multiplication(mul_term_coeff, Witness(mul_term_lhs), Witness(mul_term_rhs))
expr.push_multiplication_term(
mul_term_coeff,
Witness(mul_term_lhs),
Witness(mul_term_rhs),
)
}

for _ in 0..num_lin_comb_terms {
let lin_term_coeff = read_field_element::<FIELD_ELEMENT_NUM_BYTES, _>(&mut reader)?;
let lin_term_variable = read_u32(&mut reader)?;
expr.term_addition(lin_term_coeff, Witness(lin_term_variable))
expr.push_addition_term(lin_term_coeff, Witness(lin_term_variable))
}

let q_c = read_field_element::<FIELD_ELEMENT_NUM_BYTES, _>(&mut reader)?;
Expand All @@ -147,12 +151,15 @@ impl Expression {
Ok(expr)
}

pub fn term_addition(&mut self, coefficient: acir_field::FieldElement, variable: Witness) {
/// Adds a new linear term to the `Expression`.
pub fn push_addition_term(&mut self, coefficient: FieldElement, variable: Witness) {
self.linear_combinations.push((coefficient, variable))
}
pub fn term_multiplication(

/// Adds a new quadratic term to the `Expression`.
pub fn push_multiplication_term(
&mut self,
coefficient: acir_field::FieldElement,
coefficient: FieldElement,
lhs: Witness,
rhs: Witness,
) {
Expand Down Expand Up @@ -522,8 +529,8 @@ fn serialization_roundtrip() {

//
let mut expr = Expression::default();
expr.term_addition(FieldElement::from(123i128), Witness(20u32));
expr.term_multiplication(FieldElement::from(123i128), Witness(20u32), Witness(123u32));
expr.push_addition_term(FieldElement::from(123i128), Witness(20u32));
expr.push_multiplication_term(FieldElement::from(123i128), Witness(20u32), Witness(123u32));
expr.q_c = FieldElement::from(789456i128);

let (expr, got_expr) = read_write(expr);
Expand Down
18 changes: 9 additions & 9 deletions stdlib/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ pub(crate) fn bit_decomposition(
for &bit in &bit_vector {
// Bit constraint to ensure each bit is a zero or one; bit^2 - bit = 0
let mut expr = Expression::default();
expr.term_multiplication(FieldElement::one(), bit, bit);
expr.term_addition(-FieldElement::one(), bit);
expr.push_multiplication_term(FieldElement::one(), bit, bit);
expr.push_addition_term(-FieldElement::one(), bit);
binary_exprs.push(Opcode::Arithmetic(expr));

// Constraint to ensure that the bits are constrained to be a bit decomposition
// of the input
// ie \sum 2^i * x_i = input
bit_decomp_constraint.term_addition(-two_pow, bit);
bit_decomp_constraint.push_addition_term(-two_pow, bit);
two_pow = two * two_pow;
}

Expand Down Expand Up @@ -105,10 +105,10 @@ pub fn and(
// expected output; ie result = \sum 2^i x_i * y_i
let mut and_expr = Expression::default();
for (a_bit, b_bit) in a_bits.into_iter().zip(b_bits) {
and_expr.term_multiplication(two_pow, a_bit, b_bit);
and_expr.push_multiplication_term(two_pow, a_bit, b_bit);
two_pow = two * two_pow;
}
and_expr.term_addition(-FieldElement::one(), result);
and_expr.push_addition_term(-FieldElement::one(), result);

and_expr.sort();

Expand Down Expand Up @@ -144,12 +144,12 @@ pub fn xor(
// TODO: check this is the correct arithmetization
let mut xor_expr = Expression::default();
for (a_bit, b_bit) in a_bits.into_iter().zip(b_bits) {
xor_expr.term_addition(two_pow, a_bit);
xor_expr.term_addition(two_pow, b_bit);
xor_expr.push_addition_term(two_pow, a_bit);
xor_expr.push_addition_term(two_pow, b_bit);
two_pow = two * two_pow;
xor_expr.term_multiplication(-two_pow, a_bit, b_bit);
xor_expr.push_multiplication_term(-two_pow, a_bit, b_bit);
}
xor_expr.term_addition(-FieldElement::one(), result);
xor_expr.push_addition_term(-FieldElement::one(), result);

xor_expr.sort();
let mut new_gates = Vec::new();
Expand Down

0 comments on commit d389385

Please sign in to comment.