diff --git a/plonky2/src/gadgets/lookup.rs b/plonky2/src/gadgets/lookup.rs index 3a3bba8da5..cc60cbe7ea 100644 --- a/plonky2/src/gadgets/lookup.rs +++ b/plonky2/src/gadgets/lookup.rs @@ -1,8 +1,6 @@ -use alloc::sync::Arc; - use crate::field::extension::Extendable; use crate::gates::lookup::LookupGate; -use crate::gates::lookup_table::LookupTableGate; +use crate::gates::lookup_table::{LookupTable, LookupTableGate}; use crate::gates::noop::NoopGate; use crate::hash::hash_types::RichField; use crate::iop::target::Target; @@ -47,7 +45,7 @@ pub const SMALLER_TABLE: [u16; 8] = [2, 24, 56, 100, 128, 16, 20, 49]; impl, const D: usize> CircuitBuilder { /// Adds a lookup table to the list of stored lookup tables `self.luts` based on a table of (input, output) pairs. It returns the index of the LUT within `self.luts`. - pub fn add_lookup_table_from_pairs(&mut self, table: Arc>) -> usize { + pub fn add_lookup_table_from_pairs(&mut self, table: LookupTable) -> usize { self.update_luts_from_pairs(table) } diff --git a/plonky2/src/gates/lookup.rs b/plonky2/src/gates/lookup.rs index cbf0049099..0ecac5018e 100644 --- a/plonky2/src/gates/lookup.rs +++ b/plonky2/src/gates/lookup.rs @@ -4,6 +4,7 @@ use alloc::sync::Arc; use alloc::vec::Vec; use core::usize; +use super::lookup_table::LookupTable; use crate::field::extension::Extendable; use crate::field::packed::PackedField; use crate::gates::gate::Gate; @@ -22,17 +23,19 @@ use crate::plonk::vars::{ }; use crate::util::serialization::{Buffer, IoResult, Read, Write}; +pub type Lookup = Vec<(Target, Target)>; + /// A gate which stores (input, output) lookup pairs made elsewhere in the trace. It doesn't check any constraints itself. #[derive(Debug, Clone)] pub struct LookupGate { /// Number of lookups per gate. pub num_slots: usize, /// LUT associated to the gate. - lut: Arc>, + lut: LookupTable, } impl LookupGate { - pub fn new_from_table(config: &CircuitConfig, lut: Arc>) -> Self { + pub fn new_from_table(config: &CircuitConfig, lut: LookupTable) -> Self { Self { num_slots: Self::num_slots(config), lut, @@ -142,7 +145,7 @@ impl, const D: usize> PackedEvaluableBase for #[derive(Clone, Debug, Default)] pub struct LookupGenerator { row: usize, - lut: Arc>, + lut: LookupTable, slot_nb: usize, } diff --git a/plonky2/src/gates/lookup_table.rs b/plonky2/src/gates/lookup_table.rs index 3869e2f260..b4ad902ab8 100644 --- a/plonky2/src/gates/lookup_table.rs +++ b/plonky2/src/gates/lookup_table.rs @@ -24,23 +24,21 @@ use crate::plonk::vars::{ }; use crate::util::serialization::{Buffer, IoResult, Read, Write}; +pub type LookupTable = Arc>; + /// A gate which stores the set of (input, output) value pairs of a lookup table, and their multiplicities. #[derive(Debug, Clone)] pub struct LookupTableGate { /// Number of lookup entries per gate. pub num_slots: usize, /// Lookup table associated to the gate. - pub lut: Arc>, + pub lut: LookupTable, /// First row of the lookup table. last_lut_row: usize, } impl LookupTableGate { - pub fn new_from_table( - config: &CircuitConfig, - lut: Arc>, - last_lut_row: usize, - ) -> Self { + pub fn new_from_table(config: &CircuitConfig, lut: LookupTable, last_lut_row: usize) -> Self { Self { num_slots: Self::num_slots(config), lut, @@ -164,7 +162,7 @@ impl, const D: usize> PackedEvaluableBase for #[derive(Clone, Debug, Default)] pub struct LookupTableGenerator { row: usize, - lut: Arc>, + lut: LookupTable, slot_nb: usize, num_slots: usize, last_lut_row: usize, diff --git a/plonky2/src/lookup_test.rs b/plonky2/src/lookup_test.rs index e220451ffd..04b5720383 100644 --- a/plonky2/src/lookup_test.rs +++ b/plonky2/src/lookup_test.rs @@ -9,6 +9,7 @@ mod tests { use log::{Level, LevelFilter}; use crate::gadgets::lookup::{OTHER_TABLE, SMALLER_TABLE, TIP5_TABLE}; + use crate::gates::lookup_table::LookupTable; use crate::gates::noop::NoopGate; use crate::plonk::prover::prove; use crate::util::timing::TimingTree; @@ -54,7 +55,7 @@ mod tests { LOGGER_INITIALIZED.call_once(|| init_logger().unwrap()); let tip5_table = TIP5_TABLE.to_vec(); - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::::new(config); @@ -129,7 +130,7 @@ mod tests { let first_out = tip5_table[look_val_a]; let second_out = tip5_table[look_val_b]; - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let other_table = OTHER_TABLE.to_vec(); @@ -142,7 +143,7 @@ mod tests { let s = first_out + second_out; let final_out = other_table[s as usize]; - let table2: Arc> = Arc::new((0..256).zip_eq(other_table).collect()); + let table2: LookupTable = Arc::new((0..256).zip_eq(other_table).collect()); let table2_index = builder.add_lookup_table_from_pairs(table2); let output_final = builder.add_lookup_from_index(sum, table2_index); @@ -208,11 +209,11 @@ mod tests { let init_b = 2; let tab: Vec = SMALLER_TABLE.to_vec(); - let table: Arc> = Arc::new((2..10).zip_eq(tab).collect()); + let table: LookupTable = Arc::new((2..10).zip_eq(tab).collect()); let other_table = OTHER_TABLE.to_vec(); - let table2: Arc> = Arc::new((0..256).zip_eq(other_table).collect()); + let table2: LookupTable = Arc::new((0..256).zip_eq(other_table).collect()); let small_index = builder.add_lookup_table_from_pairs(table.clone()); let output_a = builder.add_lookup_from_index(initial_a, small_index); @@ -293,7 +294,7 @@ mod tests { let look_val_b = 2; let tip5_table = TIP5_TABLE.to_vec(); - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let out_a = table[look_val_a].1; let out_b = table[look_val_b].1; @@ -310,7 +311,7 @@ mod tests { let other_table = OTHER_TABLE.to_vec(); - let table2: Arc> = Arc::new((0..256).zip_eq(other_table).collect()); + let table2: LookupTable = Arc::new((0..256).zip_eq(other_table).collect()); let s = out_a + out_b; let out_final = table2[s as usize].1; @@ -384,7 +385,7 @@ mod tests { let look_val_b = 2; let tip5_table = TIP5_TABLE.to_vec(); - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let table_index = builder.add_lookup_table_from_pairs(table.clone()); let output_a = builder.add_lookup_from_index(initial_a, table_index); diff --git a/plonky2/src/plonk/circuit_builder.rs b/plonky2/src/plonk/circuit_builder.rs index db9f1cf5f6..8955a2bdb9 100644 --- a/plonky2/src/plonk/circuit_builder.rs +++ b/plonky2/src/plonk/circuit_builder.rs @@ -25,7 +25,8 @@ use crate::gates::arithmetic_base::ArithmeticGate; use crate::gates::arithmetic_extension::ArithmeticExtensionGate; use crate::gates::constant::ConstantGate; use crate::gates::gate::{CurrentSlot, Gate, GateInstance, GateRef}; -use crate::gates::lookup::LookupGate; +use crate::gates::lookup::{Lookup, LookupGate}; +use crate::gates::lookup_table::LookupTable; use crate::gates::noop::NoopGate; use crate::gates::public_input::PublicInputGate; use crate::gates::selectors::{selector_ends_lookups, selector_polynomials, selectors_lookup}; @@ -129,10 +130,10 @@ pub struct CircuitBuilder, const D: usize> { lookup_rows: Vec, /// For each LUT index, vector of `(looking_in, looking_out)` pairs. - lut_to_lookups: Vec>, + lut_to_lookups: Vec, // Lookup tables in the form of `Vec<(input_value, output_value)>`. - luts: Vec>>, + luts: Vec, /// Optional common data. When it is `Some(goal_data)`, the `build` function panics if the resulting /// common data doesn't equal `goal_data`. @@ -581,12 +582,12 @@ impl, const D: usize> CircuitBuilder { } /// Checks whether a LUT is already stored in `self.luts` - pub fn is_stored(&self, lut: Arc>) -> Option { + pub fn is_stored(&self, lut: LookupTable) -> Option { self.luts.iter().position(|elt| *elt == lut) } /// Returns the LUT at index `idx`. - pub fn get_lut(&self, idx: usize) -> Arc> { + pub fn get_lut(&self, idx: usize) -> LookupTable { assert!( idx < self.luts.len(), "index idx: {} greater than the total number of created LUTS: {}", @@ -632,7 +633,7 @@ impl, const D: usize> CircuitBuilder { .copied() .zip_eq(table.iter().copied()) .collect(); - let lut: Arc> = Arc::new(pairs); + let lut: LookupTable = Arc::new(pairs); // If the LUT `lut` is already stored in `self.luts`, return its index. Otherwise, append `table` to `self.luts` and return its index. if let Some(idx) = self.is_stored(lut.clone()) { @@ -646,7 +647,7 @@ impl, const D: usize> CircuitBuilder { } /// Adds a table to the vector of LUTs in the circuit builder. - pub fn update_luts_from_pairs(&mut self, table: Arc>) -> usize { + pub fn update_luts_from_pairs(&mut self, table: LookupTable) -> usize { // If the LUT `table` is already stored in `self.luts`, return its index. Otherwise, append `table` to `self.luts` and return its index. if let Some(idx) = self.is_stored(table.clone()) { idx diff --git a/plonky2/src/plonk/circuit_data.rs b/plonky2/src/plonk/circuit_data.rs index e713265641..7d91a3a3be 100644 --- a/plonky2/src/plonk/circuit_data.rs +++ b/plonky2/src/plonk/circuit_data.rs @@ -1,5 +1,4 @@ use alloc::collections::BTreeMap; -use alloc::sync::Arc; use alloc::vec; use alloc::vec::Vec; use core::ops::{Range, RangeFrom}; @@ -19,6 +18,8 @@ use crate::fri::structure::{ }; use crate::fri::{FriConfig, FriParams}; use crate::gates::gate::GateRef; +use crate::gates::lookup::Lookup; +use crate::gates::lookup_table::LookupTable; use crate::gates::selectors::SelectorsInfo; use crate::hash::hash_types::{HashOutTarget, MerkleCapTarget, RichField}; use crate::hash::merkle_tree::MerkleCap; @@ -317,7 +318,7 @@ pub struct ProverOnlyCircuitData< ///The concrete placement of the lookup gates for each lookup table index. pub lookup_rows: Vec, /// A vector of (looking_in, looking_out) pairs for for each lookup table index. - pub lut_to_lookups: Vec>, + pub lut_to_lookups: Vec, } /// Circuit data required by the verifier, but not the prover. @@ -380,7 +381,7 @@ pub struct CommonCircuitData, const D: usize> { pub num_lookup_selectors: usize, /// The stored lookup tables. - pub luts: Vec>>, + pub luts: Vec, } impl, const D: usize> CommonCircuitData { diff --git a/plonky2/src/plonk/proof.rs b/plonky2/src/plonk/proof.rs index f9d5f1d1e7..bf70cacabc 100644 --- a/plonky2/src/plonk/proof.rs +++ b/plonky2/src/plonk/proof.rs @@ -456,6 +456,7 @@ mod tests { use crate::field::types::Sample; use crate::fri::reduction_strategies::FriReductionStrategy; + use crate::gates::lookup_table::LookupTable; use crate::gates::noop::NoopGate; use crate::iop::witness::PartialWitness; use crate::plonk::circuit_builder::CircuitBuilder; @@ -529,7 +530,7 @@ mod tests { 241, 25, 149, 105, 156, 51, 53, 168, 145, 247, 223, 79, 78, 226, 15, 222, 82, 115, 70, 210, 27, 41, 1, 170, 40, 131, 192, 229, 248, 255, ]; - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let config = CircuitConfig::standard_recursion_config(); let mut builder = CircuitBuilder::::new(config); let lut_index = builder.add_lookup_table_from_pairs(table); diff --git a/plonky2/src/recursion/recursive_verifier.rs b/plonky2/src/recursion/recursive_verifier.rs index 94a35080d2..613766e455 100644 --- a/plonky2/src/recursion/recursive_verifier.rs +++ b/plonky2/src/recursion/recursive_verifier.rs @@ -201,6 +201,7 @@ mod tests { use crate::fri::reduction_strategies::FriReductionStrategy; use crate::fri::FriConfig; use crate::gadgets::lookup::{OTHER_TABLE, TIP5_TABLE}; + use crate::gates::lookup_table::LookupTable; use crate::gates::noop::NoopGate; use crate::iop::witness::{PartialWitness, WitnessWrite}; use crate::plonk::circuit_data::{CircuitConfig, VerifierOnlyCircuitData}; @@ -421,7 +422,7 @@ mod tests { let look_val_b = 2; let tip5_table = TIP5_TABLE.to_vec(); - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let out_a = table[look_val_a].1; let out_b = table[look_val_b].1; @@ -482,7 +483,7 @@ mod tests { let first_out = tip5_table[look_val_a]; let second_out = tip5_table[look_val_b]; - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let other_table = OTHER_TABLE.to_vec(); @@ -495,7 +496,7 @@ mod tests { let s = first_out + second_out; let final_out = other_table[s as usize]; - let table2: Arc> = Arc::new((0..256).zip_eq(other_table).collect()); + let table2: LookupTable = Arc::new((0..256).zip_eq(other_table).collect()); let other_index = builder.add_lookup_table_from_pairs(table2); let output_final = builder.add_lookup_from_index(sum, other_index); @@ -556,7 +557,7 @@ mod tests { let look_val_b = 2; let tip5_table = TIP5_TABLE.to_vec(); - let table: Arc> = Arc::new((0..256).zip_eq(tip5_table).collect()); + let table: LookupTable = Arc::new((0..256).zip_eq(tip5_table).collect()); let out_a = table[look_val_a].1; let out_b = table[look_val_b].1; diff --git a/plonky2/src/util/serialization/mod.rs b/plonky2/src/util/serialization/mod.rs index 8cce51e634..7c8141769c 100644 --- a/plonky2/src/util/serialization/mod.rs +++ b/plonky2/src/util/serialization/mod.rs @@ -31,6 +31,7 @@ use crate::fri::reduction_strategies::FriReductionStrategy; use crate::fri::{FriConfig, FriParams}; use crate::gadgets::polynomial::PolynomialCoeffsExtTarget; use crate::gates::gate::GateRef; +use crate::gates::lookup::Lookup; use crate::gates::selectors::SelectorsInfo; use crate::hash::hash_types::{HashOutTarget, MerkleCapTarget, RichField}; use crate::hash::merkle_proofs::{MerkleProof, MerkleProofTarget}; @@ -1152,9 +1153,9 @@ pub trait Read { Ok(lut) } - /// Reads a target lookup table stored as `Vec<(Target, Target)>` from `self`. + /// Reads a target lookup table stored as `Lookup` from `self`. #[inline] - fn read_target_lut(&mut self) -> IoResult> { + fn read_target_lut(&mut self) -> IoResult { let length = self.read_usize()?; let mut lut = Vec::with_capacity(length); for _ in 0..length {