Skip to content

Commit d5570cc

Browse files
author
GOURIOU Lénaïck
committed
refactor(fiat-shamir): make hash_state clonable instead of reset, update relevant traits bound to derive clone
1 parent f08ae64 commit d5570cc

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

src/codec/keccak_codec.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use num_traits::identities::One;
3232
const R: usize = 136;
3333
const N: usize = 136 + 64;
3434

35+
#[derive(Clone)]
3536
pub struct KeccakPermutationState {
3637
pub state: [u8; 200],
3738
pub rate: usize,
@@ -100,6 +101,7 @@ impl KeccakPermutationState {
100101
}
101102
}
102103

104+
#[derive(Clone)]
103105
pub struct KeccakDuplexSponge {
104106
pub state: KeccakPermutationState,
105107
pub rate: usize,
@@ -173,6 +175,7 @@ fn cardinal<F: PrimeField>() -> BigUint {
173175
BigUint::from_bytes_le(bytes.as_ref()) + BigUint::one()
174176
}
175177

178+
#[derive(Clone)]
176179
pub struct ByteSchnorrCodec<G, H>
177180
where
178181
G: Group + GroupEncoding + GroupSerialisation,

src/codec/shake_codec.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::codec::r#trait::Codec;
2727
///
2828
/// The codec is initialized with a domain separator and absorbs serialized
2929
/// group elements. It outputs challenges compatible with the group’s scalar field.
30+
#[derive(Clone)]
3031
pub struct ShakeCodec<G: Group> {
3132
/// Internal SHAKE128 hasher state.
3233
hasher: Shake128,

src/fiat_shamir.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<P, C, G> NISigmaProtocol<P, C, G>
5252
where
5353
G: Group + GroupEncoding,
5454
P: SigmaProtocol<Commitment = Vec<G>, Challenge = <G as Group>::Scalar>,
55-
C: Codec<Challenge = <G as Group>::Scalar>,
55+
C: Codec<Challenge = <G as Group>::Scalar> + Clone,
5656
{
5757
/// Creates a new non-interactive Sigma protocol, identified by a domain separator (usually fixed per protocol instantiation), and an initialized Sigma protocol instance.
5858
pub fn new(iv: &[u8], instance: P) -> Self {
@@ -67,7 +67,7 @@ where
6767

6868
/// Produces a non-interactive proof for a witness and serializes it as a vector of bytes.
6969
pub fn prove(&mut self, witness: &P::Witness, rng: &mut (impl RngCore + CryptoRng)) -> Vec<u8> {
70-
self.hash_state = C::new(&self.domain_sep);
70+
let mut codec = self.hash_state.clone();
7171

7272
let (commitment, prover_state) = self.sigmap.prover_commit(witness, rng);
7373
// Commitment data for challenge generation
@@ -76,8 +76,7 @@ where
7676
data.extend_from_slice(commit.to_bytes().as_ref());
7777
}
7878
// Fiat Shamir challenge
79-
let challenge = self
80-
.hash_state
79+
let challenge = codec
8180
.prover_message(&data)
8281
.verifier_challenge();
8382
// Prover's response
@@ -93,7 +92,7 @@ where
9392

9493
/// Verify a non-interactive serialized proof and returns a Result: `Ok(())` if the proof verifies successfully, `Err(())` otherwise.
9594
pub fn verify(&mut self, proof: &[u8]) -> Result<(), ProofError> {
96-
self.hash_state = C::new(&self.domain_sep);
95+
let mut codec = self.hash_state.clone();
9796

9897
let (commitment, response) = self.sigmap.deserialize_batchable(proof).unwrap();
9998
// Commitment data for challenge generation
@@ -102,8 +101,7 @@ where
102101
data.extend_from_slice(commit.to_bytes().as_ref());
103102
}
104103
// Recompute the challenge
105-
let challenge = self
106-
.hash_state
104+
let challenge = codec
107105
.prover_message(&data)
108106
.verifier_challenge();
109107
// Verification of the proof

src/proof_builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ where
4646
impl<G> ProofBuilder<G>
4747
where
4848
G: Group + GroupSerialisation,
49+
ShakeCodec<G>: Clone,
4950
{
5051
/// Creates a new proof builder with a Schnorr protocol instance using the given domain separator.
5152
pub fn new(domain_sep: &[u8]) -> Self {

0 commit comments

Comments
 (0)