fhe-study/arith/src/ring.rs
arith/src/ring.rsRaw
1use rand::{distributions::Distribution, Rng};
2use std::fmt::Debug;
3use std::iter::Sum;
4use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
5
6/// Represents a ring element. Currently implemented by ring_nq.rs#Rq and
7/// ring_torus.rs#Tn. Is not a 'pure algebraic ring', but more a custom trait
8/// definition which includes methods like `mod_switch`.
9// assumed to be mod (X^N +1)
10pub trait Ring:
11 Sized
12 + Add<Output = Self>
13 + AddAssign
14 + Sum
15 + Sub<Output = Self>
16 + SubAssign
17 + Mul<Output = Self> // internal product
18 + Mul<u64, Output = Self> // scalar mul, external product
19 + Mul<Self::C, Output = Self>
20 + Neg<Output = Self>
21 + PartialEq
22 + Debug
23 + Clone
24 + Copy
25 + Sum<<Self as Add>::Output>
26 + Sum<<Self as Mul>::Output>
27{
28 /// C defines the coefficient type
29 type C: Debug + Clone;
30
31 const Q: u64;
32 const N: usize;
33
34 fn coeffs(&self) -> Vec<Self::C>;
35 fn zero() -> Self;
36 // note/wip/warning: dist (0,q) with f64, will output more '0=q' elements than other values
37 fn rand(rng: impl Rng, dist: impl Distribution<f64>) -> Self;
38
39 fn from_vec(coeffs: Vec<Self::C>) -> Self;
40
41 fn decompose(&self, beta: u32, l: u32) -> Vec<Self>;
42
43 fn remodule<const P: u64>(&self) -> impl Ring;
44 fn mod_switch<const P: u64>(&self) -> impl Ring;
45
46 /// returns [ [(num/den) * self].round() ] mod q
47 /// ie. performs the multiplication and division over f64, and then it
48 /// rounds the result, only applying the mod Q (if the ring is mod Q) at the
49 /// end.
50 fn mul_div_round(&self, num: u64, den: u64) -> Self;
51}