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, Sub, SubAssign};
5
6/// Represents a ring element. Currently implemented by ring_n.rs#R and ring_nq.rs#Rq.
7pub trait Ring:
8 Sized
9 + Add<Output = Self>
10 + AddAssign
11 + Sum
12 + Sub<Output = Self>
13 + SubAssign
14 + Mul<Output = Self>
15 + Mul<u64, Output = Self> // scalar mul
16 + PartialEq
17 + Debug
18 + Clone
19 + Sum<<Self as Add>::Output>
20 + Sum<<Self as Mul>::Output>
21{
22 /// C defines the coefficient type
23 type C: Debug + Clone;
24
25 fn coeffs(&self) -> Vec<Self::C>;
26 fn zero() -> Self;
27 // note/wip/warning: dist (0,q) with f64, will output more '0=q' elements than other values
28 fn rand(rng: impl Rng, dist: impl Distribution<f64>) -> Self;
29
30 fn from_vec(coeffs: Vec<Self::C>) -> Self;
31
32 fn decompose(&self, beta: u32, l: u32) -> Vec<Self>;
33}