Skip to content

Commit

Permalink
add proc_lc test & update eval
Browse files Browse the repository at this point in the history
  • Loading branch information
flyq committed Oct 26, 2023
1 parent 90cbc26 commit 0a5128b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 12 additions & 2 deletions crates/bellpepper-core/src/lc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,24 @@ impl<Scalar: PrimeField> LinearCombination<Scalar> {
}

pub fn eval(&self, input_assignment: &[Scalar], aux_assignment: &[Scalar]) -> Scalar {
let one = Scalar::ONE;

let inputs_acc = self
.iter_inputs()
.fold(Scalar::ZERO, |acc, (&index, coeff)| {
acc + coeff.mul(input_assignment[index])
acc + if coeff != &one {
coeff.mul(input_assignment[index])
} else {
input_assignment[index]
}
});

let aux_acc = self.iter_aux().fold(Scalar::ZERO, |acc, (&index, coeff)| {
acc + coeff.mul(aux_assignment[index])
acc + if coeff != &one {
coeff.mul(aux_assignment[index])
} else {
aux_assignment[index]
}
});

inputs_acc + aux_acc
Expand Down
18 changes: 18 additions & 0 deletions crates/bellpepper-core/src/util_cs/test_cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,24 @@ mod tests {
assert!(cs.get("test1/test2/hehe") == Fr::ONE);
}

#[test]
fn test_proc_lc() {
let one = Fr::ONE;
let two = Fr::ONE + Fr::ONE;

let mut lc = LinearCombination::<Fr>::zero();
lc = lc + (two, Variable::new_unchecked(Index::Input(1)));
lc = lc + (two, Variable::new_unchecked(Index::Aux(0)));

lc = lc + (-one, Variable::new_unchecked(Index::Input(1)));
lc = lc + (-one, Variable::new_unchecked(Index::Input(1)));

lc = lc + (-one, Variable::new_unchecked(Index::Aux(0)));
lc = lc + (-one, Variable::new_unchecked(Index::Aux(0)));
let res = proc_lc(&lc);
assert!(res.is_empty());
}

#[test]
fn test_eval_lc() {
let one = Fr::ONE;
Expand Down

0 comments on commit 0a5128b

Please sign in to comment.