From 32938709477b740883af9754c2910a6cd281fbf0 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Mon, 31 Jul 2023 08:37:49 -0400 Subject: [PATCH] Add SmallPowers struct --- evm/src/cross_table_lookup.rs | 6 +++--- field/src/types.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/evm/src/cross_table_lookup.rs b/evm/src/cross_table_lookup.rs index 61480d0584..4748124e03 100644 --- a/evm/src/cross_table_lookup.rs +++ b/evm/src/cross_table_lookup.rs @@ -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; @@ -79,14 +79,14 @@ impl Column { } pub fn le_bits>>(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>>(cs: I) -> Self { Self::linear_combination( cs.into_iter() .map(|c| *c.borrow()) - .zip(F::from_canonical_u16(256).powers()), + .zip(SmallPowers::new(256)), ) } diff --git a/field/src/types.rs b/field/src/types.rs index c95155be35..6eb5fed99d 100644 --- a/field/src/types.rs +++ b/field/src/types.rs @@ -598,3 +598,29 @@ impl Powers { } } } + +/// An iterator similar to `Powers`, but which base fits in a `u32`. +#[derive(Clone)] +pub struct SmallPowers { + base: u32, + current: F, +} + +impl SmallPowers { + pub fn new(base: u32) -> Self { + Self { + base, + current: F::ONE, + } + } +} + +impl Iterator for SmallPowers { + type Item = F; + + fn next(&mut self) -> Option { + let result = self.current; + self.current = self.current.mul_u32(self.base); + Some(result) + } +}