Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions merkle-hash-bench/src/hashes/plonky3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(feature = "plonky3")]
use {
crate::{register_hash, Field, HashFn, SmolHasher},
crate::{mod_ring::fields::Bn254Field, register_hash, Field, HashFn, SmolHasher},
bytemuck::cast_slice_mut,
p3_bn254_fr::{Bn254Fr, FFBn254Fr, Poseidon2Bn254},
p3_field::{
Expand All @@ -12,6 +12,7 @@ use {
p3_rescue::Rescue,
p3_symmetric::Permutation,
rand::rng,
std::mem::transmute,
};

type RescueGoldilocks = Rescue<Goldilocks, MdsMatrixGoldilocks, 8, 7>;
Expand Down Expand Up @@ -105,17 +106,20 @@ impl SmolHasher for Poseidon2Bn254<3> {
Bn254Fr::ZERO,
];
let state = self.permute(state);
hash.copy_from_slice(state[0].value.to_bytes().as_slice());
hash.copy_from_slice(bytes_from_fr(state[0]).as_slice());
}
}
}

fn fr_from_bytes(bytes: &[u8]) -> Bn254Fr {
let mut bytes: [u8; 32] = bytes.try_into().unwrap();
bytes[31] = 0; // Force smaller than modulus.
Bn254Fr {
value: FFBn254Fr::from_bytes(&bytes).unwrap(),
}
let element = FFBn254Fr::from_bytes(&bytes).unwrap();
unsafe { transmute(element) }
}

fn bytes_from_fr(element: Bn254Fr) -> [u8; 32] {
unsafe { transmute(element) }
}

impl SmolHasher for MonolithMersenne31<MdsMatrixMersenne31, 16, 5> {
Expand Down
6 changes: 3 additions & 3 deletions merkle-hash-bench/src/hashes/poseidon2_t2_ruint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl Poseidon2T2Ruint {
pub fn new() -> Self {
let mut rng = rand::rng();
Self {
first: rng.gen(),
middle: array::from_fn(|_| rng.gen()),
last: rng.gen(),
first: rng.random(),
middle: array::from_fn(|_| rng.random()),
last: rng.random(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions merkle-hash-bench/src/hashes/poseidon2_t3_ruint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ impl Poseidon2T3Ruint {
pub fn new() -> Self {
let mut rng = rand::rng();
Self {
first: rng.gen(),
middle: array::from_fn(|_| rng.gen()),
last: rng.gen(),
first: rng.random(),
middle: array::from_fn(|_| rng.random()),
last: rng.random(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion merkle-hash-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct Args {

fn main() -> Result<()> {
let args: Args = argh::from_env();
let duration = Duration::from_secs_f64(args.duration.unwrap_or(0.01));
let duration = Duration::from_secs_f64(args.duration.unwrap_or(0.1));

// Consrtuct all hashers.
let hashes = HASHES.iter().map(|ctor| ctor()).collect::<Vec<_>>();
Expand Down
6 changes: 3 additions & 3 deletions merkle-hash-bench/src/mod_ring/ruint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl<const BITS: usize, const LIMBS: usize> UintMont for Uint<BITS, LIMBS> {

fn random<R: rand::Rng + ?Sized>(rng: &mut R, max: Self) -> Self {
let mut result = Self::ZERO;
unsafe { // TODO: Update rand crate,
for limb in result.as_limbs_mut() {
*limb = rng.gen();
unsafe {
for limb in result.as_limbs_mut() {
*limb = rng.random();
}
}
result %= max;
Expand Down
Loading