mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
refactored RingRNS
This commit is contained in:
@@ -19,7 +19,7 @@ pub struct Table<O> {
|
||||
}
|
||||
|
||||
impl Table<u64> {
|
||||
pub fn new(prime: Prime<u64>, nth_root: u64) -> Self {
|
||||
pub fn new(prime: Prime<u64>, nth_root: u64) -> Table<u64> {
|
||||
assert!(
|
||||
nth_root & (nth_root - 1) == 0,
|
||||
"invalid argument: nth_root = {} is not a power of two",
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::dft::DFT;
|
||||
use crate::modulus::prime::Prime;
|
||||
use crate::poly::{Poly, PolyRNS};
|
||||
use num::traits::Unsigned;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Ring<O: Unsigned> {
|
||||
pub n: usize,
|
||||
@@ -21,9 +22,9 @@ impl<O: Unsigned> Ring<O> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RingRNS<'a, O: Unsigned>(pub &'a [Ring<O>]);
|
||||
pub struct RingRNS<O: Unsigned>(pub Vec<Arc<Ring<O>>>);
|
||||
|
||||
impl<O: Unsigned> RingRNS<'_, O> {
|
||||
impl<O: Unsigned> RingRNS<O> {
|
||||
pub fn n(&self) -> usize {
|
||||
self.0[0].n()
|
||||
}
|
||||
@@ -42,6 +43,6 @@ impl<O: Unsigned> RingRNS<'_, O> {
|
||||
|
||||
pub fn at_level(&self, level: usize) -> RingRNS<O> {
|
||||
assert!(level <= self.0.len());
|
||||
RingRNS(&self.0[..level + 1])
|
||||
RingRNS(self.0[..level + 1].to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::ring::RingRNS;
|
||||
use crate::scalar::ScalarRNS;
|
||||
extern crate test;
|
||||
|
||||
impl RingRNS<'_, u64> {
|
||||
impl RingRNS<u64> {
|
||||
/// Updates b to floor(a / q[b.level()]).
|
||||
pub fn div_floor_by_last_modulus<const NTT: bool>(
|
||||
&self,
|
||||
|
||||
@@ -5,19 +5,16 @@ use crate::poly::PolyRNS;
|
||||
use crate::ring::{Ring, RingRNS};
|
||||
use crate::scalar::ScalarRNS;
|
||||
use num_bigint::BigInt;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn new_rings(n: usize, moduli: Vec<u64>) -> Vec<Ring<u64>> {
|
||||
assert!(!moduli.is_empty(), "moduli cannot be empty");
|
||||
let rings: Vec<Ring<u64>> = moduli
|
||||
.into_iter()
|
||||
.map(|prime| Ring::new(n, prime, 1))
|
||||
.collect();
|
||||
return rings;
|
||||
}
|
||||
|
||||
impl<'a> RingRNS<'a, u64> {
|
||||
pub fn new(rings: &'a [Ring<u64>]) -> Self {
|
||||
RingRNS(rings)
|
||||
impl RingRNS<u64> {
|
||||
pub fn new(n: usize, moduli: Vec<u64>) -> Self {
|
||||
assert!(!moduli.is_empty(), "moduli cannot be empty");
|
||||
let rings: Vec<Arc<Ring<u64>>> = moduli
|
||||
.into_iter()
|
||||
.map(|prime| Arc::new(Ring::new(n, prime, 1)))
|
||||
.collect();
|
||||
return RingRNS(rings);
|
||||
}
|
||||
|
||||
pub fn modulus(&self) -> BigInt {
|
||||
@@ -92,7 +89,7 @@ impl<'a> RingRNS<'a, u64> {
|
||||
}
|
||||
}
|
||||
|
||||
impl RingRNS<'_, u64> {
|
||||
impl RingRNS<u64> {
|
||||
pub fn ntt_inplace<const LAZY: bool>(&self, a: &mut PolyRNS<u64>) {
|
||||
self.0
|
||||
.iter()
|
||||
@@ -122,7 +119,7 @@ impl RingRNS<'_, u64> {
|
||||
}
|
||||
}
|
||||
|
||||
impl RingRNS<'_, u64> {
|
||||
impl RingRNS<u64> {
|
||||
#[inline(always)]
|
||||
pub fn add<const REDUCE: REDUCEMOD>(
|
||||
&self,
|
||||
|
||||
@@ -12,7 +12,7 @@ impl Ring<u64> {
|
||||
}
|
||||
}
|
||||
|
||||
impl RingRNS<'_, u64> {
|
||||
impl RingRNS<u64> {
|
||||
pub fn fill_uniform(&self, source: &mut Source, a: &mut PolyRNS<u64>) {
|
||||
self.0
|
||||
.iter()
|
||||
|
||||
Reference in New Issue
Block a user