From c3d9388213f07cb6b76f5ab2ddb952e7e30e93e1 Mon Sep 17 00:00:00 2001 From: Hossein Moghaddas Date: Tue, 23 Jan 2024 03:44:53 +0100 Subject: [PATCH] Add Merlin, `squeeze_bits` does not work --- Cargo.toml | 1 + src/sponge/merlin/mod.rs | 31 +++++++++++++++++++++++++++++++ src/sponge/mod.rs | 5 +++++ 3 files changed, 37 insertions(+) create mode 100644 src/sponge/merlin/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 2ef07709..219bd443 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,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 } ark-r1cs-std = { version = "^0.4.0", optional = true, default-features = false } ark-snark = { version = "^0.4.0", default-features = false } diff --git a/src/sponge/merlin/mod.rs b/src/sponge/merlin/mod.rs new file mode 100644 index 00000000..cb873df7 --- /dev/null +++ b/src/sponge/merlin/mod.rs @@ -0,0 +1,31 @@ +use crate::sponge::{Absorb, CryptographicSponge}; +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 { + let mut dest = Vec::with_capacity(num_bytes); + self.challenge_bytes(b"", &mut dest); + dest + } + + fn squeeze_bits(&mut self, num_bits: usize) -> Vec { + let num_bytes = (num_bits + 7) / 8; + let mut tmp = Vec::with_capacity(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::>(); + dest[..num_bits].to_vec() + } +} diff --git a/src/sponge/mod.rs b/src/sponge/mod.rs index bd8a1d92..19baba85 100644 --- a/src/sponge/mod.rs +++ b/src/sponge/mod.rs @@ -17,6 +17,11 @@ pub use absorb::*; /// [cos]: https://eprint.iacr.org/2019/1076 pub mod poseidon; +/// The sponge for Merlin +/// +/// +pub mod merlin; + #[cfg(test)] mod test;