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 crate::modulus::WordOps;
7use num::traits::Unsigned;
8use std::sync::Arc;
9
10pub struct Ring<O: Unsigned> {
11 pub n: usize,
12 pub modulus: Prime<O>,
13 pub dft: Box<dyn DFT<O>>,
14}
15
16impl<O: Unsigned> Ring<O> {
17
18 pub fn log_n(&self) -> usize{
19 return self.n().log2();
20 }
21
22 pub fn n(&self) -> usize {
23 return self.n;
24 }
25
26 pub fn new_poly(&self) -> Poly<u64> {
27 Poly::<u64>::new(self.n())
28 }
29}
30
31pub struct RingRNS<O: Unsigned>(pub Vec<Arc<Ring<O>>>);
32
33impl<O: Unsigned> RingRNS<O> {
34
35 pub fn log_n(&self) -> usize{
36 return self.n().log2();
37 }
38
39 pub fn n(&self) -> usize {
40 self.0[0].n()
41 }
42
43 pub fn new_polyrns(&self) -> PolyRNS<u64> {
44 PolyRNS::<u64>::new(self.n(), self.level())
45 }
46
47 pub fn new_poly(&self) -> Poly<u64> {
48 Poly::<u64>::new(self.n())
49 }
50
51 pub fn max_level(&self) -> usize {
52 self.0.len() - 1
53 }
54
55 pub fn level(&self) -> usize {
56 self.0.len() - 1
57 }
58
59 pub fn at_level(&self, level: usize) -> RingRNS<O> {
60 assert!(level <= self.0.len());
61 RingRNS(self.0[..level + 1].to_vec())
62 }
63}