Forgot to exponentiate from bits in computation of subgroup_x. Saves 80 gates.

This commit is contained in:
wborgeaud 2021-07-23 08:53:00 +02:00
parent bcf524bed0
commit 6f8053cc37
2 changed files with 9 additions and 6 deletions

View File

@ -271,8 +271,7 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
let g = self.constant(F::MULTIPLICATIVE_GROUP_GENERATOR);
let phi = self.constant(F::primitive_root_of_unity(n_log));
let reversed_x = self.le_sum(x_index_bits.iter().rev());
let phi = self.exp(phi, reversed_x, n_log);
let phi = self.exp_from_bits(phi, x_index_bits.iter().rev());
self.mul(g, phi)
});

View File

@ -170,13 +170,17 @@ 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 `exponent`, given by its little-endian bits.
pub fn exp_from_bits(&mut self, base: Target, exponent_bits: &[Target]) -> Target {
pub fn exp_from_bits(
&mut self,
base: Target,
exponent_bits: impl Iterator<Item = impl Borrow<Target>>,
) -> Target {
let mut current = base;
let one = self.one();
let mut product = one;
for &bit in exponent_bits {
let multiplicand = self.select(bit, current, one);
for bit in exponent_bits {
let multiplicand = self.select(*bit.borrow(), current, one);
product = self.mul(product, multiplicand);
current = self.mul(current, current);
}
@ -210,7 +214,7 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Exponentiate `base` to the power of `exponent`, where `exponent < 2^num_bits`.
pub fn exp(&mut self, base: Target, exponent: Target, num_bits: usize) -> Target {
let exponent_bits = self.split_le(exponent, num_bits);
self.exp_from_bits(base, &exponent_bits)
self.exp_from_bits(base, exponent_bits.iter())
}
/// Exponentiate `base` to the power of a known `exponent`.