Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: NTRU Prime #35

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 75 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
resolver = "2"
members = [
"ml-kem",
"ml-kem", "ntru",
]

[profile.bench]
Expand Down
15 changes: 15 additions & 0 deletions ntru/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "ntru"
version = "0.1.0"
edition = "2021"

[dependencies]
hybrid-array = { path="../../hybrid-array", features = ["extra-sizes"] }
rand_core = "0.6.4"
sha2 = "0.10.8"

[dev-dependencies]
aes="0.8.4"
hex = "0.4.3"
itertools = "0.13.0"
rayon="1.10.0"
70 changes: 70 additions & 0 deletions ntru/src/algebra/f3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! arithmetic mod 3

use super::fq::Fq;
use crate::const_time::i32_mod_u14;
use core::ops::Deref;

/// always represented as -1,0,1
#[derive(Eq, PartialEq, Debug, Copy, Clone, Default)]
pub struct Small(i8);

impl Small {
pub const ZERO: Small = Small(0);
pub const ONE: Small = Small(1);
pub const MONE: Small = Small(-1);

pub(super) fn new_i32(n: i32) -> Self {
debug_assert!(n < 2);
debug_assert!(n > -2);
Small(n as i8)
}
#[must_use]
pub fn new_i8(n: i8) -> Self {
debug_assert!(n < 2);
debug_assert!(n > -2);
Small(n)
}

#[must_use]
pub const fn freeze(x: i16) -> Self {
Small((i32_mod_u14((x as i32) + 1, 3).wrapping_sub(1)) as i8)
}
}

/// the benefit is from outside, anyone can access the inner value as number,
/// but no one can modify it without refreezing
impl Deref for Small {
type Target = i8;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<Q> From<Fq<Q>> for Small {
fn from(value: Fq<Q>) -> Self {
Small::freeze(*value)
}
}

#[cfg(test)]
mod test {
use super::Small;
fn naive_freeze(x: i16) -> i8 {
// returns values in the set [-2, 2]
let res = (x % 3) as i8;
if res > 1 {
return res - 3;
}
if res < -1 {
return res + 3;
}
res
}
#[test]
fn test_freeze() {
for i in i16::MIN..i16::MAX {
assert_eq!(*Small::freeze(i), naive_freeze(i));
}
}
}
Loading
Loading