Skip to content

Commit 28350eb

Browse files
committed
Don't be silly and read 0's on start.
1 parent 92ca761 commit 28350eb

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

src/hash/sponge.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<U: Unit, C: Sponge<U = U>> DuplexHash<U> for DuplexSponge<C> {
5353
Self {
5454
state: C::new(iv),
5555
absorb_pos: 0,
56-
squeeze_pos: 0,
56+
squeeze_pos: C::RATE,
5757
}
5858
}
5959

src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! To build a secure Fiat-Shamir transform, the minimal requirement is a permutation function over some field,
1616
//! be it $\mathbb{F}_{2^8}$ or any large-characteristic prime field $\mathbb{F}_p$.
1717
//! - **Retro-compatibility** with MD hashes.
18-
//! We have a legacy interface for [`sha2``], [`blake2`], and any hash function that satisfies the [`digest::Digest`] trait.
18+
//! We have a legacy interface for [`sha2`], [`blake2`], and any hash function that satisfies the [`digest::Digest`] trait.
1919
//! - **Preprocessing**.
2020
//! In recursive SNARKs, minimizing the number of hash invocations
2121
//! while maintaining security is crucial. We offer tools for preprocessing the Transcript (i.e., the state of the Fiat-Shamir transform) to achieve this goal.
@@ -26,11 +26,11 @@
2626
//!
2727
//! The library does three things:
2828
//!
29-
//! - Assist in the construction of a protocol transcript for a public-coin zero-knowledge proof ([Arthur]),
30-
//! - Assist in the deserialization and verification of a public-coin protocol ([Merlin]).
29+
//! - Assist in the construction of a protocol transcript for a public-coin zero-knowledge proof ([`Arthur`]),
30+
//! - Assist in the deserialization and verification of a public-coin protocol ([`Merlin`]).
3131
//!
3232
//! The basic idea behind Nimue is that prover and verifier "commit" to the protocol before running the actual protocol.
33-
//! They a string encoding the sequence of messages sent from the prover and the verifier (the [IOPattern]), which is used as an "IV" to initialize the hash function for the Fiat-Shamir heuristic.
33+
//! They a string encoding the sequence of messages sent from the prover and the verifier (the [`IOPattern`]), which is used as an "IV" to initialize the hash function for the Fiat-Shamir heuristic.
3434
//!
3535
//! There are prover just proceeds with concatenation, without ever worrying
3636
//! about encoding length and special flags to embed in the hash function.

src/plugins/ark/anemoi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl Sponge
3939

4040
fn new(tag: [u8; 32]) -> Self {
4141
let mut state = Self::default();
42-
state[0] = anemoi::bls12_381::Felt::from_le_bytes_mod_order(&tag);
42+
state[RATE] = anemoi::bls12_381::Felt::from_le_bytes_mod_order(&tag);
4343
state
4444
}
4545

src/plugins/ark/poseidon.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl<F: PrimeField, const R: usize, const N: usize> PoseidonSponge<F, R, N> {
4949
let mut new_state = Vec::new();
5050
for i in 0..state.len() {
5151
let mut cur = F::zero();
52-
for (j, state_elem) in state.iter().enumerate() {
53-
let term = state_elem.mul(&self.mds[i][j]);
52+
for (j, &state_elem) in state.iter().enumerate() {
53+
let term = state_elem * self.mds[i][j];
5454
cur.add_assign(&term);
5555
}
5656
new_state.push(cur);
@@ -76,9 +76,9 @@ where
7676

7777
fn new(iv: [u8; 32]) -> Self {
7878
assert!(N >= 1);
79-
let mut ark_sponge = Self::default();
80-
ark_sponge.state[R] = F::from_be_bytes_mod_order(&iv);
81-
ark_sponge
79+
let mut sponge = Self::default();
80+
sponge.state[R] = F::from_be_bytes_mod_order(&iv);
81+
sponge
8282
}
8383

8484
fn permute(&mut self) {

src/plugins/ark/tests.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
use crate::{hash::sponge::DuplexSponge, IOPattern, UnitTranscript};
1+
use crate::{hash::sponge::DuplexSponge, DefaultHash, DuplexHash, IOPattern, Unit, UnitTranscript};
22
use ark_bls12_381::Fr;
33
use ark_ff::{MontFp, Zero};
44

55
use super::poseidon::PoseidonSponge;
66

7-
type H = DuplexSponge<PoseidonSponge<Fr, 2, 3>>;
8-
type F = Fr;
7+
/// Test that the algebraic hashes do use the IV generated from the IO Pattern.
8+
fn check_iv_is_used<H: DuplexHash<F>, F: Unit + Copy + Default + Eq + core::fmt::Debug>() {
9+
let io1 = IOPattern::<H, F>::new("test").squeeze(1, "out");
10+
let io2 = IOPattern::<H, F>::new("another_test").squeeze(1, "out");
911

12+
let [mut arthur1, mut arthur2] = [io1.to_arthur(), io2.to_arthur()];
13+
let mut c = [F::default(); 2];
14+
arthur1.fill_challenge_units(&mut c[0..1]).unwrap();
15+
arthur2.fill_challenge_units(&mut c[1..2]).unwrap();
16+
assert_ne!(c[0], c[1]);
17+
}
18+
19+
#[test]
20+
fn test_iv_is_used() {
21+
check_iv_is_used::<DefaultHash, u8>();
22+
check_iv_is_used::<DuplexSponge<PoseidonSponge<Fr, 2, 3>>, Fr>();
23+
}
24+
25+
/// Check that poseidon can indeed be instantiated and doesn't do terribly stupid things like give 0 challenges.
1026
#[test]
1127
fn test_poseidon_basic() {
28+
type F = Fr;
29+
type H = DuplexSponge<PoseidonSponge<F, 2, 3>>;
30+
1231
let io = IOPattern::<H, F>::new("test")
1332
.absorb(1, "in")
1433
.squeeze(10, "out");

src/traits.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::errors::IOPatternError;
22
use crate::Unit;
33

44
pub trait UnitTranscript<U: Unit> {
5+
56
fn public_units(&mut self, input: &[U]) -> Result<(), IOPatternError>;
7+
68
fn fill_challenge_units(&mut self, output: &mut [U]) -> Result<(), IOPatternError>;
79
}
810

0 commit comments

Comments
 (0)