diff --git a/benches/ntt.rs b/benches/ntt.rs index e763ec5..5cc1a65 100644 --- a/benches/ntt.rs +++ b/benches/ntt.rs @@ -3,8 +3,8 @@ use math::{modulus::prime::Prime,dft::ntt::Table}; use math::dft::DFT; fn forward_inplace(c: &mut Criterion) { - fn runner + 'static>(prime_instance: &mut Prime, nth_root: u64) -> Box { - let ntt_table: Table<'_, u64> = Table::::new(prime_instance, nth_root); + fn runner + 'static>(prime_instance: Prime, nth_root: u64) -> Box { + let ntt_table: Table = Table::::new(prime_instance, nth_root); let mut a: Vec = vec![0; (nth_root >> 1) as usize]; for i in 0..a.len(){ a[i] = i as u64; @@ -21,7 +21,7 @@ fn forward_inplace(c: &mut Criterion) { let runners = [ ("prime", { - runner::>(&mut prime_instance, 1<>(prime_instance, 1< + 'static>(prime_instance: &mut Prime, nth_root: u64) -> Box { - let ntt_table: Table<'_, u64> = Table::::new(prime_instance, nth_root); + fn runner + 'static>(prime_instance: Prime, nth_root: u64) -> Box { + let ntt_table: Table = Table::::new(prime_instance, nth_root); let mut a: Vec = vec![0; (nth_root >> 1) as usize]; for i in 0..a.len(){ a[i] = i as u64; @@ -46,11 +46,11 @@ fn forward_inplace_lazy(c: &mut Criterion) { let mut b: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime> = c.benchmark_group("forward_inplace_lazy"); for log_nth_root in 11..17 { - let mut prime_instance: Prime = Prime::::new(0x1fffffffffe00001, 1); + let prime_instance: Prime = Prime::::new(0x1fffffffffe00001, 1); let runners = [ ("prime", { - runner::>(&mut prime_instance, 1<>(prime_instance, 1< + 'static>(prime_instance: &mut Prime, nth_root: u64) -> Box { - let ntt_table: Table<'_, u64> = Table::::new(prime_instance, nth_root); + fn runner + 'static>(prime_instance: Prime, nth_root: u64) -> Box { + let ntt_table: Table = Table::::new(prime_instance, nth_root); let mut a: Vec = vec![0; (nth_root >> 1) as usize]; for i in 0..a.len(){ a[i] = i as u64; @@ -75,11 +75,11 @@ fn backward_inplace(c: &mut Criterion) { let mut b: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime> = c.benchmark_group("backward_inplace"); for log_nth_root in 11..18 { - let mut prime_instance: Prime = Prime::::new(0x1fffffffffe00001, 1); + let prime_instance: Prime = Prime::::new(0x1fffffffffe00001, 1); let runners = [ ("prime", { - runner::>(&mut prime_instance, 1<>(prime_instance, 1< + 'static>(prime_instance: &mut Prime, nth_root: u64) -> Box { - let ntt_table: Table<'_, u64> = Table::::new(prime_instance, nth_root); + fn runner + 'static>(prime_instance: Prime, nth_root: u64) -> Box { + let ntt_table: Table = Table::::new(prime_instance, nth_root); let mut a: Vec = vec![0; (nth_root >> 1) as usize]; for i in 0..a.len(){ a[i] = i as u64; @@ -104,11 +104,11 @@ fn backward_inplace_lazy(c: &mut Criterion) { let mut b: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime> = c.benchmark_group("backward_inplace_lazy"); for log_nth_root in 11..17 { - let mut prime_instance: Prime = Prime::::new(0x1fffffffffe00001, 1); + let prime_instance: Prime = Prime::::new(0x1fffffffffe00001, 1); let runners = [ ("prime", { - runner::>(&mut prime_instance, 1<>(prime_instance, 1<` let q_base: u64 = 0x1fffffffffe00001; // Example prime base - let q_power: u64 = 1; // Example power + let q_power: usize = 1; // Example power let mut prime_instance: Prime = Prime::::new(q_base, q_power); // Display the fields of `Prime` to verify @@ -17,7 +18,7 @@ fn main() { let n: u64 = 1024; let nth_root: u64 = n<<1; - let ntt_table: Table<'_, u64> = Table::::new(&mut prime_instance, nth_root); + let ntt_table: Table = Table::::new(prime_instance, nth_root); let mut a: Vec = vec![0; (nth_root >> 1) as usize]; @@ -35,4 +36,6 @@ fn main() { println!("{:?}", a); + let r : Ring = Ring::::new(n as usize, q_base, q_power); + } \ No newline at end of file diff --git a/src/dft/ntt.rs b/src/dft/ntt.rs index bbf1d70..21ade41 100644 --- a/src/dft/ntt.rs +++ b/src/dft/ntt.rs @@ -6,8 +6,8 @@ use crate::modulus::WordOps; use crate::dft::DFT; use itertools::izip; -pub struct Table<'a, O>{ - prime:&'a Prime, +pub struct Table{ + prime:Prime, psi_forward_rev:Vec>, psi_backward_rev: Vec>, q:O, @@ -15,8 +15,8 @@ pub struct Table<'a, O>{ four_q:O, } -impl<'a> Table<'a, u64> { - pub fn new(prime: &'a mut Prime, nth_root: u64)->Self{ +impl Table< u64> { + pub fn new(prime: Prime, nth_root: u64)->Self{ assert!(nth_root&(nth_root-1) == 0, "invalid argument: nth_root = {} is not a power of two", nth_root); @@ -47,13 +47,15 @@ impl<'a> Table<'a, u64> { psi_backward_rev[i_rev] = prime.shoup.prepare(powers_backward); } + let q: u64 = prime.q(); + Self{ prime: prime, psi_forward_rev: psi_forward_rev, psi_backward_rev: psi_backward_rev, - q:prime.q(), - two_q:prime.q()<<1, - four_q:prime.q()<<2, + q:q, + two_q:q<<1, + four_q:q<<2, } } @@ -64,7 +66,7 @@ impl<'a> Table<'a, u64> { } -impl<'a> DFT for Table<'a,u64>{ +impl DFT for Table{ fn forward_inplace(&self, a: &mut [u64]){ self.forward_inplace(a) } @@ -82,7 +84,7 @@ impl<'a> DFT for Table<'a,u64>{ } } -impl<'a> Table<'a,u64>{ +impl Table{ pub fn forward_inplace_lazy(&self, a: &mut [u64]){ self.forward_inplace_core::(a); diff --git a/src/modulus/impl_u64/prime.rs b/src/modulus/impl_u64/prime.rs index d2c4514..9e17550 100644 --- a/src/modulus/impl_u64/prime.rs +++ b/src/modulus/impl_u64/prime.rs @@ -9,7 +9,7 @@ impl Prime{ /// Returns a new instance of Prime. /// Panics if q_base is not a prime > 2 and /// if q_base^q_power would overflow u64. - pub fn new(q_base: u64, q_power: u64) -> Self{ + pub fn new(q_base: u64, q_power: usize) -> Self{ assert!(is_prime(q_base) && q_base > 2); Self::new_unchecked(q_base, q_power) } @@ -17,7 +17,7 @@ impl Prime{ /// Returns a new instance of Prime. /// Does not check if q_base is a prime > 2. /// Panics if q_base^q_power would overflow u64. - pub fn new_unchecked(q_base: u64, q_power: u64) -> Self { + pub fn new_unchecked(q_base: u64, q_power: usize) -> Self { let mut q = q_base; for _i in 1..q_power{ @@ -31,7 +31,7 @@ impl Prime{ phi *= q_base } - Self { + let mut prime: Prime = Self { q:q, q_base:q_base, q_power:q_power, @@ -39,7 +39,12 @@ impl Prime{ montgomery:MontgomeryPrecomp::new(q), shoup:ShoupPrecomp::new(q), phi:phi, - } + }; + + prime.check_factors(); + + prime + } pub fn q(&self) -> u64{ @@ -50,7 +55,7 @@ impl Prime{ self.q_base } - pub fn q_power(&self) -> u64{ + pub fn q_power(&self) -> usize{ self.q_power } @@ -84,9 +89,7 @@ impl Prime{ impl Prime{ /// Returns the smallest nth primitive root of q_base. - pub fn primitive_root(&mut self) -> u64{ - - self.check_factors(); + pub fn primitive_root(&self) -> u64{ let mut candidate: u64 = 1u64; let mut not_found: bool = true; @@ -113,7 +116,7 @@ impl Prime{ } /// Returns an nth primitive root of q = q_base^q_power in Montgomery. - pub fn primitive_nth_root(&mut self, nth_root:u64) -> u64{ + pub fn primitive_nth_root(&self, nth_root:u64) -> u64{ assert!(self.q & (nth_root-1) == 1, "invalid prime: q = {} % nth_root = {} = {} != 1", self.q, nth_root, self.q & (nth_root-1)); diff --git a/src/modulus/prime.rs b/src/modulus/prime.rs index ed1c9eb..d051763 100644 --- a/src/modulus/prime.rs +++ b/src/modulus/prime.rs @@ -1,11 +1,11 @@ -use crate::modulus::montgomery::{MontgomeryPrecomp, Montgomery}; -use crate::modulus::shoup::{ShoupPrecomp}; - +use crate::modulus::montgomery::MontgomeryPrecomp; +use crate::modulus::shoup::ShoupPrecomp; +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Prime { pub q: O, /// q_base^q_powers pub q_base: O, - pub q_power: O, + pub q_power: usize, pub factors: Vec, /// distinct factors of q-1 pub montgomery: MontgomeryPrecomp, pub shoup:ShoupPrecomp, diff --git a/src/modulus/shoup.rs b/src/modulus/shoup.rs index 246d6e8..901be14 100644 --- a/src/modulus/shoup.rs +++ b/src/modulus/shoup.rs @@ -14,6 +14,7 @@ impl Shoup { } } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ShoupPrecomp{ pub q: O, pub one: Shoup, diff --git a/src/ring.rs b/src/ring.rs index a0119da..d26868b 100644 --- a/src/ring.rs +++ b/src/ring.rs @@ -7,5 +7,5 @@ use crate::dft::DFT; pub struct Ring{ pub n:usize, pub modulus:Prime, - pub dft:dyn DFT, + pub dft:Box>, } \ No newline at end of file diff --git a/src/ring/impl_u64/mod.rs b/src/ring/impl_u64/mod.rs index 6235b7d..f21c8c0 100644 --- a/src/ring/impl_u64/mod.rs +++ b/src/ring/impl_u64/mod.rs @@ -1 +1,2 @@ -pub mod automorphism; \ No newline at end of file +pub mod automorphism; +pub mod ring; \ No newline at end of file diff --git a/src/ring/impl_u64/ring.rs b/src/ring/impl_u64/ring.rs new file mode 100644 index 0000000..debdcf3 --- /dev/null +++ b/src/ring/impl_u64/ring.rs @@ -0,0 +1,23 @@ +use crate::ring::Ring; +use crate::dft::ntt::Table; +use crate::modulus::prime::Prime; +use crate::poly::Poly; + +impl Ring{ + pub fn new(n:usize, q_base:u64, q_power:usize) -> Self{ + let prime: Prime = Prime::::new(q_base, q_power); + Self { + n: n, + modulus: prime.clone(), + dft: Box::new(Table::::new(prime, (2 * n) as u64)), + } + } + + pub fn n(&self) -> usize{ + return self.n + } + + pub fn new_poly(&self) -> Poly{ + Poly::::new(self.n()) + } +} \ No newline at end of file