Bug fix in AffineExpression::assign()

This commit is contained in:
Georg Wiese
2023-09-07 16:16:04 +02:00
committed by GitHub
parent 78d089f1e0
commit 958bd88478

View File

@@ -133,14 +133,12 @@ where
/// Incorporates the case where the symbolic variable `key` is assigned
/// the value `value`.
pub fn assign(&mut self, key: K, value: T) {
let mut offset = 0.into();
for (k, coeff) in &mut self.coefficients {
if *k == key {
offset += *coeff * value;
self.offset += *coeff * value;
*coeff = 0.into();
}
}
self.offset -= offset;
self.clean = false;
}
}
@@ -576,6 +574,34 @@ mod test {
input.iter().map(|x| (*x).into()).enumerate().collect()
}
#[test]
pub fn test_affine_assign() {
let mut a = AffineExpression::<_, GoldilocksField> {
coefficients: convert(vec![2, 3]),
offset: 3.into(),
clean: true,
};
a.assign(0, 3.into());
assert_eq!(
a,
AffineExpression {
coefficients: convert(vec![0, 3]),
offset: 9.into(),
clean: false
},
);
// Now, the expression is 3b + 9. It should be able to solve for b
// such that 3b + 9 = 0.
let updates = a.solve().unwrap();
assert_eq!(
updates.constraints,
[(1, Constraint::Assignment((-3).into()))]
);
a.assign(1, (-3).into());
assert_eq!(a.constant_value().unwrap(), 0.into());
}
#[test]
pub fn test_affine_neg() {
let a = AffineExpression {