added rlwe basic sk encryption

This commit is contained in:
Jean-Philippe Bossuat
2025-05-06 16:43:17 +02:00
parent e35924f44c
commit fe6f99b9ce
3 changed files with 102 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
use base2k::{Backend, FFT64, MatZnxDft, MatZnxDftAlloc, Module, VecZnx, VecZnxAlloc}; use base2k::{Backend, FFT64, MatZnxDft, MatZnxDftAlloc, Module, VecZnx, VecZnxAlloc, VecZnxDft, VecZnxDftAlloc};
pub struct Ciphertext<T> { pub struct Ciphertext<T> {
data: T, data: T,
@@ -48,7 +48,12 @@ impl<T> Plaintext<T> {
} }
} }
pub(crate) type CipherVecZnx<C> = Ciphertext<VecZnx<C>>; pub(crate) type CtVecZnx<C> = Ciphertext<VecZnx<C>>;
pub(crate) type CtVecZnxDft<C, B: Backend> = Ciphertext<VecZnxDft<C, B>>;
pub(crate) type CtMatZnxDft<C, B: Backend> = Ciphertext<MatZnxDft<C, B>>;
pub(crate) type PtVecZnx<C> = Plaintext<VecZnx<C>>;
pub(crate) type PtVecZnxDft<C, B: Backend> = Plaintext<VecZnxDft<C, B>>;
pub(crate) type PtMatZnxDft<C, B: Backend> = Plaintext<MatZnxDft<C, B>>;
impl Ciphertext<VecZnx<Vec<u8>>> { impl Ciphertext<VecZnx<Vec<u8>>> {
pub fn new<B: Backend>(module: &Module<B>, log_base2k: usize, log_q: usize, cols: usize) -> Self { pub fn new<B: Backend>(module: &Module<B>, log_base2k: usize, log_q: usize, cols: usize) -> Self {
@@ -70,6 +75,16 @@ impl Plaintext<VecZnx<Vec<u8>>> {
} }
} }
impl<B: Backend> Ciphertext<VecZnxDft<Vec<u8>, B>> {
pub fn new(module: &Module<B>, log_base2k: usize, log_q: usize, cols: usize) -> Self {
Self {
data: module.new_vec_znx_dft(cols, derive_size(log_base2k, log_q)),
log_base2k: log_base2k,
log_q: log_q,
}
}
}
impl<B: Backend> Ciphertext<MatZnxDft<Vec<u8>, B>> { impl<B: Backend> Ciphertext<MatZnxDft<Vec<u8>, B>> {
pub fn new(module: &Module<B>, log_base2k: usize, rows: usize, cols_in: usize, cols_out: usize, log_q: usize) -> Self { pub fn new(module: &Module<B>, log_base2k: usize, rows: usize, cols_in: usize, cols_out: usize, log_q: usize) -> Self {
Self { Self {

View File

@@ -1,20 +1,21 @@
use base2k::{ use base2k::{
AddNormal, Backend, FFT64, FillUniform, Module, ScalarZnxDftOps, ScalarZnxDftToRef, Scratch, VecZnx, VecZnxBigAlloc, AddNormal, Backend, FFT64, FillUniform, Module, ScalarZnxDftOps, ScalarZnxDftToRef, Scratch, VecZnx, VecZnxAlloc,
VecZnxBigOps, VecZnxBigScratch, VecZnxDftAlloc, VecZnxDftOps, VecZnxToMut, VecZnxToRef, ZnxInfos, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxDftToMut, VecZnxDftToRef,
VecZnxToMut, VecZnxToRef, ZnxInfos,
}; };
use sampling::source::Source; use sampling::source::Source;
use crate::{ use crate::{
elem::{CipherVecZnx, Plaintext}, elem::{CtVecZnx, CtVecZnxDft, PtVecZnx},
keys::SecretKey, keys::SecretKey,
}; };
pub trait EncryptSk<B: Backend, D> { pub trait EncryptSk<B: Backend, D, P> {
fn encrypt<P, S>( fn encrypt<S>(
module: &Module<B>, module: &Module<B>,
res: &mut D, res: &mut D,
pt: Option<&Plaintext<P>>, pt: Option<&P>,
sk: &SecretKey<S>, sk: &SecretKey<S>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
@@ -22,7 +23,6 @@ pub trait EncryptSk<B: Backend, D> {
sigma: f64, sigma: f64,
bound: f64, bound: f64,
) where ) where
P: VecZnxToRef,
S: ScalarZnxDftToRef<B>; S: ScalarZnxDftToRef<B>;
fn encrypt_tmp_bytes(module: &Module<B>, size: usize) -> usize { fn encrypt_tmp_bytes(module: &Module<B>, size: usize) -> usize {
@@ -30,14 +30,15 @@ pub trait EncryptSk<B: Backend, D> {
} }
} }
impl<C> EncryptSk<FFT64, CipherVecZnx<C>> for CipherVecZnx<C> impl<C, P> EncryptSk<FFT64, CtVecZnx<C>, PtVecZnx<P>> for CtVecZnx<C>
where where
VecZnx<C>: VecZnxToMut + VecZnxToRef, VecZnx<C>: VecZnxToMut + VecZnxToRef,
VecZnx<P>: VecZnxToRef,
{ {
fn encrypt<P, S>( fn encrypt<S>(
module: &Module<FFT64>, module: &Module<FFT64>,
ct: &mut CipherVecZnx<C>, ct: &mut CtVecZnx<C>,
pt: Option<&Plaintext<P>>, pt: Option<&PtVecZnx<P>>,
sk: &SecretKey<S>, sk: &SecretKey<S>,
source_xa: &mut Source, source_xa: &mut Source,
source_xe: &mut Source, source_xe: &mut Source,
@@ -45,7 +46,6 @@ where
sigma: f64, sigma: f64,
bound: f64, bound: f64,
) where ) where
P: VecZnxToRef,
S: ScalarZnxDftToRef<FFT64>, S: ScalarZnxDftToRef<FFT64>,
{ {
let log_base2k: usize = ct.log_base2k(); let log_base2k: usize = ct.log_base2k();
@@ -60,9 +60,10 @@ where
{ {
let (mut c0_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, size); let (mut c0_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, size);
module.vec_znx_dft(&mut c0_dft, 0, &ct_mut, 1);
// c0_dft = DFT(a) * DFT(s) // c0_dft = DFT(a) * DFT(s)
module.svp_apply(&mut c0_dft, 0, &sk.data().to_ref(), 0, &ct_mut, 1); module.svp_apply_inplace(&mut c0_dft, 0, &sk.data().to_ref(), 0);
// c0_big = IDFT(c0_dft) // c0_big = IDFT(c0_dft)
module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut c0_dft, 0); module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut c0_dft, 0);
@@ -79,3 +80,73 @@ where
module.vec_znx_big_normalize(log_base2k, &mut ct_mut, 0, &c0_big, 0, scratch_1); module.vec_znx_big_normalize(log_base2k, &mut ct_mut, 0, &c0_big, 0, scratch_1);
} }
} }
pub trait EncryptZeroSk<B: Backend, D> {
fn encrypt_zero<S>(
module: &Module<B>,
res: &mut D,
sk: &SecretKey<S>,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch,
sigma: f64,
bound: f64,
) where
S: ScalarZnxDftToRef<B>;
fn encrypt_zero_tmp_bytes(module: &Module<B>, size: usize) -> usize {
(module.bytes_of_vec_znx(1, size) | module.bytes_of_vec_znx_dft(1, size))
+ module.bytes_of_vec_znx_big(1, size)
+ module.bytes_of_vec_znx(1, size)
+ module.vec_znx_big_normalize_tmp_bytes()
}
}
impl<C> EncryptZeroSk<FFT64, CtVecZnxDft<C, FFT64>> for CtVecZnxDft<C, FFT64>
where
VecZnxDft<C, FFT64>: VecZnxDftToMut<FFT64> + VecZnxDftToRef<FFT64>,
{
fn encrypt_zero<S>(
module: &Module<FFT64>,
ct: &mut CtVecZnxDft<C, FFT64>,
sk: &SecretKey<S>,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch,
sigma: f64,
bound: f64,
) where
S: ScalarZnxDftToRef<FFT64>,
{
let log_base2k: usize = ct.log_base2k();
let log_q: usize = ct.log_q();
let mut ct_mut: VecZnxDft<&mut [u8], FFT64> = ct.data_mut().to_mut();
let size: usize = ct_mut.size();
// ct[1] = DFT(a)
{
let (mut tmp_znx, _) = scratch.tmp_vec_znx(module, 1, size);
tmp_znx.fill_uniform(log_base2k, 1, size, source_xa);
module.vec_znx_dft(&mut ct_mut, 1, &tmp_znx, 0);
}
let (mut c0_big, scratch_1) = scratch.tmp_vec_znx_big(module, 1, size);
{
let (mut tmp_dft, _) = scratch_1.tmp_vec_znx_dft(module, 1, size);
// c0_dft = DFT(a) * DFT(s)
module.svp_apply(&mut tmp_dft, 0, &sk.data().to_ref(), 0, &ct_mut, 1);
// c0_big = IDFT(c0_dft)
module.vec_znx_idft_tmp_a(&mut c0_big, 0, &mut tmp_dft, 0);
}
// c0_big += e
c0_big.add_normal(log_base2k, 0, log_q, source_xe, sigma, bound);
// c0 = norm(c0_big = -as + e)
let (mut tmp_znx, scratch_2) = scratch_1.tmp_vec_znx(module, 1, size);
module.vec_znx_big_normalize(log_base2k, &mut tmp_znx, 0, &c0_big, 0, scratch_2);
// ct[0] = DFT(-as + e)
module.vec_znx_dft(&mut ct_mut, 0, &tmp_znx, 0);
}
}

View File

@@ -15,7 +15,7 @@ impl<T> SecretKey<T> {
&self.data &self.data
} }
pub fn data_mut(&self) -> &mut T { pub fn data_mut(&mut self) -> &mut T {
&mut self.data &mut self.data
} }
} }