diff --git a/base2k/src/sampling.rs b/base2k/src/sampling.rs index 212658a..b2d6f22 100644 --- a/base2k/src/sampling.rs +++ b/base2k/src/sampling.rs @@ -297,8 +297,6 @@ where #[cfg(test)] mod tests { - use std::fmt::Display; - use super::{AddNormal, FillUniform}; use crate::vec_znx_ops::*; use crate::znx_base::*; diff --git a/base2k/src/scalar_znx_dft_ops.rs b/base2k/src/scalar_znx_dft_ops.rs index a4b3ccc..a51d72f 100644 --- a/base2k/src/scalar_znx_dft_ops.rs +++ b/base2k/src/scalar_znx_dft_ops.rs @@ -2,8 +2,8 @@ use crate::ffi::svp; use crate::ffi::vec_znx_dft::vec_znx_dft_t; use crate::znx_base::{ZnxInfos, ZnxView, ZnxViewMut}; use crate::{ - Backend, FFT64, Module, ScalarToRef, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef, VecZnx, - VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, VecZnxToRef, ZnxSliceSize, + Backend, FFT64, Module, ScalarToRef, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef, + VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, }; pub trait ScalarZnxDftAlloc { diff --git a/rlwe/src/elem.rs b/rlwe/src/elem.rs index 3cb1360..1126ed4 100644 --- a/rlwe/src/elem.rs +++ b/rlwe/src/elem.rs @@ -1,13 +1,12 @@ use base2k::{ - Backend, DataView, DataViewMut, MatZnxDft, MatZnxDftAlloc, MatZnxDftToMut, MatZnxDftToRef, Module, VecZnx, VecZnxAlloc, - VecZnxDft, VecZnxDftAlloc, VecZnxDftToMut, VecZnxDftToRef, VecZnxToMut, VecZnxToRef, ZnxInfos, + Backend, DataView, DataViewMut, MatZnxDft, MatZnxDftAlloc, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnxDftToRef, VecZnx, + VecZnxAlloc, VecZnxDft, VecZnxDftAlloc, VecZnxDftToMut, VecZnxDftToRef, VecZnxToMut, VecZnxToRef, ZnxInfos, }; -pub trait Infos -where - T: ZnxInfos, -{ - fn inner(&self) -> &T; +pub trait Infos { + type Inner: ZnxInfos; + + fn inner(&self) -> &Self::Inner; /// Returns the ring degree of the polynomials. fn n(&self) -> usize { @@ -48,17 +47,16 @@ where fn log_q(&self) -> usize; } -pub struct Ciphertext { - data: T, +pub struct RLWECt{ + data: VecZnx, log_base2k: usize, log_q: usize, } -impl Infos for Ciphertext -where - T: ZnxInfos, -{ - fn inner(&self) -> &T { +impl Infos for RLWECt { + type Inner = T; + + fn inner(&self) -> &Self::Inner { &self.data } @@ -90,11 +88,10 @@ pub struct Plaintext { log_q: usize, } -impl Infos for Plaintext -where - T: ZnxInfos, -{ - fn inner(&self) -> &T { +impl Infos for Plaintext { + type Inner = T; + + fn inner(&self) -> &Self::Inner { &self.data } diff --git a/rlwe/src/encryption.rs b/rlwe/src/encryption.rs index de3146f..e0f9e1f 100644 --- a/rlwe/src/encryption.rs +++ b/rlwe/src/encryption.rs @@ -6,13 +6,16 @@ use base2k::{ use sampling::source::Source; -use crate::{elem::Infos, keys::SecretKey}; +use crate::{ + elem::{Ciphertext, Infos, Plaintext}, + keys::SecretKey, +}; -pub trait EncryptSk { +pub trait EncryptSk { fn encrypt( module: &Module, - res: &mut D, - pt: Option<&P>, + res: &mut Ciphertext, + pt: Option<&Plaintext

>, sk: &SecretKey, source_xa: &mut Source, source_xe: &mut Source, @@ -22,20 +25,18 @@ pub trait EncryptSk { ) where S: ScalarZnxDftToRef; - fn encrypt_tmp_bytes(module: &Module, size: usize) -> usize { - (module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_big(1, size) - } + fn encrypt_scratch_bytes(module: &Module, size: usize) -> usize; } -impl EncryptSk for C +impl EncryptSk for Ciphertext where - C: VecZnxToMut + ZnxInfos + Infos, - P: VecZnxToRef, + C: VecZnxToMut + ZnxInfos, + P: VecZnxToRef + ZnxInfos, { fn encrypt( module: &Module, - ct: &mut C, - pt: Option<&P>, + ct: &mut Ciphertext, + pt: Option<&Plaintext

>, sk: &SecretKey, source_xa: &mut Source, source_xe: &mut Source, @@ -76,6 +77,41 @@ where // c0 = norm(c0_big = -as + m + e) module.vec_znx_big_normalize(log_base2k, &mut ct_mut, 0, &c0_big, 0, scratch_1); } + + fn encrypt_scratch_bytes(module: &Module, size: usize) -> usize { + (module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_big(1, size) + } +} + +impl Ciphertext +where + C: VecZnxToMut + ZnxInfos, +{ + pub fn encrypt_sk( + &mut self, + module: &Module, + pt: Option<&Plaintext

>, + sk: &SecretKey, + source_xa: &mut Source, + source_xe: &mut Source, + scratch: &mut Scratch, + sigma: f64, + bound: f64, + ) where + P: VecZnxToRef + ZnxInfos, + S: ScalarZnxDftToRef, + { + >::encrypt( + module, self, pt, sk, source_xa, source_xe, scratch, sigma, bound, + ); + } + + pub fn encrypt_sk_scratch_bytes

(module: &Module, size: usize) -> usize + where + Self: EncryptSk, + { + >::encrypt_scratch_bytes(module, size) + } } pub trait EncryptZeroSk { @@ -91,17 +127,12 @@ pub trait EncryptZeroSk { ) where S: ScalarZnxDftToRef; - fn encrypt_zero_tmp_bytes(module: &Module, 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() - } + fn encrypt_zero_scratch_bytes(module: &Module, size: usize) -> usize; } impl EncryptZeroSk for C where - C: VecZnxDftToMut + ZnxInfos + Infos, + C: VecZnxDftToMut + ZnxInfos + Infos, { fn encrypt_zero( module: &Module, @@ -146,4 +177,53 @@ where // ct[0] = DFT(-as + e) module.vec_znx_dft(&mut ct_mut, 0, &tmp_znx, 0); } + + fn encrypt_zero_scratch_bytes(module: &Module, 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() + } +} + +#[cfg(test)] +mod tests { + use base2k::{FFT64, Module, ScratchOwned, VecZnx, Scalar}; + use sampling::source::Source; + + use crate::{elem::{Ciphertext, Infos, Plaintext}, keys::SecretKey}; + + #[test] + fn encrypt_sk_vec_znx_fft64() { + let module: Module = Module::::new(32); + let log_base2k: usize = 8; + let log_q: usize = 54; + + let sigma: f64 = 3.2; + let bound: f64 = sigma * 6; + + let mut ct: Ciphertext>> = Ciphertext::>>::new(&module, log_base2k, log_q, 2); + let mut pt: Plaintext>> = Plaintext::>>::new(&module, log_base2k, log_q); + + let mut source_xe = Source::new([0u8; 32]); + let mut source_xa: Source = Source::new([0u8; 32]); + + + let mut scratch: ScratchOwned = ScratchOwned::new(ct.encrypt_encsk_scratch_bytes(&module, ct.size())); + + let mut sk: SecretKey>> = SecretKey::new(&module); + let mut sk_prep + sk.svp_prepare(&module, &mut sk_prep); + + ct.encrypt_sk( + &module, + Some(&pt), + &sk_prep, + &mut source_xa, + &mut source_xe, + scratch.borrow(), + sigma, + bound, + ); + } } diff --git a/rlwe/src/keys.rs b/rlwe/src/keys.rs index 77f1d9a..ee8bb94 100644 --- a/rlwe/src/keys.rs +++ b/rlwe/src/keys.rs @@ -1,6 +1,5 @@ use base2k::{ - Backend, FFT64, Module, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftOps, ScalarZnxDftToMut, Scratch, VecZnxDft, - VecZnxDftAlloc, VecZnxDftToMut, + Backend, Module, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScalarZnxDftToMut, Scratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftToMut, ZnxInfos, FFT64 }; use sampling::source::Source; @@ -43,6 +42,16 @@ impl SecretKey>> { } } +type SecretKeyPrep = SecretKey>; + +impl SecretKey, B>> { + pub fn new(module: &Module) -> Self{ + Self{ + data: module.new_scalar_znx_dft(1) + } + } +} + pub struct PublicKey { data: VecZnxDft, }