arnaucube
arnaucube/poulpyPublic Self-hosted repository
Added LWESecret
Jean-Philippe Bossuat jeanphilippe.bossuat@gmail.com
Thu, 12 Jun 2025 11:03:54 +0200
commit

ec4253bb1cb056e674f8e35d9916a352a9e34a6c

parent

d5dc9e690201ef58e2e1bbe413d3fd69d25d59f3

4 files changed, 74 insertions(+), 14 deletions(-)

jump to
M backend/src/scalar_znx.rs → backend/src/scalar_znx.rs

@@ -104,12 +104,12 @@ }

} impl<D: From<Vec<u8>>> ScalarZnx<D> { - pub(crate) fn bytes_of<S: Sized>(n: usize, cols: usize) -> usize { - n * cols * size_of::<S>() + pub(crate) fn bytes_of(n: usize, cols: usize) -> usize { + n * cols * size_of::<i64>() } - pub(crate) fn new<S: Sized>(n: usize, cols: usize) -> Self { - let data = alloc_aligned::<u8>(Self::bytes_of::<S>(n, cols)); + pub fn new(n: usize, cols: usize) -> Self { + let data = alloc_aligned::<u8>(Self::bytes_of(n, cols)); Self { data: data.into(), n,

@@ -117,9 +117,9 @@ cols,

} } - pub(crate) fn new_from_bytes<S: Sized>(n: usize, cols: usize, bytes: impl Into<Vec<u8>>) -> Self { + pub(crate) fn new_from_bytes(n: usize, cols: usize, bytes: impl Into<Vec<u8>>) -> Self { let data: Vec<u8> = bytes.into(); - assert!(data.len() == Self::bytes_of::<S>(n, cols)); + assert!(data.len() == Self::bytes_of(n, cols)); Self { data: data.into(), n,

@@ -131,7 +131,7 @@

pub type ScalarZnxOwned = ScalarZnx<Vec<u8>>; pub(crate) fn bytes_of_scalar_znx<B: Backend>(module: &Module<B>, cols: usize) -> usize { - ScalarZnxOwned::bytes_of::<i64>(module.n(), cols) + ScalarZnxOwned::bytes_of(module.n(), cols) } pub trait ScalarZnxAlloc {

@@ -142,13 +142,13 @@ }

impl<B: Backend> ScalarZnxAlloc for Module<B> { fn bytes_of_scalar_znx(&self, cols: usize) -> usize { - ScalarZnxOwned::bytes_of::<i64>(self.n(), cols) + ScalarZnxOwned::bytes_of(self.n(), cols) } fn new_scalar_znx(&self, cols: usize) -> ScalarZnxOwned { - ScalarZnxOwned::new::<i64>(self.n(), cols) + ScalarZnxOwned::new(self.n(), cols) } fn new_scalar_znx_from_bytes(&self, cols: usize, bytes: Vec<u8>) -> ScalarZnxOwned { - ScalarZnxOwned::new_from_bytes::<i64>(self.n(), cols, bytes) + ScalarZnxOwned::new_from_bytes(self.n(), cols, bytes) } }
M core/src/blind_rotation/key.rs → core/src/blind_rotation/key.rs

@@ -1,7 +1,7 @@

use backend::{Backend, FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}; use sampling::source::Source; -use crate::{AutomorphismKey, GGSWCiphertext, GLWESecret, SecretDistribution}; +use crate::{AutomorphismKey, GGSWCiphertext, GLWESecret, LWESecret, SecretDistribution}; pub struct BlindRotationKeyCGGI<B: Backend> { pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>,

@@ -27,7 +27,7 @@ pub fn generate_from_sk<DataSkGLWE, DataSkLWE>(

&mut self, module: &Module<FFT64>, sk_glwe: &GLWESecret<DataSkGLWE, FFT64>, - sk_lwe: &GLWESecret<DataSkLWE, FFT64>, + sk_lwe: &LWESecret<DataSkLWE>, source_xa: &mut Source, source_xe: &mut Source, sigma: f64,
M core/src/glwe_keys.rs → core/src/keys.rs

@@ -17,6 +17,66 @@ ZERO, // Debug mod

NONE, // Unitialized } +pub struct LWESecret<T> { + pub(crate) data: ScalarZnx<T>, + pub(crate) dist: SecretDistribution, +} + +impl LWESecret<Vec<u8>> { + pub fn alloc(n: usize) -> Self { + Self { + data: ScalarZnx::new(n, 1), + dist: SecretDistribution::NONE, + } + } +} + +impl<DataSelf> LWESecret<DataSelf> { + pub fn n(&self) -> usize { + self.data.n() + } + + pub fn log_n(&self) -> usize { + self.data.log_n() + } + + pub fn rank(&self) -> usize { + self.data.cols() + } +} + +impl<D: AsRef<[u8]> + AsMut<[u8]>> LWESecret<D> { + pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) { + self.data.fill_ternary_prob(0, prob, source); + self.dist = SecretDistribution::TernaryProb(prob); + } + + pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) { + self.data.fill_ternary_hw(0, hw, source); + self.dist = SecretDistribution::TernaryFixed(hw); + } + + pub fn fill_binary_prob(&mut self, prob: f64, source: &mut Source) { + self.data.fill_binary_prob(0, prob, source); + self.dist = SecretDistribution::BinaryProb(prob); + } + + pub fn fill_binary_hw(&mut self, hw: usize, source: &mut Source) { + self.data.fill_binary_hw(0, hw, source); + self.dist = SecretDistribution::BinaryFixed(hw); + } + + pub fn fill_binary_block(&mut self, block_size: usize, source: &mut Source) { + self.data.fill_binary_block(0, block_size, source); + self.dist = SecretDistribution::BinaryBlock(block_size); + } + + pub fn fill_zero(&mut self) { + self.data.zero(); + self.dist = SecretDistribution::ZERO; + } +} + pub struct GLWESecret<T, B: Backend> { pub(crate) data: ScalarZnx<T>, pub(crate) data_fourier: ScalarZnxDft<T, B>,
M core/src/lib.rs → core/src/lib.rs

@@ -5,10 +5,10 @@ pub mod gglwe_ciphertext;

pub mod ggsw_ciphertext; pub mod glwe_ciphertext; pub mod glwe_ciphertext_fourier; -pub mod glwe_keys; pub mod glwe_ops; pub mod glwe_packing; pub mod glwe_plaintext; +pub mod keys; pub mod keyswitch_key; pub mod tensor_key; #[cfg(test)]

@@ -24,10 +24,10 @@ pub use gglwe_ciphertext::*;

pub use ggsw_ciphertext::*; pub use glwe_ciphertext::*; pub use glwe_ciphertext_fourier::*; -pub use glwe_keys::*; pub use glwe_ops::*; pub use glwe_packing::*; pub use glwe_plaintext::*; +pub use keys::*; pub use keyswitch_key::*; pub use tensor_key::*;