mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-07 16:23:12 +00:00
Optimize some exp methods to use ExponentiationGate (#151)
This commit is contained in:
parent
8b8e4d223d
commit
079baff718
@ -4,7 +4,6 @@ use crate::field::extension_field::Extendable;
|
|||||||
use crate::gates::exponentiation::ExponentiationGate;
|
use crate::gates::exponentiation::ExponentiationGate;
|
||||||
use crate::iop::target::Target;
|
use crate::iop::target::Target;
|
||||||
use crate::plonk::circuit_builder::CircuitBuilder;
|
use crate::plonk::circuit_builder::CircuitBuilder;
|
||||||
use crate::util::log2_ceil;
|
|
||||||
|
|
||||||
impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
||||||
/// Computes `-x`.
|
/// Computes `-x`.
|
||||||
@ -97,12 +96,8 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Exponentiate `base` to the power of `2^power_log`.
|
/// Exponentiate `base` to the power of `2^power_log`.
|
||||||
// TODO: Test
|
pub fn exp_power_of_2(&mut self, base: Target, power_log: usize) -> Target {
|
||||||
pub fn exp_power_of_2(&mut self, mut base: Target, power_log: usize) -> Target {
|
self.exp_u64(base, 1 << power_log)
|
||||||
for _ in 0..power_log {
|
|
||||||
base = self.square(base);
|
|
||||||
}
|
|
||||||
base
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Test
|
// TODO: Test
|
||||||
@ -110,12 +105,12 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
pub fn exp_from_bits(
|
pub fn exp_from_bits(
|
||||||
&mut self,
|
&mut self,
|
||||||
base: Target,
|
base: Target,
|
||||||
exponent_bits: impl Iterator<Item = impl Borrow<Target>>,
|
exponent_bits: impl IntoIterator<Item = impl Borrow<Target>>,
|
||||||
) -> Target {
|
) -> Target {
|
||||||
let zero = self.zero();
|
let zero = self.zero();
|
||||||
let gate = ExponentiationGate::new(self.config.clone());
|
let gate = ExponentiationGate::new(self.config.clone());
|
||||||
let num_power_bits = gate.num_power_bits;
|
let num_power_bits = gate.num_power_bits;
|
||||||
let mut exp_bits_vec: Vec<Target> = exponent_bits.map(|b| *b.borrow()).collect();
|
let mut exp_bits_vec: Vec<Target> = exponent_bits.into_iter().map(|b| *b.borrow()).collect();
|
||||||
while exp_bits_vec.len() < num_power_bits {
|
while exp_bits_vec.len() < num_power_bits {
|
||||||
exp_bits_vec.push(zero);
|
exp_bits_vec.push(zero);
|
||||||
}
|
}
|
||||||
@ -139,10 +134,16 @@ impl<F: Extendable<D>, const D: usize> CircuitBuilder<F, D> {
|
|||||||
|
|
||||||
/// Exponentiate `base` to the power of a known `exponent`.
|
/// Exponentiate `base` to the power of a known `exponent`.
|
||||||
// TODO: Test
|
// TODO: Test
|
||||||
pub fn exp_u64(&mut self, base: Target, exponent: u64) -> Target {
|
pub fn exp_u64(&mut self, base: Target, mut exponent: u64) -> Target {
|
||||||
let exp_target = self.constant(F::from_canonical_u64(exponent));
|
let mut exp_bits = Vec::new();
|
||||||
let num_bits = log2_ceil(exponent as usize + 1);
|
while exponent != 0 {
|
||||||
self.exp(base, exp_target, num_bits)
|
let bit = exponent & 1;
|
||||||
|
let bit_target = self.constant(F::from_canonical_u64(bit));
|
||||||
|
exp_bits.push(bit_target);
|
||||||
|
exponent >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.exp_from_bits(base, exp_bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes `x / y`. Results in an unsatisfiable instance if `y = 0`.
|
/// Computes `x / y`. Results in an unsatisfiable instance if `y = 0`.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user