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
7 changes: 4 additions & 3 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 @@ -41,9 +42,9 @@ 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"]
merkle_tree = ["crh"]
sponge = [ "merlin" ]
commitment = [ "crh" ]
merkle_tree = [ "crh" ]
encryption = []
prf = []
snark = []
Expand Down
32 changes: 32 additions & 0 deletions crypto-primitives/src/sponge/merlin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::sponge::{Absorb, CryptographicSponge};
use crate::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
///
///
mmagician marked this conversation as resolved.
Show resolved Hide resolved
pub mod merlin;

#[cfg(test)]
mod test;

Expand Down
Loading