diff --git a/Cargo.lock b/Cargo.lock index 3585fd6..215ec83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,9 +227,9 @@ dependencies = [ [[package]] name = "itertools" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" dependencies = [ "either", ] @@ -267,11 +267,13 @@ name = "math" version = "0.1.0" dependencies = [ "criterion", - "itertools 0.13.0", + "itertools 0.14.0", + "num", "num-bigint", "num-traits", "primality-test", "prime_factorization", + "sampling", ] [[package]] @@ -531,6 +533,14 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "sampling" +version = "0.1.0" +dependencies = [ + "rand_chacha", + "rand_core", +] + [[package]] name = "serde" version = "1.0.216" diff --git a/Cargo.toml b/Cargo.toml index eecba5a..634efaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["math"] \ No newline at end of file +members = ["math", "sampling"] diff --git a/math/Cargo.toml b/math/Cargo.toml index 3cb66bc..c35a5c5 100644 --- a/math/Cargo.toml +++ b/math/Cargo.toml @@ -4,12 +4,14 @@ version = "0.1.0" edition = "2021" [dependencies] +num = "0.4.3" primality-test = "0.3.0" num-bigint = "0.4.6" num-traits = "0.2.19" prime_factorization = "1.0.5" -itertools = "0.13.0" +itertools = "0.14.0" criterion = "0.5.1" +sampling = { path = "../sampling" } [[bench]] name = "ntt" @@ -21,4 +23,8 @@ harness = false [[bench]] name = "ring_rns" +harness = false + +[[bench]] +name = "sampling" harness = false \ No newline at end of file diff --git a/math/benches/sampling.rs b/math/benches/sampling.rs new file mode 100644 index 0000000..dae8997 --- /dev/null +++ b/math/benches/sampling.rs @@ -0,0 +1,40 @@ +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use math::ring::{Ring, RingRNS}; +use math::ring::impl_u64::ring_rns::new_rings; +use math::poly::PolyRNS; +use sampling::source::Source; + +fn fill_uniform(c: &mut Criterion) { + fn runner(r: RingRNS) -> Box { + + let mut a: PolyRNS = r.new_polyrns(); + let seed: [u8; 32] = [0;32]; + let mut source: Source = Source::new(seed); + + Box::new(move || { + r.fill_uniform(&mut source, &mut a); + }) + } + + let mut b: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime> = c.benchmark_group("fill_uniform"); + for log_n in 11..18 { + + let n = 1< = vec![0x1fffffffffe00001u64, 0x1fffffffffc80001u64, 0x1fffffffffb40001, 0x1fffffffff500001]; + let rings: Vec> = new_rings(n, moduli); + let ring_rns: RingRNS<'_, u64> = RingRNS::new(&rings); + + let runners = [ + (format!("prime/n={}/level={}", n, ring_rns.level()), { + runner(ring_rns) + }), + ]; + + for (name, mut runner) in runners { + b.bench_with_input(name, &(), |b, _| b.iter(&mut runner)); + } + } +} + +criterion_group!(benches, fill_uniform); +criterion_main!(benches); diff --git a/math/src/dft/ntt.rs b/math/src/dft/ntt.rs index 7bd2702..ff9ec6c 100644 --- a/math/src/dft/ntt.rs +++ b/math/src/dft/ntt.rs @@ -150,7 +150,6 @@ impl Table{ debug_assert!(*b < self.four_q, "b:{} q:{}", b, self.four_q); a.reduce_once_assign(self.two_q); let bt: u64 = self.prime.barrett.mul_external::(t, *b); - debug_assert!(bt < self.two_q, "bt:{} two_q:{}", bt, self.two_q); *b = *a + self.two_q-bt; *a += bt; if !LAZY { diff --git a/math/src/lib.rs b/math/src/lib.rs index e98dc31..7fdcca5 100644 --- a/math/src/lib.rs +++ b/math/src/lib.rs @@ -5,6 +5,7 @@ pub mod modulus; pub mod dft; pub mod ring; pub mod poly; +pub mod scalar; pub const CHUNK: usize= 8; diff --git a/math/src/modulus.rs b/math/src/modulus.rs index f516678..1ec3d40 100644 --- a/math/src/modulus.rs +++ b/math/src/modulus.rs @@ -15,6 +15,7 @@ pub const BARRETTLAZY: REDUCEMOD = 5; pub trait WordOps{ fn log2(self) -> O; fn reverse_bits_msb(self, n:u32) -> O; + fn mask(self) -> O; } impl WordOps for u64{ @@ -26,6 +27,10 @@ impl WordOps for u64{ fn reverse_bits_msb(self, n: u32) -> u64{ self.reverse_bits() >> (usize::BITS - n) } + #[inline(always)] + fn mask(self) -> u64{ + (1< for usize{ @@ -37,6 +42,10 @@ impl WordOps for usize{ fn reverse_bits_msb(self, n: u32) -> usize{ self.reverse_bits() >> (usize::BITS - n) } + #[inline(always)] + fn mask(self) -> usize{ + (1<{ diff --git a/math/src/modulus/barrett.rs b/math/src/modulus/barrett.rs index fea91f1..efaee2e 100644 --- a/math/src/modulus/barrett.rs +++ b/math/src/modulus/barrett.rs @@ -14,8 +14,6 @@ impl Barrett { } } -pub struct BarrettRNS(pub Vec>); - #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct BarrettPrecomp{ pub q: O, diff --git a/math/src/ring.rs b/math/src/ring.rs index d9aaffe..df0675d 100644 --- a/math/src/ring.rs +++ b/math/src/ring.rs @@ -1,17 +1,18 @@ pub mod impl_u64; +use num::traits::Unsigned; use crate::modulus::prime::Prime; use crate::poly::{Poly, PolyRNS}; use crate::dft::DFT; -pub struct Ring{ +pub struct Ring{ pub n:usize, pub modulus:Prime, pub dft:Box>, } -impl Ring{ +impl Ring{ pub fn n(&self) -> usize{ return self.n } @@ -21,9 +22,9 @@ impl Ring{ } } -pub struct RingRNS<'a, O>(& 'a [Ring]); +pub struct RingRNS<'a, O: Unsigned>(& 'a [Ring]); -impl RingRNS<'_, O> { +impl RingRNS<'_, O> { pub fn n(&self) -> usize{ self.0[0].n() diff --git a/math/src/ring/impl_u64/mod.rs b/math/src/ring/impl_u64/mod.rs index 81c756d..a5125f6 100644 --- a/math/src/ring/impl_u64/mod.rs +++ b/math/src/ring/impl_u64/mod.rs @@ -1,4 +1,5 @@ pub mod automorphism; pub mod ring; pub mod ring_rns; -pub mod rescaling_rns; \ No newline at end of file +pub mod rescaling_rns; +pub mod sampling; \ No newline at end of file diff --git a/math/src/ring/impl_u64/rescaling_rns.rs b/math/src/ring/impl_u64/rescaling_rns.rs index 0f9387b..0aecf61 100644 --- a/math/src/ring/impl_u64/rescaling_rns.rs +++ b/math/src/ring/impl_u64/rescaling_rns.rs @@ -1,6 +1,7 @@ use crate::ring::RingRNS; -use crate::poly::{Poly, PolyRNS}; -use crate::modulus::barrett::BarrettRNS; +use crate::poly::PolyRNS; +use crate::modulus::barrett::Barrett; +use crate::scalar::ScalarRNS; use crate::modulus::ONCE; extern crate test; @@ -12,7 +13,7 @@ impl RingRNS<'_, u64>{ assert!(b.level() >= a.level()-1, "invalid input b: b.level()={} < a.level()-1={}", b.level(), a.level()-1); let level = self.level(); self.0[level].intt::(a.at(level), buf.at_mut(0)); - let rescaling_constants: BarrettRNS = self.rescaling_constant(); + let rescaling_constants: ScalarRNS> = self.rescaling_constant(); let (buf_ntt_q_scaling, buf_ntt_qi_scaling) = buf.0.split_at_mut(1); for (i, r) in self.0[0..level].iter().enumerate(){ r.ntt::(&buf_ntt_q_scaling[0], &mut buf_ntt_qi_scaling[0]); @@ -25,7 +26,7 @@ impl RingRNS<'_, u64>{ pub fn div_floor_by_last_modulus_ntt_inplace(&self, buf: &mut PolyRNS, b: &mut PolyRNS){ let level = self.level(); self.0[level].intt::(b.at(level), buf.at_mut(0)); - let rescaling_constants: BarrettRNS = self.rescaling_constant(); + let rescaling_constants: ScalarRNS> = self.rescaling_constant(); let (buf_ntt_q_scaling, buf_ntt_qi_scaling) = buf.0.split_at_mut(1); for (i, r) in self.0[0..level].iter().enumerate(){ r.ntt::(&buf_ntt_q_scaling[0], &mut buf_ntt_qi_scaling[0]); @@ -37,7 +38,7 @@ impl RingRNS<'_, u64>{ pub fn div_floor_by_last_modulus(&self, a: &PolyRNS, b: &mut PolyRNS){ assert!(b.level() >= a.level()-1, "invalid input b: b.level()={} < a.level()-1={}", b.level(), a.level()-1); let level = self.level(); - let rescaling_constants:crate::modulus::barrett::BarrettRNS = self.rescaling_constant(); + let rescaling_constants:ScalarRNS> = self.rescaling_constant(); for (i, r) in self.0[0..level].iter().enumerate(){ r.sum_aqqmb_prod_c_scalar_barrett::(a.at(level), a.at(i), &rescaling_constants.0[i], b.at_mut(i)); } @@ -46,7 +47,7 @@ impl RingRNS<'_, u64>{ /// Updates a to floor(b / q[b.level()]). pub fn div_floor_by_last_modulus_inplace(&self, a: &mut PolyRNS){ let level = self.level(); - let rescaling_constants: BarrettRNS = self.rescaling_constant(); + let rescaling_constants: ScalarRNS> = self.rescaling_constant(); let (a_i, a_level) = a.split_at_mut(level); for (i, r) in self.0[0..level].iter().enumerate(){ r.sum_aqqmb_prod_c_scalar_barrett_inplace::(&a_level[0], &rescaling_constants.0[i], &mut a_i[i]); @@ -77,8 +78,10 @@ impl RingRNS<'_, u64>{ #[cfg(test)] mod tests { use num_bigint::BigInt; + use num_bigint::Sign; use crate::ring::Ring; use crate::ring::impl_u64::ring_rns::new_rings; + use sampling::source::Source; use super::*; #[test] @@ -86,21 +89,20 @@ mod tests { let n = 1<<10; let moduli: Vec = vec![0x1fffffffffc80001u64, 0x1fffffffffe00001u64]; let rings: Vec> = new_rings(n, moduli); - let ring_rns = RingRNS::new(&rings); + let ring_rns: RingRNS<'_, u64> = RingRNS::new(&rings); + let seed: [u8; 32] = [0;32]; + let mut source: Source = Source::new(seed); let mut a: PolyRNS = ring_rns.new_polyrns(); let mut b: PolyRNS = ring_rns.new_polyrns(); let mut c: PolyRNS = ring_rns.at_level(ring_rns.level()-1).new_polyrns(); - // Allocates an rns poly with values [0..n] - let mut coeffs_a: Vec = (0..n).map(|i|{BigInt::from(i)}).collect(); - ring_rns.from_bigint_inplace(&coeffs_a, 1, &mut a); + // Allocates a random PolyRNS + ring_rns.fill_uniform(&mut source, &mut a); - // Scales by q_level both a and coeffs_a - let scalar: u64 = ring_rns.0[ring_rns.level()].modulus.q; - ring_rns.mul_scalar_inplace::(&scalar, &mut a); - let scalar_big = BigInt::from(scalar); - coeffs_a.iter_mut().for_each(|a|{*a *= &scalar_big}); + // Maps PolyRNS to [BigInt] + let mut coeffs_a: Vec = (0..n).map(|i|{BigInt::from(i)}).collect(); + ring_rns.at_level(a.level()).to_bigint_inplace(&a, 1, &mut coeffs_a); // Performs c = intt(ntt(a) / q_level) ring_rns.ntt_inplace::(&mut a); @@ -112,7 +114,14 @@ mod tests { ring_rns.at_level(c.level()).to_bigint_inplace(&c, 1, &mut coeffs_c); // Performs floor division on a - coeffs_a.iter_mut().for_each(|a|{*a /= &scalar_big}); + let scalar_big = BigInt::from(ring_rns.0[ring_rns.level()].modulus.q); + coeffs_a.iter_mut().for_each(|a|{ + // Emulates floor division in [0, q-1] and maps to [-(q-1)/2, (q-1)/2-1] + *a /= &scalar_big; + if a.sign() == Sign::Minus { + *a -= 1; + } + }); assert!(coeffs_a == coeffs_c); } diff --git a/math/src/ring/impl_u64/ring_rns.rs b/math/src/ring/impl_u64/ring_rns.rs index 3ece9b1..25a6b01 100644 --- a/math/src/ring/impl_u64/ring_rns.rs +++ b/math/src/ring/impl_u64/ring_rns.rs @@ -1,7 +1,8 @@ use crate::ring::{Ring, RingRNS}; use crate::poly::PolyRNS; use crate::modulus::montgomery::Montgomery; -use crate::modulus::barrett::BarrettRNS; +use crate::modulus::barrett::Barrett; +use crate::scalar::ScalarRNS; use crate::modulus::REDUCEMOD; use num_bigint::BigInt; @@ -25,10 +26,10 @@ impl<'a> RingRNS<'a, u64>{ modulus } - pub fn rescaling_constant(&self) -> BarrettRNS { + pub fn rescaling_constant(&self) -> ScalarRNS> { let level = self.level(); let q_scale: u64 = self.0[level].modulus.q; - BarrettRNS((0..level).map(|i| {self.0[i].modulus.barrett.prepare(self.0[i].modulus.q - self.0[i].modulus.inv(q_scale))}).collect()) + ScalarRNS((0..level).map(|i| {self.0[i].modulus.barrett.prepare(self.0[i].modulus.q - self.0[i].modulus.inv(q_scale))}).collect()) } pub fn from_bigint_inplace(&self, coeffs: &[BigInt], step:usize, a: &mut PolyRNS){ diff --git a/math/src/ring/impl_u64/sampling.rs b/math/src/ring/impl_u64/sampling.rs new file mode 100644 index 0000000..c788440 --- /dev/null +++ b/math/src/ring/impl_u64/sampling.rs @@ -0,0 +1,18 @@ +use sampling::source::Source; +use crate::modulus::WordOps; +use crate::ring::{Ring, RingRNS}; +use crate::poly::{Poly, PolyRNS}; + +impl Ring{ + pub fn fill_uniform(&self, source: &mut Source, a: &mut Poly){ + let max:u64 = self.modulus.q; + let mask: u64 = max.mask(); + a.0.iter_mut().for_each(|a|{*a = source.next_u64n(max, mask)}); + } +} + +impl RingRNS<'_, u64>{ + pub fn fill_uniform(&self, source: &mut Source, a: &mut PolyRNS){ + self.0.iter().enumerate().for_each(|(i, r)|{r.fill_uniform(source, a.at_mut(i))}); + } +} \ No newline at end of file diff --git a/math/src/scalar.rs b/math/src/scalar.rs new file mode 100644 index 0000000..1f13e60 --- /dev/null +++ b/math/src/scalar.rs @@ -0,0 +1,2 @@ +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ScalarRNS(pub Vec); \ No newline at end of file diff --git a/sampling/Cargo.toml b/sampling/Cargo.toml new file mode 100644 index 0000000..6f17c91 --- /dev/null +++ b/sampling/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "sampling" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand_chacha = "0.3.1" +rand_core = "0.6.4" \ No newline at end of file diff --git a/sampling/src/lib.rs b/sampling/src/lib.rs new file mode 100644 index 0000000..b5cb700 --- /dev/null +++ b/sampling/src/lib.rs @@ -0,0 +1 @@ +pub mod source; \ No newline at end of file diff --git a/sampling/src/source.rs b/sampling/src/source.rs new file mode 100644 index 0000000..55f651c --- /dev/null +++ b/sampling/src/source.rs @@ -0,0 +1,45 @@ +use rand_chacha::rand_core::SeedableRng; +use rand_core::RngCore; +use rand_chacha::{ChaCha8Rng}; + +const MAXF64: f64 = 9007199254740992.0; + +pub struct Source{ + source:ChaCha8Rng, +} + +impl Source{ + pub fn new(seed: [u8;32]) -> Source{ + Source{source:ChaCha8Rng::from_seed(seed)} + } + + pub fn new_seed(&mut self) -> [u8;32]{ + let mut seed: [u8; 32] = [0u8;32]; + self.source.fill_bytes(&mut seed); + seed + } + + #[inline(always)] + pub fn next_u64(&mut self) -> u64{ + self.source.next_u64() + } + + #[inline(always)] + pub fn next_u64n(&mut self, max: u64, mask: u64) -> u64{ + let mut x: u64 = self.next_u64() & mask; + while x >= max{ + x = self.next_u64() & mask; + } + x + } + + #[inline(always)] + pub fn next_f64(&mut self, min: f64, max: f64) -> f64{ + min + ((self.next_u64()<<11>>11) as f64)/MAXF64 * (max-min) + } + + #[inline(always)] + pub fn fill_bytes(&mut self, bytes: &mut [u8]){ + self.source.fill_bytes(bytes) + } +} \ No newline at end of file