Make exp_complement_bits take an iterator to avoid cloning.

This commit is contained in:
wborgeaud 2021-07-22 16:18:13 +02:00
parent ca3a2fcfc8
commit 1d92191227
2 changed files with 10 additions and 8 deletions

View File

@ -33,13 +33,9 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
// The evaluation vector needs to be reordered first.
let mut evals = last_evals.to_vec();
reverse_index_bits_in_place(&mut evals);
let mut old_x_index_bits = old_x_index_bits.to_vec();
old_x_index_bits.reverse();
// Want `g^(arity - rev_old_x_index)` as in the out-of-circuit version.
// Compute it as `g^(arity-1-rev_old_x_index) * g`, where the first term is gotten using two's complement.
// TODO: Once the exponentiation gate lands, we won't need the bits and will be able to compute
// `g^(arity-rev_old_x_index)` directly.
let start = self.exp_from_complement_bits(gt, &old_x_index_bits);
let start = self.exp_from_complement_bits(gt, old_x_index_bits.iter().rev());
let coset_start = self.mul_many(&[start, gt, x]);
// The answer is gotten by interpolating {(x*g^i, P(x*g^i))} and evaluating at beta.

View File

@ -1,3 +1,5 @@
use std::borrow::Borrow;
use crate::circuit_builder::CircuitBuilder;
use crate::field::extension_field::Extendable;
use crate::target::Target;
@ -187,15 +189,19 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
// TODO: Optimize this, maybe with a new gate.
// TODO: Test
/// Exponentiate `base` to the power of `2^bit_length-1-exponent`, given by its little-endian bits.
pub fn exp_from_complement_bits(&mut self, base: Target, exponent_bits: &[Target]) -> Target {
pub fn exp_from_complement_bits(
&mut self,
base: Target,
exponent_bits: impl ExactSizeIterator<Item = impl Borrow<Target>> + Clone,
) -> Target {
let mut current = base;
let one_ext = self.one_extension();
let mut product = self.one();
for &bit in exponent_bits {
for bit in exponent_bits {
let current_ext = self.convert_to_ext(current);
// TODO: Add base field select.
let multiplicand = self.select(bit, one_ext, current_ext);
let multiplicand = self.select(*bit.borrow(), one_ext, current_ext);
product = self.mul(product, multiplicand.0[0]);
current = self.mul(current, current);
}