Skip to content

Commit a9b5814

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 a9b5814

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

Cargo.lock

Lines changed: 15 additions & 1 deletion
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
9+
[dev-dependencies]

ntru/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![cfg_attr(not(test), no_std)]
2+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3+
#![doc(
4+
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
5+
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
6+
)]
7+
#![warn(clippy::pedantic)] // Be pedantic by default
8+
#![allow(
9+
clippy::cast_possible_truncation,
10+
clippy::cast_lossless,
11+
clippy::cast_possible_wrap,
12+
clippy::cast_sign_loss,
13+
// We shall stick to the naming as reference implementation
14+
// especially for variables
15+
clippy::unreadable_literal,
16+
clippy::many_single_char_names,
17+
clippy::similar_names,
18+
)]
19+
20+
pub mod params;

ntru/src/params.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//! The parameters for NTRU prime as described in section 3.4
2+
//! SX is parameters for Streamlined NTRU Prime with `p = x`.
3+
//! Lx is parameters for `NTRULPRime` with `p = x`;
4+
5+
use hybrid_array::{
6+
sizes::{U1277, U1278, U1305, U1521, U1713, U1905, U2025, U2552},
7+
typenum::{U1013, U1014, U653, U654, U761, U762, U857, U858, U953, U954},
8+
ArraySize,
9+
};
10+
11+
pub trait NtruCommon: 'static {
12+
type P: ArraySize;
13+
/// P + 1
14+
type P1: ArraySize;
15+
/// P + P - 1
16+
type PPM1: ArraySize;
17+
const Q: u16;
18+
const W: i16;
19+
}
20+
21+
pub trait NtruLRPrime {
22+
const DELTA: u16;
23+
const TAU0: i16;
24+
const TAU1: i32;
25+
const TAU2: i32;
26+
const TAU3: i32;
27+
}
28+
29+
macro_rules! impl_ntru_common {
30+
($ty:ident, $p: ident, $p1: ident, $ppm1: ident, $q: literal, $w: literal ) => {
31+
impl NtruCommon for $ty {
32+
type P = $p;
33+
type P1 = $p1;
34+
type PPM1 = $ppm1;
35+
const Q: u16 = $q;
36+
const W: i16 = $w;
37+
}
38+
};
39+
}
40+
41+
macro_rules! impl_ntru_lrprime {
42+
($ty:ident, $delta: literal, $tau0: literal, $tau1: literal, $tau2: literal, $tau3: literal ) => {
43+
impl NtruLRPrime for $ty {
44+
const DELTA: u16 = $delta;
45+
const TAU0: i16 = $tau0;
46+
const TAU1: i32 = $tau1;
47+
const TAU2: i32 = $tau2;
48+
const TAU3: i32 = $tau3;
49+
}
50+
};
51+
}
52+
53+
pub struct S653;
54+
pub struct S761;
55+
pub struct S857;
56+
pub struct S953;
57+
pub struct S1013;
58+
pub struct S1277;
59+
pub struct L653;
60+
pub struct L761;
61+
pub struct L857;
62+
pub struct L953;
63+
pub struct L1013;
64+
pub struct L1277;
65+
66+
impl_ntru_common!(S653, U653, U654, U1305, 4621, 288);
67+
impl_ntru_common!(S761, U761, U762, U1521, 4591, 286);
68+
impl_ntru_common!(S857, U857, U858, U1713, 5167, 322);
69+
impl_ntru_common!(S953, U953, U954, U1905, 6343, 396);
70+
impl_ntru_common!(S1013, U1013, U1014, U2025, 7177, 448);
71+
impl_ntru_common!(S1277, U1277, U1278, U2552, 7879, 429);
72+
impl_ntru_common!(L653, U653, U654, U1305, 4621, 252);
73+
impl_ntru_common!(L761, U761, U762, U1521, 4591, 250);
74+
impl_ntru_common!(L857, U857, U858, U1713, 5167, 281);
75+
impl_ntru_common!(L953, U953, U954, U1905, 6343, 345);
76+
impl_ntru_common!(L1013, U1013, U1014, U2025, 7177, 392);
77+
impl_ntru_common!(L1277, U1277, U1278, U2552, 7879, 429);
78+
impl_ntru_lrprime!(L653, 289, 2175, 113, 2031, 290);
79+
impl_ntru_lrprime!(L761, 292, 2156, 114, 2007, 287);
80+
impl_ntru_lrprime!(L857, 329, 2433, 101, 2265, 324);
81+
impl_ntru_lrprime!(L953, 404, 2997, 82, 2798, 400);
82+
impl_ntru_lrprime!(L1013, 450, 3367, 73, 3143, 449);
83+
impl_ntru_lrprime!(L1277, 502, 3724, 66, 3469, 469);

0 commit comments

Comments
 (0)