Skip to content

Commit b8cc8de

Browse files
author
Ahmed
committed
ntru: Initial Commit: Add NTRU parameters
Those are parameters as specified by NIST submission Signed-off-by: Ahmed <>
1 parent 4fed1c1 commit b8cc8de

File tree

23 files changed

+1725
-6
lines changed

23 files changed

+1725
-6
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[workspace]
22
resolver = "2"
33
members = [
4-
"ml-kem",
4+
"ml-kem", "ntru",
55
]
66

77
[profile.bench]

ntru/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "ntru"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
hybrid-array = { path="../../hybrid-array", features = ["extra-sizes"] }
8+
rand_core = "0.6.4"
9+
sha2 = "0.10.8"
10+
11+
[dev-dependencies]
12+
aes="0.8.4"
13+
hex="0.4.3"
14+
itertools = "0.13.0"
15+
rayon="1.10.0"

ntru/src/algebra/f3.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! arithmetic mod 3
2+
3+
use super::fq::Fq;
4+
use crate::const_time::i32_mod_u14;
5+
use core::ops::Deref;
6+
7+
/// always represented as -1,0,1
8+
#[derive(Eq, PartialEq, Debug, Copy, Clone, Default)]
9+
pub struct Small(i8);
10+
11+
impl Small {
12+
pub const ZERO: Small = Small(0);
13+
pub const ONE: Small = Small(1);
14+
pub const MONE: Small = Small(-1);
15+
16+
pub(super) fn new_i32(n: i32) -> Self {
17+
debug_assert!(n < 2);
18+
debug_assert!(n > -2);
19+
Small(n as i8)
20+
}
21+
pub fn new_i8(n: i8) -> Self {
22+
debug_assert!(n < 2);
23+
debug_assert!(n > -2);
24+
Small(n)
25+
}
26+
27+
#[must_use]
28+
pub const fn freeze(x: i16) -> Self {
29+
Small((i32_mod_u14((x as i32) + 1, 3).wrapping_sub(1)) as i8)
30+
}
31+
}
32+
33+
/// the benefit is from outside, anyone can access the inner value as number,
34+
/// but no one can modify it without refreezing
35+
impl Deref for Small {
36+
type Target = i8;
37+
38+
fn deref(&self) -> &Self::Target {
39+
&self.0
40+
}
41+
}
42+
43+
impl<Q> From<Fq<Q>> for Small {
44+
fn from(value: Fq<Q>) -> Self {
45+
Small::freeze(*value)
46+
}
47+
}
48+
49+
#[cfg(test)]
50+
mod test {
51+
use super::Small;
52+
fn naive_freeze(x: i16) -> i8 {
53+
// returns values in the set [-2, 2]
54+
let res = (x % 3) as i8;
55+
if res > 1 {
56+
return res - 3;
57+
}
58+
if res < -1 {
59+
return res + 3;
60+
}
61+
res
62+
}
63+
#[test]
64+
fn test_freeze() {
65+
for i in i16::MIN..i16::MAX {
66+
assert_eq!(*Small::freeze(i), naive_freeze(i));
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)