arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/base2k/src/svp.rs
base2k/src/svp.rsRaw
  1use crate::ffi::svp;
  2use crate::{Module, VecZnx, VecZnxDft};
  3
  4use crate::Infos;
  5use rand::seq::SliceRandom;
  6use rand_core::RngCore;
  7use rand_distr::{Distribution, WeightedIndex};
  8use sampling::source::Source;
  9
 10pub struct Scalar(pub Vec<i64>);
 11
 12impl Module {
 13    pub fn new_scalar(&self) -> Scalar {
 14        Scalar::new(self.n())
 15    }
 16}
 17
 18impl Scalar {
 19    pub fn new(n: usize) -> Self {
 20        Self(vec![i64::default(); Self::buffer_size(n)])
 21    }
 22
 23    pub fn buffer_size(n: usize) -> usize {
 24        n
 25    }
 26
 27    pub fn from_buffer(&mut self, n: usize, buf: &[i64]) {
 28        let size: usize = Self::buffer_size(n);
 29        assert!(
 30            buf.len() >= size,
 31            "invalid buffer: buf.len()={} < self.buffer_size(n={})={}",
 32            buf.len(),
 33            n,
 34            size
 35        );
 36        self.0 = Vec::from(&buf[..size])
 37    }
 38
 39    pub fn as_ptr(&self) -> *const i64 {
 40        self.0.as_ptr()
 41    }
 42
 43    pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) {
 44        let choices: [i64; 3] = [-1, 0, 1];
 45        let weights: [f64; 3] = [prob / 2.0, 1.0 - prob, prob / 2.0];
 46        let dist: WeightedIndex<f64> = WeightedIndex::new(&weights).unwrap();
 47        self.0
 48            .iter_mut()
 49            .for_each(|x: &mut i64| *x = choices[dist.sample(source)]);
 50    }
 51
 52    pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) {
 53        self.0[..hw]
 54            .iter_mut()
 55            .for_each(|x: &mut i64| *x = (((source.next_u32() & 1) as i64) << 1) - 1);
 56        self.0.shuffle(source);
 57    }
 58}
 59
 60pub struct SvpPPol(pub *mut svp::svp_ppol_t, pub usize);
 61
 62/// A prepared [crate::Scalar] for [SvpPPolOps::svp_apply_dft].
 63/// An [SvpPPol] an be seen as a [VecZnxDft] of one limb.
 64/// The backend array of an [SvpPPol] is allocated in C and must be freed manually.
 65impl SvpPPol {
 66    /// Returns the ring degree of the [SvpPPol].
 67    pub fn n(&self) -> usize {
 68        self.1
 69    }
 70
 71    pub fn from_bytes(size: usize, bytes: &mut [u8]) -> SvpPPol {
 72        SvpPPol(bytes.as_mut_ptr() as *mut svp::svp_ppol_t, size)
 73    }
 74
 75    /// Returns the number of limbs of the [SvpPPol], which is always 1.
 76    pub fn limbs(&self) -> usize {
 77        1
 78    }
 79}
 80
 81pub trait SvpPPolOps {
 82    /// Allocates a new [SvpPPol].
 83    fn new_svp_ppol(&self) -> SvpPPol;
 84
 85    /// Returns the minimum number of bytes necessary to allocate
 86    /// a new [SvpPPol] through [SvpPPol::from_bytes].
 87    fn bytes_of_svp_ppol(&self) -> usize;
 88
 89    /// Prepares a [crate::Scalar] for a [SvpPPolOps::svp_apply_dft].
 90    fn svp_prepare(&self, svp_ppol: &mut SvpPPol, a: &Scalar);
 91
 92    /// Applies the [SvpPPol] x [VecZnxDft] product, where each limb of
 93    /// the [VecZnxDft] is multiplied with [SvpPPol].
 94    fn svp_apply_dft(&self, c: &mut VecZnxDft, a: &SvpPPol, b: &VecZnx, b_limbs: usize);
 95}
 96
 97impl SvpPPolOps for Module {
 98    fn new_svp_ppol(&self) -> SvpPPol {
 99        unsafe { SvpPPol(svp::new_svp_ppol(self.0), self.n()) }
100    }
101
102    fn bytes_of_svp_ppol(&self) -> usize {
103        unsafe { svp::bytes_of_svp_ppol(self.0) as usize }
104    }
105
106    fn svp_prepare(&self, svp_ppol: &mut SvpPPol, a: &Scalar) {
107        unsafe { svp::svp_prepare(self.0, svp_ppol.0, a.as_ptr()) }
108    }
109
110    fn svp_apply_dft(&self, c: &mut VecZnxDft, a: &SvpPPol, b: &VecZnx, b_limbs: usize) {
111        assert!(
112            c.limbs() >= b_limbs,
113            "invalid c_vector: c_vector.limbs()={} < b.limbs()={}",
114            c.limbs(),
115            b_limbs
116        );
117        unsafe {
118            svp::svp_apply_dft(
119                self.0,
120                c.0,
121                b_limbs as u64,
122                a.0,
123                b.as_ptr(),
124                b_limbs as u64,
125                b.n() as u64,
126            )
127        }
128    }
129}