-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathkeccak.rs
More file actions
92 lines (81 loc) · 2.6 KB
/
keccak.rs
File metadata and controls
92 lines (81 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::marker::PhantomData;
use tiny_keccak::{Keccak, Hasher};
use ark_ec::CurveGroup;
use ark_ff::{BigInteger, PrimeField};
use crate::transcript::Transcript;
/// KecccakTranscript implements the Transcript trait using the Keccak hash
pub struct KeccakTranscript<C: CurveGroup> {
sponge: Keccak,
phantom: PhantomData<C>,
}
#[derive(Debug)]
pub struct KeccakConfig {}
impl<C: CurveGroup> Transcript<C> for KeccakTranscript<C> {
type TranscriptConfig = KeccakConfig;
fn new(config: &Self::TranscriptConfig) -> Self {
let _ = config;
let sponge = Keccak::v256();
Self {
sponge,
phantom: PhantomData,
}
}
fn absorb(&mut self, v: &C::ScalarField) {
self.sponge.update(&(v.into_bigint().to_bytes_le()));
}
fn absorb_vec(&mut self, v: &[C::ScalarField]) {
for _v in v {
self.sponge.update(&(_v.into_bigint().to_bytes_le()));
}
}
fn absorb_point(&mut self, p: &C) {
let mut serialized = vec![];
p.serialize_compressed(&mut serialized).unwrap();
self.sponge.update(&(serialized))
}
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> {
// TODO
vec![]
}
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()
.map(|c| C::ScalarField::from_le_bytes_mod_order(&[*c]))
.collect();
c[..n].to_vec()
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use ark_pallas::{
// constraints::GVar,
Fr, Projective
};
use ark_std::UniformRand;
/// WARNING the method poseidon_test_config is for tests only
#[cfg(test)]
pub fn keccak_test_config<F: PrimeField>() -> KeccakConfig {
KeccakConfig {}
}
#[test]
fn test_transcript_get_challenge() {
let mut rng = ark_std::test_rng();
const n: usize = 10;
let config = keccak_test_config::<Fr>();
// init transcript
let mut transcript = KeccakTranscript::<Projective>::new(&config);
let v: Vec<Fr> = vec![Fr::rand(&mut rng); n];
let challenges = transcript.get_challenges(v.len());
assert_eq!(challenges.len(), n);
}
}