Skip to content

Commit 182e30b

Browse files
authored
fix(sponge): update implementation to comply with new specification (#39)
1 parent 961c8a6 commit 182e30b

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

src/duplex_sponge/keccak.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ pub struct KeccakPermutationState {
2020

2121
impl Default for KeccakPermutationState {
2222
fn default() -> Self {
23-
Self::new()
23+
Self::new([0u8; 32])
2424
}
2525
}
2626

2727
impl KeccakPermutationState {
28-
pub fn new() -> Self {
28+
pub fn new(iv: [u8; 32]) -> Self {
29+
let rate = 136;
30+
let mut state = [0u8; N];
31+
state[rate..rate + 32].copy_from_slice(&iv);
32+
2933
KeccakPermutationState {
30-
state: [0u8; 200],
31-
rate: 136,
34+
state,
35+
rate,
3236
capacity: 64,
3337
}
3438
}
@@ -70,15 +74,16 @@ pub struct KeccakDuplexSponge {
7074
impl KeccakDuplexSponge {
7175
pub fn new(iv: &[u8]) -> Self {
7276
assert_eq!(iv.len(), 32);
73-
let state = KeccakPermutationState::new();
77+
78+
let state = KeccakPermutationState::new(iv.try_into().unwrap());
7479
let rate = R;
7580
let capacity = N - R;
7681
KeccakDuplexSponge {
7782
state,
7883
rate,
7984
capacity,
8085
absorb_index: 0,
81-
squeeze_index: 0,
86+
squeeze_index: rate,
8287
}
8388
}
8489
}
@@ -92,16 +97,14 @@ impl DuplexSpongeInterface for KeccakDuplexSponge {
9297
self.squeeze_index = self.rate;
9398

9499
while !input.is_empty() {
95-
if self.absorb_index == self.rate {
100+
if self.absorb_index < self.rate {
101+
self.state.state[self.absorb_index] = input[0];
102+
self.absorb_index += 1;
103+
input = &input[1..];
104+
} else {
96105
self.state.permute();
97106
self.absorb_index = 0;
98107
}
99-
100-
let chunk_size = usize::min(self.rate - self.absorb_index, input.len());
101-
let dest = &mut self.state.state[self.absorb_index..self.absorb_index + chunk_size];
102-
dest.copy_from_slice(&input[..chunk_size]);
103-
self.absorb_index += chunk_size;
104-
input = &input[chunk_size..];
105108
}
106109
}
107110

@@ -116,11 +119,11 @@ impl DuplexSpongeInterface for KeccakDuplexSponge {
116119
}
117120

118121
let chunk_size = usize::min(self.rate - self.squeeze_index, length);
119-
self.squeeze_index += chunk_size;
120-
length -= chunk_size;
121122
output.extend_from_slice(
122123
&self.state.state[self.squeeze_index..self.squeeze_index + chunk_size],
123124
);
125+
self.squeeze_index += chunk_size;
126+
length -= chunk_size;
124127
}
125128

126129
output

src/tests/composition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn composition_proof_correct() {
2727
let domain_sep = b"hello world";
2828

2929
// definitions of the underlying protocols
30-
let (morph1, witness1) = dleq(<G as Group>::Scalar::random(&mut rng), G::random(&mut rng));
30+
let (morph1, witness1) = dleq(G::random(&mut rng), <G as Group>::Scalar::random(&mut rng));
3131
let (morph2, _) = pedersen_commitment(
3232
G::random(&mut rng),
3333
<G as Group>::Scalar::random(&mut rng),

src/tests/relations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn test_discrete_logarithm() {
2020
#[test]
2121
fn test_dleq() {
2222
let mut rng = OsRng;
23-
dleq(Scalar::random(&mut rng), G::random(&mut rng));
23+
dleq(G::random(&mut rng), Scalar::random(&mut rng));
2424
}
2525

2626
#[test]
@@ -100,7 +100,7 @@ fn noninteractive_discrete_logarithm() {
100100
#[test]
101101
fn noninteractive_dleq() {
102102
let mut rng = OsRng;
103-
let (morphismp, witness) = dleq(Scalar::random(&mut rng), G::random(&mut rng));
103+
let (morphismp, witness) = dleq(G::random(&mut rng), Scalar::random(&mut rng));
104104

105105
// The SigmaProtocol induced by morphismp
106106
let protocol = SchnorrProof::from(morphismp);

src/tests/spec/allVectors.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
{
22
"discrete_logarithm": {
33
"Context": "79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65",
4-
"Proof": "80c96c2822d816de609d4b72dd0b2a9409a3402338c977467225e7f506a60f3153a7f447450d7336c0ef15e4151349d91495306d216d5fe2ff3e660bcaf227c4794cb0e0887f5bcff6d4a6189cf9a494",
4+
"Proof": "80c96c2822d816de609d4b72dd0b2a9409a3402338c977467225e7f506a60f3153a7f447450d7336c0ef15e4151349d95aab19ad1899c809b16226a60fa8abc9532bbf779d112bc4c0c4a6d2f5a95b23",
55
"Statement": "TODO"
66
},
77
"dleq": {
88
"Context": "79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65",
9-
"Proof": "a01abd54895b7df2d476b2371e1796278a114f7dd1514e05cc1c0c07d40957268684c8887aa3f8cee33856ca325412f5a4fffa7226a983c8fcd9bb59dbb7a72e5c4eacd80958c3685d7abaa477ba6d738b35998ea1d0089166d17ea0a206d2991bf0b87f1f5c977f93fdccf9ec820d989656662f146460d48e56bfc2f6482285",
9+
"Proof": "a01abd54895b7df2d476b2371e1796278a114f7dd1514e05cc1c0c07d40957268684c8887aa3f8cee33856ca325412f5859a8bb9d31747dafffcfe70acd32bcb30b45db8333cd157f561039e654e2f8314ee648604afdb4c2a4c30ae9649119259a2f6ced2950f9865565a385e99055cd11368efbb2f550f1f75a4ae80f207cd",
1010
"Statement": "TODO"
1111
},
1212
"pedersen_commitment": {
1313
"Context": "79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65",
14-
"Proof": "91c620e60e68502ab1e0f0fa6b9f7e3225f678596da80c0e950e4149078562518ad37ed6177c71ebd6e2ca5fc32457d8228aa82bf0293a2d70def71e0e1f434af472458907c4827b694987a903126dd050b3ed6234dcd4d176f05582d3dab5515f790c5cdc927972d631a2ddceb53edb",
14+
"Proof": "91c620e60e68502ab1e0f0fa6b9f7e3225f678596da80c0e950e4149078562518ad37ed6177c71ebd6e2ca5fc32457d80b4264ea8a92bec6bbd5624af41159e00f6c65a81a25e4f148b174c3fd4f7caa163bd697e16ad7885f148b018b18dc32f68a23028d68c18140c0b71e06c6d43b",
1515
"Statement": "TODO"
1616
},
1717
"pedersen_commitment_dleq": {
1818
"Context": "79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65",
19-
"Proof": "8e670749a002c02e0b343a47c0194743d9164d5026ddec0a9572a742748305f83b2fc679858f2f97debd72a08ec59dc38e5d6c8cc6cb284f4012d4eb41a807d1463ad0d8976f78baff1da1fdf2ad39027e8c66e0625b15740a72fc9e866f1d1014a32947fd44c55553eb2c13d21d639640b5d070987d8befea62367b235278d80a313d50f72e5c70de5fc1db95e042b3723344136144cc71c5515c5aa03d95d1",
19+
"Proof": "8e670749a002c02e0b343a47c0194743d9164d5026ddec0a9572a742748305f83b2fc679858f2f97debd72a08ec59dc38e5d6c8cc6cb284f4012d4eb41a807d1463ad0d8976f78baff1da1fdf2ad39027e8c66e0625b15740a72fc9e866f1d106eb5822a300f6365cbc7809e81cf2c3576c10a286a14bf1107881684beba788c3cd7575ff3afaa135fb1b549621acdf7c9fe8602401dfc25d4fde441f2b17c2e",
2020
"Statement": "TODO"
2121
},
2222
"bbs_blind_commitment_computation": {
2323
"Context": "79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65",
24-
"Proof": "803d5d4fdb311967832758ae7402d03304b570f97c0756e5385a50622d0ac7b5de87fe14d15041b1564ba4893a1187304ed12592b9ca9c5ca92a87c3960f0bcae541ddf880271c361cca15c67e13bc504cf96235363e99bb3e126b111c220c77427873389d2397cf0798d251ec82ced1649b5d0e9b2f95410a68b5b66158e50832488e540853a8c79a17d8b8290266ec150af102dd9ca4a6f076399da893b1f2caa78d192590708c02ab561eb3a01aa1",
25-
24+
"Proof": "803d5d4fdb311967832758ae7402d03304b570f97c0756e5385a50622d0ac7b5de87fe14d15041b1564ba4893a11873043bb167cd1e1a68828e788be7ff518768d9c225d59785d6a9db3cabe9aaedf214fce23f4ec1665481b35bfd4b0d586058170e2ca22185242535ec49b173da9f12d00fa7e4a22440d1bf7cd04da48b0aca6b035a3be434cc2184036191ae61d535404fda6bed2505adb6eba56a8b8f456fe948de0a5531376fc7e726198e8be8d",
2625
"Statement": "TODO"
2726
}
2827
}

src/tests/spec/test_vectors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ macro_rules! generate_ni_function {
4646
}
4747

4848
generate_ni_function!(NI_discrete_logarithm, discrete_logarithm, srandom);
49-
generate_ni_function!(NI_dleq, dleq, srandom, prandom);
49+
generate_ni_function!(NI_dleq, dleq, prandom, srandom);
5050
generate_ni_function!(
5151
NI_pedersen_commitment,
5252
pedersen_commitment,

src/tests/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn discrete_logarithm<G: Group + GroupEncoding>(
2727

2828
/// LinearMap for knowledge of a discrete logarithm equality between two pairs.
2929
#[allow(non_snake_case)]
30-
pub fn dleq<G: Group + GroupEncoding>(x: G::Scalar, H: G) -> (LinearRelation<G>, Vec<G::Scalar>) {
30+
pub fn dleq<G: Group + GroupEncoding>(H: G, x: G::Scalar) -> (LinearRelation<G>, Vec<G::Scalar>) {
3131
let mut morphismp: LinearRelation<G> = LinearRelation::new();
3232

3333
let var_x = morphismp.allocate_scalar();

0 commit comments

Comments
 (0)