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}
67
68impl GadgetCiphertext {
69 pub fn new(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> Self {
70 let cols: usize = (log_q + log_base2k - 1) / log_base2k;
71 let mut value: Vec<VmpPMat> = Vec::new();
72 (0..rows).for_each(|_| value.push(module.new_vmp_pmat(rows, cols)));
73 Self {
74 value,
75 log_base2k,
76 log_q,
77 }
78 }
79
80 pub fn n(&self) -> usize {
81 self.value[0].n
82 }
83
84 pub fn rows(&self) -> usize {
85 self.value[0].rows
86 }
87
88 pub fn cols(&self) -> usize {
89 self.value[0].cols
90 }
91
92 pub fn degree(&self) -> usize {
93 self.value.len() - 1
94 }
95
96 pub fn log_q(&self) -> usize {
97 self.log_q
98 }
99
100 pub fn log_base2k(&self) -> usize {
101 self.log_base2k
102 }
103}
104
105pub struct RGSWCiphertext {
106 pub value: [GadgetCiphertext; 2],
107 pub log_base2k: usize,
108 pub log_q: usize,
109 pub log_p: usize,
110}