-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: add u32 related hash functions #27
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,9 @@ use crate::{ | |
/// - Samples a field element in a prime field of size `p` by sampling uniformly an element in the | ||
/// range (0..1 << log_2(p)). This avoids modulo bias. | ||
#[derive(Clone, Debug)] | ||
pub struct SerializingChallenger32<F, Inner> { | ||
pub struct SerializingChallenger32<F, T, Inner> { | ||
tamirhemo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
inner: Inner, | ||
_marker: PhantomData<F>, | ||
_marker: PhantomData<(F, T)>, | ||
} | ||
|
||
/// Given a challenger that can observe and sample bytes, produces a challenger that is able to | ||
|
@@ -41,7 +41,7 @@ pub struct SerializingChallenger64<F, Inner> { | |
_marker: PhantomData<F>, | ||
} | ||
|
||
impl<F: PrimeField32, Inner: CanObserve<u8>> SerializingChallenger32<F, Inner> { | ||
impl<F: PrimeField32, T, Inner: CanObserve<T>> SerializingChallenger32<F, T, Inner> { | ||
pub fn new(inner: Inner) -> Self { | ||
Self { | ||
inner, | ||
|
@@ -50,56 +50,77 @@ impl<F: PrimeField32, Inner: CanObserve<u8>> SerializingChallenger32<F, Inner> { | |
} | ||
} | ||
|
||
impl<F, H> SerializingChallenger32<F, HashChallenger<u8, H, 32>> | ||
impl<F, T, H, const N: usize> SerializingChallenger32<F, T, HashChallenger<T, H, N>> | ||
where | ||
F: PrimeField32, | ||
H: CryptographicHasher<u8, [u8; 32]>, | ||
H: CryptographicHasher<T, [T; N]>, | ||
T: Clone, | ||
{ | ||
pub fn from_hasher(initial_state: Vec<u8>, hasher: H) -> Self { | ||
pub fn from_hasher(initial_state: Vec<T>, hasher: H) -> Self { | ||
Self::new(HashChallenger::new(initial_state, hasher)) | ||
} | ||
} | ||
|
||
impl<F: PrimeField32, Inner: CanObserve<u8>> CanObserve<F> for SerializingChallenger32<F, Inner> { | ||
impl<F: PrimeField32, Inner: CanObserve<u8>> CanObserve<F> for SerializingChallenger32<F, u8, Inner> { | ||
fn observe(&mut self, value: F) { | ||
self.inner | ||
.observe_slice(&value.as_canonical_u32().to_le_bytes()); | ||
} | ||
} | ||
|
||
impl<F: PrimeField32, const N: usize, Inner: CanObserve<u8>> CanObserve<Hash<F, u8, N>> | ||
for SerializingChallenger32<F, Inner> | ||
impl<F: PrimeField32, Inner: CanObserve<u32>> CanObserve<F> for SerializingChallenger32<F, u32, Inner> { | ||
fn observe(&mut self, value: F) { | ||
self.inner | ||
.observe(value.as_canonical_u32()); | ||
} | ||
} | ||
|
||
impl<F: PrimeField32, T, const N: usize, Inner: CanObserve<T>> CanObserve<Hash<F, T, N>> | ||
for SerializingChallenger32<F, T, Inner> | ||
{ | ||
fn observe(&mut self, values: Hash<F, u8, N>) { | ||
fn observe(&mut self, values: Hash<F, T, N>) { | ||
for value in values { | ||
self.inner.observe(value); | ||
} | ||
} | ||
} | ||
|
||
impl<F, EF, Inner> CanSample<EF> for SerializingChallenger32<F, Inner> | ||
impl<F, EF, Inner> CanSample<EF> for SerializingChallenger32<F, u8, Inner> | ||
where | ||
F: PrimeField32, | ||
EF: ExtensionField<F>, | ||
Inner: CanSample<u8>, | ||
{ | ||
fn sample(&mut self) -> EF { | ||
let modulus = F::ORDER_U64 as u32; | ||
let log_size = log2_ceil_u64(F::ORDER_U64); | ||
let pow_of_two_bound = (1 << log_size) - 1; | ||
// Perform rejection sampling over the uniform range (0..log2_ceil(p)) | ||
let sample_base = |inner: &mut Inner| loop { | ||
let log_size = log2_ceil_u64(F::ORDER_U64).saturating_sub(1); | ||
let bound = (1 << log_size) - 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The plonky3 sampling uses rejection sampling (probably changes since the last time you update the branch), so we should keep that in our versions as well |
||
let sample_base = |inner: &mut Inner| { | ||
let value = u32::from_le_bytes(inner.sample_array::<4>()); | ||
let value = value & pow_of_two_bound; | ||
if value < modulus { | ||
return F::from_canonical_u32(value); | ||
} | ||
F::from_canonical_u32(value & bound) | ||
}; | ||
EF::from_base_fn(|_| sample_base(&mut self.inner)) | ||
} | ||
} | ||
|
||
impl<F, EF, Inner> CanSample<EF> for SerializingChallenger32<F, u32, Inner> | ||
where | ||
F: PrimeField32, | ||
EF: ExtensionField<F>, | ||
Inner: CanSample<u32>, | ||
{ | ||
fn sample(&mut self) -> EF { | ||
let log_size = log2_ceil_u64(F::ORDER_U64).saturating_sub(1); | ||
let bound = (1 << log_size) - 1; | ||
let sample_base = |inner: &mut Inner| { | ||
let value = inner.sample(); | ||
F::from_canonical_u32(value & bound) | ||
}; | ||
EF::from_base_fn(|_| sample_base(&mut self.inner)) | ||
} | ||
} | ||
|
||
impl<F, Inner> CanSampleBits<usize> for SerializingChallenger32<F, Inner> | ||
|
||
impl<F, Inner> CanSampleBits<usize> for SerializingChallenger32<F, u8, Inner> | ||
where | ||
F: PrimeField32, | ||
Inner: CanSample<u8>, | ||
|
@@ -113,15 +134,31 @@ where | |
} | ||
} | ||
|
||
impl<F, Inner> GrindingChallenger for SerializingChallenger32<F, Inner> | ||
impl<F, Inner> CanSampleBits<usize> for SerializingChallenger32<F, u32, Inner> | ||
where | ||
F: PrimeField32, | ||
Inner: CanSample<u32>, | ||
{ | ||
fn sample_bits(&mut self, bits: usize) -> usize { | ||
debug_assert!(bits < (usize::BITS as usize)); | ||
// Limiting the number of bits to the field size | ||
debug_assert!((1 << bits) <= F::ORDER_U64 as usize); | ||
let rand_usize = self.inner.sample() as usize; | ||
rand_usize & ((1 << bits) - 1) | ||
} | ||
} | ||
|
||
|
||
impl<F, Inner> GrindingChallenger for SerializingChallenger32<F, u8, Inner> | ||
where | ||
F: PrimeField32, | ||
Inner: CanSample<u8> + CanObserve<u8> + Clone + Send + Sync, | ||
{ | ||
type Witness = F; | ||
|
||
#[instrument(name = "grind for proof-of-work witness", skip_all)] | ||
fn grind(&mut self, bits: usize) -> Self::Witness { | ||
fn grind(&mut self, bits: usize) -> Self::Witness | ||
{ | ||
let witness = (0..F::ORDER_U64) | ||
.into_par_iter() | ||
.map(|i| F::from_canonical_u64(i)) | ||
|
@@ -132,13 +169,40 @@ where | |
} | ||
} | ||
|
||
impl<F, Inner> FieldChallenger<F> for SerializingChallenger32<F, Inner> | ||
impl<F, Inner> GrindingChallenger for SerializingChallenger32<F, u32, Inner> | ||
where | ||
F: PrimeField32, | ||
Inner: CanSample<u32> + CanObserve<u32> + Clone + Send + Sync, | ||
{ | ||
type Witness = F; | ||
|
||
#[instrument(name = "grind for proof-of-work witness", skip_all)] | ||
fn grind(&mut self, bits: usize) -> Self::Witness | ||
{ | ||
let witness = (0..F::ORDER_U64) | ||
.into_par_iter() | ||
.map(|i| F::from_canonical_u64(i)) | ||
.find_any(|witness| self.clone().check_witness(bits, *witness)) | ||
.expect("failed to find witness"); | ||
assert!(self.check_witness(bits, witness)); | ||
witness | ||
} | ||
} | ||
|
||
impl<F, Inner> FieldChallenger<F> for SerializingChallenger32<F, u8, Inner> | ||
where | ||
F: PrimeField32, | ||
Inner: CanSample<u8> + CanObserve<u8> + Clone + Send + Sync, | ||
{ | ||
} | ||
|
||
impl<F, Inner> FieldChallenger<F> for SerializingChallenger32<F, u32, Inner> | ||
where | ||
F: PrimeField32, | ||
Inner: CanSample<u32> + CanObserve<u32> + Clone + Send + Sync, | ||
{ | ||
} | ||
|
||
impl<F: PrimeField64, Inner: CanObserve<u8>> SerializingChallenger64<F, Inner> { | ||
pub fn new(inner: Inner) -> Self { | ||
Self { | ||
|
@@ -182,16 +246,11 @@ where | |
Inner: CanSample<u8>, | ||
{ | ||
fn sample(&mut self) -> EF { | ||
let modulus = F::ORDER_U64; | ||
let log_size = log2_ceil_u64(F::ORDER_U64); | ||
let pow_of_two_bound = (1 << log_size) - 1; | ||
// Perform rejection sampling over the uniform range (0..log2_ceil(p)) | ||
let sample_base = |inner: &mut Inner| loop { | ||
let log_size = log2_ceil_u64(F::ORDER_U64).saturating_sub(1); | ||
let bound = (1 << log_size) - 1; | ||
let sample_base = |inner: &mut Inner| { | ||
let value = u64::from_le_bytes(inner.sample_array::<8>()); | ||
let value = value & pow_of_two_bound; | ||
if value < modulus { | ||
return F::from_canonical_u64(value); | ||
} | ||
F::from_canonical_u64(value & bound) | ||
}; | ||
EF::from_base_fn(|_| sample_base(&mut self.inner)) | ||
} | ||
|
@@ -235,4 +294,4 @@ where | |
F: PrimeField64, | ||
Inner: CanSample<u8> + CanObserve<u8> + Clone + Send + Sync, | ||
{ | ||
} | ||
} |
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 |
---|---|---|
|
@@ -96,4 +96,4 @@ where | |
.map(|x| PW::from_fn(|i| x.as_slice()[i].as_canonical_u64())), | ||
) | ||
} | ||
} | ||
} |
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.
I think that we should have another struct
Blake3zk
or something that uses our precompiles, it could even be in the sp1 recursion crate. The reason is that we would eventually want the normal Blake3 to implement hashing onu32
(this might be useful for SIMD utilization. of the prover)