Skip to content
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

Implementation of CryptographicSponge for Merlin #136

Merged
merged 13 commits into from
Oct 17, 2024
5 changes: 3 additions & 2 deletions crypto-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ark-serialize = { version = "^0.4.0", default-features = false, features = [ "de
blake2 = { version = "0.10", default-features = false }
sha2 = { version = "0.10", default-features = false }
digest = { version = "0.10", default-features = false }
merlin = { version = "3.0.0", default-features = false, optional = true }

ark-r1cs-std = { version = "^0.4.0", optional = true, default-features = false }
ark-snark = { version = "^0.4.0", default-features = false }
Expand All @@ -42,8 +43,8 @@ print-trace = [ "ark-std/print-trace" ]
parallel = [ "std", "rayon", "ark-ec/parallel", "ark-std/parallel", "ark-ff/parallel" ]
r1cs = [ "ark-r1cs-std", "tracing" ]
crh = [ "sponge" ]
sponge = []
commitment = ["crh"]
sponge = [ "merlin" ]
commitment = [ "crh" ]
merkle_tree = ["crh", "hashbrown"]
encryption = []
prf = []
Expand Down
6 changes: 5 additions & 1 deletion crypto-primitives/src/merkle_tree/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::crh::TwoToOneCRHSchemeGadget;
use crate::merkle_tree::{Config, IdentityDigestConverter};
use crate::merkle_tree::Config;
use crate::{crh::CRHSchemeGadget, merkle_tree::Path};
use ark_ff::PrimeField;
use ark_r1cs_std::prelude::*;
Expand All @@ -9,11 +9,15 @@ use ark_std::fmt::Debug;
#[cfg(not(feature = "std"))]
use ark_std::vec::Vec;

#[cfg(test)]
use crate::merkle_tree::IdentityDigestConverter;

pub trait DigestVarConverter<From, To: ?Sized> {
type TargetType: Borrow<To>;
fn convert(from: From) -> Result<Self::TargetType, SynthesisError>;
}

#[cfg(test)]
impl<T> DigestVarConverter<T, T> for IdentityDigestConverter<T> {
type TargetType = T;

Expand Down
2 changes: 2 additions & 0 deletions crypto-primitives/src/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ pub trait DigestConverter<From, To: ?Sized> {
}

/// A trivial converter where digest of previous layer's hash is the same as next layer's input.
#[cfg(test)]
pub struct IdentityDigestConverter<T> {
_prev_layer_digest: T,
}

#[cfg(test)]
impl<T> DigestConverter<T, T> for IdentityDigestConverter<T> {
type TargetType = T;
fn convert(item: T) -> Result<T, Error> {
Expand Down
33 changes: 33 additions & 0 deletions crypto-primitives/src/sponge/merlin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::sponge::{Absorb, CryptographicSponge};
#[cfg(not(feature = "std"))]
use ark_std::vec::Vec;
pub use merlin::Transcript;

impl CryptographicSponge for Transcript {
type Config = &'static [u8];

fn new(params: &Self::Config) -> Self {
Transcript::new(*params)
}

fn absorb(&mut self, input: &impl Absorb) {
self.append_message(b"", &input.to_sponge_bytes_as_vec());
}

fn squeeze_bytes(&mut self, num_bytes: usize) -> Vec<u8> {
let mut dest = vec![0; num_bytes];
self.challenge_bytes(b"", &mut dest);
dest
}

fn squeeze_bits(&mut self, num_bits: usize) -> Vec<bool> {
let num_bytes = (num_bits + 7) / 8;
let mut tmp = vec![0; num_bytes];
self.challenge_bytes(b"", &mut tmp);
let dest = tmp
.iter()
.flat_map(|byte| (0..8u32).rev().map(move |i| (byte >> i) & 1 == 1))
.collect::<Vec<_>>();
dest[..num_bits].to_vec()
}
}
5 changes: 5 additions & 0 deletions crypto-primitives/src/sponge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub use absorb::*;
/// [cos]: https://eprint.iacr.org/2019/1076
pub mod poseidon;

/// The sponge for [Merlin][merlin]
///
/// [merlin]: https://merlin.cool/
pub mod merlin;

#[cfg(test)]
mod test;

Expand Down
Loading