Skip to content

Commit

Permalink
Add SmallPowers struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Jul 31, 2023
1 parent c661576 commit 3293870
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions evm/src/cross_table_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;
use plonky2::field::extension::{Extendable, FieldExtension};
use plonky2::field::packed::PackedField;
use plonky2::field::polynomial::PolynomialValues;
use plonky2::field::types::Field;
use plonky2::field::types::{Field, SmallPowers};
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::iop::target::Target;
Expand Down Expand Up @@ -79,14 +79,14 @@ impl<F: Field> Column<F> {
}

pub fn le_bits<I: IntoIterator<Item = impl Borrow<usize>>>(cs: I) -> Self {
Self::linear_combination(cs.into_iter().map(|c| *c.borrow()).zip(F::TWO.powers()))
Self::linear_combination(cs.into_iter().map(|c| *c.borrow()).zip(SmallPowers::new(2)))
}

pub fn le_bytes<I: IntoIterator<Item = impl Borrow<usize>>>(cs: I) -> Self {
Self::linear_combination(
cs.into_iter()
.map(|c| *c.borrow())
.zip(F::from_canonical_u16(256).powers()),
.zip(SmallPowers::new(256)),
)
}

Expand Down
26 changes: 26 additions & 0 deletions field/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,29 @@ impl<F: Field> Powers<F> {
}
}
}

/// An iterator similar to `Powers`, but which base fits in a `u32`.
#[derive(Clone)]
pub struct SmallPowers<F: Field> {
base: u32,
current: F,
}

impl<F: Field> SmallPowers<F> {
pub fn new(base: u32) -> Self {
Self {
base,
current: F::ONE,
}
}
}

impl<F: Field> Iterator for SmallPowers<F> {
type Item = F;

fn next(&mut self) -> Option<F> {
let result = self.current;
self.current = self.current.mul_u32(self.base);
Some(result)
}
}

0 comments on commit 3293870

Please sign in to comment.