Distinguish between gglwe_to_ggsw key and tensor_key + update key repreentation

This commit is contained in:
Pro7ech
2025-10-27 11:28:53 +01:00
parent 41ca5aafcc
commit 8d4c19a304
59 changed files with 2812 additions and 1596 deletions

View File

@@ -0,0 +1,252 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef,
GGLWEToGGSWKey, GGLWEToGGSWKeyToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
};
pub struct GGLWEToGGSWKeyPrepared<D: Data, BE: Backend> {
pub(crate) keys: Vec<GGLWEPrepared<D, BE>>,
}
impl<D: Data, BE: Backend> LWEInfos for GGLWEToGGSWKeyPrepared<D, BE> {
fn n(&self) -> Degree {
self.keys[0].n()
}
fn base2k(&self) -> Base2K {
self.keys[0].base2k()
}
fn k(&self) -> TorusPrecision {
self.keys[0].k()
}
fn size(&self) -> usize {
self.keys[0].size()
}
}
impl<D: Data, BE: Backend> GLWEInfos for GGLWEToGGSWKeyPrepared<D, BE> {
fn rank(&self) -> Rank {
self.keys[0].rank_out()
}
}
impl<D: Data, BE: Backend> GGLWEInfos for GGLWEToGGSWKeyPrepared<D, BE> {
fn rank_in(&self) -> Rank {
self.rank_out()
}
fn rank_out(&self) -> Rank {
self.keys[0].rank_out()
}
fn dsize(&self) -> Dsize {
self.keys[0].dsize()
}
fn dnum(&self) -> Dnum {
self.keys[0].dnum()
}
}
pub trait GGLWEToGGSWKeyPreparedFactory<BE: Backend> {
fn alloc_gglwe_to_ggsw_key_prepared_from_infos<A>(&self, infos: &A) -> GGLWEToGGSWKeyPrepared<Vec<u8>, BE>
where
A: GGLWEInfos;
fn alloc_gglwe_to_ggsw_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GGLWEToGGSWKeyPrepared<Vec<u8>, BE>;
fn bytes_of_gglwe_to_ggsw_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
fn bytes_of_gglwe_to_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize;
fn prepare_gglwe_to_ggsw_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
fn prepare_gglwe_to_ggsw_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<BE>)
where
R: GGLWEToGGSWKeyPreparedToMut<BE>,
O: GGLWEToGGSWKeyToRef;
}
impl<BE: Backend> GGLWEToGGSWKeyPreparedFactory<BE> for Module<BE>
where
Self: GGLWEPreparedFactory<BE>,
{
fn alloc_gglwe_to_ggsw_key_prepared_from_infos<A>(&self, infos: &A) -> GGLWEToGGSWKeyPrepared<Vec<u8>, BE>
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEToGGSWKeyPrepared"
);
self.alloc_gglwe_to_ggsw_key_prepared(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
fn alloc_gglwe_to_ggsw_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GGLWEToGGSWKeyPrepared<Vec<u8>, BE> {
GGLWEToGGSWKeyPrepared {
keys: (0..rank.as_usize())
.map(|_| self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize))
.collect(),
}
}
fn bytes_of_gglwe_to_ggsw_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEToGGSWKeyPrepared"
);
self.bytes_of_gglwe_to_ggsw(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
fn bytes_of_gglwe_to_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
rank.as_usize() * self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize)
}
fn prepare_gglwe_to_ggsw_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.prepare_gglwe_tmp_bytes(infos)
}
fn prepare_gglwe_to_ggsw_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<BE>)
where
R: GGLWEToGGSWKeyPreparedToMut<BE>,
O: GGLWEToGGSWKeyToRef,
{
let res: &mut GGLWEToGGSWKeyPrepared<&mut [u8], BE> = &mut res.to_mut();
let other: &GGLWEToGGSWKey<&[u8]> = &other.to_ref();
assert_eq!(res.keys.len(), other.keys.len());
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
self.prepare_gglwe(a, b, scratch);
}
}
}
impl<BE: Backend> GGLWEToGGSWKeyPrepared<Vec<u8>, BE> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: GGLWEToGGSWKeyPreparedFactory<BE>,
{
module.alloc_gglwe_to_ggsw_key_prepared_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
M: GGLWEToGGSWKeyPreparedFactory<BE>,
{
module.alloc_gglwe_to_ggsw_key_prepared(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: GGLWEToGGSWKeyPreparedFactory<BE>,
{
module.bytes_of_gglwe_to_ggsw_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: GGLWEToGGSWKeyPreparedFactory<BE>,
{
module.bytes_of_gglwe_to_ggsw(base2k, k, rank, dnum, dsize)
}
}
impl<D: DataMut, BE: Backend> GGLWEToGGSWKeyPrepared<D, BE> {
pub fn prepare<M, O>(&mut self, module: &M, other: &O, scratch: &mut Scratch<BE>)
where
M: GGLWEToGGSWKeyPreparedFactory<BE>,
O: GGLWEToGGSWKeyToRef,
{
module.prepare_gglwe_to_ggsw_key(self, other, scratch);
}
}
impl<D: DataMut, BE: Backend> GGLWEToGGSWKeyPrepared<D, BE> {
// Returns a mutable reference to GGLWEPrepared_{s}([s[i]*s[0], s[i]*s[1], ..., s[i]*s[rank]])
pub fn at_mut(&mut self, i: usize) -> &mut GGLWEPrepared<D, BE> {
assert!((i as u32) < self.rank());
&mut self.keys[i]
}
}
impl<D: DataRef, BE: Backend> GGLWEToGGSWKeyPrepared<D, BE> {
// Returns a reference to GGLWEPrepared_{s}([s[i]*s[0], s[i]*s[1], ..., s[i]*s[rank]])
pub fn at(&self, i: usize) -> &GGLWEPrepared<D, BE> {
assert!((i as u32) < self.rank());
&self.keys[i]
}
}
pub trait GGLWEToGGSWKeyPreparedToRef<BE: Backend> {
fn to_ref(&self) -> GGLWEToGGSWKeyPrepared<&[u8], BE>;
}
impl<D: DataRef, BE: Backend> GGLWEToGGSWKeyPreparedToRef<BE> for GGLWEToGGSWKeyPrepared<D, BE>
where
GGLWEPrepared<D, BE>: GGLWEPreparedToRef<BE>,
{
fn to_ref(&self) -> GGLWEToGGSWKeyPrepared<&[u8], BE> {
GGLWEToGGSWKeyPrepared {
keys: self.keys.iter().map(|c| c.to_ref()).collect(),
}
}
}
pub trait GGLWEToGGSWKeyPreparedToMut<BE: Backend> {
fn to_mut(&mut self) -> GGLWEToGGSWKeyPrepared<&mut [u8], BE>;
}
impl<D: DataMut, BE: Backend> GGLWEToGGSWKeyPreparedToMut<BE> for GGLWEToGGSWKeyPrepared<D, BE>
where
GGLWEPrepared<D, BE>: GGLWEPreparedToMut<BE>,
{
fn to_mut(&mut self) -> GGLWEToGGSWKeyPrepared<&mut [u8], BE> {
GGLWEToGGSWKeyPrepared {
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
}
}
}

View File

@@ -109,7 +109,7 @@ where
)
}
fn bytes_of_glwe_switching_key_prepared(
fn bytes_of_glwe_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
@@ -125,7 +125,7 @@ where
where
A: GGLWEInfos,
{
self.bytes_of_glwe_switching_key_prepared(
self.bytes_of_glwe_key_prepared(
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -199,7 +199,7 @@ impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
where
M: GLWESwitchingKeyPreparedFactory<B>,
{
module.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
module.bytes_of_glwe_key_prepared(base2k, k, rank_in, rank_out, dnum, dsize)
}
}

View File

@@ -2,29 +2,27 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef,
GLWEInfos, GLWETensorKey, GLWETensorKeyToRef, LWEInfos, Rank, TorusPrecision,
GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
};
#[derive(PartialEq, Eq)]
pub struct GLWETensorKeyPrepared<D: Data, B: Backend> {
pub(crate) keys: Vec<GGLWEPrepared<D, B>>,
}
pub struct GLWETensorKeyPrepared<D: Data, B: Backend>(pub(crate) GGLWEPrepared<D, B>);
impl<D: Data, B: Backend> LWEInfos for GLWETensorKeyPrepared<D, B> {
fn n(&self) -> Degree {
self.keys[0].n()
self.0.n()
}
fn base2k(&self) -> Base2K {
self.keys[0].base2k()
self.0.base2k()
}
fn k(&self) -> TorusPrecision {
self.keys[0].k()
self.0.k()
}
fn size(&self) -> usize {
self.keys[0].size()
self.0.size()
}
}
@@ -40,15 +38,15 @@ impl<D: Data, B: Backend> GGLWEInfos for GLWETensorKeyPrepared<D, B> {
}
fn rank_out(&self) -> Rank {
self.keys[0].rank_out()
self.0.rank_out()
}
fn dsize(&self) -> Dsize {
self.keys[0].dsize()
self.0.dsize()
}
fn dnum(&self) -> Dnum {
self.keys[0].dnum()
self.0.dnum()
}
}
@@ -65,11 +63,7 @@ where
rank: Rank,
) -> GLWETensorKeyPrepared<Vec<u8>, B> {
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
GLWETensorKeyPrepared {
keys: (0..pairs)
.map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
GLWETensorKeyPrepared(self.alloc_gglwe_prepared(base2k, k, Rank(pairs), rank, dnum, dsize))
}
fn alloc_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> GLWETensorKeyPrepared<Vec<u8>, B>
@@ -91,8 +85,8 @@ where
}
fn bytes_of_tensor_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let pairs: usize = (((rank.0 + 1) * rank.0) >> 1).max(1) as usize;
pairs * self.bytes_of_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize)
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
self.bytes_of_gglwe_prepared(base2k, k, Rank(pairs), rank, dnum, dsize)
}
fn bytes_of_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> usize
@@ -117,17 +111,10 @@ where
fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GLWETensorKeyPreparedToMut<B>,
O: GLWETensorKeyToRef,
R: GGLWEPreparedToMut<B>,
O: GGLWEToRef,
{
let mut res: GLWETensorKeyPrepared<&mut [u8], B> = res.to_mut();
let other: GLWETensorKey<&[u8]> = other.to_ref();
assert_eq!(res.keys.len(), other.keys.len());
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
self.prepare_gglwe(a, b, scratch);
}
self.prepare_gglwe(res, other, scratch);
}
}
@@ -165,28 +152,6 @@ impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> {
}
}
impl<D: DataMut, B: Backend> GLWETensorKeyPrepared<D, B> {
// Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEPrepared<D, B> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
let rank: usize = self.rank_out().into();
&mut self.keys[i * rank + j - (i * (i + 1) / 2)]
}
}
impl<D: DataRef, B: Backend> GLWETensorKeyPrepared<D, B> {
// Returns a reference to GGLWE_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared<D, B> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
let rank: usize = self.rank_out().into();
&self.keys[i * rank + j - (i * (i + 1) / 2)]
}
}
impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
where
@@ -200,39 +165,27 @@ impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> GLWETensorKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: GLWETensorKeyToRef,
O: GGLWEToRef,
M: GLWETensorKeyPreparedFactory<B>,
{
module.prepare_tensor_key(self, other, scratch);
}
}
pub trait GLWETensorKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWETensorKeyPreparedToMut<B> for GLWETensorKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWETensorKeyPrepared<D, B>
where
GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
{
fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B> {
GLWETensorKeyPrepared {
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
}
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
self.0.to_mut()
}
}
pub trait GLWETensorKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWETensorKeyPreparedToRef<B> for GLWETensorKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GLWETensorKeyPrepared<D, B>
where
GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B> {
GLWETensorKeyPrepared {
keys: self.keys.iter().map(|c| c.to_ref()).collect(),
}
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
self.0.to_ref()
}
}

View File

@@ -7,9 +7,9 @@ use crate::layouts::{
};
#[derive(PartialEq, Eq)]
pub struct GLWEToLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
pub struct GLWEToLWEKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> LWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
impl<D: Data, B: Backend> LWEInfos for GLWEToLWEKeyPrepared<D, B> {
fn base2k(&self) -> Base2K {
self.0.base2k()
}
@@ -27,13 +27,13 @@ impl<D: Data, B: Backend> LWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
}
}
impl<D: Data, B: Backend> GLWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
impl<D: Data, B: Backend> GLWEInfos for GLWEToLWEKeyPrepared<D, B> {
fn rank(&self) -> Rank {
self.rank_out()
}
}
impl<D: Data, B: Backend> GGLWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
impl<D: Data, B: Backend> GGLWEInfos for GLWEToLWEKeyPrepared<D, B> {
fn rank_in(&self) -> Rank {
self.0.rank_in()
}
@@ -51,65 +51,65 @@ impl<D: Data, B: Backend> GGLWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
}
}
pub trait GLWEToLWESwitchingKeyPreparedFactory<B: Backend>
pub trait GLWEToLWEKeyPreparedFactory<B: Backend>
where
Self: GLWESwitchingKeyPreparedFactory<B>,
{
fn alloc_glwe_to_lwe_switching_key_prepared(
fn alloc_glwe_to_lwe_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
) -> GLWEToLWEKeyPrepared<Vec<u8>, B> {
GLWEToLWEKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
}
fn alloc_glwe_to_lwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>
fn alloc_glwe_to_lwe_key_prepared_from_infos<A>(&self, infos: &A) -> GLWEToLWEKeyPrepared<Vec<u8>, B>
where
A: GGLWEInfos,
{
debug_assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
"rank_out > 1 is not supported for GLWEToLWEKeyPrepared"
);
debug_assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
"dsize > 1 is not supported for GLWEToLWEKeyPrepared"
);
self.alloc_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
self.alloc_glwe_to_lwe_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
fn bytes_of_glwe_to_lwe_switching_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
fn bytes_of_glwe_to_lwe_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
self.bytes_of_glwe_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
}
fn bytes_of_glwe_to_lwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> usize
fn bytes_of_glwe_to_lwe_key_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
debug_assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
"rank_out > 1 is not supported for GLWEToLWEKeyPrepared"
);
debug_assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
"dsize > 1 is not supported for GLWEToLWEKeyPrepared"
);
self.bytes_of_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
self.bytes_of_glwe_to_lwe_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
fn prepare_glwe_to_lwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize
fn prepare_glwe_to_lwe_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.prepare_glwe_switching_key_tmp_bytes(infos)
}
fn prepare_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
fn prepare_glwe_to_lwe_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
@@ -118,61 +118,61 @@ where
}
}
impl<B: Backend> GLWEToLWESwitchingKeyPreparedFactory<B> for Module<B> where Self: GLWESwitchingKeyPreparedFactory<B> {}
impl<B: Backend> GLWEToLWEKeyPreparedFactory<B> for Module<B> where Self: GLWESwitchingKeyPreparedFactory<B> {}
impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<B: Backend> GLWEToLWEKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyPreparedFactory<B>,
M: GLWEToLWEKeyPreparedFactory<B>,
{
module.alloc_glwe_to_lwe_switching_key_prepared_from_infos(infos)
module.alloc_glwe_to_lwe_key_prepared_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self
where
M: GLWEToLWESwitchingKeyPreparedFactory<B>,
M: GLWEToLWEKeyPreparedFactory<B>,
{
module.alloc_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum)
module.alloc_glwe_to_lwe_key_prepared(base2k, k, rank_in, dnum)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyPreparedFactory<B>,
M: GLWEToLWEKeyPreparedFactory<B>,
{
module.bytes_of_glwe_to_lwe_switching_key_prepared_from_infos(infos)
module.bytes_of_glwe_to_lwe_key_prepared_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize
where
M: GLWEToLWESwitchingKeyPreparedFactory<B>,
M: GLWEToLWEKeyPreparedFactory<B>,
{
module.bytes_of_glwe_to_lwe_switching_key_prepared(base2k, k, rank_in, dnum)
module.bytes_of_glwe_to_lwe_key_prepared(base2k, k, rank_in, dnum)
}
}
impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<B: Backend> GLWEToLWEKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A)
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyPreparedFactory<B>,
M: GLWEToLWEKeyPreparedFactory<B>,
{
module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos);
module.prepare_glwe_to_lwe_key_tmp_bytes(infos);
}
}
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
impl<D: DataMut, B: Backend> GLWEToLWEKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: GLWEToLWESwitchingKeyPreparedFactory<B>,
M: GLWEToLWEKeyPreparedFactory<B>,
{
module.prepare_glwe_to_lwe_switching_key(self, other, scratch);
module.prepare_glwe_to_lwe_key(self, other, scratch);
}
}
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GLWEToLWEKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
@@ -181,7 +181,7 @@ where
}
}
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWEToLWEKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
@@ -190,7 +190,7 @@ where
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKeyPrepared<D, B> {
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for GLWEToLWEKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
@@ -200,7 +200,7 @@ impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKe
}
}
impl<D: DataRef, B: Backend> GLWESwitchingKeyDegrees for GLWEToLWESwitchingKeyPrepared<D, B> {
impl<D: DataRef, B: Backend> GLWESwitchingKeyDegrees for GLWEToLWEKeyPrepared<D, B> {
fn input_degree(&self) -> &Degree {
&self.0.input_degree
}

View File

@@ -86,7 +86,7 @@ where
}
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))
self.bytes_of_glwe_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

View File

@@ -8,9 +8,9 @@ use crate::layouts::{
/// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE].
#[derive(PartialEq, Eq)]
pub struct LWEToGLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
pub struct LWEToGLWEKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
impl<D: Data, B: Backend> LWEInfos for LWEToGLWESwitchingKeyPrepared<D, B> {
impl<D: Data, B: Backend> LWEInfos for LWEToGLWEKeyPrepared<D, B> {
fn base2k(&self) -> Base2K {
self.0.base2k()
}
@@ -28,13 +28,13 @@ impl<D: Data, B: Backend> LWEInfos for LWEToGLWESwitchingKeyPrepared<D, B> {
}
}
impl<D: Data, B: Backend> GLWEInfos for LWEToGLWESwitchingKeyPrepared<D, B> {
impl<D: Data, B: Backend> GLWEInfos for LWEToGLWEKeyPrepared<D, B> {
fn rank(&self) -> Rank {
self.rank_out()
}
}
impl<D: Data, B: Backend> GGLWEInfos for LWEToGLWESwitchingKeyPrepared<D, B> {
impl<D: Data, B: Backend> GGLWEInfos for LWEToGLWEKeyPrepared<D, B> {
fn dsize(&self) -> Dsize {
self.0.dsize()
}
@@ -52,71 +52,65 @@ impl<D: Data, B: Backend> GGLWEInfos for LWEToGLWESwitchingKeyPrepared<D, B> {
}
}
pub trait LWEToGLWESwitchingKeyPreparedFactory<B: Backend>
pub trait LWEToGLWEKeyPreparedFactory<B: Backend>
where
Self: GLWESwitchingKeyPreparedFactory<B>,
{
fn alloc_lwe_to_glwe_switching_key_prepared(
fn alloc_lwe_to_glwe_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_out: Rank,
dnum: Dnum,
) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
LWEToGLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
) -> LWEToGLWEKeyPrepared<Vec<u8>, B> {
LWEToGLWEKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
}
fn alloc_lwe_to_glwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B>
fn alloc_lwe_to_glwe_key_prepared_from_infos<A>(&self, infos: &A) -> LWEToGLWEKeyPrepared<Vec<u8>, B>
where
A: GGLWEInfos,
{
debug_assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
"rank_in > 1 is not supported for LWEToGLWEKey"
);
debug_assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
"dsize > 1 is not supported for LWEToGLWEKey"
);
self.alloc_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
self.alloc_lwe_to_glwe_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
fn bytes_of_lwe_to_glwe_switching_key_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_out: Rank,
dnum: Dnum,
) -> usize {
self.bytes_of_glwe_switching_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1))
fn bytes_of_lwe_to_glwe_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
self.bytes_of_glwe_key_prepared(base2k, k, Rank(1), rank_out, dnum, Dsize(1))
}
fn bytes_of_lwe_to_glwe_switching_key_prepared_from_infos<A>(&self, infos: &A) -> usize
fn bytes_of_lwe_to_glwe_key_prepared_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
debug_assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
"rank_in > 1 is not supported for LWEToGLWEKey"
);
debug_assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
"dsize > 1 is not supported for LWEToGLWEKey"
);
self.bytes_of_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
self.bytes_of_lwe_to_glwe_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
fn prepare_lwe_to_glwe_switching_key_tmp_bytes<A>(&self, infos: &A)
fn prepare_lwe_to_glwe_key_tmp_bytes<A>(&self, infos: &A)
where
A: GGLWEInfos,
{
self.prepare_glwe_switching_key_tmp_bytes(infos);
}
fn prepare_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
fn prepare_lwe_to_glwe_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
@@ -125,61 +119,61 @@ where
}
}
impl<B: Backend> LWEToGLWESwitchingKeyPreparedFactory<B> for Module<B> where Self: GLWESwitchingKeyPreparedFactory<B> {}
impl<B: Backend> LWEToGLWEKeyPreparedFactory<B> for Module<B> where Self: GLWESwitchingKeyPreparedFactory<B> {}
impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<B: Backend> LWEToGLWEKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyPreparedFactory<B>,
M: LWEToGLWEKeyPreparedFactory<B>,
{
module.alloc_lwe_to_glwe_switching_key_prepared_from_infos(infos)
module.alloc_lwe_to_glwe_key_prepared_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self
where
M: LWEToGLWESwitchingKeyPreparedFactory<B>,
M: LWEToGLWEKeyPreparedFactory<B>,
{
module.alloc_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum)
module.alloc_lwe_to_glwe_key_prepared(base2k, k, rank_out, dnum)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyPreparedFactory<B>,
M: LWEToGLWEKeyPreparedFactory<B>,
{
module.bytes_of_lwe_to_glwe_switching_key_prepared_from_infos(infos)
module.bytes_of_lwe_to_glwe_key_prepared_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize
where
M: LWEToGLWESwitchingKeyPreparedFactory<B>,
M: LWEToGLWEKeyPreparedFactory<B>,
{
module.bytes_of_lwe_to_glwe_switching_key_prepared(base2k, k, rank_out, dnum)
module.bytes_of_lwe_to_glwe_key_prepared(base2k, k, rank_out, dnum)
}
}
impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<B: Backend> LWEToGLWEKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A)
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyPreparedFactory<B>,
M: LWEToGLWEKeyPreparedFactory<B>,
{
module.prepare_lwe_to_glwe_switching_key_tmp_bytes(infos);
module.prepare_lwe_to_glwe_key_tmp_bytes(infos);
}
}
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> {
impl<D: DataMut, B: Backend> LWEToGLWEKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: LWEToGLWESwitchingKeyPreparedFactory<B>,
M: LWEToGLWEKeyPreparedFactory<B>,
{
module.prepare_lwe_to_glwe_switching_key(self, other, scratch);
module.prepare_lwe_to_glwe_key(self, other, scratch);
}
}
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWEToGLWESwitchingKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWEToGLWEKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
@@ -188,7 +182,7 @@ where
}
}
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWEToGLWESwitchingKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWEToGLWEKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToMut<B>,
{
@@ -197,7 +191,7 @@ where
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKeyPrepared<D, B> {
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for LWEToGLWEKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}

View File

@@ -1,4 +1,5 @@
mod gglwe;
mod gglwe_to_ggsw_key;
mod ggsw;
mod glwe;
mod glwe_automorphism_key;
@@ -6,11 +7,12 @@ mod glwe_public_key;
mod glwe_secret;
mod glwe_switching_key;
mod glwe_tensor_key;
mod glwe_to_lwe_switching_key;
mod glwe_to_lwe_key;
mod lwe_switching_key;
mod lwe_to_glwe_switching_key;
mod lwe_to_glwe_key;
pub use gglwe::*;
pub use gglwe_to_ggsw_key::*;
pub use ggsw::*;
pub use glwe::*;
pub use glwe_automorphism_key::*;
@@ -18,6 +20,6 @@ pub use glwe_public_key::*;
pub use glwe_secret::*;
pub use glwe_switching_key::*;
pub use glwe_tensor_key::*;
pub use glwe_to_lwe_switching_key::*;
pub use glwe_to_lwe_key::*;
pub use lwe_switching_key::*;
pub use lwe_to_glwe_switching_key::*;
pub use lwe_to_glwe_key::*;