poulpy/math/src/ring.rs
math/src/ring.rsRaw
1pub mod impl_u64;
2
3use crate::dft::DFT;
4use crate::modulus::prime::Prime;
5use crate::poly::{Poly, PolyRNS};
6use num::traits::Unsigned;
7use std::sync::Arc;
8
9pub struct Ring<O: Unsigned> {
10 pub n: usize,
11 pub modulus: Prime<O>,
12 pub dft: Box<dyn DFT<O>>,
13}
14
15impl<O: Unsigned> Ring<O> {
16 pub fn n(&self) -> usize {
17 return self.n;
18 }
19
20 pub fn new_poly(&self) -> Poly<u64> {
21 Poly::<u64>::new(self.n())
22 }
23}
24
25pub struct RingRNS<O: Unsigned>(pub Vec<Arc<Ring<O>>>);
26
27impl<O: Unsigned> RingRNS<O> {
28 pub fn n(&self) -> usize {
29 self.0[0].n()
30 }
31
32 pub fn new_polyrns(&self) -> PolyRNS<u64> {
33 PolyRNS::<u64>::new(self.n(), self.level())
34 }
35
36 pub fn max_level(&self) -> usize {
37 self.0.len() - 1
38 }
39
40 pub fn level(&self) -> usize {
41 self.0.len() - 1
42 }
43
44 pub fn at_level(&self, level: usize) -> RingRNS<O> {
45 assert!(level <= self.0.len());
46 RingRNS(self.0[..level + 1].to_vec())
47 }
48}