Merge pull request #256 from mir-protocol/fix_reduce

Small fixes in the `le_sum` and `reduce` gadgets
This commit is contained in:
wborgeaud 2021-09-21 18:55:54 +02:00 committed by GitHub
commit a407ed5b7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -32,6 +32,18 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
bits: impl ExactSizeIterator<Item = impl Borrow<BoolTarget>> + Clone,
) -> Target {
let num_bits = bits.len();
if num_bits == 0 {
return self.zero();
} else if num_bits == 1 {
let mut bits = bits;
return bits.next().unwrap().borrow().target;
} else if num_bits == 2 {
let two = self.two();
let mut bits = bits;
let b0 = bits.next().unwrap().borrow().target;
let b1 = bits.next().unwrap().borrow().target;
return self.mul_add(two, b1, b0);
}
debug_assert!(
BaseSumGate::<2>::START_LIMBS + num_bits <= self.config.num_routed_wires,
"Not enough routed wires."

View File

@ -137,12 +137,6 @@ impl<const D: usize> ReducingFactorTarget<D> {
acc
}
/// Reduces a length `n` vector of `ExtensionTarget`s using `n/2` `ArithmeticExtensionGate`s.
/// It does this by batching two steps of Horner's method in each gate.
/// Here's an example with `n=4, alpha=2, D=1`:
/// 1st gate: 2 0 4 4 3 4 11 <- 2*0+4=4, 2*4+3=11
/// 2nd gate: 2 11 2 24 1 24 49 <- 2*11+2=24, 2*24+1=49
/// which verifies that `2.reduce([1,2,3,4]) = 49`.
pub fn reduce<F>(
&mut self,
terms: &[ExtensionTarget<D>], // Could probably work with a `DoubleEndedIterator` too.
@ -155,7 +149,7 @@ impl<const D: usize> ReducingFactorTarget<D> {
self.count += l as u64;
let mut terms_vec = terms.to_vec();
let mut acc = terms_vec.pop().unwrap();
let mut acc = builder.zero_extension();
terms_vec.reverse();
for x in terms_vec {