poulpy/rlwe/src/ciphertext.rs
rlwe/src/ciphertext.rsRaw
1use crate::elem::Elem;
2use crate::parameters::Parameters;
3use crate::plaintext::Plaintext;
4use base2k::{Module, VecZnx, VmpPMat, VmpPMatOps};
5
6pub struct Ciphertext(pub Elem);
7
8impl Ciphertext {
9 pub fn new(
10 module: &Module,
11 log_base2k: usize,
12 log_q: usize,
13 degree: usize,
14 log_scale: usize,
15 ) -> Self {
16 Self(Elem::new(module, log_base2k, log_q, degree, log_scale))
17 }
18
19 pub fn n(&self) -> usize {
20 self.0.n()
21 }
22
23 pub fn degree(&self) -> usize {
24 self.0.degree()
25 }
26
27 pub fn log_q(&self) -> usize {
28 self.0.log_q()
29 }
30
31 pub fn limbs(&self) -> usize {
32 self.0.limbs()
33 }
34
35 pub fn at(&self, i: usize) -> &VecZnx {
36 self.0.at(i)
37 }
38
39 pub fn at_mut(&mut self, i: usize) -> &mut VecZnx {
40 self.0.at_mut(i)
41 }
42
43 pub fn log_base2k(&self) -> usize {
44 self.0.log_base2k()
45 }
46
47 pub fn log_scale(&self) -> usize {
48 self.0.log_scale
49 }
50
51 pub fn as_plaintext(&self) -> Plaintext {
52 unsafe { Plaintext(std::ptr::read(&self.0)) }
53 }
54}
55
56impl Parameters {
57 pub fn new_ciphertext(&self, log_q: usize) -> Ciphertext {
58 Ciphertext::new(self.module(), self.log_base2k(), log_q, self.log_scale(), 1)
59 }
60}
61
62pub struct GadgetCiphertext {
63 pub value: Vec<VmpPMat>,
64 pub log_base2k: usize,
65 pub log_q: usize,
66 pub log_scale: usize,
67}
68
69impl GadgetCiphertext {
70 pub fn new(
71 module: &Module,
72 log_base2k: usize,
73 rows: usize,
74 log_q: usize,
75 log_scale: usize,
76 ) -> Self {
77 let cols: usize = (log_q + log_base2k - 1) / log_base2k;
78 let mut value: Vec<VmpPMat> = Vec::new();
79 (0..rows).for_each(|_| value.push(module.new_vmp_pmat(rows, cols)));
80 Self {
81 value,
82 log_base2k,
83 log_q,
84 log_scale,
85 }
86 }
87
88 pub fn n(&self) -> usize {
89 self.value[0].n
90 }
91
92 pub fn rows(&self) -> usize {
93 self.value[0].rows
94 }
95
96 pub fn cols(&self) -> usize {
97 self.value[0].cols
98 }
99
100 pub fn degree(&self) -> usize {
101 self.value.len() - 1
102 }
103
104 pub fn log_q(&self) -> usize {
105 self.log_q
106 }
107
108 pub fn log_base2k(&self) -> usize {
109 self.log_base2k
110 }
111}
112
113pub struct RGSWCiphertext {
114 pub value: [GadgetCiphertext; 2],
115 pub log_base2k: usize,
116 pub log_q: usize,
117 pub log_p: usize,
118}