Skip to content

Commit

Permalink
additional tables containing individual columns
Browse files Browse the repository at this point in the history
  • Loading branch information
querolita committed Oct 3, 2023
1 parent 9545036 commit 74cd60a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
11 changes: 9 additions & 2 deletions book/src/specs/kimchi.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,15 @@ pub const XOR_TABLE_ID: i32 = 0;
/// The range check table ID.
pub const RANGE_CHECK_TABLE_ID: i32 = 1;

/// The table ID associated with the sparse lookup table.
pub const SPARSE_TABLE_ID: i32 = 2;
/// The table ID associated with the 16-bit lookup table.
pub const BITS16_TABLE_ID: i32 = 2;

/// The table ID associated with the 1-column sparse lookup table.
pub const SPARSE_TABLE_ID: i32 = 3;

/// The table ID associated with the 2-column sparse lookup table.
pub const RESET_TABLE_ID: i32 = 4;

```


Expand Down
27 changes: 27 additions & 0 deletions kimchi/src/circuits/lookup/tables/bits16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::circuits::lookup::tables::LookupTable;
use ark_ff::Field;

use super::BITS16_TABLE_ID;

//~ The lookup table for 16-bits

/// Returns the lookup table for all 16-bit values
///
/// # Panics
///
/// Will panic if `data` is invalid.
pub fn bits16_table<F: Field>() -> LookupTable<F> {
let mut data = vec![vec![]; 1];

// All of the 16-bit values
for i in 0u64..=0xFFFF {
data[0].push(F::from(i));
}

LookupTable {
id: BITS16_TABLE_ID,
data,
}
}

pub const TABLE_SIZE: usize = 65536;
23 changes: 19 additions & 4 deletions kimchi/src/circuits/lookup/tables/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use ark_ff::{FftField, One, Zero};
use poly_commitment::PolyComm;
use serde::{Deserialize, Serialize};

pub mod bits16;
pub mod range_check;
pub mod reset;
pub mod sparse;
pub mod xor;

Expand All @@ -13,16 +15,25 @@ pub const XOR_TABLE_ID: i32 = 0;
/// The range check table ID.
pub const RANGE_CHECK_TABLE_ID: i32 = 1;

/// The table ID associated with the sparse lookup table.
pub const SPARSE_TABLE_ID: i32 = 2;
/// The table ID associated with the 16-bit lookup table.
pub const BITS16_TABLE_ID: i32 = 2;

/// The table ID associated with the 1-column sparse lookup table.
pub const SPARSE_TABLE_ID: i32 = 3;

/// The table ID associated with the 2-column sparse lookup table.
pub const RESET_TABLE_ID: i32 = 4;

//~ spec:endcode

/// Enumerates the different 'fixed' lookup tables used by individual gates
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum GateLookupTable {
Xor,
RangeCheck,
Bits16,
Sparse,
Reset,
}

/// A table of values that can be used for a lookup, along with the ID for the table.
Expand Down Expand Up @@ -68,17 +79,21 @@ pub fn get_table<F: FftField>(table_name: GateLookupTable) -> LookupTable<F> {
match table_name {
GateLookupTable::Xor => xor::xor_table(),
GateLookupTable::RangeCheck => range_check::range_check_table(),
GateLookupTable::Bits16 => bits16::bits16_table(),
GateLookupTable::Sparse => sparse::sparse_table(),
GateLookupTable::Reset => reset::reset_table(),
}
}

impl GateLookupTable {
/// Returns the lookup table associated to a [`GateLookupTable`].
/// Returns the size of a lookup table associated to a [`GateLookupTable`].
pub fn table_size(&self) -> usize {
match self {
GateLookupTable::Xor => xor::TABLE_SIZE,
GateLookupTable::RangeCheck => range_check::TABLE_SIZE,
GateLookupTable::Sparse => sparse::TABLE_SIZE,
GateLookupTable::Bits16 | GateLookupTable::Reset | GateLookupTable::Sparse => {
sparse::TABLE_SIZE
}
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions kimchi/src/circuits/lookup/tables/reset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::circuits::lookup::tables::LookupTable;
use ark_ff::Field;

use super::RESET_TABLE_ID;

//~ The lookup table for 16-bit expansion for Keccak words encoding.
//~ This is a 2-column table containing the reset sparse representation of the 16-bit values.
//~ The first column contains the 16-bit values, and the second column contains their expansion to 64-bit values.

/// Returns the sparse lookup table
///
/// # Panics
///
/// Will panic if `data` is invalid.
pub fn reset_table<F: Field>() -> LookupTable<F> {
let mut data = vec![vec![]; 2];

// Sparse expansion table for all of the 16-bit values
for i in 0u64..=0xFFFF {
data[0].push(F::from(i));
// Uses the fact that the expansion coincides with the hexadecimal interpretation of the index expressed in binary
// (i.e. expanding 1b gives 0x0001, expanding 0b gives 0x0000)
data[1].push(F::from(
u64::from_str_radix(&format!("{:b}", i), 16).unwrap(),
));
}

LookupTable {
id: RESET_TABLE_ID,
data,
}
}

pub const TABLE_SIZE: usize = 65536;
10 changes: 3 additions & 7 deletions kimchi/src/circuits/lookup/tables/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::circuits::lookup::tables::{LookupTable, SPARSE_TABLE_ID};
use ark_ff::Field;

//~ The lookup table for 16-bit expansion for Keccak words encoding.
//~ This is a 2-column table containing the sparse representation of the 16-bit values.
//~ The first column contains the 16-bit values, and the second column contains their expansion to 64-bit values.
//~ This is a 1-column table containing the sparse representation of all 16-bit preimages.

/// Returns the sparse lookup table
///
Expand All @@ -13,12 +12,9 @@ use ark_ff::Field;
pub fn sparse_table<F: Field>() -> LookupTable<F> {
let mut data = vec![vec![]; 2];

// Sparse expansion table for all of the 16-bit values
// Sparse expansion table
for i in 0u64..=0xFFFF {
data[0].push(F::from(i));
// Uses the fact that the expansion coincides with the hexadecimal interpretation of the index expressed in binary
// (i.e. expanding 1b gives 0x0001, expanding 0b gives 0x0000)
data[1].push(F::from(
data[0].push(F::from(
u64::from_str_radix(&format!("{:b}", i), 16).unwrap(),
));
}
Expand Down

0 comments on commit 74cd60a

Please sign in to comment.