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;
7
8pub struct Ring<O: Unsigned> {
9 pub n: usize,
10 pub modulus: Prime<O>,
11 pub dft: Box<dyn DFT<O>>,
12}
13
14impl<O: Unsigned> Ring<O> {
15 pub fn n(&self) -> usize {
16 return self.n;
17 }
18
19 pub fn new_poly(&self) -> Poly<u64> {
20 Poly::<u64>::new(self.n())
21 }
22}
23
24pub struct RingRNS<'a, O: Unsigned>(pub &'a [Ring<O>]);
25
26impl<O: Unsigned> RingRNS<'_, O> {
27 pub fn n(&self) -> usize {
28 self.0[0].n()
29 }
30
31 pub fn new_polyrns(&self) -> PolyRNS<u64> {
32 PolyRNS::<u64>::new(self.n(), self.level())
33 }
34
35 pub fn max_level(&self) -> usize {
36 self.0.len() - 1
37 }
38
39 pub fn level(&self) -> usize {
40 self.0.len() - 1
41 }
42
43 pub fn at_level(&self, level: usize) -> RingRNS<O> {
44 assert!(level <= self.0.len());
45 RingRNS(&self.0[..level + 1])
46 }
47}