This commit is contained in:
Pro7ech
2025-10-14 23:39:16 +02:00
parent 72dca47cbe
commit 779e02acc4
94 changed files with 784 additions and 1688 deletions

View File

@@ -1,13 +1,13 @@
use poulpy_hal::{
api::{TakeMatZnx, TakeScalarZnx, TakeSvpPPol, TakeVecZnx, TakeVecZnxDft, TakeVmpPMat},
layouts::{Backend, Scratch},
api::{ScratchAvailable, ScratchTakeBasic},
layouts::{Backend, Module, Scratch},
};
use crate::{
dist::Distribution,
layouts::{
AutomorphismKey, Degree, GGLWE, GGLWEInfos, GGSW, GGSWInfos, GLWE, GLWEInfos, GLWEPlaintext, GLWEPublicKey, GLWESecret,
GLWESwitchingKey, Rank, TensorKey,
GLWESwitchingKey, GetDegree, Rank, TensorKey,
prepared::{
AutomorphismKeyPrepared, GGLWEPrepared, GGSWPrepared, GLWEPublicKeyPrepared, GLWESecretPrepared,
GLWESwitchingKeyPrepared, TensorKeyPrepared,
@@ -15,119 +15,15 @@ use crate::{
},
};
pub trait TakeGLWECt {
fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self)
where
A: GLWEInfos;
}
pub trait TakeGLWECtSlice {
fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWE<&mut [u8]>>, &mut Self)
where
A: GLWEInfos;
}
pub trait TakeGLWEPt<B: Backend> {
fn take_glwe_pt<A>(&mut self, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self)
where
A: GLWEInfos;
}
pub trait TakeGGLWE {
fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self)
where
A: GGLWEInfos;
}
pub trait TakeGGLWEPrepared<B: Backend> {
fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos;
}
pub trait TakeGGSW {
fn take_ggsw<A>(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self)
where
A: GGSWInfos;
}
pub trait TakeGGSWPrepared<B: Backend> {
fn take_ggsw_prepared<A>(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self)
where
A: GGSWInfos;
}
pub trait TakeGGSWPreparedSlice<B: Backend> {
fn take_ggsw_prepared_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GGSWPrepared<&mut [u8], B>>, &mut Self)
where
A: GGSWInfos;
}
pub trait TakeGLWESecret {
fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self);
}
pub trait TakeGLWESecretPrepared<B: Backend> {
fn take_glwe_secret_prepared(&mut self, n: Degree, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self);
}
pub trait TakeGLWEPk {
fn take_glwe_pk<A>(&mut self, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self)
where
A: GLWEInfos;
}
pub trait TakeGLWEPkPrepared<B: Backend> {
fn take_glwe_pk_prepared<A>(&mut self, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GLWEInfos;
}
pub trait TakeGLWESwitchingKey {
fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self)
where
A: GGLWEInfos;
}
pub trait TakeGGLWESwitchingKeyPrepared<B: Backend> {
fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos;
}
pub trait TakeTensorKey {
fn take_tensor_key<A>(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self)
where
A: GGLWEInfos;
}
pub trait TakeGGLWETensorKeyPrepared<B: Backend> {
fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos;
}
pub trait TakeGGLWEAutomorphismKey {
fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self)
where
A: GGLWEInfos;
}
pub trait TakeGGLWEAutomorphismKeyPrepared<B: Backend> {
fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos;
}
impl<B: Backend> TakeGLWECt for Scratch<B>
pub trait ScratchTakeCore<B: Backend>
where
Scratch<B>: TakeVecZnx,
Self: ScratchTakeBasic<B> + ScratchAvailable,
{
fn take_glwe_ct<A>(&mut self, infos: &A) -> (GLWE<&mut [u8]>, &mut Self)
fn take_glwe_ct<A>(&mut self, module: &Module<B>, infos: &A) -> (GLWE<&mut [u8]>, &mut Self)
where
A: GLWEInfos,
{
let (data, scratch) = self.take_vec_znx(infos.n().into(), (infos.rank() + 1).into(), infos.size());
let (data, scratch) = self.take_vec_znx(module, (infos.rank() + 1).into(), infos.size());
(
GLWE {
k: infos.k(),
@@ -137,12 +33,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGLWECtSlice for Scratch<B>
where
Scratch<B>: TakeVecZnx,
{
fn take_glwe_ct_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GLWE<&mut [u8]>>, &mut Self)
where
A: GLWEInfos,
@@ -156,12 +47,7 @@ where
}
(cts, scratch)
}
}
impl<B: Backend> TakeGLWEPt<B> for Scratch<B>
where
Scratch<B>: TakeVecZnx,
{
fn take_glwe_pt<A>(&mut self, infos: &A) -> (GLWEPlaintext<&mut [u8]>, &mut Self)
where
A: GLWEInfos,
@@ -176,12 +62,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGGLWE for Scratch<B>
where
Scratch<B>: TakeMatZnx,
{
fn take_gglwe<A>(&mut self, infos: &A) -> (GGLWE<&mut [u8]>, &mut Self)
where
A: GGLWEInfos,
@@ -203,12 +84,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGGLWEPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVmpPMat<B>,
{
fn take_gglwe_prepared<A>(&mut self, infos: &A) -> (GGLWEPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos,
@@ -230,12 +106,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGGSW for Scratch<B>
where
Scratch<B>: TakeMatZnx,
{
fn take_ggsw<A>(&mut self, infos: &A) -> (GGSW<&mut [u8]>, &mut Self)
where
A: GGSWInfos,
@@ -257,12 +128,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGGSWPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVmpPMat<B>,
{
fn take_ggsw_prepared<A>(&mut self, infos: &A) -> (GGSWPrepared<&mut [u8], B>, &mut Self)
where
A: GGSWInfos,
@@ -284,12 +150,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGGSWPreparedSlice<B> for Scratch<B>
where
Scratch<B>: TakeGGSWPrepared<B>,
{
fn take_ggsw_prepared_slice<A>(&mut self, size: usize, infos: &A) -> (Vec<GGSWPrepared<&mut [u8], B>>, &mut Self)
where
A: GGSWInfos,
@@ -303,12 +164,7 @@ where
}
(cts, scratch)
}
}
impl<B: Backend> TakeGLWEPk for Scratch<B>
where
Scratch<B>: TakeVecZnx,
{
fn take_glwe_pk<A>(&mut self, infos: &A) -> (GLWEPublicKey<&mut [u8]>, &mut Self)
where
A: GLWEInfos,
@@ -324,12 +180,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGLWEPkPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVecZnxDft<B>,
{
fn take_glwe_pk_prepared<A>(&mut self, infos: &A) -> (GLWEPublicKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GLWEInfos,
@@ -345,12 +196,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGLWESecret for Scratch<B>
where
Scratch<B>: TakeScalarZnx,
{
fn take_glwe_secret(&mut self, n: Degree, rank: Rank) -> (GLWESecret<&mut [u8]>, &mut Self) {
let (data, scratch) = self.take_scalar_znx(n.into(), rank.into());
(
@@ -361,12 +207,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGLWESecretPrepared<B> for Scratch<B>
where
Scratch<B>: TakeSvpPPol<B>,
{
fn take_glwe_secret_prepared(&mut self, n: Degree, rank: Rank) -> (GLWESecretPrepared<&mut [u8], B>, &mut Self) {
let (data, scratch) = self.take_svp_ppol(n.into(), rank.into());
(
@@ -377,12 +218,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGLWESwitchingKey for Scratch<B>
where
Scratch<B>: TakeMatZnx,
{
fn take_glwe_switching_key<A>(&mut self, infos: &A) -> (GLWESwitchingKey<&mut [u8]>, &mut Self)
where
A: GGLWEInfos,
@@ -397,12 +233,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGGLWESwitchingKeyPrepared<B> for Scratch<B>
where
Scratch<B>: TakeGGLWEPrepared<B>,
{
fn take_gglwe_switching_key_prepared<A>(&mut self, infos: &A) -> (GLWESwitchingKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos,
@@ -417,12 +248,7 @@ where
scratch,
)
}
}
impl<B: Backend> TakeGGLWEAutomorphismKey for Scratch<B>
where
Scratch<B>: TakeMatZnx,
{
fn take_gglwe_automorphism_key<A>(&mut self, infos: &A) -> (AutomorphismKey<&mut [u8]>, &mut Self)
where
A: GGLWEInfos,
@@ -430,12 +256,7 @@ where
let (data, scratch) = self.take_glwe_switching_key(infos);
(AutomorphismKey { key: data, p: 0 }, scratch)
}
}
impl<B: Backend> TakeGGLWEAutomorphismKeyPrepared<B> for Scratch<B>
where
Scratch<B>: TakeGGLWESwitchingKeyPrepared<B>,
{
fn take_gglwe_automorphism_key_prepared<A>(&mut self, infos: &A) -> (AutomorphismKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos,
@@ -443,12 +264,7 @@ where
let (data, scratch) = self.take_gglwe_switching_key_prepared(infos);
(AutomorphismKeyPrepared { key: data, p: 0 }, scratch)
}
}
impl<B: Backend> TakeTensorKey for Scratch<B>
where
Scratch<B>: TakeMatZnx,
{
fn take_tensor_key<A>(&mut self, infos: &A) -> (TensorKey<&mut [u8]>, &mut Self)
where
A: GGLWEInfos,
@@ -478,12 +294,7 @@ where
}
(TensorKey { keys }, scratch)
}
}
impl<B: Backend> TakeGGLWETensorKeyPrepared<B> for Scratch<B>
where
Scratch<B>: TakeVmpPMat<B>,
{
fn take_gglwe_tensor_key_prepared<A>(&mut self, infos: &A) -> (TensorKeyPrepared<&mut [u8], B>, &mut Self)
where
A: GGLWEInfos,
@@ -515,3 +326,5 @@ where
(TensorKeyPrepared { keys }, scratch)
}
}
impl<B: Backend> ScratchTakeCore<B> for Scratch<B> where Self: ScratchTakeBasic<B> + ScratchAvailable {}