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

Adding new range check32 table implementation #2732

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions o1vm/src/interpreters/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,24 @@ pub trait InterpreterEnv {
self.lookup_2bits(value);
}

// Adds a lookup to the ByteLookup table for each byte of a 32-bit value
fn lookup_32bits(&mut self, value: &Self::Variable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you trying to achieve here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • yeah, why is it not enough to have two 16bit tables?...

self.add_lookup(Lookup::read_one(
LookupTableIDs::ByteLookup,
vec![
value.clone(),
value.clone() + Self::constant(1 << 8),
value.clone() + Self::constant(1 << 16),
value.clone() + Self::constant(1 << 24),
],
));
}

/// Range checks with 4 lookups to the ByteLookup table that a value
fn range_check32(&mut self, value: &Self::Variable) {
self.lookup_32bits(value);
}

fn range_check64(&mut self, _value: &Self::Variable) {
// TODO
}
Expand Down
25 changes: 24 additions & 1 deletion o1vm/src/lookups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub enum LookupTableIDs {
SyscallLookup = 9,
/// Input/Output of Keccak steps
KeccakStepLookup = 10,
/// Single-column table of 2^32 entries with the sparse representation of all values
RangeCheck32Lookup = 11,
}

impl LookupTableID for LookupTableIDs {
Expand All @@ -71,6 +73,7 @@ impl LookupTableID for LookupTableIDs {
8 => RegisterLookup,
9 => SyscallLookup,
10 => KeccakStepLookup,
11 => RangeCheck32Lookup,
_ => panic!("Invalid table ID"),
}
}
Expand All @@ -82,6 +85,7 @@ impl LookupTableID for LookupTableIDs {
AtMost4Lookup => 5,
ByteLookup => 1 << 8,
RangeCheck16Lookup | SparseLookup | ResetLookup => 1 << 16,
RangeCheck32Lookup => 1 << 32,
MemoryLookup | RegisterLookup | SyscallLookup | KeccakStepLookup => {
panic!("RAM Tables do not have a fixed length")
}
Expand All @@ -91,7 +95,7 @@ impl LookupTableID for LookupTableIDs {
fn is_fixed(&self) -> bool {
match self {
PadLookup | RoundConstantsLookup | AtMost4Lookup | ByteLookup | RangeCheck16Lookup
| SparseLookup | ResetLookup => true,
| SparseLookup | ResetLookup | RangeCheck32Lookup => true,
MemoryLookup | RegisterLookup | SyscallLookup | KeccakStepLookup => false,
}
}
Expand All @@ -117,6 +121,7 @@ impl LookupTableID for LookupTableIDs {
Self::RegisterLookup,
Self::SyscallLookup,
Self::KeccakStepLookup,
Self::RangeCheck32Lookup,
]
}
}
Expand All @@ -139,6 +144,8 @@ pub(crate) trait FixedLookupTables<F> {
fn table_sparse() -> LookupTable<F>;
/// Returns the reset table
fn table_reset() -> LookupTable<F>;
/// Returns the range check 32 table
fn table_range_check_32() -> LookupTable<F>;
}

impl<F: Field> FixedLookupTables<F> for LookupTable<F> {
Expand All @@ -160,6 +167,13 @@ impl<F: Field> FixedLookupTables<F> for LookupTable<F> {
None
}
}
RangeCheck32Lookup => {
if idx < id.length() && table.entries[idx] == value {
Some(idx)
} else {
None
}
}
PadLookup => {
// Because this table starts with entry 1
if idx - 1 < id.length() && table.entries[idx - 1] == value {
Expand Down Expand Up @@ -275,4 +289,13 @@ impl<F: Field> FixedLookupTables<F> for LookupTable<F> {
.collect(),
}
}

fn table_range_check_32() -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really want to keep 2^32 * 4 bytes in memory? This is madness!

Self {
table_id: RangeCheck32Lookup,
entries: (0..RangeCheck32Lookup.length())
.map(|i| vec![F::from(i as u32)])
.collect(),
}
}
}
Loading