Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions acir/src/native_types/expression/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,18 @@ impl Mul<&Expression> for &Expression {
}
while i1 < self.linear_combinations.len() {
let (a_c, a_w) = self.linear_combinations[i1];
output.linear_combinations.push((rhs.q_c * a_c, a_w));
let coeff = rhs.q_c * a_c;
if !coeff.is_zero() {
output.linear_combinations.push((coeff, a_w));
}
i1 += 1;
}
while i2 < rhs.linear_combinations.len() {
let (b_c, b_w) = rhs.linear_combinations[i1];
output.linear_combinations.push((self.q_c * b_c, b_w));
let (b_c, b_w) = rhs.linear_combinations[i2];
let coeff = self.q_c * b_c;
if !coeff.is_zero() {
output.linear_combinations.push((coeff, b_w));
}
i2 += 1;
}

Expand All @@ -222,3 +228,63 @@ fn single_mul(w: Witness, b: &Expression) -> Expression {
..Default::default()
}
}

#[test]
fn add_smoketest() {
let a = Expression {
mul_terms: vec![],
linear_combinations: vec![(FieldElement::from(2u128), Witness(2))],
q_c: FieldElement::from(2u128),
};

let b = Expression {
mul_terms: vec![],
linear_combinations: vec![(FieldElement::from(4u128), Witness(4))],
q_c: FieldElement::one(),
};

assert_eq!(
&a + &b,
Expression {
mul_terms: vec![],
linear_combinations: vec![
(FieldElement::from(2u128), Witness(2)),
(FieldElement::from(4u128), Witness(4))
],
q_c: FieldElement::from(3u128)
}
);

// Enforce commutativity
assert_eq!(&a + &b, &b + &a);
}

#[test]
fn mul_smoketest() {
let a = Expression {
mul_terms: vec![],
linear_combinations: vec![(FieldElement::from(2u128), Witness(2))],
q_c: FieldElement::from(2u128),
};

let b = Expression {
mul_terms: vec![],
linear_combinations: vec![(FieldElement::from(4u128), Witness(4))],
q_c: FieldElement::one(),
};

assert_eq!(
&a * &b,
Expression {
mul_terms: vec![(FieldElement::from(8u128), Witness(2), Witness(4)),],
linear_combinations: vec![
(FieldElement::from(2u128), Witness(2)),
(FieldElement::from(8u128), Witness(4))
],
q_c: FieldElement::from(2u128)
}
);

// Enforce commutativity
assert_eq!(&a * &b, &b * &a);
}