arnaucube
arnaucube/poulpyPublic Self-hosted repository
wip
Jean-Philippe Bossuat jeanphilippe.bossuat@gmail.com
Thu, 09 Jan 2025 18:02:26 +0100
commit

0886e439e003de1f647b01debf68e1a7d7619986

parent

236978d1b097468ddb2fdc8d9dcd759364a46d6b

M Cargo.lock → Cargo.lock

@@ -179,6 +179,12 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index"

@@ -200,10 +206,26 @@ "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"

@@ -273,6 +295,7 @@ name = "math"

version = "0.1.0" dependencies = [ "criterion", + "indexmap", "itertools 0.14.0", "num", "num-bigint",
M math/Cargo.toml → 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]]
M math/src/lib.rs → 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 {
M math/src/ring/impl_u64/packing.rs → 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<u64>{

@@ -41,11 +42,13 @@

x_pow } - pub fn pack<const ZEROGARBAGE: bool, const NTT: bool>(&self, polys: &mut HashMap<usize, Poly<u64>>, log_gap: usize) -> Poly<u64>{ + pub fn pack<'a, const ZEROGARBAGE: bool, const NTT: bool>(&self, polys: &'a mut HashMap<usize, &'a mut Poly<u64>>, log_gap: usize) -> &'a Poly<u64>{ - 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<<log_nth_root; + let log_start: usize = log_n - log_gap; + let mut log_end: usize = log_n; let mut keys: Vec<usize> = polys.keys().copied().collect(); keys.sort();

@@ -76,9 +79,99 @@

self.a_mul_b_scalar_barrett_into_a::<ONCE>(&n_inv, poly); } - Poly::<u64>::default() + let x_pow2: Vec<Poly<u64>> = self.gen_x_pow_2::<true, false>(log_n); + let mut tmpa: Poly<u64> = self.new_poly(); + let mut tmpb: Poly<u64> = 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<u64>> = polys.get(&i); + let option_hi: Option<&&mut Poly<u64>> = 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::<ONCE>(&x_pow2[log_n-i-1], transmute(*hi as *const Poly<u64> as *mut Poly<u64>)); + } + + 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<u64> = *hi as *const Poly<u64> as *mut Poly<u64>; + let ptr_lo: *mut Poly<u64> = *lo as *const Poly<u64> as *mut Poly<u64>; + assert!(ptr_hi != ptr_lo, "something went seriously wrong"); + + unsafe{ + self.a_add_b_into_b::<ONCE>(hi, transmute(ptr_lo)); + } + } + None =>{ + unsafe{ + polys.insert(j, transmute(*hi as *const Poly<u64> as *mut Poly<u64>)); + } + }, + } + + polys.remove(&(j+t)); + } + + None =>{}, + } + + let option_lo: Option<&&mut Poly<u64>> = polys.get(&i); + let option_hi: Option<&&mut Poly<u64>> = 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::<true>(&tmpa, gal_el, 2<<self.log_n(), &mut tmpb); + }else{ + self.automorphism::<true>(*lo, gal_el, nth_root, &mut tmpa); + } + unsafe{ + self.a_add_b_into_b::<ONCE>(&tmpa, transmute(*lo as *const Poly<u64> as *mut Poly<u64>)); + } + } + + None =>{ + match option_hi{ + Some(hi) =>{ + let gal_el: usize = self.galois_element(1<<(i-1), i == 0, log_nth_root); + + self.automorphism::<true>(*hi, gal_el, nth_root, &mut tmpa); + + unsafe{ + self.a_sub_b_into_a::<1, ONCE>(&tmpa, transmute(*hi as *const Poly<u64> as *mut Poly<u64>)) + } + } + + None =>{} + } + } + } + } + } + + *polys.get(&0).unwrap() } } + // Returns the largest fn max_pow2_gap(vec: &[usize]) -> usize{
M math/src/ring/impl_u64/ring.rs → 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;

@@ -38,6 +39,29 @@ a.0.iter_mut()

.step_by(step) .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<<log_nth_root; + gal_el &= (nth_root-1); + + if gen_2{ + return nth_root - gal_el + } + gal_el } }