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