From ec4253bb1cb056e674f8e35d9916a352a9e34a6c Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Thu, 12 Jun 2025 11:03:54 +0200 Subject: [PATCH] Added LWESecret --- backend/src/scalar_znx.rs | 20 +++++----- core/src/blind_rotation/key.rs | 4 +- core/src/{glwe_keys.rs => keys.rs} | 60 ++++++++++++++++++++++++++++++ core/src/lib.rs | 4 +- 4 files changed, 74 insertions(+), 14 deletions(-) rename core/src/{glwe_keys.rs => keys.rs} (76%) diff --git a/backend/src/scalar_znx.rs b/backend/src/scalar_znx.rs index e252a9b..09e7292 100644 --- a/backend/src/scalar_znx.rs +++ b/backend/src/scalar_znx.rs @@ -104,12 +104,12 @@ impl + AsRef<[u8]>> ScalarZnx { } impl>> ScalarZnx { - pub(crate) fn bytes_of(n: usize, cols: usize) -> usize { - n * cols * size_of::() + pub(crate) fn bytes_of(n: usize, cols: usize) -> usize { + n * cols * size_of::() } - pub(crate) fn new(n: usize, cols: usize) -> Self { - let data = alloc_aligned::(Self::bytes_of::(n, cols)); + pub fn new(n: usize, cols: usize) -> Self { + let data = alloc_aligned::(Self::bytes_of(n, cols)); Self { data: data.into(), n, @@ -117,9 +117,9 @@ impl>> ScalarZnx { } } - pub(crate) fn new_from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { + pub(crate) fn new_from_bytes(n: usize, cols: usize, bytes: impl Into>) -> Self { let data: Vec = bytes.into(); - assert!(data.len() == Self::bytes_of::(n, cols)); + assert!(data.len() == Self::bytes_of(n, cols)); Self { data: data.into(), n, @@ -131,7 +131,7 @@ impl>> ScalarZnx { pub type ScalarZnxOwned = ScalarZnx>; pub(crate) fn bytes_of_scalar_znx(module: &Module, cols: usize) -> usize { - ScalarZnxOwned::bytes_of::(module.n(), cols) + ScalarZnxOwned::bytes_of(module.n(), cols) } pub trait ScalarZnxAlloc { @@ -142,13 +142,13 @@ pub trait ScalarZnxAlloc { impl ScalarZnxAlloc for Module { fn bytes_of_scalar_znx(&self, cols: usize) -> usize { - ScalarZnxOwned::bytes_of::(self.n(), cols) + ScalarZnxOwned::bytes_of(self.n(), cols) } fn new_scalar_znx(&self, cols: usize) -> ScalarZnxOwned { - ScalarZnxOwned::new::(self.n(), cols) + ScalarZnxOwned::new(self.n(), cols) } fn new_scalar_znx_from_bytes(&self, cols: usize, bytes: Vec) -> ScalarZnxOwned { - ScalarZnxOwned::new_from_bytes::(self.n(), cols, bytes) + ScalarZnxOwned::new_from_bytes(self.n(), cols, bytes) } } diff --git a/core/src/blind_rotation/key.rs b/core/src/blind_rotation/key.rs index 4076e14..b4c5d40 100644 --- a/core/src/blind_rotation/key.rs +++ b/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 { pub(crate) data: Vec, B>>, @@ -27,7 +27,7 @@ impl BlindRotationKeyCGGI { &mut self, module: &Module, sk_glwe: &GLWESecret, - sk_lwe: &GLWESecret, + sk_lwe: &LWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, diff --git a/core/src/glwe_keys.rs b/core/src/keys.rs similarity index 76% rename from core/src/glwe_keys.rs rename to core/src/keys.rs index be8aa43..9549f15 100644 --- a/core/src/glwe_keys.rs +++ b/core/src/keys.rs @@ -17,6 +17,66 @@ pub(crate) enum SecretDistribution { NONE, // Unitialized } +pub struct LWESecret { + pub(crate) data: ScalarZnx, + pub(crate) dist: SecretDistribution, +} + +impl LWESecret> { + pub fn alloc(n: usize) -> Self { + Self { + data: ScalarZnx::new(n, 1), + dist: SecretDistribution::NONE, + } + } +} + +impl LWESecret { + 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 + AsMut<[u8]>> LWESecret { + 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 { pub(crate) data: ScalarZnx, pub(crate) data_fourier: ScalarZnxDft, diff --git a/core/src/lib.rs b/core/src/lib.rs index 3aa05ea..1bffc5a 100644 --- a/core/src/lib.rs +++ b/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::*;