This commit is contained in:
Pro7ech
2025-10-13 23:49:09 +02:00
parent d32b4738c3
commit 0533cdff8a
27 changed files with 776 additions and 2022 deletions

View File

@@ -3,9 +3,8 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc,
GLWESwitchingKeyPreparedAllocBytes, GLWESwitchingKeyPreparedAllocBytesFromInfos, GLWESwitchingKeyPreparedAllocFromInfos,
GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef,
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
};
@@ -77,20 +76,9 @@ impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
}
}
pub trait AutomorphismKeyPreparedAlloc<B: Backend> {
fn automorphism_key_prepared_alloc(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> AutomorphismKeyPrepared<Vec<u8>, B>;
}
impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B>
pub trait AutomorphismKeyPreparedAlloc<B: Backend>
where
Module<B>: GLWESwitchingKeyPreparedAlloc<B>,
Self: GLWESwitchingKeyPreparedAlloc<B>,
{
fn automorphism_key_prepared_alloc(
&self,
@@ -105,18 +93,7 @@ where
p: 0,
}
}
}
pub trait AutomorphismKeyPreparedAllocFromInfos<B: Backend> {
fn automorphism_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
where
A: GGLWEInfos;
}
impl<B: Backend> AutomorphismKeyPreparedAllocFromInfos<B> for Module<B>
where
Module<B>: GLWESwitchingKeyPreparedAllocFromInfos<B>,
{
fn automorphism_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
where
A: GGLWEInfos,
@@ -126,28 +103,15 @@ where
infos.rank_out(),
"rank_in != rank_out is not supported for AutomorphismKeyPrepared"
);
AutomorphismKeyPrepared {
key: self.glwe_switching_key_prepared_alloc_from_infos(infos),
p: 0,
}
self.automorphism_key_prepared_alloc(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
pub trait AutomorphismKeyPreparedAllocBytes<B: Backend> {
fn automorphism_key_prepared_alloc_bytes(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize;
}
impl<B: Backend> AutomorphismKeyPreparedAllocBytes<B> for Module<B>
where
Module<B>: GLWESwitchingKeyPreparedAllocBytes<B>,
{
fn automorphism_key_prepared_alloc_bytes(
&self,
base2k: Base2K,
@@ -158,18 +122,7 @@ where
) -> usize {
self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank, rank, dnum, dsize)
}
}
pub trait AutomorphismKeyPreparedAllocBytesFromInfos<B: Backend> {
fn automorphism_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
}
impl<B: Backend> AutomorphismKeyPreparedAllocBytesFromInfos<B> for Module<B>
where
Module<B>: GLWESwitchingKeyPreparedAllocBytesFromInfos<B>,
{
fn automorphism_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
@@ -179,15 +132,23 @@ where
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
);
self.glwe_switching_key_prepared_alloc_bytes_from_infos(infos)
self.automorphism_key_prepared_alloc_bytes(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {}
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
Module<B>: AutomorphismKeyPreparedAllocFromInfos<B>,
Module<B>: AutomorphismKeyPreparedAlloc<B>,
{
module.automorphism_key_prepared_alloc_from_infos(infos)
}
@@ -202,28 +163,22 @@ impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: AutomorphismKeyPreparedAllocBytesFromInfos<B>,
Module<B>: AutomorphismKeyPreparedAlloc<B>,
{
module.automorphism_key_prepared_alloc_bytes_from_infos(infos)
}
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
Module<B>: AutomorphismKeyPreparedAllocBytes<B>,
Module<B>: AutomorphismKeyPreparedAlloc<B>,
{
module.automorphism_key_prepared_alloc_bytes(base2k, k, rank, dnum, dsize)
}
}
pub trait AutomorphismKeyPrepareTmpBytes {
fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
}
impl<B: Backend> AutomorphismKeyPrepareTmpBytes for Module<B>
pub trait AutomorphismKeyPrepare<B: Backend>
where
Module<B>: GLWESwitchingKeyPrepareTmpBytes,
Self: GLWESwitchingKeyPrepare<B>,
{
fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
where
@@ -231,28 +186,7 @@ where
{
self.glwe_switching_key_prepare_tmp_bytes(infos)
}
}
impl<D: DataRef, B: Backend> AutomorphismKeyPrepared<D, B> {
pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize
where
Module<B>: AutomorphismKeyPrepareTmpBytes,
{
module.automorphism_key_prepare_tmp_bytes(self)
}
}
pub trait AutomorphismKeyPrepare<B: Backend> {
fn automorphism_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement;
}
impl<B: Backend> AutomorphismKeyPrepare<B> for Module<B>
where
Module<B>: GLWESwitchingKeyPrepare<B>,
{
fn automorphism_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
@@ -263,6 +197,27 @@ where
}
}
impl<B: Backend> AutomorphismKeyPrepare<B> for Module<B> where Module<B>: GLWESwitchingKeyPrepare<B> {}
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes(&self, module: &Module<B>) -> usize
where
Module<B>: GLWESwitchingKeyPrepare<B>,
{
module.automorphism_key_prepare_tmp_bytes(self)
}
}
impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
pub fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
where
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
Module<B>: AutomorphismKeyPrepare<B>,
{
module.automorphism_key_prepare(self, other, scratch);
}
}
pub trait AutomorphismKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>;
}