Added LWESecret

This commit is contained in:
Jean-Philippe Bossuat
2025-06-12 11:03:54 +02:00
parent d5dc9e6902
commit ec4253bb1c
4 changed files with 74 additions and 14 deletions

View File

@@ -104,12 +104,12 @@ impl<D: AsMut<[u8]> + AsRef<[u8]>> ScalarZnx<D> {
} }
impl<D: From<Vec<u8>>> ScalarZnx<D> { impl<D: From<Vec<u8>>> ScalarZnx<D> {
pub(crate) fn bytes_of<S: Sized>(n: usize, cols: usize) -> usize { pub(crate) fn bytes_of(n: usize, cols: usize) -> usize {
n * cols * size_of::<S>() n * cols * size_of::<i64>()
} }
pub(crate) fn new<S: Sized>(n: usize, cols: usize) -> Self { pub fn new(n: usize, cols: usize) -> Self {
let data = alloc_aligned::<u8>(Self::bytes_of::<S>(n, cols)); let data = alloc_aligned::<u8>(Self::bytes_of(n, cols));
Self { Self {
data: data.into(), data: data.into(),
n, n,
@@ -117,9 +117,9 @@ impl<D: From<Vec<u8>>> ScalarZnx<D> {
} }
} }
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(); let data: Vec<u8> = bytes.into();
assert!(data.len() == Self::bytes_of::<S>(n, cols)); assert!(data.len() == Self::bytes_of(n, cols));
Self { Self {
data: data.into(), data: data.into(),
n, n,
@@ -131,7 +131,7 @@ impl<D: From<Vec<u8>>> ScalarZnx<D> {
pub type ScalarZnxOwned = ScalarZnx<Vec<u8>>; pub type ScalarZnxOwned = ScalarZnx<Vec<u8>>;
pub(crate) fn bytes_of_scalar_znx<B: Backend>(module: &Module<B>, cols: usize) -> usize { 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 { pub trait ScalarZnxAlloc {
@@ -142,13 +142,13 @@ pub trait ScalarZnxAlloc {
impl<B: Backend> ScalarZnxAlloc for Module<B> { impl<B: Backend> ScalarZnxAlloc for Module<B> {
fn bytes_of_scalar_znx(&self, cols: usize) -> usize { 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 { 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 { 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)
} }
} }

View File

@@ -1,7 +1,7 @@
use backend::{Backend, FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}; use backend::{Backend, FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut};
use sampling::source::Source; use sampling::source::Source;
use crate::{AutomorphismKey, GGSWCiphertext, GLWESecret, SecretDistribution}; use crate::{AutomorphismKey, GGSWCiphertext, GLWESecret, LWESecret, SecretDistribution};
pub struct BlindRotationKeyCGGI<B: Backend> { pub struct BlindRotationKeyCGGI<B: Backend> {
pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>, pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>,
@@ -27,7 +27,7 @@ impl BlindRotationKeyCGGI<FFT64> {
&mut self, &mut self,
module: &Module<FFT64>, module: &Module<FFT64>,
sk_glwe: &GLWESecret<DataSkGLWE, FFT64>, sk_glwe: &GLWESecret<DataSkGLWE, FFT64>,
sk_lwe: &GLWESecret<DataSkLWE, FFT64>, sk_lwe: &LWESecret<DataSkLWE>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
sigma: f64, sigma: f64,

View File

@@ -17,6 +17,66 @@ pub(crate) enum SecretDistribution {
NONE, // Unitialized 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 struct GLWESecret<T, B: Backend> {
pub(crate) data: ScalarZnx<T>, pub(crate) data: ScalarZnx<T>,
pub(crate) data_fourier: ScalarZnxDft<T, B>, pub(crate) data_fourier: ScalarZnxDft<T, B>,

View File

@@ -5,10 +5,10 @@ pub mod gglwe_ciphertext;
pub mod ggsw_ciphertext; pub mod ggsw_ciphertext;
pub mod glwe_ciphertext; pub mod glwe_ciphertext;
pub mod glwe_ciphertext_fourier; pub mod glwe_ciphertext_fourier;
pub mod glwe_keys;
pub mod glwe_ops; pub mod glwe_ops;
pub mod glwe_packing; pub mod glwe_packing;
pub mod glwe_plaintext; pub mod glwe_plaintext;
pub mod keys;
pub mod keyswitch_key; pub mod keyswitch_key;
pub mod tensor_key; pub mod tensor_key;
#[cfg(test)] #[cfg(test)]
@@ -24,10 +24,10 @@ pub use gglwe_ciphertext::*;
pub use ggsw_ciphertext::*; pub use ggsw_ciphertext::*;
pub use glwe_ciphertext::*; pub use glwe_ciphertext::*;
pub use glwe_ciphertext_fourier::*; pub use glwe_ciphertext_fourier::*;
pub use glwe_keys::*;
pub use glwe_ops::*; pub use glwe_ops::*;
pub use glwe_packing::*; pub use glwe_packing::*;
pub use glwe_plaintext::*; pub use glwe_plaintext::*;
pub use keys::*;
pub use keyswitch_key::*; pub use keyswitch_key::*;
pub use tensor_key::*; pub use tensor_key::*;