Skip to content

Commit 512af6b

Browse files
committed
use ronkathon sha3 primitive
1 parent 9e01b74 commit 512af6b

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

src/hashes/sha3.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ const RHO: [[u32; 5]; 5] =
4141
27, 20, 39, 8, 14,
4242
]];
4343

44+
/// Type alias for SHA3-224.
45+
pub type Sha3_224 = Sha3<28>;
46+
/// Type alias for SHA3-256.
47+
pub type Sha3_256 = Sha3<32>;
48+
/// Type alias for SHA3-384.
49+
pub type Sha3_384 = Sha3<48>;
50+
/// Type alias for SHA3-512.
51+
pub type Sha3_512 = Sha3<64>;
52+
/// Type alias for SHAKE128.
53+
pub type Shake128 = Shake<128>;
54+
/// Type alias for SHAKE256.
55+
pub type Shake256 = Shake<256>;
56+
4457
#[derive(Clone, Debug)]
4558
struct KeccakState {
4659
lanes: [[u64; 5]; 5],
@@ -277,19 +290,6 @@ impl<const SECURITY_BITS: usize> Shake<SECURITY_BITS> {
277290
}
278291
}
279292

280-
/// Type alias for SHA3-224.
281-
pub type Sha3_224 = Sha3<28>;
282-
/// Type alias for SHA3-256.
283-
pub type Sha3_256 = Sha3<32>;
284-
/// Type alias for SHA3-384.
285-
pub type Sha3_384 = Sha3<48>;
286-
/// Type alias for SHA3-512.
287-
pub type Sha3_512 = Sha3<64>;
288-
/// Type alias for SHAKE128.
289-
pub type Shake128 = Shake<128>;
290-
/// Type alias for SHAKE256.
291-
pub type Shake256 = Shake<256>;
292-
293293
#[cfg(test)]
294294
mod tests {
295295
use hex_literal::hex;

src/kem/kyber/auxiliary.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,48 @@
22
//! - [`prf`] - Pseudorandom function
33
//! - [`h`], [`g`] - Hash function
44
//! - [`Xof`] - Extendable output function
5-
use sha3::{
6-
digest::{ExtendableOutput, Update, XofReader},
7-
Digest, Shake128, Shake256,
8-
};
5+
6+
use crate::hashes::sha3::{Sha3_256, Sha3_512, Shake128, Shake256};
97

108
pub fn prf<const ETA: usize>(s: &[u8], b: u8) -> [u8; 64 * ETA] {
119
assert!(s.len() == 32);
1210

13-
let mut hasher = Shake256::default();
14-
hasher.update(&s);
11+
let mut hasher = Shake256::new();
12+
hasher.update(s);
1513
hasher.update(&[b]);
1614
let mut res = [0u8; 64 * ETA];
17-
XofReader::read(&mut hasher.finalize_xof(), &mut res);
15+
hasher.squeeze(&mut res);
1816
res
1917
}
2018

21-
pub fn h(s: &[u8]) -> [u8; 32] { sha3::Sha3_256::digest(s).into() }
19+
pub fn h(s: &[u8]) -> [u8; 32] {
20+
let mut hasher = Sha3_256::new();
21+
hasher.update(s);
22+
hasher.finalize()
23+
}
2224

2325
pub fn j(s: &[u8]) -> [u8; 32] {
24-
let mut hasher = Shake256::default();
26+
let mut hasher = Shake256::new();
2527
hasher.update(s);
26-
let mut reader = hasher.finalize_xof();
2728
let mut res = [0u8; 32];
28-
XofReader::read(&mut reader, &mut res);
29+
hasher.squeeze(&mut res);
2930
res
3031
}
3132

3233
pub fn g(c: &[u8]) -> ([u8; 32], [u8; 32]) {
33-
let res = sha3::Sha3_512::digest(c);
34-
(res[..32].try_into().unwrap(), res[32..].try_into().unwrap())
34+
let mut hasher = Sha3_512::new();
35+
hasher.update(c);
36+
let res = hasher.finalize();
37+
let (h0, h1) = res.split_at(32);
38+
(h0.try_into().unwrap(), h1.try_into().unwrap())
3539
}
3640

3741
pub struct Xof(Shake128);
3842

3943
impl Xof {
40-
pub fn init() -> Self { Self(Shake128::default()) }
44+
pub fn init() -> Self { Self(Shake128::new()) }
4145

42-
pub fn absorb(mut self, input: &[u8]) -> impl XofReader {
43-
self.0.update(input);
44-
self.0.finalize_xof()
45-
}
46+
pub fn absorb(&mut self, input: &[u8]) { self.0.update(input); }
4647

47-
pub fn squeeze(reader: &mut impl XofReader, output: &mut [u8]) {
48-
XofReader::read(reader, output);
49-
}
48+
pub fn squeeze(&mut self, output: &mut [u8]) { self.0.squeeze(output); }
5049
}

src/kem/kyber/encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn byte_decode_polyvec<B: Basis, const D: usize, const K: usize, const d: us
9393
let mut f = Vec::with_capacity(K);
9494

9595
for bytes in encoded_bytes.chunks(32 * d) {
96-
let coeffs = byte_decode::<d, D>(bytes.try_into().unwrap());
96+
let coeffs = byte_decode::<d, D>(bytes);
9797
f.push(Polynomial { coefficients: coeffs, basis: basis.clone() })
9898
}
9999

@@ -109,7 +109,7 @@ mod tests {
109109
fn generate_test_data() -> [MlKemField; 256] {
110110
let mut data = [MlKemField { value: 0 }; 256];
111111
for i in 0..256 {
112-
data[i].value = i as usize;
112+
data[i].value = i;
113113
}
114114
data
115115
}

src/kem/kyber/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl<const K: usize> MlKem<K> {
375375
[(); 64 * eta1 * 8]:,
376376
{
377377
let (ek_pke, dk_pke) = self.kpke.pke_keygen::<eta1>(d);
378-
let ek: [u8; 384 * K + 32] = ek_pke.0.clone();
378+
let ek: [u8; 384 * K + 32] = ek_pke.0;
379379
let h = h(&ek);
380380

381381
(MlKemEncapsKey(ek), MlKemDecapsKey { dk_pke, ek_pke, h, z })

src/kem/kyber/sampling.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ pub fn sample_ntt(rho: &[u8], j: u8, i: u8) -> [MlKemField; 256] {
1010

1111
let mut ntt = [MlKemField::ZERO; 256];
1212

13-
let mut xof = Xof::init().absorb(&input);
13+
let mut xof = Xof::init();
14+
xof.absorb(&input);
1415
let mut j = 0;
1516
while j < 256 {
1617
let mut buf = [0u8; 3];
17-
Xof::squeeze(&mut xof, &mut buf);
18+
xof.squeeze(&mut buf);
1819

1920
let d_1 = buf[0] as usize + ((buf[1] as usize & 0xf) << 8);
2021
let d_2 = (buf[1] >> 4) as usize + ((buf[2] as usize) << 4);

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#![feature(const_option)]
2323
#![feature(generic_const_exprs)]
2424
#![feature(specialization)]
25-
#![feature(test)]
2625
#![warn(missing_docs)]
2726

2827
pub mod algebra;

0 commit comments

Comments
 (0)