From 03a7b68a31a1d72bf4923af569996c5a93293e96 Mon Sep 17 00:00:00 2001 From: peinlcy Date: Tue, 5 Dec 2023 13:04:43 +0800 Subject: [PATCH 1/3] add hash bytes --- plonky2/field/src/cfft/concurrent.rs | 4 ++-- plonky2/plonky2/src/hash/mod.rs | 1 + plonky2/plonky2/src/hash/utils.rs | 34 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 plonky2/plonky2/src/hash/utils.rs diff --git a/plonky2/field/src/cfft/concurrent.rs b/plonky2/field/src/cfft/concurrent.rs index de53b3e7..24e775f8 100644 --- a/plonky2/field/src/cfft/concurrent.rs +++ b/plonky2/field/src/cfft/concurrent.rs @@ -1,6 +1,6 @@ +use maybe_rayon::current_num_threads; use maybe_rayon::{ - current_num_threads, IndexedParallelIterator, MaybeParChunks, MaybeParChunksMut, - MaybeParIterMut, ParallelIterator, + IndexedParallelIterator, MaybeParChunks, MaybeParChunksMut, MaybeParIterMut, ParallelIterator, }; use plonky2_util::log2_strict; diff --git a/plonky2/plonky2/src/hash/mod.rs b/plonky2/plonky2/src/hash/mod.rs index a76b03ac..777ad525 100644 --- a/plonky2/plonky2/src/hash/mod.rs +++ b/plonky2/plonky2/src/hash/mod.rs @@ -11,3 +11,4 @@ pub mod poseidon; pub mod poseidon2; pub mod poseidon2_goldilocks; pub mod poseidon_goldilocks; +pub mod utils; diff --git a/plonky2/plonky2/src/hash/utils.rs b/plonky2/plonky2/src/hash/utils.rs new file mode 100644 index 00000000..26aea60c --- /dev/null +++ b/plonky2/plonky2/src/hash/utils.rs @@ -0,0 +1,34 @@ +use crate::plonk::config::{GenericConfig, GenericHashOut, Hasher, PoseidonGoldilocksConfig}; +use maybe_rayon::{MaybeParIter, ParallelIterator}; +use plonky2_field::types::Field; + +const D: usize = 2; +type C = PoseidonGoldilocksConfig; +type F = >::F; +type H = >::Hasher; + +pub const GOLDILOCKS_FIELD_U8_LEN: usize = 8; + +pub fn hash_u8_array(input: &[u8]) -> [u8; 32] { + let field_elements = bytes_to_u64s(input) + .par_iter() + .map(|&i| F::from_canonical_u64(i)) + .collect::>(); + let hash = H::hash_no_pad(&field_elements); + hash.to_bytes().as_slice().try_into().unwrap() +} + +pub fn bytes_to_u64s(bytes: &[u8]) -> Vec { + assert!( + bytes.len() % GOLDILOCKS_FIELD_U8_LEN == 0, + "Bytes must be divisible by 8" + ); + bytes + .chunks(GOLDILOCKS_FIELD_U8_LEN) + .map(|chunk| { + let mut bytes = [0u8; 8]; + bytes.copy_from_slice(chunk); + u64::from_be_bytes(bytes) + }) + .collect() +} From 7f08ec64c6864e27e5b74d18ca05fc3ff9146b7e Mon Sep 17 00:00:00 2001 From: peinlcy Date: Tue, 5 Dec 2023 13:14:58 +0800 Subject: [PATCH 2/3] add poseidon hash bytes --- plonky2/plonky2/src/hash/utils.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/plonky2/plonky2/src/hash/utils.rs b/plonky2/plonky2/src/hash/utils.rs index 26aea60c..855dd8b6 100644 --- a/plonky2/plonky2/src/hash/utils.rs +++ b/plonky2/plonky2/src/hash/utils.rs @@ -1,20 +1,25 @@ +use super::hash_types::RichField; use crate::plonk::config::{GenericConfig, GenericHashOut, Hasher, PoseidonGoldilocksConfig}; use maybe_rayon::{MaybeParIter, ParallelIterator}; -use plonky2_field::types::Field; - -const D: usize = 2; -type C = PoseidonGoldilocksConfig; -type F = >::F; -type H = >::Hasher; +use plonky2_field::extension::Extendable; pub const GOLDILOCKS_FIELD_U8_LEN: usize = 8; -pub fn hash_u8_array(input: &[u8]) -> [u8; 32] { +pub fn poseidon_hash_bytes(input: &[u8]) -> [u8; 32] { + const D: usize = 2; + type C = PoseidonGoldilocksConfig; + type F = >::F; + hash_bytes::(input) +} + +pub fn hash_bytes, C: GenericConfig, const D: usize>( + input: &[u8], +) -> [u8; 32] { let field_elements = bytes_to_u64s(input) .par_iter() .map(|&i| F::from_canonical_u64(i)) .collect::>(); - let hash = H::hash_no_pad(&field_elements); + let hash = C::InnerHasher::hash_no_pad(&field_elements); hash.to_bytes().as_slice().try_into().unwrap() } From e0af7fe6db17018894054f6890acfaa0b59c0781 Mon Sep 17 00:00:00 2001 From: peinlcy Date: Tue, 5 Dec 2023 17:31:01 +0800 Subject: [PATCH 3/3] padding enough to input bytes in hash_bytes --- plonky2/plonky2/src/hash/utils.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/plonky2/plonky2/src/hash/utils.rs b/plonky2/plonky2/src/hash/utils.rs index 855dd8b6..c2553264 100644 --- a/plonky2/plonky2/src/hash/utils.rs +++ b/plonky2/plonky2/src/hash/utils.rs @@ -15,7 +15,16 @@ pub fn poseidon_hash_bytes(input: &[u8]) -> [u8; 32] { pub fn hash_bytes, C: GenericConfig, const D: usize>( input: &[u8], ) -> [u8; 32] { - let field_elements = bytes_to_u64s(input) + let u64_arr = if input.len() % GOLDILOCKS_FIELD_U8_LEN == 0 { + bytes_to_u64s(input) + } else { + let padding_count = GOLDILOCKS_FIELD_U8_LEN - input.len() % GOLDILOCKS_FIELD_U8_LEN; + let mut bytes = vec![0 as u8; padding_count]; + bytes.extend_from_slice(input); + bytes_to_u64s(bytes.as_slice()) + }; + + let field_elements = u64_arr .par_iter() .map(|&i| F::from_canonical_u64(i)) .collect::>();