Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[o1js] Deduplicate lookup tables #1336

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions kimchi/src/circuits/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use crate::{
domain_constant_evaluation::DomainConstantEvaluations,
domains::EvaluationDomains,
gate::{CircuitGate, GateType},
lookup::{index::LookupConstraintSystem, lookups::LookupFeatures, tables::LookupTable},
lookup::{
index::LookupConstraintSystem,
lookups::LookupFeatures,
tables::{GateLookupTables, LookupTable},
},
polynomial::{WitnessEvals, WitnessOverDomains, WitnessShifts},
polynomials::permutation::Shifts,
wires::*,
Expand Down Expand Up @@ -706,11 +710,18 @@ impl<F: PrimeField + SquareRootField> Builder<F> {
}
// And we add the built-in tables, depending on the features.
let LookupFeatures { patterns, .. } = &lookup_features;
let mut gate_lookup_tables = GateLookupTables {
xor: false,
range_check: false,
};
for pattern in patterns.into_iter() {
if let Some(gate_table) = pattern.table() {
lookup_domain_size += gate_table.table_size();
gate_lookup_tables[gate_table] = true
}
}
for gate_table in gate_lookup_tables.into_iter() {
lookup_domain_size += gate_table.table_size();
}
lookup_domain_size
};

Expand Down
47 changes: 47 additions & 0 deletions kimchi/src/circuits/lookup/tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,53 @@ pub enum GateLookupTable {
RangeCheck,
}

/// Enumerates the different 'fixed' lookup tables used by individual gates
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GateLookupTables {
pub xor: bool,
pub range_check: bool,
}

impl std::ops::Index<GateLookupTable> for GateLookupTables {
type Output = bool;

fn index(&self, index: GateLookupTable) -> &Self::Output {
match index {
GateLookupTable::Xor => &self.xor,
GateLookupTable::RangeCheck => &self.range_check,
}
}
}

impl std::ops::IndexMut<GateLookupTable> for GateLookupTables {
fn index_mut(&mut self, index: GateLookupTable) -> &mut Self::Output {
match index {
GateLookupTable::Xor => &mut self.xor,
GateLookupTable::RangeCheck => &mut self.range_check,
}
}
}

impl IntoIterator for GateLookupTables {
type Item = GateLookupTable;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
// Destructor pattern to make sure we add new lookup patterns.
let GateLookupTables { xor, range_check } = self;

let mut patterns = Vec::with_capacity(2);

if xor {
patterns.push(GateLookupTable::Xor)
}
if range_check {
patterns.push(GateLookupTable::RangeCheck)
}
patterns.into_iter()
}
}

/// A table of values that can be used for a lookup, along with the ID for the table.
#[derive(Debug, Clone)]
pub struct LookupTable<F> {
Expand Down