Files
poulpy/poulpy-core/src/layouts/prepared/lwe_ksk.rs
2025-10-18 11:59:50 +02:00

217 lines
6.2 KiB
Rust

use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, RingDegree, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
};
#[derive(PartialEq, Eq)]
pub struct LWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> LWEInfos for LWESwitchingKeyPrepared<D, B> {
fn base2k(&self) -> Base2K {
self.0.base2k()
}
fn k(&self) -> TorusPrecision {
self.0.k()
}
fn n(&self) -> RingDegree {
self.0.n()
}
fn size(&self) -> usize {
self.0.size()
}
}
impl<D: Data, B: Backend> GLWEInfos for LWESwitchingKeyPrepared<D, B> {
fn rank(&self) -> Rank {
self.rank_out()
}
}
impl<D: Data, B: Backend> GGLWEInfos for LWESwitchingKeyPrepared<D, B> {
fn dsize(&self) -> Dsize {
self.0.dsize()
}
fn rank_in(&self) -> Rank {
self.0.rank_in()
}
fn rank_out(&self) -> Rank {
self.0.rank_out()
}
fn dnum(&self) -> Dnum {
self.0.dnum()
}
}
pub trait LWESwitchingKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
{
fn alloc_lwe_switching_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
dnum: Dnum,
) -> LWESwitchingKeyPrepared<Vec<u8>, B> {
LWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)))
}
fn alloc_lwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> LWESwitchingKeyPrepared<Vec<u8>, B>
where
A: GGLWEInfos,
{
debug_assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
debug_assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
debug_assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
self.alloc_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum())
}
fn bytes_of_lwe_switching_key_prepared(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
}
fn bytes_of_lwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
debug_assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
debug_assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
debug_assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
self.bytes_of_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum())
}
}
impl<B: Backend> LWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GLWESwitchingKeyPreparedAlloc<B> {}
impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: LWESwitchingKeyPreparedAlloc<B>,
{
module.alloc_lwe_switching_key_prepared_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self
where
M: LWESwitchingKeyPreparedAlloc<B>,
{
module.alloc_lwe_switching_key_prepared(base2k, k, dnum)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: LWESwitchingKeyPreparedAlloc<B>,
{
module.bytes_of_lwe_switching_key_prepared_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize
where
M: LWESwitchingKeyPreparedAlloc<B>,
{
module.bytes_of_lwe_switching_key_prepared(base2k, k, dnum)
}
}
pub trait LWESwitchingKeyPrepare<B: Backend>
where
Self: GLWESwitchingKeyPrepare<B>,
{
fn prepare_lwe_switching_key_tmp_bytes<A>(&self, infos: &A)
where
A: GGLWEInfos,
{
self.prepare_glwe_switching_key_tmp_bytes(infos);
}
fn prepare_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: LWESwitchingKeyPreparedToMut<B>,
O: LWESwitchingKeyToRef,
{
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
}
}
impl<B: Backend> LWESwitchingKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A)
where
A: GGLWEInfos,
M: LWESwitchingKeyPrepare<B>,
{
module.prepare_lwe_switching_key_tmp_bytes(infos);
}
}
impl<D: DataMut, B: Backend> LWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: LWESwitchingKeyToRef,
M: LWESwitchingKeyPrepare<B>,
{
module.prepare_lwe_switching_key(self, other, scratch);
}
}
pub trait LWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> LWESwitchingKeyPreparedToRef<B> for LWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
{
fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> {
LWESwitchingKeyPrepared(self.0.to_ref())
}
}
pub trait LWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> LWESwitchingKeyPreparedToMut<B> for LWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
{
fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> {
LWESwitchingKeyPrepared(self.0.to_mut())
}
}