Skip to content

Commit

Permalink
Merge branch 'main' of github.com:WizardOfMenlo/whir
Browse files Browse the repository at this point in the history
  • Loading branch information
WizardOfMenlo committed Sep 9, 2024
2 parents 79bb398 + e3abbf4 commit e39377d
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 196 deletions.
24 changes: 12 additions & 12 deletions src/bin/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Goldilocks1, AvailableMerkle::SHA3) => {
(AvailableFields::Goldilocks1, AvailableMerkle::Keccak256) => {
use fields::Field64 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -128,9 +128,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Goldilocks2, AvailableMerkle::SHA3) => {
(AvailableFields::Goldilocks2, AvailableMerkle::Keccak256) => {
use fields::Field64_2 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -144,9 +144,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Goldilocks3, AvailableMerkle::SHA3) => {
(AvailableFields::Goldilocks3, AvailableMerkle::Keccak256) => {
use fields::Field64_3 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -160,9 +160,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Field128, AvailableMerkle::SHA3) => {
(AvailableFields::Field128, AvailableMerkle::Keccak256) => {
use fields::Field128 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -176,9 +176,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Field192, AvailableMerkle::SHA3) => {
(AvailableFields::Field192, AvailableMerkle::Keccak256) => {
use fields::Field192 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -192,9 +192,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Field256, AvailableMerkle::SHA3) => {
(AvailableFields::Field256, AvailableMerkle::Keccak256) => {
use fields::Field256 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand Down
24 changes: 12 additions & 12 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Goldilocks1, AvailableMerkle::SHA3) => {
(AvailableFields::Goldilocks1, AvailableMerkle::Keccak256) => {
use fields::Field64 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -99,9 +99,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Goldilocks2, AvailableMerkle::SHA3) => {
(AvailableFields::Goldilocks2, AvailableMerkle::Keccak256) => {
use fields::Field64_2 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -115,9 +115,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Goldilocks3, AvailableMerkle::SHA3) => {
(AvailableFields::Goldilocks3, AvailableMerkle::Keccak256) => {
use fields::Field64_3 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -131,9 +131,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Field128, AvailableMerkle::SHA3) => {
(AvailableFields::Field128, AvailableMerkle::Keccak256) => {
use fields::Field128 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -147,9 +147,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Field192, AvailableMerkle::SHA3) => {
(AvailableFields::Field192, AvailableMerkle::Keccak256) => {
use fields::Field192 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand All @@ -163,9 +163,9 @@ fn main() {
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
}

(AvailableFields::Field256, AvailableMerkle::SHA3) => {
(AvailableFields::Field256, AvailableMerkle::Keccak256) => {
use fields::Field256 as F;
use merkle_tree::sha3 as mt;
use merkle_tree::keccak as mt;

let (leaf_hash_params, two_to_one_params) = mt::default_config::<F>(&mut rng);
run_whir::<F, mt::MerkleTreeParams<F>>(args, leaf_hash_params, two_to_one_params);
Expand Down
6 changes: 3 additions & 3 deletions src/cmdline_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ impl FromStr for AvailableFields {

#[derive(Debug, Clone, Copy, Serialize)]
pub enum AvailableMerkle {
SHA3,
Keccak256,
Blake3,
}

impl FromStr for AvailableMerkle {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "SHA3" {
Ok(Self::SHA3)
if s == "Keccak" {
Ok(Self::Keccak256)
} else if s == "Blake3" {
Ok(Self::Blake3)
} else {
Expand Down
119 changes: 0 additions & 119 deletions src/crypto/merkle_tree/blake2.rs

This file was deleted.

38 changes: 19 additions & 19 deletions src/crypto/merkle_tree/sha3.rs → src/crypto/merkle_tree/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use sha3::Digest;
#[derive(
Debug, Default, Clone, Copy, Eq, PartialEq, Hash, CanonicalSerialize, CanonicalDeserialize,
)]
pub struct SHA3Digest([u8; 32]);
pub struct KeccakDigest([u8; 32]);

impl Absorb for SHA3Digest {
impl Absorb for KeccakDigest {
fn to_sponge_bytes(&self, dest: &mut Vec<u8>) {
dest.extend_from_slice(&self.0);
}
Expand All @@ -27,24 +27,24 @@ impl Absorb for SHA3Digest {
}
}

impl From<[u8; 32]> for SHA3Digest {
impl From<[u8; 32]> for KeccakDigest {
fn from(value: [u8; 32]) -> Self {
SHA3Digest(value)
KeccakDigest(value)
}
}

impl AsRef<[u8]> for SHA3Digest {
impl AsRef<[u8]> for KeccakDigest {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

pub struct SHA3LeafHash<F>(PhantomData<F>);
pub struct SHA3TwoToOneCRHScheme;
pub struct KeccakLeafHash<F>(PhantomData<F>);
pub struct KeccakTwoToOneCRHScheme;

impl<F: CanonicalSerialize + Send> CRHScheme for SHA3LeafHash<F> {
impl<F: CanonicalSerialize + Send> CRHScheme for KeccakLeafHash<F> {
type Input = [F];
type Output = SHA3Digest;
type Output = KeccakDigest;
type Parameters = ();

fn setup<R: RngCore>(_: &mut R) -> Result<Self::Parameters, ark_crypto_primitives::Error> {
Expand All @@ -58,19 +58,19 @@ impl<F: CanonicalSerialize + Send> CRHScheme for SHA3LeafHash<F> {
let mut buf = vec![];
CanonicalSerialize::serialize_compressed(input.borrow(), &mut buf)?;

let mut h = sha3::Sha3_256::new();
let mut h = sha3::Keccak256::new();
h.update(&buf);

let mut output = [0; 32];
output.copy_from_slice(&h.finalize()[..]);
HashCounter::add();
Ok(SHA3Digest(output))
Ok(KeccakDigest(output))
}
}

impl TwoToOneCRHScheme for SHA3TwoToOneCRHScheme {
type Input = SHA3Digest;
type Output = SHA3Digest;
impl TwoToOneCRHScheme for KeccakTwoToOneCRHScheme {
type Input = KeccakDigest;
type Output = KeccakDigest;
type Parameters = ();

fn setup<R: RngCore>(_: &mut R) -> Result<Self::Parameters, ark_crypto_primitives::Error> {
Expand All @@ -82,13 +82,13 @@ impl TwoToOneCRHScheme for SHA3TwoToOneCRHScheme {
left_input: T,
right_input: T,
) -> Result<Self::Output, ark_crypto_primitives::Error> {
let mut h = sha3::Sha3_256::new();
let mut h = sha3::Keccak256::new();
h.update(&left_input.borrow().0);
h.update(&right_input.borrow().0);
let mut output = [0; 32];
output.copy_from_slice(&h.finalize()[..]);
HashCounter::add();
Ok(SHA3Digest(output))
Ok(KeccakDigest(output))
}

fn compress<T: Borrow<Self::Output>>(
Expand All @@ -100,8 +100,8 @@ impl TwoToOneCRHScheme for SHA3TwoToOneCRHScheme {
}
}

pub type LeafH<F> = SHA3LeafHash<F>;
pub type CompressH = SHA3TwoToOneCRHScheme;
pub type LeafH<F> = KeccakLeafHash<F>;
pub type CompressH = KeccakTwoToOneCRHScheme;

#[derive(Debug, Default, Clone)]
pub struct MerkleTreeParams<F>(PhantomData<F>);
Expand All @@ -110,7 +110,7 @@ impl<F: CanonicalSerialize + Send> Config for MerkleTreeParams<F> {
type Leaf = [F];

type LeafDigest = <LeafH<F> as CRHScheme>::Output;
type LeafInnerDigestConverter = IdentityDigestConverter<SHA3Digest>;
type LeafInnerDigestConverter = IdentityDigestConverter<KeccakDigest>;
type InnerDigest = <CompressH as TwoToOneCRHScheme>::Output;

type LeafHash = LeafH<F>;
Expand Down
3 changes: 1 addition & 2 deletions src/crypto/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod blake2;
pub mod blake3;
pub mod keccak;
pub mod mock;
pub mod sha3;

use std::{borrow::Borrow, marker::PhantomData, sync::atomic::AtomicUsize};

Expand Down
Loading

0 comments on commit e39377d

Please sign in to comment.