-
Notifications
You must be signed in to change notification settings - Fork 12
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
u8 pair table #225
Closed
u8 pair table #225
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cba2ce8
byte-pair: Table for assert_byte and pairs of bytes
bb01626
Merge branch 'master' into byte-pair
c0dca74
byte-pair: fix after merge
33de71c
byte-pair: merge U8Pair with RangeTable for easy usage
7cd9cf1
byte-pair: zero padding
b082764
Merge branch 'master' into byte-pair
naure 7ee46ca
Merge branch 'master' into byte-pair
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use ff::Field; | ||
use ff_ext::ExtensionField; | ||
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; | ||
use std::{collections::HashMap, marker::PhantomData, 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, | ||
}; | ||
|
||
const NUM_U8_PAIRS: usize = 1 << 16; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct U8PairTableConfig { | ||
tbl_a: Fixed, | ||
tbl_b: Fixed, | ||
mlt: WitIn, | ||
} | ||
|
||
pub struct U8PairTableCircuit<E>(PhantomData<E>); | ||
|
||
impl<E: ExtensionField> U8PairTableCircuit<E> { | ||
pub fn construct_circuit(cb: &mut CircuitBuilder<E>) -> Result<U8PairTableConfig, ZKVMError> { | ||
let tbl_a = cb.create_fixed(|| "tbl_a")?; | ||
let tbl_b = cb.create_fixed(|| "tbl_b")?; | ||
let mlt = cb.create_witin(|| "mlt")?; | ||
|
||
let rlc_record = cb.rlc_chip_record(vec![ | ||
Expression::Constant(E::BaseField::from(ROMType::U8Pair as u64)), | ||
Expression::Fixed(tbl_a.clone()), | ||
Expression::Fixed(tbl_b.clone()), | ||
]); | ||
|
||
cb.lk_table_record(|| "u8_pair_table", rlc_record, mlt.expr())?; | ||
|
||
Ok(U8PairTableConfig { tbl_a, tbl_b, mlt }) | ||
} | ||
|
||
pub fn generate_fixed_traces( | ||
config: &U8PairTableConfig, | ||
fixed: &mut RowMajorMatrix<E::BaseField>, | ||
) { | ||
assert!(fixed.num_instances() >= NUM_U8_PAIRS); | ||
|
||
fixed | ||
.par_iter_mut() | ||
.with_min_len(MIN_PAR_SIZE) | ||
.zip((0..NUM_U8_PAIRS).into_par_iter()) | ||
.for_each(|(row, i)| { | ||
let a = i & 0xff; | ||
let b = (i >> 8) & 0xff; | ||
set_fixed_val!(row, config.tbl_a, E::BaseField::from(a as u64)); | ||
set_fixed_val!(row, config.tbl_b, E::BaseField::from(b as u64)); | ||
}); | ||
|
||
// Fill the rest with zeros. | ||
fixed.par_iter_mut().skip(NUM_U8_PAIRS).for_each(|row| { | ||
set_fixed_val!(row, config.tbl_a, E::BaseField::ZERO); | ||
set_fixed_val!(row, config.tbl_b, E::BaseField::ZERO); | ||
}); | ||
} | ||
|
||
pub fn assign_instances( | ||
config: &U8PairTableConfig, | ||
multiplicity: &[HashMap<u64, usize>], | ||
witness: &mut RowMajorMatrix<E::BaseField>, | ||
) { | ||
assert!(witness.num_instances() >= NUM_U8_PAIRS); | ||
|
||
let mut mlts = vec![0; NUM_U8_PAIRS]; | ||
for (idx, mlt) in &multiplicity[ROMType::U8Pair as usize] { | ||
mlts[*idx as usize] = *mlt; | ||
} | ||
|
||
witness | ||
.par_iter_mut() | ||
.with_min_len(MIN_PAR_SIZE) | ||
.zip(mlts.into_par_iter()) | ||
.for_each(|(row, mlt)| { | ||
set_val!(row, config.mlt, E::BaseField::from(mlt as u64)); | ||
}); | ||
|
||
// Fill the rest with zeros. | ||
witness.par_iter_mut().skip(NUM_U8_PAIRS).for_each(|row| { | ||
set_val!(row, config.mlt, E::BaseField::ZERO); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit concern is about this extra overhead on original "assert_byte" since it will occur a slow version.
To address this, an option is iterating limbs via chunk(2) therefore assert_u8_pair works.
There are other place to invoke assert_byte() directly
ceno/ceno_zkvm/src/uint.rs
Line 285 in b300e9b
Which I believe for potiential performance pitfall we should refactor it traverse in chunk as well