Skip to content

Commit 7405b00

Browse files
author
Ahmed
committed
ntru: add hashing support for streamlined ntru
Signed-off-by: Ahmed <>
1 parent dc4f868 commit 7405b00

5 files changed

Lines changed: 77 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ntru/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
hybrid-array = { path="../../hybrid-array", features = ["extra-sizes"] }
88
rand_core = "0.6.4"
9+
sha2 = "0.10.8"
910

1011
[dev-dependencies]
1112
rayon="1.10.0"

ntru/src/hashes/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
mod streamlined;
2+
3+
use crate::encoded::AsymEnc;
4+
use hybrid_array::Array;
5+
use sha2::{Digest, Sha512};
6+
7+
/// # Panics
8+
/// This functions should never panic
9+
#[must_use]
10+
pub fn hash_prefix(b: u8, data: &[u8]) -> [u8; 32] {
11+
let mut hasher = Sha512::new();
12+
hasher.update([b]);
13+
hasher.update(data);
14+
let result = hasher.finalize();
15+
result[..32].try_into().unwrap()
16+
}
17+
/// # Panics
18+
/// This functions should never panic
19+
#[must_use]
20+
pub fn hash_prefix_many(b: u8, data1: &[u8], data2: &[&[u8]]) -> [u8; 32] {
21+
let mut hasher = Sha512::new();
22+
hasher.update([b]);
23+
hasher.update(data1);
24+
for data in data2 {
25+
hasher.update(data);
26+
}
27+
let result = hasher.finalize();
28+
result[..32].try_into().unwrap()
29+
}
30+
31+
pub trait HashOps {
32+
///TODO I dont like this api send the first element of y first since it
33+
/// is treated differently
34+
/// Also I don't want hashing to depend on particular choise of hash function
35+
/// maybe users prefer to later switch to sha3
36+
fn hash_session(b: u8, y: &[&[u8]]) -> [u8; 32];
37+
fn hash_confirm<Params: AsymEnc>(
38+
r: &Array<u8, Params::InputsBytes>,
39+
cache: &[u8; 32],
40+
) -> [u8; 32];
41+
}

ntru/src/hashes/streamlined.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use super::{hash_prefix, hash_prefix_many, HashOps};
2+
use crate::params::{Streamlined, StreamlinedNtru};
3+
use hybrid_array::{Array, ArraySize};
4+
5+
impl<P> HashOps for Streamlined<P>
6+
where
7+
P: ArraySize,
8+
Streamlined<P>: StreamlinedNtru + Sized,
9+
{
10+
fn hash_session(b: u8, y: &[&[u8]]) -> [u8; 32] {
11+
let x = hash_prefix(3, y[0]);
12+
hash_prefix_many(b, &x, &y[1..])
13+
}
14+
15+
fn hash_confirm<Params: crate::encoded::AsymEnc>(
16+
r: &Array<u8, Params::InputsBytes>,
17+
cache: &[u8; 32],
18+
) -> [u8; 32] {
19+
let x = hash_prefix(3, r);
20+
hash_prefix_many(2, &x, &[cache])
21+
}
22+
}

ntru/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod algebra;
2222
pub mod const_time;
2323
mod core;
2424
pub mod encoded;
25+
pub mod hashes;
2526
pub mod params;
2627
use hybrid_array::sizes::{U1013, U1277, U653, U761, U857, U953};
2728
use params::{Lpr, Streamlined};

0 commit comments

Comments
 (0)