arnaucube
arnaucube/poulpyPublic Self-hosted repository
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 zero(&mut self) {
 52        self.0.zero()
 53    }
 54
 55    pub fn as_plaintext(&self) -> Plaintext {
 56        unsafe { Plaintext(std::ptr::read(&self.0)) }
 57    }
 58}
 59
 60impl Parameters {
 61    pub fn new_ciphertext(&self, log_q: usize) -> Ciphertext {
 62        Ciphertext::new(self.module(), self.log_base2k(), log_q, self.log_scale(), 1)
 63    }
 64}
 65
 66pub struct GadgetCiphertext {
 67    pub value: Vec<VmpPMat>,
 68    pub log_base2k: usize,
 69    pub log_q: usize,
 70}
 71
 72impl GadgetCiphertext {
 73    pub fn new(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> Self {
 74        let cols: usize = (log_q + log_base2k - 1) / log_base2k;
 75        let mut value: Vec<VmpPMat> = Vec::new();
 76        (0..rows).for_each(|_| value.push(module.new_vmp_pmat(rows, cols)));
 77        Self {
 78            value,
 79            log_base2k,
 80            log_q,
 81        }
 82    }
 83
 84    pub fn n(&self) -> usize {
 85        self.value[0].n
 86    }
 87
 88    pub fn rows(&self) -> usize {
 89        self.value[0].rows
 90    }
 91
 92    pub fn cols(&self) -> usize {
 93        self.value[0].cols
 94    }
 95
 96    pub fn degree(&self) -> usize {
 97        self.value.len() - 1
 98    }
 99
100    pub fn log_q(&self) -> usize {
101        self.log_q
102    }
103
104    pub fn log_base2k(&self) -> usize {
105        self.log_base2k
106    }
107}
108
109pub struct RGSWCiphertext {
110    pub value: [GadgetCiphertext; 2],
111    pub log_base2k: usize,
112    pub log_q: usize,
113    pub log_p: usize,
114}