This commit is contained in:
Pro7ech
2025-10-13 12:14:11 +02:00
parent 662e533eac
commit cf377ff243
94 changed files with 1892 additions and 1235 deletions

View File

@@ -4,23 +4,25 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace},
AutomorphismKeyToRef, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepareTmpBytes, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedToMut, GLWESwitchingKeyPreparedToRef,
},
};
#[derive(PartialEq, Eq)]
pub struct GGLWEAutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWESwitchingKeyPrepared<D, B>,
pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GLWESwitchingKeyPrepared<D, B>,
pub(crate) p: i64,
}
impl<D: Data, B: Backend> GGLWEAutomorphismKeyPrepared<D, B> {
impl<D: Data, B: Backend> AutomorphismKeyPrepared<D, B> {
pub fn p(&self) -> i64 {
self.p
}
}
impl<D: Data, B: Backend> LWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
impl<D: Data, B: Backend> LWEInfos for AutomorphismKeyPrepared<D, B> {
fn n(&self) -> Degree {
self.key.n()
}
@@ -38,13 +40,23 @@ impl<D: Data, B: Backend> LWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
}
}
impl<D: Data, B: Backend> GLWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
pub trait SetP {
fn set_p(&mut self, p: i64);
}
impl<D: Data, B: Backend> SetP for AutomorphismKeyPrepared<D, B> {
fn set_p(&mut self, p: i64) {
self.p = p
}
}
impl<D: Data, B: Backend> GLWEInfos for AutomorphismKeyPrepared<D, B> {
fn rank(&self) -> Rank {
self.rank_out()
}
}
impl<D: Data, B: Backend> GGLWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
fn rank_in(&self) -> Rank {
self.key.rank_in()
}
@@ -62,7 +74,7 @@ impl<D: Data, B: Backend> GGLWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
}
}
impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self
where
A: GGLWEInfos,
@@ -73,8 +85,8 @@ impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
);
GGLWEAutomorphismKeyPrepared::<Vec<u8>, B> {
key: GGLWESwitchingKeyPrepared::alloc(module, infos),
AutomorphismKeyPrepared::<Vec<u8>, B> {
key: GLWESwitchingKeyPrepared::alloc(module, infos),
p: 0,
}
}
@@ -83,8 +95,8 @@ impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
where
Module<B>: VmpPMatAlloc<B>,
{
GGLWEAutomorphismKeyPrepared {
key: GGLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize),
AutomorphismKeyPrepared {
key: GLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
@@ -99,43 +111,104 @@ impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
);
GGLWESwitchingKeyPrepared::alloc_bytes(module, infos)
GLWESwitchingKeyPrepared::alloc_bytes(module, infos)
}
pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
Module<B>: VmpPMatAllocBytes,
{
GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize)
GLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize)
}
}
impl<B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GGLWEAutomorphismKeyPrepared<Vec<u8>, B>
pub trait AutomorphismKeyPrepareTmpBytes {
fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A)
where
A: GGLWEInfos;
}
impl<B: Backend> AutomorphismKeyPrepareTmpBytes for Module<B>
where
GGLWESwitchingKeyPrepared<Vec<u8>, B>: PrepareScratchSpace<B, A>,
Module<B>: GLWESwitchingKeyPrepareTmpBytes,
{
fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize {
GGLWESwitchingKeyPrepared::prepare_scratch_space(module, infos)
fn automorphism_key_prepare_tmp_bytes<A>(&self, infos: &A)
where
A: GGLWEInfos,
{
self.glwe_switching_key_prepare_tmp_bytes(infos)
}
}
impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWEAutomorphismKey<DR>> for GGLWEAutomorphismKeyPrepared<D, B>
where
Module<B>: VmpPrepare<B>,
{
fn prepare(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DR>, scratch: &mut Scratch<B>) {
self.key.prepare(module, &other.key, scratch);
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: &R, other: &O, scratch: &Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B>,
O: AutomorphismKeyToRef;
}
impl<B: Backend> AutomorphismKeyPrepare<B> for Module<B> {
fn automorphism_key_prepare<R, O>(&self, res: &R, other: &O, scratch: &Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B>,
O: AutomorphismKeyToRef,
{
self.key.prepare(self, &other.to_ref().key, scratch);
self.p = other.p;
}
}
impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> for GGLWEAutomorphismKey<D>
pub trait AutomorphismKeyPrepareAlloc<B: Backend> {
fn automorphism_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>) -> AutomorphismKeyPrepared<Vec<u8>, B>
where
O: AutomorphismKeyToRef;
}
impl<B: Backend> AutomorphismKeyPrepareAlloc<B> for Module<B>
where
Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
{
fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc(module, self);
atk_prepared.prepare(module, self, scratch);
fn automorphism_key_prepare_alloc<O>(&self, other: &O, scratch: &mut Scratch<B>) -> AutomorphismKeyPrepared<Vec<u8>, B>
where
O: AutomorphismKeyToRef,
{
let mut atk_prepared: AutomorphismKeyPrepared<Vec<u8>, B> = AutomorphismKeyPrepared::alloc(self, &other.to_ref());
atk_prepared.prepare(self, &other.to_ref(), scratch);
atk_prepared
}
}
pub trait AutomorphismKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> AutomorphismKeyPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
fn to_mut(&mut self) -> AutomorphismKeyPrepared<&mut [u8], B> {
AutomorphismKeyPrepared {
p: self.p,
key: self.key.to_mut(),
}
}
}
pub trait AutomorphismKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> AutomorphismKeyPreparedToRef<B> for AutomorphismKeyPrepared<D, B> {
fn to_ref(&self) -> AutomorphismKeyPrepared<&[u8], B> {
AutomorphismKeyPrepared {
p: self.p,
key: self.key.to_ref(),
}
}
}