Skip to content

Commit

Permalink
ntru: add hashing support for streamlined ntru
Browse files Browse the repository at this point in the history
Signed-off-by: Ahmed <>
  • Loading branch information
Ahmed committed Jul 8, 2024
1 parent dc4f868 commit 7405b00
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ntru/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
hybrid-array = { path="../../hybrid-array", features = ["extra-sizes"] }
rand_core = "0.6.4"
sha2 = "0.10.8"

[dev-dependencies]
rayon="1.10.0"
41 changes: 41 additions & 0 deletions ntru/src/hashes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
mod streamlined;

use crate::encoded::AsymEnc;
use hybrid_array::Array;
use sha2::{Digest, Sha512};

/// # Panics
/// This functions should never panic
#[must_use]
pub fn hash_prefix(b: u8, data: &[u8]) -> [u8; 32] {
let mut hasher = Sha512::new();
hasher.update([b]);
hasher.update(data);
let result = hasher.finalize();
result[..32].try_into().unwrap()
}
/// # Panics
/// This functions should never panic
#[must_use]
pub fn hash_prefix_many(b: u8, data1: &[u8], data2: &[&[u8]]) -> [u8; 32] {
let mut hasher = Sha512::new();
hasher.update([b]);
hasher.update(data1);
for data in data2 {
hasher.update(data);
}
let result = hasher.finalize();
result[..32].try_into().unwrap()
}

pub trait HashOps {
///TODO I dont like this api send the first element of y first since it
/// is treated differently
/// Also I don't want hashing to depend on particular choise of hash function
/// maybe users prefer to later switch to sha3
fn hash_session(b: u8, y: &[&[u8]]) -> [u8; 32];
fn hash_confirm<Params: AsymEnc>(
r: &Array<u8, Params::InputsBytes>,
cache: &[u8; 32],
) -> [u8; 32];
}
22 changes: 22 additions & 0 deletions ntru/src/hashes/streamlined.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::{hash_prefix, hash_prefix_many, HashOps};
use crate::params::{Streamlined, StreamlinedNtru};
use hybrid_array::{Array, ArraySize};

impl<P> HashOps for Streamlined<P>
where
P: ArraySize,
Streamlined<P>: StreamlinedNtru + Sized,
{
fn hash_session(b: u8, y: &[&[u8]]) -> [u8; 32] {
let x = hash_prefix(3, y[0]);
hash_prefix_many(b, &x, &y[1..])
}

fn hash_confirm<Params: crate::encoded::AsymEnc>(
r: &Array<u8, Params::InputsBytes>,
cache: &[u8; 32],
) -> [u8; 32] {
let x = hash_prefix(3, r);
hash_prefix_many(2, &x, &[cache])
}
}
1 change: 1 addition & 0 deletions ntru/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod algebra;
pub mod const_time;
mod core;
pub mod encoded;
pub mod hashes;
pub mod params;
use hybrid_array::sizes::{U1013, U1277, U653, U761, U857, U953};
use params::{Lpr, Streamlined};
Expand Down

0 comments on commit 7405b00

Please sign in to comment.