Skip to content

Commit

Permalink
keccak/sha3 finalize do not change sponge so remove absorb afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
grandchildrice committed Oct 10, 2023
1 parent bc7b58c commit 0685d37
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/transcript/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl<C: CurveGroup> Transcript<C> for KeccakTranscript<C> {
fn get_challenge(&mut self) -> C::ScalarField {
let mut output = [0u8; 32];
self.sponge.clone().finalize(&mut output);
self.sponge.update(&[output[0]]);
C::ScalarField::from_le_bytes_mod_order(&[output[0]])
}
fn get_challenge_nbits(&mut self, nbits: usize) -> Vec<bool> {
Expand All @@ -51,7 +50,6 @@ impl<C: CurveGroup> Transcript<C> for KeccakTranscript<C> {
fn get_challenges(&mut self, n: usize) -> Vec<C::ScalarField> {
let mut output = [0u8; 32];
self.sponge.clone().finalize(&mut output);
self.sponge.update(&[output[0]]);

let c: Vec<C::ScalarField> = output
.iter()
Expand All @@ -77,7 +75,7 @@ pub mod tests {
}

#[test]
fn test_transcript_get_challenge() {
fn test_transcript_get_challenges_len() {
let mut rng = ark_std::test_rng();

const n: usize = 10;
Expand All @@ -89,4 +87,15 @@ pub mod tests {
let challenges = transcript.get_challenges(v.len());
assert_eq!(challenges.len(), n);
}

#[test]
fn test_transcript_get_challenge() {
let config = keccak_test_config::<Fr>();
// init transcript
let mut transcript = KeccakTranscript::<Projective>::new(&config);
transcript.absorb(&Fr::from(42_u32));
let c = transcript.get_challenge();
let c_2 = transcript.get_challenge();
assert_eq!(c, c_2);
}
}
15 changes: 12 additions & 3 deletions src/transcript/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl<C: CurveGroup> Transcript<C> for SHA3Transcript<C> {
}
fn get_challenge(&mut self) -> C::ScalarField {
let output = self.sponge.clone().finalize_boxed(200);
self.sponge.update(&[output[0]]);
C::ScalarField::from_le_bytes_mod_order(&[output[0]])
}
fn get_challenge_nbits(&mut self, nbits: usize) -> Vec<bool> {
Expand All @@ -50,7 +49,6 @@ impl<C: CurveGroup> Transcript<C> for SHA3Transcript<C> {
}
fn get_challenges(&mut self, n: usize) -> Vec<C::ScalarField> {
let output = self.sponge.clone().finalize_boxed(n);
self.sponge.update(&[output[0]]);

let c = output
.iter()
Expand All @@ -76,7 +74,7 @@ pub mod tests {
}

#[test]
fn test_transcript_get_challenge() {
fn test_transcript_get_challenges_len() {
let mut rng = ark_std::test_rng();

const n: usize = 10;
Expand All @@ -88,4 +86,15 @@ pub mod tests {
let challenges = transcript.get_challenges(v.len());
assert_eq!(challenges.len(), n);
}

#[test]
fn test_transcript_get_challenge() {
let config = sha3_test_config::<Fr>();
// init transcript
let mut transcript = SHA3Transcript::<Projective>::new(&config);
transcript.absorb(&Fr::from(42_u32));
let c = transcript.get_challenge();
let c_2 = transcript.get_challenge();
assert_eq!(c, c_2);
}
}

0 comments on commit 0685d37

Please sign in to comment.