-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
_Issue #214_ A generalization of #234 for all range or scalar tables. - Moved and refactored the implementation of u16. - Implementation without generics because I find it cleaner and it compiles faster. - Definition of separate circuits `U5TableCircuit`, `U8TableCircuit`, `U16TableCircuit` using a parameter trait. - Fix soundness of `assert_u8`. --------- Co-authored-by: Aurélien Nicolas <[email protected]>
- Loading branch information
Showing
9 changed files
with
204 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,35 @@ | ||
use std::{collections::HashMap, marker::PhantomData, mem::MaybeUninit}; | ||
//! Definition of the range tables and their circuits. | ||
use crate::{ | ||
circuit_builder::CircuitBuilder, | ||
error::ZKVMError, | ||
expression::{Expression, Fixed, ToExpr, WitIn}, | ||
scheme::constants::MIN_PAR_SIZE, | ||
set_fixed_val, set_val, | ||
structs::ROMType, | ||
tables::TableCircuit, | ||
uint::constants::RANGE_CHIP_BIT_WIDTH, | ||
witness::RowMajorMatrix, | ||
}; | ||
use ff_ext::ExtensionField; | ||
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; | ||
mod range_impl; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct RangeTableConfig { | ||
u16_tbl: Fixed, | ||
u16_mlt: WitIn, | ||
} | ||
|
||
pub struct RangeTableCircuit<E>(PhantomData<E>); | ||
|
||
impl<E: ExtensionField> TableCircuit<E> for RangeTableCircuit<E> { | ||
type TableConfig = RangeTableConfig; | ||
type FixedInput = (); | ||
type WitnessInput = (); | ||
|
||
fn name() -> String { | ||
"RANGE".into() | ||
} | ||
|
||
fn construct_circuit(cb: &mut CircuitBuilder<E>) -> Result<RangeTableConfig, ZKVMError> { | ||
let u16_tbl = cb.create_fixed(|| "u16_tbl")?; | ||
let u16_mlt = cb.create_witin(|| "u16_mlt")?; | ||
mod range_circuit; | ||
use range_circuit::{RangeTable, RangeTableCircuit}; | ||
|
||
let u16_table_values = cb.rlc_chip_record(vec![ | ||
Expression::Constant(E::BaseField::from(ROMType::U16 as u64)), | ||
Expression::Fixed(u16_tbl.clone()), | ||
]); | ||
use crate::structs::ROMType; | ||
|
||
cb.lk_table_record(|| "u16 table", u16_table_values, u16_mlt.expr())?; | ||
|
||
Ok(RangeTableConfig { u16_tbl, u16_mlt }) | ||
pub struct U5Table; | ||
impl RangeTable for U5Table { | ||
const ROM_TYPE: ROMType = ROMType::U5; | ||
fn len() -> usize { | ||
1 << 5 | ||
} | ||
} | ||
pub type U5TableCircuit<E> = RangeTableCircuit<E, U5Table>; | ||
|
||
fn generate_fixed_traces( | ||
config: &RangeTableConfig, | ||
num_fixed: usize, | ||
_input: &(), | ||
) -> RowMajorMatrix<E::BaseField> { | ||
let num_u16s = 1 << 16; | ||
let mut fixed = RowMajorMatrix::<E::BaseField>::new(num_u16s, num_fixed); | ||
fixed | ||
.par_iter_mut() | ||
.with_min_len(MIN_PAR_SIZE) | ||
.zip((0..num_u16s).into_par_iter()) | ||
.for_each(|(row, i)| { | ||
set_fixed_val!(row, config.u16_tbl, E::BaseField::from(i as u64)); | ||
}); | ||
|
||
fixed | ||
pub struct U8Table; | ||
impl RangeTable for U8Table { | ||
const ROM_TYPE: ROMType = ROMType::U8; | ||
fn len() -> usize { | ||
1 << 8 | ||
} | ||
} | ||
pub type U8TableCircuit<E> = RangeTableCircuit<E, U8Table>; | ||
|
||
fn assign_instances( | ||
config: &Self::TableConfig, | ||
num_witin: usize, | ||
multiplicity: &[HashMap<u64, usize>], | ||
_input: &(), | ||
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> { | ||
let multiplicity = &multiplicity[ROMType::U16 as usize]; | ||
let mut u16_mlt = vec![0; 1 << RANGE_CHIP_BIT_WIDTH]; | ||
for (limb, mlt) in multiplicity { | ||
u16_mlt[*limb as usize] = *mlt; | ||
} | ||
|
||
let mut witness = RowMajorMatrix::<E::BaseField>::new(u16_mlt.len(), num_witin); | ||
witness | ||
.par_iter_mut() | ||
.with_min_len(MIN_PAR_SIZE) | ||
.zip(u16_mlt.into_par_iter()) | ||
.for_each(|(row, mlt)| { | ||
set_val!(row, config.u16_mlt, E::BaseField::from(mlt as u64)); | ||
}); | ||
|
||
Ok(witness) | ||
pub struct U16Table; | ||
impl RangeTable for U16Table { | ||
const ROM_TYPE: ROMType = ROMType::U16; | ||
fn len() -> usize { | ||
1 << 16 | ||
} | ||
} | ||
pub type U16TableCircuit<E> = RangeTableCircuit<E, U16Table>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! Range tables as circuits with trait TableCircuit. | ||
use super::range_impl::RangeTableConfig; | ||
|
||
use std::{collections::HashMap, marker::PhantomData}; | ||
|
||
use crate::{ | ||
circuit_builder::CircuitBuilder, error::ZKVMError, structs::ROMType, tables::TableCircuit, | ||
witness::RowMajorMatrix, | ||
}; | ||
use ff_ext::ExtensionField; | ||
|
||
/// Use this trait as parameter to RangeTableCircuit. | ||
pub trait RangeTable { | ||
const ROM_TYPE: ROMType; | ||
|
||
fn len() -> usize; | ||
|
||
fn content() -> Vec<u64> { | ||
(0..Self::len() as u64).collect() | ||
} | ||
} | ||
|
||
pub struct RangeTableCircuit<E, R>(PhantomData<(E, R)>); | ||
|
||
impl<E: ExtensionField, RANGE: RangeTable> TableCircuit<E> for RangeTableCircuit<E, RANGE> { | ||
type TableConfig = RangeTableConfig; | ||
type FixedInput = (); | ||
type WitnessInput = (); | ||
|
||
fn name() -> String { | ||
format!("RANGE_{:?}", RANGE::ROM_TYPE) | ||
} | ||
|
||
fn construct_circuit(cb: &mut CircuitBuilder<E>) -> Result<RangeTableConfig, ZKVMError> { | ||
cb.namespace( | ||
|| Self::name(), | ||
|cb| RangeTableConfig::construct_circuit(cb, RANGE::ROM_TYPE), | ||
) | ||
} | ||
|
||
fn generate_fixed_traces( | ||
config: &RangeTableConfig, | ||
num_fixed: usize, | ||
_input: &(), | ||
) -> RowMajorMatrix<E::BaseField> { | ||
config.generate_fixed_traces(num_fixed, RANGE::content()) | ||
} | ||
|
||
fn assign_instances( | ||
config: &Self::TableConfig, | ||
num_witin: usize, | ||
multiplicity: &[HashMap<u64, usize>], | ||
_input: &(), | ||
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> { | ||
let multiplicity = &multiplicity[RANGE::ROM_TYPE as usize]; | ||
config.assign_instances(num_witin, multiplicity, RANGE::len()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//! The implementation of range tables. No generics. | ||
use ff_ext::ExtensionField; | ||
use goldilocks::SmallField; | ||
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; | ||
use std::{collections::HashMap, mem::MaybeUninit}; | ||
|
||
use crate::{ | ||
circuit_builder::CircuitBuilder, | ||
error::ZKVMError, | ||
expression::{Expression, Fixed, ToExpr, WitIn}, | ||
scheme::constants::MIN_PAR_SIZE, | ||
set_fixed_val, set_val, | ||
structs::ROMType, | ||
witness::RowMajorMatrix, | ||
}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct RangeTableConfig { | ||
fixed: Fixed, | ||
mlt: WitIn, | ||
} | ||
|
||
impl RangeTableConfig { | ||
pub fn construct_circuit<E: ExtensionField>( | ||
cb: &mut CircuitBuilder<E>, | ||
rom_type: ROMType, | ||
) -> Result<Self, ZKVMError> { | ||
let fixed = cb.create_fixed(|| "fixed")?; | ||
let mlt = cb.create_witin(|| "mlt")?; | ||
|
||
let rlc_record = cb.rlc_chip_record(vec![ | ||
(rom_type as usize).into(), | ||
Expression::Fixed(fixed.clone()), | ||
]); | ||
|
||
cb.lk_table_record(|| "record", rlc_record, mlt.expr())?; | ||
|
||
Ok(Self { fixed, mlt }) | ||
} | ||
|
||
pub fn generate_fixed_traces<F: SmallField>( | ||
&self, | ||
num_fixed: usize, | ||
content: Vec<u64>, | ||
) -> RowMajorMatrix<F> { | ||
let mut fixed = RowMajorMatrix::<F>::new(content.len(), num_fixed); | ||
|
||
// Fill the padding with zeros, if any. | ||
fixed.par_iter_mut().skip(content.len()).for_each(|row| { | ||
set_fixed_val!(row, self.fixed, F::ZERO); | ||
}); | ||
|
||
fixed | ||
.par_iter_mut() | ||
.with_min_len(MIN_PAR_SIZE) | ||
.zip(content.into_par_iter()) | ||
.for_each(|(row, i)| { | ||
set_fixed_val!(row, self.fixed, F::from(i)); | ||
}); | ||
|
||
fixed | ||
} | ||
|
||
pub fn assign_instances<F: SmallField>( | ||
&self, | ||
num_witin: usize, | ||
multiplicity: &HashMap<u64, usize>, | ||
length: usize, | ||
) -> Result<RowMajorMatrix<F>, ZKVMError> { | ||
let mut witness = RowMajorMatrix::<F>::new(length, num_witin); | ||
|
||
let mut mlts = vec![0; length]; | ||
for (idx, mlt) in multiplicity { | ||
mlts[*idx as usize] = *mlt; | ||
} | ||
|
||
// Fill the padding with zeros, if any. | ||
witness.par_iter_mut().skip(length).for_each(|row| { | ||
set_val!(row, self.mlt, F::ZERO); | ||
}); | ||
|
||
witness | ||
.par_iter_mut() | ||
.with_min_len(MIN_PAR_SIZE) | ||
.zip(mlts.into_par_iter()) | ||
.for_each(|(row, mlt)| { | ||
set_val!(row, self.mlt, F::from(mlt as u64)); | ||
}); | ||
|
||
Ok(witness) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters