fix remaining issues before fixing tests

This commit is contained in:
Pro7ech
2025-10-18 11:59:50 +02:00
parent a282e88126
commit 0b8dcb1f16
34 changed files with 224 additions and 160 deletions

View File

@@ -38,9 +38,7 @@ impl<D: DataMut> GGSW<D> {
}
}
impl<BE: Backend> GGSWFromGGLWE<BE> for Module<BE> where Self: GGSWExpandRows<BE> + GLWECopy {}
pub trait GGSWFromGGLWE<BE: Backend>
impl<BE: Backend> GGSWFromGGLWE<BE> for Module<BE>
where
Self: GGSWExpandRows<BE> + GLWECopy,
{
@@ -77,6 +75,20 @@ where
}
}
pub trait GGSWFromGGLWE<BE: Backend> {
fn ggsw_from_gglwe_tmp_bytes<R, A>(&self, res_infos: &R, tsk_infos: &A) -> usize
where
R: GGSWInfos,
A: GGLWEInfos;
fn ggsw_from_gglwe<R, A, T>(&self, res: &mut R, a: &A, tsk: &T, scratch: &mut Scratch<BE>)
where
R: GGSWToMut,
A: GGLWEToRef,
T: TensorKeyPreparedToRef<BE>,
Scratch<BE>: ScratchTakeCore<BE>;
}
impl<BE: Backend> GGSWExpandRows<BE> for Module<BE> where
Self: Sized
+ ModuleN
@@ -99,7 +111,7 @@ impl<BE: Backend> GGSWExpandRows<BE> for Module<BE> where
{
}
pub(crate) trait GGSWExpandRows<BE: Backend>
pub trait GGSWExpandRows<BE: Backend>
where
Self: Sized
+ ModuleN

View File

@@ -2,4 +2,4 @@ mod glwe_ct;
mod lwe_ct;
pub use glwe_ct::*;
pub use lwe_ct::*;
// pub use lwe_ct::*;

View File

@@ -61,7 +61,7 @@ pub trait AutomorphismKeyCompressedEncryptSk<BE: Backend> {
impl<BE: Backend> AutomorphismKeyCompressedEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk<BE>,
Self: ModuleN + GaloisElement + VecZnxAutomorphism + GLWESwitchingKeyCompressedEncryptSk<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn automorphism_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -99,7 +99,7 @@ where
let (mut sk_out, scratch_1) = scratch.take_glwe_secret(self, sk.rank());
{
for i in 0..res.rank_out().into(){
for i in 0..res.rank_out().into() {
self.vec_znx_automorphism(
self.galois_element_inv(p),
&mut sk_out.data.as_vec_znx_mut(),
@@ -107,7 +107,7 @@ where
&sk.data.as_vec_znx(),
i,
);
};
}
}
self.glwe_switching_key_compressed_encrypt_sk(&mut res.key, sk, &sk_out, seed_xa, source_xe, scratch_1);

View File

@@ -67,7 +67,7 @@ pub trait GGLWECompressedEncryptSk<BE: Backend> {
impl<BE: Backend> GGLWECompressedEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN
Self: ModuleN
+ GLWEEncryptSkInternal<BE>
+ GLWEEncryptSk<BE>
+ VecZnxDftBytesOf

View File

@@ -64,7 +64,7 @@ pub trait GLWESwitchingKeyCompressedEncryptSk<BE: Backend> {
impl<BE: Backend> GLWESwitchingKeyCompressedEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN + GGLWECompressedEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + VecZnxSwitchRing,
Self: ModuleN + GGLWECompressedEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + VecZnxSwitchRing,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize

View File

@@ -1,9 +1,9 @@
use poulpy_hal::{
api::{
ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf,
VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolBytesOf, SvpPrepare, VecZnxBigBytesOf, VecZnxBigNormalize,
VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyTmpA,
},
layouts::{Backend, DataMut, DataRef, Module, Scratch},
layouts::{Backend, DataMut, Module, Scratch},
oep::{SvpPPolAllocBytesImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl},
source::Source,
};
@@ -13,7 +13,7 @@ use crate::{
encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk,
encryption::gglwe_tsk::TensorKeyEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank, TensorKey,
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank,
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
},
};
@@ -29,23 +29,22 @@ impl TensorKeyCompressed<Vec<u8>> {
}
impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
pub fn encrypt_sk<DataSk: DataRef, BE: Backend>(
pub fn encrypt_sk<S, M, BE: Backend>(
&mut self,
module: &Module<BE>,
sk: &GLWESecret<DataSk>,
module: &M,
sk: &S,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
GLWESecret<DataSk>: GetDist,
Module<BE>: GGLWETensorKeyCompressedEncryptSk<BE>,
S: GLWESecretToRef + GetDist,
M: GGLWETensorKeyCompressedEncryptSk<BE>,
{
module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch);
}
}
pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
@@ -64,7 +63,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
impl<BE: Backend> GGLWETensorKeyCompressedEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN
Self: ModuleN
+ GLWESwitchingKeyCompressedEncryptSk<BE>
+ TensorKeyEncryptSk<BE>
+ VecZnxDftApply<BE>
@@ -82,7 +81,7 @@ where
{
fn gglwe_tensor_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos
A: GGLWEInfos,
{
self.tensor_key_encrypt_sk_tmp_bytes(infos)
}
@@ -154,4 +153,4 @@ where
}
}
}
}
}

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{
api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace},
layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero},
layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero},
source::Source,
};
@@ -26,22 +26,23 @@ impl GGSWCompressed<Vec<u8>> {
impl<DataSelf: DataMut> GGSWCompressed<DataSelf> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, BE: Backend>(
pub fn encrypt_sk<P, S, M, BE: Backend>(
&mut self,
module: &Module<BE>,
pt: &ScalarZnx<DataPt>,
sk: &GLWESecretPrepared<DataSk, BE>,
module: &M,
pt: &P,
sk: &S,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
Module<BE>: GGSWCompressedEncryptSk<BE>,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>,
M: GGSWCompressedEncryptSk<BE>,
{
module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch);
}
}
pub trait GGSWCompressedEncryptSk<BE: Backend> {
fn ggsw_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
@@ -63,11 +64,12 @@ pub trait GGSWCompressedEncryptSk<BE: Backend> {
impl<BE: Backend> GGSWCompressedEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN + GLWEEncryptSkInternal<BE> + GGSWEncryptSk<BE> + VecZnxAddScalarInplace + VecZnxNormalizeInplace<BE>,
Self: ModuleN + GLWEEncryptSkInternal<BE> + GGSWEncryptSk<BE> + VecZnxAddScalarInplace + VecZnxNormalizeInplace<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn ggsw_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where A: GGSWInfos,
where
A: GGSWInfos,
{
self.ggsw_encrypt_sk_tmp_bytes(infos)
}
@@ -139,4 +141,4 @@ where
}
}
}
}
}

View File

@@ -1,15 +1,17 @@
use poulpy_hal::{
api::{VecZnxDftBytesOf, VecZnxNormalizeTmpBytes},
layouts::{Backend, DataMut, DataRef, Module, Scratch},
layouts::{Backend, DataMut, Module, Scratch},
source::Source,
};
use crate::{
encryption::{SIGMA, glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}},
encryption::{
SIGMA,
glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
},
layouts::{
GLWE, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos,
GLWEInfos, GLWEPlaintextToRef, LWEInfos,
compressed::{GLWECompressed, GLWECompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
prepared::GLWESecretPreparedToRef,
},
};
@@ -25,22 +27,23 @@ impl GLWECompressed<Vec<u8>> {
impl<D: DataMut> GLWECompressed<D> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, BE: Backend>(
pub fn encrypt_sk<M, P, S, BE: Backend>(
&mut self,
module: &Module<BE>,
pt: &GLWEPlaintext<DataPt>,
sk: &GLWESecretPrepared<DataSk, BE>,
module: &M,
pt: &P,
sk: &S,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
Module<BE>: GLWECompressedEncryptSk<BE>,
M: GLWECompressedEncryptSk<BE>,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<BE>,
{
module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch);
}
}
pub trait GLWECompressedEncryptSk<BE: Backend> {
fn glwe_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
@@ -62,9 +65,8 @@ pub trait GLWECompressedEncryptSk<BE: Backend> {
impl<BE: Backend> GLWECompressedEncryptSk<BE> for Module<BE>
where
Module<BE>: GLWEEncryptSkInternal<BE> + GLWEEncryptSk<BE>,
Self: GLWEEncryptSkInternal<BE> + GLWEEncryptSk<BE>,
{
fn glwe_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
@@ -105,4 +107,4 @@ where
res.seed = seed_xa;
}
}
}

View File

@@ -68,7 +68,7 @@ pub trait AutomorphismKeyEncryptSk<BE: Backend> {
impl<BE: Backend> AutomorphismKeyEncryptSk<BE> for Module<BE>
where
Module<BE>: GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphism + GaloisElement,
Self: GLWESwitchingKeyEncryptSk<BE> + VecZnxAutomorphism + GaloisElement,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn automorphism_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -137,7 +137,7 @@ pub trait GGLWEAutomorphismKeyEncryptPk<BE: Backend> {
impl<BE: Backend> GGLWEAutomorphismKeyEncryptPk<BE> for Module<BE>
where
Module<BE>:,
Self:,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn automorphism_key_encrypt_pk_tmp_bytes<A>(&self, _infos: &A) -> usize

View File

@@ -73,7 +73,7 @@ pub trait GGLWEEncryptSk<BE: Backend> {
impl<BE: Backend> GGLWEEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN
Self: ModuleN
+ GLWEEncryptSk<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxDftBytesOf

View File

@@ -66,8 +66,7 @@ pub trait GLWESwitchingKeyEncryptSk<BE: Backend> {
) where
R: GLWESwitchingKeyToMut,
S1: GLWESecretToRef,
S2: GLWESecretToRef,
Scratch<BE>: ScratchTakeCore<BE>;
S2: GLWESecretToRef;
}
impl<BE: Backend> GLWESwitchingKeyEncryptSk<BE> for Module<BE>

View File

@@ -1,7 +1,7 @@
use poulpy_hal::{
api::{
ModuleN, ScratchAvailable, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply,
VecZnxDftBytesOf, VecZnxIdftApplyTmpA,
ModuleN, ScratchTakeBasic, SvpApplyDftToDft, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf,
VecZnxIdftApplyTmpA,
},
layouts::{Backend, DataMut, Module, Scratch},
source::Source,
@@ -37,7 +37,7 @@ impl<DataSelf: DataMut> TensorKey<DataSelf> {
) where
M: TensorKeyEncryptSk<BE>,
S: GLWESecretToRef + GetDist,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch);
}
@@ -62,7 +62,7 @@ pub trait TensorKeyEncryptSk<BE: Backend> {
impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN
Self: ModuleN
+ GLWESwitchingKeyEncryptSk<BE>
+ VecZnxDftBytesOf
+ VecZnxBigBytesOf

View File

@@ -64,7 +64,7 @@ pub trait GGSWEncryptSk<BE: Backend> {
impl<BE: Backend> GGSWEncryptSk<BE> for Module<BE>
where
Module<BE>: ModuleN
Self: ModuleN
+ GLWEEncryptSkInternal<BE>
+ GLWEEncryptSk<BE>
+ VecZnxDftBytesOf

View File

@@ -38,7 +38,7 @@ impl GLWE<Vec<u8>> {
}
impl<D: DataMut> GLWE<D> {
pub fn encrypt_sk<R, P, S, M, BE: Backend>(
pub fn encrypt_sk<P, S, M, BE: Backend>(
&mut self,
module: &M,
pt: &P,
@@ -133,7 +133,7 @@ pub trait GLWEEncryptSk<BE: Backend> {
impl<BE: Backend> GLWEEncryptSk<BE> for Module<BE>
where
Module<BE>: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal<BE>,
Self: Sized + ModuleN + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + GLWEEncryptSkInternal<BE>,
Scratch<BE>: ScratchAvailable,
{
fn glwe_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
@@ -262,7 +262,7 @@ pub trait GLWEEncryptPk<BE: Backend> {
impl<BE: Backend> GLWEEncryptPk<BE> for Module<BE>
where
Module<BE>: GLWEEncryptPkInternal<BE> + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes,
Self: GLWEEncryptPkInternal<BE> + VecZnxDftBytesOf + SvpPPolBytesOf + VecZnxBigBytesOf + VecZnxNormalizeTmpBytes,
{
fn glwe_encrypt_pk_tmp_bytes<A>(&self, infos: &A) -> usize
where
@@ -330,7 +330,7 @@ pub(crate) trait GLWEEncryptPkInternal<BE: Backend> {
impl<BE: Backend> GLWEEncryptPkInternal<BE> for Module<BE>
where
Module<BE>: SvpPrepare<BE>
Self: SvpPrepare<BE>
+ SvpApplyDftToDft<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddNormal<BE>
@@ -445,7 +445,7 @@ pub(crate) trait GLWEEncryptSkInternal<BE: Backend> {
impl<BE: Backend> GLWEEncryptSkInternal<BE> for Module<BE>
where
Module<BE>: ModuleN
Self: ModuleN
+ VecZnxDftBytesOf
+ VecZnxBigNormalize<BE>
+ VecZnxDftApply<BE>
@@ -459,7 +459,7 @@ where
+ VecZnxAddNormal
+ VecZnxNormalize<BE>
+ VecZnxSub,
Scratch<BE>: ScratchAvailable + ScratchTakeBasic,
Scratch<BE>: ScratchTakeBasic,
{
fn glwe_encrypt_sk_internal<R, P, S>(
&self,

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{
api::{ScratchOwnedAlloc, ScratchOwnedBorrow},
layouts::{Backend, DataMut, DataRef, Module, Scratch, ScratchOwned},
layouts::{Backend, DataMut, Module, Scratch, ScratchOwned},
source::Source,
};
@@ -14,14 +14,10 @@ use crate::{
};
impl<D: DataMut> GLWEPublicKey<D> {
pub fn generate<S: DataRef, BE: Backend>(
&mut self,
module: &Module<BE>,
sk: &GLWESecretPrepared<S, BE>,
source_xa: &mut Source,
source_xe: &mut Source,
) where
Module<BE>: GLWEPublicKeyGenerate<BE>,
pub fn generate<S, M, BE: Backend>(&mut self, module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
where
S: GLWESecretPreparedToRef<BE>,
M: GLWEPublicKeyGenerate<BE>,
{
module.glwe_public_key_generate(self, sk, source_xa, source_xe);
}
@@ -36,7 +32,7 @@ pub trait GLWEPublicKeyGenerate<BE: Backend> {
impl<BE: Backend> GLWEPublicKeyGenerate<BE> for Module<BE>
where
Module<BE>: GLWEEncryptSk<BE>,
Self: GLWEEncryptSk<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{

View File

@@ -64,7 +64,7 @@ pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend> {
impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE>
where
Module<BE>: ModuleN + GLWESwitchingKeyEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + VecZnxAutomorphismInplace<BE>,
Self: ModuleN + GLWESwitchingKeyEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + VecZnxAutomorphismInplace<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize

View File

@@ -67,7 +67,7 @@ pub struct GLWESwitchingKey<D: Data> {
pub(crate) sk_out_n: usize, // Degree of sk_out
}
pub(crate) trait GLWESwitchingKeySetMetaData {
pub trait GLWESwitchingKeySetMetaData {
fn set_sk_in_n(&mut self, sk_in_n: usize);
fn set_sk_out_n(&mut self, sk_out_n: usize);
}
@@ -82,7 +82,7 @@ impl<D: DataMut> GLWESwitchingKeySetMetaData for GLWESwitchingKey<D> {
}
}
pub(crate) trait GLWESwtichingKeyGetMetaData {
pub trait GLWESwtichingKeyGetMetaData {
fn sk_in_n(&self) -> usize;
fn sk_out_n(&self) -> usize;
}

View File

@@ -8,7 +8,7 @@ use poulpy_hal::{
use crate::{
dist::Distribution,
layouts::{Base2K, GLWEInfos, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision},
layouts::{Base2K, GLWEInfos, GetDist, GetRingDegree, LWEInfos, Rank, RingDegree, TorusPrecision},
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -64,6 +64,12 @@ impl<D: Data> LWEInfos for GLWESecret<D> {
}
}
impl<D: Data> GetDist for GLWESecret<D> {
fn get_dist(&self) -> Distribution {
self.dist
}
}
impl<D: Data> GLWEInfos for GLWESecret<D> {
fn rank(&self) -> Rank {
Rank(self.data.cols() as u32)

View File

@@ -17,6 +17,7 @@ mod lwe_to_glwe_ksk;
pub mod compressed;
pub mod prepared;
pub use compressed::*;
pub use gglwe_atk::*;
pub use gglwe_ct::*;
pub use gglwe_ksk::*;
@@ -32,6 +33,7 @@ pub use lwe_ksk::*;
pub use lwe_pt::*;
pub use lwe_sk::*;
pub use lwe_to_glwe_ksk::*;
pub use prepared::*;
use poulpy_hal::layouts::{Backend, Module};

View File

@@ -198,7 +198,7 @@ where
impl<B: Backend> TensorKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: TensorKeyPrepare<B>,
@@ -208,7 +208,7 @@ impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
}
impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: TensorKeyToRef,
M: TensorKeyPrepare<B>,

View File

@@ -18,7 +18,7 @@ pub struct GLWEPublicKeyPrepared<D: Data, B: Backend> {
pub(crate) dist: Distribution,
}
pub(crate) trait SetDist {
pub trait SetDist {
fn set_dist(&mut self, dist: Distribution);
}

View File

@@ -63,7 +63,7 @@ where
self.alloc_glwe_secret_prepared(infos.rank())
}
fn bytes_of_glwe_secret(&self, rank: Rank) -> usize {
fn bytes_of_glwe_secret_prepared(&self, rank: Rank) -> usize {
self.bytes_of_svp_ppol(rank.into())
}
fn bytes_of_glwe_secret_prepared_from_infos<A>(&self, infos: &A) -> usize
@@ -71,7 +71,7 @@ where
A: GLWEInfos,
{
assert_eq!(self.ring_degree(), infos.n());
self.bytes_of_glwe_secret(infos.rank())
self.bytes_of_glwe_secret_prepared(infos.rank())
}
}
@@ -105,7 +105,7 @@ impl<B: Backend> GLWESecretPrepared<Vec<u8>, B> {
where
M: GLWESecretPreparedAlloc<B>,
{
module.bytes_of_glwe_secret(rank)
module.bytes_of_glwe_secret_prepared(rank)
}
}

View File

@@ -172,7 +172,7 @@ impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
}
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: GLWEToLWESwitchingKeyToRef,
M: GLWEToLWESwitchingKeyPrepare<B>,

View File

@@ -180,7 +180,7 @@ impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
}
impl<D: DataMut, B: Backend> LWESwitchingKeyPrepared<D, B> {
fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: LWESwitchingKeyToRef,
M: LWESwitchingKeyPrepare<B>,

View File

@@ -179,7 +179,7 @@ impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
}
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> {
fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: LWEToGLWESwitchingKeyToRef,
M: LWEToGLWESwitchingKeyPrepare<B>,

View File

@@ -1,19 +1,16 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace,
},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned, ScalarZnx, ScalarZnxToRef, ZnxZero},
api::{ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, VecZnxSubScalarInplace},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl},
};
use crate::decryption::GLWEDecryption;
use crate::layouts::{
GGLWE, GGLWEToRef, GGLWEInfos, GLWEPlaintext, LWEInfos,
GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
};
use crate::decryption::GLWEDecryption;
impl<D: DataRef> GGLWE<D> {
pub fn assert_noise<M, BE, DataSk, DataWant>(
&self,
module: &M,
@@ -24,12 +21,15 @@ impl<D: DataRef> GGLWE<D> {
DataSk: DataRef,
DataWant: DataRef,
M: GGLWENoise<BE>,
BE: Backend + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
BE: Backend
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>
+ VecZnxSubScalarInplaceImpl<BE>,
{
module.gglwe_assert_noise(self, sk_prepared, pt_want, max_noise);
}
// pub fn assert_noise<B, DataSk, DataWant>(
// &self,
// module: &Module<B>,
@@ -79,7 +79,6 @@ impl<D: DataRef> GGLWE<D> {
// }
}
pub trait GGLWENoise<BE: Backend> {
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
@@ -89,10 +88,14 @@ pub trait GGLWENoise<BE: Backend> {
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>;
}
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
impl<BE: Backend> GGLWENoise<BE> for Module<BE>
where
Module<BE>: GLWEDecryption<BE>,
Scratch<BE>: ScratchTakeBasic + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>,
{
fn gglwe_assert_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: f64)
where
@@ -101,9 +104,8 @@ where
P: ScalarZnxToRef,
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE> + VecZnxSubScalarInplaceImpl<BE>,
{
let res: &GGLWE<&[u8]> = &res.to_ref();
let dsize: usize = res.dsize().into();
let base2k: usize = res.base2k().into();
@@ -112,7 +114,12 @@ where
(0..res.rank_in().into()).for_each(|col_i| {
(0..res.dnum().into()).for_each(|row_i| {
self.glwe_decrypt(&res.at(row_i, col_i), &mut pt, sk_prepared, scratch.borrow());
self.glwe_decrypt(
&res.at(row_i, col_i),
&mut pt,
sk_prepared,
scratch.borrow(),
);
self.vec_znx_sub_scalar_inplace(&mut pt.data, 0, (dsize - 1) + row_i * dsize, pt_want, col_i);
@@ -129,4 +136,4 @@ where
});
});
}
}
}

View File

@@ -1,18 +1,17 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddScalarInplace, VecZnxBigAddInplace,
VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes,
VecZnxSubInplace,
ScratchTakeBasic,
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxAddScalarInplace,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize,
VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxIdftApplyTmpA, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
},
layouts::{Backend, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ScratchOwned, VecZnxBig, VecZnxDft, ZnxZero},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
};
use crate::layouts::{GGSW, GGSWInfos, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared, GGSWToRef};
use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::decryption::GLWEDecryption;
use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
impl<D: DataRef> GGSW<D> {
pub fn assert_noise<M, BE, DataSk, DataScalar, F>(
@@ -20,7 +19,7 @@ impl<D: DataRef> GGSW<D> {
module: &M,
sk_prepared: &GLWESecretPrepared<DataSk, BE>,
pt_want: &ScalarZnx<DataScalar>,
max_noise: F
max_noise: F,
) where
DataSk: DataRef,
DataScalar: DataRef,
@@ -91,7 +90,6 @@ where
BE: ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
F: Fn(usize) -> f64,
{
let res: &GGSW<&[u8]> = &res.to_ref();
let sk_prepared: &GLWESecretPrepared<&[u8], BE> = &sk_prepared.to_ref();
@@ -126,7 +124,12 @@ where
);
}
self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow());
self.glwe_decrypt(
&res.at(row_i, col_j),
&mut pt_have,
sk_prepared,
scratch.borrow(),
);
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
@@ -136,7 +139,7 @@ where
pt.data.zero();
});
});
});
}
fn ggsw_print_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P)
@@ -180,7 +183,12 @@ where
);
}
self.glwe_decrypt(&res.at(row_i, col_j), &mut pt_have, sk_prepared, scratch.borrow());
self.glwe_decrypt(
&res.at(row_i, col_j),
&mut pt_have,
sk_prepared,
scratch.borrow(),
);
self.vec_znx_sub_inplace(&mut pt_have.data, 0, &pt.data, 0);
let std_pt: f64 = pt_have.data.std(base2k, 0).log2();
@@ -188,5 +196,5 @@ where
pt.data.zero();
});
});
}
}
}
}

View File

@@ -1,8 +1,8 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxSubInplace, ScratchTakeBasic,
ScratchOwnedAlloc, ScratchOwnedBorrow, ScratchTakeBasic, SvpApplyDftToDftInplace, VecZnxBigAddInplace,
VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSubInplace,
},
layouts::{Backend, DataRef, Module, Scratch, ScratchOwned},
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
@@ -96,7 +96,6 @@ impl<D: DataRef> GLWE<D> {
// }
}
pub trait GLWENoise<BE: Backend> {
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
where
@@ -126,7 +125,11 @@ where
+ VecZnxSubInplace
+ VecZnxNormalizeInplace<BE>
+ GLWEDecryption<BE>,
Scratch<BE>: ScratchTakeBasic + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrowImpl<BE>
+ ScratchOwnedBorrow<BE>,
{
fn glwe_noise<R, S, P>(&self, res: &R, sk_prepared: &S, pt_want: &P, scratch: &mut Scratch<BE>) -> f64
where
@@ -157,4 +160,4 @@ where
let noise_have: f64 = self.glwe_noise(res, sk_prepared, pt_want, scratch.borrow());
assert!(noise_have <= max_noise, "{noise_have} {max_noise}");
}
}
}

View File

@@ -84,6 +84,11 @@ where
impl<BE: Backend> GLWEAdd for Module<BE> where Self: ModuleN + VecZnxAdd + VecZnxCopy + VecZnxAddInplace {}
impl<BE: Backend> GLWESub for Module<BE> where
Self: ModuleN + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxSubInplace + VecZnxSubNegateInplace
{
}
pub trait GLWESub
where
Self: ModuleN + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxSubInplace + VecZnxSubNegateInplace,