diff --git a/acir/src/native_types/expression/operators.rs b/acir/src/native_types/expression/operators.rs index 5ba37884a..35a548a2e 100644 --- a/acir/src/native_types/expression/operators.rs +++ b/acir/src/native_types/expression/operators.rs @@ -140,16 +140,16 @@ impl Sub<&Expression> for &Expression { } impl Mul<&Expression> for &Expression { - type Output = Expression; - fn mul(self, rhs: &Expression) -> Expression { + type Output = Option; + fn mul(self, rhs: &Expression) -> Option { if self.is_const() { - return self.q_c * rhs; + return Some(self.q_c * rhs); } else if rhs.is_const() { - return self * rhs.q_c; + return Some(self * rhs.q_c); } else if !(self.is_linear() && rhs.is_linear()) { // `Expression`s can only represent terms which are up to degree 2. // We then disallow multiplication of `Expression`s which have degree 2 terms. - unreachable!("Can only multiply linear terms"); + return None; } let mut output = Expression::from_field(self.q_c * rhs.q_c); @@ -210,7 +210,7 @@ impl Mul<&Expression> for &Expression { i2 += 1; } - output + Some(output) } } @@ -274,7 +274,7 @@ fn mul_smoketest() { }; assert_eq!( - &a * &b, + (&a * &b).unwrap(), Expression { mul_terms: vec![(FieldElement::from(8u128), Witness(2), Witness(4)),], linear_combinations: vec![ diff --git a/acir/src/native_types/witness.rs b/acir/src/native_types/witness.rs index 3e9beb510..740d10d29 100644 --- a/acir/src/native_types/witness.rs +++ b/acir/src/native_types/witness.rs @@ -1,5 +1,10 @@ +use std::ops::Add; + +use acir_field::FieldElement; use serde::{Deserialize, Serialize}; +use super::Expression; + // Witness might be a misnomer. This is an index that represents the position a witness will take #[derive( Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Serialize, Deserialize, @@ -28,3 +33,11 @@ impl From for Witness { Self(value) } } + +impl Add for Witness { + type Output = Expression; + + fn add(self, rhs: Witness) -> Self::Output { + Expression::from(self).add_mul(FieldElement::one(), &Expression::from(rhs)) + } +}