diff --git a/Cargo.lock b/Cargo.lock index 23c4b48..25afb8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,6 +178,12 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "getrandom" version = "0.2.15" @@ -199,12 +205,28 @@ dependencies = [ "crunchy", ] +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + [[package]] name = "hermit-abi" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" +[[package]] +name = "indexmap" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "is-terminal" version = "0.4.13" @@ -273,6 +295,7 @@ name = "math" version = "0.1.0" dependencies = [ "criterion", + "indexmap", "itertools 0.14.0", "num", "num-bigint", diff --git a/math/Cargo.toml b/math/Cargo.toml index 1c84bb0..ea9db6c 100644 --- a/math/Cargo.toml +++ b/math/Cargo.toml @@ -13,6 +13,7 @@ prime_factorization = "1.0.5" itertools = "0.14.0" criterion = "0.5.1" rand_distr = "0.4.3" +indexmap = "2.7.0" sampling = { path = "../sampling" } [[bench]] diff --git a/math/src/lib.rs b/math/src/lib.rs index b1990ff..7c19f3f 100644 --- a/math/src/lib.rs +++ b/math/src/lib.rs @@ -9,6 +9,7 @@ pub mod ring; pub mod scalar; pub const CHUNK: usize = 8; +pub const GALOISGENERATOR: usize = 5; pub mod macros { diff --git a/math/src/ring/impl_u64/packing.rs b/math/src/ring/impl_u64/packing.rs index 33a8147..558a4a2 100644 --- a/math/src/ring/impl_u64/packing.rs +++ b/math/src/ring/impl_u64/packing.rs @@ -4,6 +4,7 @@ use crate::ring::Ring; use crate::modulus::{ONCE, WordOps}; use crate::modulus::barrett::Barrett; use std::cmp::min; +use std::mem::transmute; impl Ring{ @@ -41,11 +42,13 @@ impl Ring{ x_pow } - pub fn pack(&self, polys: &mut HashMap>, log_gap: usize) -> Poly{ + pub fn pack<'a, const ZEROGARBAGE: bool, const NTT: bool>(&self, polys: &'a mut HashMap>, log_gap: usize) -> &'a Poly{ - let log_n = self.log_n(); - let log_start = log_n - log_gap; - let mut log_end = log_n; + let log_n: usize = self.log_n(); + let log_nth_root: usize = log_n+1; + let nth_root: usize = 1< = polys.keys().copied().collect(); keys.sort(); @@ -76,10 +79,100 @@ impl Ring{ self.a_mul_b_scalar_barrett_into_a::(&n_inv, poly); } - Poly::::default() + let x_pow2: Vec> = self.gen_x_pow_2::(log_n); + let mut tmpa: Poly = self.new_poly(); + let mut tmpb: Poly = self.new_poly(); + + for i in log_start..log_end{ + + let t: usize = 1<<(log_n-1-i); + + for j in 0..t{ + + let option_lo: Option<&&mut Poly> = polys.get(&i); + let option_hi: Option<&&mut Poly> = polys.get(&(i+t)); + let mut hi_exists: bool = false; + + match option_hi{ + Some(hi) =>{ + + // Unsafe code is necessary because two mutables references are + // accessed from the map. + unsafe{ + self.a_mul_b_montgomery_into_a::(&x_pow2[log_n-i-1], transmute(*hi as *const Poly as *mut Poly)); + } + + hi_exists = true; + + match option_lo{ + Some(lo) =>{ + + self.a_sub_b_into_c::<1, ONCE>(lo, hi, &mut tmpa); + + // Ensures unsafe blocks are "safe". + let ptr_hi: *mut Poly = *hi as *const Poly as *mut Poly; + let ptr_lo: *mut Poly = *lo as *const Poly as *mut Poly; + assert!(ptr_hi != ptr_lo, "something went seriously wrong"); + + unsafe{ + self.a_add_b_into_b::(hi, transmute(ptr_lo)); + } + } + None =>{ + unsafe{ + polys.insert(j, transmute(*hi as *const Poly as *mut Poly)); + } + }, + } + + polys.remove(&(j+t)); + } + + None =>{}, + } + + let option_lo: Option<&&mut Poly> = polys.get(&i); + let option_hi: Option<&&mut Poly> = polys.get(&(i+t)); + + match option_lo{ + Some(lo) =>{ + + let gal_el: usize = self.galois_element(1<<(i-1), i == 0, log_nth_root); + + if hi_exists{ + self.automorphism::(&tmpa, gal_el, 2<(*lo, gal_el, nth_root, &mut tmpa); + } + unsafe{ + self.a_add_b_into_b::(&tmpa, transmute(*lo as *const Poly as *mut Poly)); + } + } + + None =>{ + match option_hi{ + Some(hi) =>{ + let gal_el: usize = self.galois_element(1<<(i-1), i == 0, log_nth_root); + + self.automorphism::(*hi, gal_el, nth_root, &mut tmpa); + + unsafe{ + self.a_sub_b_into_a::<1, ONCE>(&tmpa, transmute(*hi as *const Poly as *mut Poly)) + } + } + + None =>{} + } + } + } + } + } + + *polys.get(&0).unwrap() } } + // Returns the largest fn max_pow2_gap(vec: &[usize]) -> usize{ let mut gap: usize = usize::MAX; diff --git a/math/src/ring/impl_u64/ring.rs b/math/src/ring/impl_u64/ring.rs index dcbf18f..8913c9b 100644 --- a/math/src/ring/impl_u64/ring.rs +++ b/math/src/ring/impl_u64/ring.rs @@ -7,6 +7,7 @@ use crate::modulus::{BARRETT, REDUCEMOD}; use crate::poly::Poly; use crate::ring::Ring; use crate::CHUNK; +use crate::GALOISGENERATOR; use num_bigint::BigInt; use num_traits::ToPrimitive; @@ -39,6 +40,29 @@ impl Ring { .enumerate() .for_each(|(i, v)| *v = (&coeffs[i] % &q_big).to_u64().unwrap()); } + + // Returns GALOISGENERATOR^gen_1 * (-1)^gen_2 mod 2^log_nth_root. + pub fn galois_element(&self, gen_1: usize, gen_2: bool, log_nth_root: usize) -> usize{ + let mut gal_el: usize = 1; + let mut gen_1_pow: usize = GALOISGENERATOR; + let mut e: usize = gen_1; + while e > 0{ + if e & 1 == 1{ + gal_el = gal_el.wrapping_mul(gen_1_pow); + } + + gen_1_pow *= gen_1_pow; + e>>=1; + } + + let nth_root = 1< {