keyswitch tests

This commit is contained in:
Pro7ech
2025-10-20 15:32:52 +02:00
parent 0c894c19db
commit 252eda36fe
60 changed files with 918 additions and 945 deletions

View File

@@ -4,10 +4,9 @@ use poulpy_hal::{
};
use crate::layouts::{
AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut,
GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWECompressed, GLWECompressedToMut, GLWECompressedToRef,
GLWEDecompress, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -15,7 +14,7 @@ use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct AutomorphismKeyCompressed<D: Data> {
pub(crate) key: GLWESwitchingKeyCompressed<D>,
pub(crate) key: GGLWECompressed<D>,
pub(crate) p: i64,
}
@@ -102,7 +101,7 @@ impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
AutomorphismKeyCompressed {
key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
key: GGLWECompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
@@ -122,7 +121,7 @@ impl AutomorphismKeyCompressed<Vec<u8>> {
}
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize)
GGLWECompressed::bytes_of(n, base2k, k, rank, dnum, dsize)
}
}
@@ -142,19 +141,19 @@ impl<D: DataRef> WriterTo for AutomorphismKeyCompressed<D> {
pub trait AutomorphismKeyDecompress
where
Self: GLWESwitchingKeyDecompress,
Self: GGLWEDecompress,
{
fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O)
where
R: AutomorphismKeyToMut + SetAutomorphismGaloisElement,
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
R: GGLWEToMut + SetAutomorphismGaloisElement,
O: GGLWECompressedToRef + GetAutomorphismGaloisElement,
{
self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key);
self.decompress_gglwe(res, other);
res.set_p(other.p());
}
}
impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWEDecompress {}
impl<D: DataMut> AutomorphismKey<D>
where
@@ -162,20 +161,32 @@ where
{
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where
O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
O: GGLWECompressedToRef + GetAutomorphismGaloisElement,
M: AutomorphismKeyDecompress,
{
module.decompress_automorphism_key(self, other);
}
}
impl<D: DataRef> GGLWECompressedToRef for AutomorphismKeyCompressed<D> {
fn to_ref(&self) -> GGLWECompressed<&[u8]> {
self.key.to_ref()
}
}
impl<D: DataMut> GGLWECompressedToMut for AutomorphismKeyCompressed<D> {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
self.key.to_mut()
}
}
pub trait AutomorphismKeyCompressedToRef {
fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>;
}
impl<D: DataRef> AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
GLWECompressed<D>: GLWECompressedToRef,
{
fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> {
AutomorphismKeyCompressed {
@@ -191,7 +202,7 @@ pub trait AutomorphismKeyCompressedToMut {
impl<D: DataMut> AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
GLWECompressed<D>: GLWECompressedToMut,
{
fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> {
AutomorphismKeyCompressed {
@@ -200,3 +211,15 @@ where
}
}
}
impl<D: DataMut> GGLWECompressedSeedMut for AutomorphismKeyCompressed<D> {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> {
&mut self.key.seed
}
}
impl<D: DataMut> SetAutomorphismGaloisElement for AutomorphismKeyCompressed<D> {
fn set_p(&mut self, p: i64) {
self.p = p
}
}

View File

@@ -258,7 +258,8 @@ where
let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
let other: &GGLWECompressed<&[u8]> = &other.to_ref();
assert_eq!(res.gglwe_layout(), other.gglwe_layout());
assert_eq!(res.dsize(), other.dsize());
assert!(res.dnum() <= other.dnum());
let rank_in: usize = res.rank_in().into();
let dnum: usize = res.dnum().into();

View File

@@ -4,8 +4,8 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut,
LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWECompressedSeedMut, GGLWEInfos, GGLWEToMut, GLWEInfos, GLWESwitchingKey,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -14,8 +14,34 @@ use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct GLWESwitchingKeyCompressed<D: Data> {
pub(crate) key: GGLWECompressed<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out
pub(crate) input_degree: Degree, // Degree of sk_in
pub(crate) output_degree: Degree, // Degree of sk_out
}
impl<D: DataMut> GGLWECompressedSeedMut for GLWESwitchingKeyCompressed<D> {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> {
&mut self.key.seed
}
}
impl<D: DataRef> GLWESwitchingKeyDegrees for GLWESwitchingKeyCompressed<D> {
fn output_degree(&self) -> &Degree {
&self.output_degree
}
fn input_degree(&self) -> &Degree {
&self.input_degree
}
}
impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWESwitchingKeyCompressed<D> {
fn output_degree(&mut self) -> &mut Degree {
&mut self.output_degree
}
fn input_degree(&mut self) -> &mut Degree {
&mut self.input_degree
}
}
impl<D: Data> LWEInfos for GLWESwitchingKeyCompressed<D> {
@@ -76,7 +102,7 @@ impl<D: DataRef> fmt::Display for GLWESwitchingKeyCompressed<D> {
write!(
f,
"(GLWESwitchingKeyCompressed: sk_in_n={} sk_out_n={}) {}",
self.sk_in_n, self.sk_out_n, self.key.data
self.input_degree, self.output_degree, self.key.data
)
}
}
@@ -100,8 +126,8 @@ impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
GLWESwitchingKeyCompressed {
key: GGLWECompressed::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
input_degree: Degree(0),
output_degree: Degree(0),
}
}
@@ -120,16 +146,16 @@ where {
impl<D: DataMut> ReaderFrom for GLWESwitchingKeyCompressed<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize;
self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize;
self.input_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.output_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.key.read_from(reader)
}
}
impl<D: DataRef> WriterTo for GLWESwitchingKeyCompressed<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?;
writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?;
writer.write_u32::<LittleEndian>(self.input_degree.into())?;
writer.write_u32::<LittleEndian>(self.output_degree.into())?;
self.key.write_to(writer)
}
}
@@ -140,13 +166,13 @@ where
{
fn decompress_glwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where
R: GLWESwitchingKeyToMut + GLWESwitchingKeySetMetaData,
O: GLWESwitchingKeyCompressedToRef,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{
let other: &GLWESwitchingKeyCompressed<&[u8]> = &other.to_ref();
self.decompress_gglwe(&mut res.to_mut().key, &other.key);
res.set_sk_in_n(other.sk_in_n);
res.set_sk_out_n(other.sk_out_n);
self.decompress_gglwe(res, other);
*res.input_degree() = *other.input_degree();
*res.output_degree() = *other.output_degree();
}
}
@@ -155,43 +181,21 @@ impl<B: Backend> GLWESwitchingKeyDecompress for Module<B> where Self: GGLWEDecom
impl<D: DataMut> GLWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where
O: GLWESwitchingKeyCompressedToRef,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: GLWESwitchingKeyDecompress,
{
module.decompress_glwe_switching_key(self, other);
}
}
pub trait GLWESwitchingKeyCompressedToMut {
fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]>;
}
impl<D: DataMut> GLWESwitchingKeyCompressedToMut for GLWESwitchingKeyCompressed<D>
where
GGLWECompressed<D>: GGLWECompressedToMut,
{
fn to_mut(&mut self) -> GLWESwitchingKeyCompressed<&mut [u8]> {
GLWESwitchingKeyCompressed {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_mut(),
}
impl<D: DataMut> GGLWECompressedToMut for GLWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
self.key.to_mut()
}
}
pub trait GLWESwitchingKeyCompressedToRef {
fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]>;
}
impl<D: DataRef> GLWESwitchingKeyCompressedToRef for GLWESwitchingKeyCompressed<D>
where
GGLWECompressed<D>: GGLWECompressedToRef,
{
fn to_ref(&self) -> GLWESwitchingKeyCompressed<&[u8]> {
GLWESwitchingKeyCompressed {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_ref(),
}
impl<D: DataRef> GGLWECompressedToRef for GLWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> GGLWECompressed<&[u8]> {
self.key.to_ref()
}
}

View File

@@ -4,17 +4,15 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos,
GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct TensorKeyCompressed<D: Data> {
pub(crate) keys: Vec<GLWESwitchingKeyCompressed<D>>,
pub(crate) keys: Vec<GGLWECompressed<D>>,
}
impl<D: Data> LWEInfos for TensorKeyCompressed<D> {
@@ -67,7 +65,7 @@ impl<D: DataMut> FillUniform for TensorKeyCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.keys
.iter_mut()
.for_each(|key: &mut GLWESwitchingKeyCompressed<D>| key.fill_uniform(log_bound, source))
.for_each(|key: &mut GGLWECompressed<D>| key.fill_uniform(log_bound, source))
}
}
@@ -100,7 +98,7 @@ impl TensorKeyCompressed<Vec<u8>> {
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyCompressed {
keys: (0..pairs)
.map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.map(|_| GGLWECompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
}
@@ -121,7 +119,7 @@ impl TensorKeyCompressed<Vec<u8>> {
pub fn bytes_of(n: Degree, 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 * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize)
pairs * GGLWECompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize)
}
}
@@ -151,8 +149,26 @@ impl<D: DataRef> WriterTo for TensorKeyCompressed<D> {
}
}
impl<D: DataMut> TensorKeyCompressed<D> {
pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyCompressed<D> {
pub trait TensorKeyCompressedAtRef<D: DataRef> {
fn at(&self, i: usize, j: usize) -> &GGLWECompressed<D>;
}
impl<D: DataRef> TensorKeyCompressedAtRef<D> for TensorKeyCompressed<D> {
fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed<D> {
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)]
}
}
pub trait TensorKeyCompressedAtMut<D: DataMut> {
fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed<D>;
}
impl<D: DataMut> TensorKeyCompressedAtMut<D> for TensorKeyCompressed<D> {
fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed<D> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -163,7 +179,7 @@ impl<D: DataMut> TensorKeyCompressed<D> {
pub trait TensorKeyDecompress
where
Self: GLWESwitchingKeyDecompress,
Self: GGLWEDecompress,
{
fn decompress_tensor_key<R, O>(&self, res: &mut R, other: &O)
where
@@ -182,12 +198,12 @@ where
);
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
self.decompress_glwe_switching_key(a, b);
self.decompress_gglwe(a, b);
}
}
}
impl<B: Backend> TensorKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
impl<B: Backend> TensorKeyDecompress for Module<B> where Self: GGLWEDecompress {}
impl<D: DataMut> TensorKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
@@ -205,7 +221,7 @@ pub trait TensorKeyCompressedToMut {
impl<D: DataMut> TensorKeyCompressedToMut for TensorKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
GGLWECompressed<D>: GGLWECompressedToMut,
{
fn to_mut(&mut self) -> TensorKeyCompressed<&mut [u8]> {
TensorKeyCompressed {
@@ -220,7 +236,7 @@ pub trait TensorKeyCompressedToRef {
impl<D: DataRef> TensorKeyCompressedToRef for TensorKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
GGLWECompressed<D>: GGLWECompressedToRef,
{
fn to_ref(&self) -> TensorKeyCompressed<&[u8]> {
TensorKeyCompressed {

View File

@@ -138,7 +138,7 @@ impl GGSWCompressed<Vec<u8>> {
base2k,
dsize,
rank,
seed: Vec::new(),
seed: vec![[0u8; 32]; dnum.as_usize() * (rank.as_usize() + 1)],
}
}
@@ -260,7 +260,7 @@ where
}
}
impl<B: Backend> GGSWDecompress for Module<B> where Self: GGSWDecompress {}
impl<B: Backend> GGSWDecompress for Module<B> where Self: GLWEDecompress {}
impl<D: DataMut> GGSW<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O)

View File

@@ -158,7 +158,6 @@ where
self.ring_degree()
);
assert_eq!(res.lwe_layout(), other.lwe_layout());
assert_eq!(res.glwe_layout(), other.glwe_layout());
let mut source: Source = Source::new(other.seed);

View File

@@ -6,11 +6,9 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank,
TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, GLWEToLWESwitchingKey, LWEInfos, Rank, TorusPrecision,
compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
};
#[derive(PartialEq, Eq, Clone)]
@@ -151,10 +149,10 @@ where
{
fn decompress_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where
R: GLWEToLWESwitchingKeyToMut,
O: GLWEToLWESwitchingKeyCompressedToRef,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{
self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0);
self.decompress_glwe_switching_key(res, other);
}
}
@@ -163,35 +161,21 @@ impl<B: Backend> GLWEToLWESwitchingKeyDecompress for Module<B> where Self: GLWES
impl<D: DataMut> GLWEToLWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where
O: GLWEToLWESwitchingKeyCompressedToRef,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: GLWEToLWESwitchingKeyDecompress,
{
module.decompress_glwe_to_lwe_switching_key(self, other);
}
}
pub trait GLWEToLWESwitchingKeyCompressedToRef {
fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]>;
}
impl<D: DataRef> GLWEToLWESwitchingKeyCompressedToRef for GLWEToLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{
fn to_ref(&self) -> GLWEToLWESwitchingKeyCompressed<&[u8]> {
GLWEToLWESwitchingKeyCompressed(self.0.to_ref())
impl<D: DataRef> GGLWECompressedToRef for GLWEToLWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> GGLWECompressed<&[u8]> {
self.0.to_ref()
}
}
pub trait GLWEToLWESwitchingKeyCompressedToMut {
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]>;
}
impl<D: DataMut> GLWEToLWESwitchingKeyCompressedToMut for GLWEToLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyCompressed<&mut [u8]> {
GLWEToLWESwitchingKeyCompressed(self.0.to_mut())
impl<D: DataMut> GGLWECompressedToMut for GLWEToLWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
self.0.to_mut()
}
}

View File

@@ -4,10 +4,9 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWESwitchingKey, Rank, TorusPrecision,
compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
};
use std::fmt;
@@ -152,10 +151,10 @@ where
{
fn decompress_lwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where
R: LWESwitchingKeyToMut,
O: LWESwitchingKeyCompressedToRef,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{
self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0);
self.decompress_glwe_switching_key(res, other);
}
}
@@ -164,35 +163,21 @@ impl<B: Backend> LWESwitchingKeyDecompress for Module<B> where Self: GLWESwitchi
impl<D: DataMut> LWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where
O: LWESwitchingKeyCompressedToRef,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: LWESwitchingKeyDecompress,
{
module.decompress_lwe_switching_key(self, other);
}
}
pub trait LWESwitchingKeyCompressedToRef {
fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]>;
}
impl<D: DataRef> LWESwitchingKeyCompressedToRef for LWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{
fn to_ref(&self) -> LWESwitchingKeyCompressed<&[u8]> {
LWESwitchingKeyCompressed(self.0.to_ref())
impl<D: DataRef> GGLWECompressedToRef for LWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> GGLWECompressed<&[u8]> {
self.0.to_ref()
}
}
pub trait LWESwitchingKeyCompressedToMut {
fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]>;
}
impl<D: DataMut> LWESwitchingKeyCompressedToMut for LWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{
fn to_mut(&mut self) -> LWESwitchingKeyCompressed<&mut [u8]> {
LWESwitchingKeyCompressed(self.0.to_mut())
impl<D: DataMut> GGLWECompressedToMut for LWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
self.0.to_mut()
}
}

View File

@@ -4,11 +4,9 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank,
TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEInfos, GGLWEToMut, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision,
compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyDecompress},
};
use std::fmt;
@@ -149,10 +147,10 @@ where
{
fn decompress_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O)
where
R: LWEToGLWESwitchingKeyToMut,
O: LWEToGLWESwitchingKeyCompressedToRef,
R: GGLWEToMut + GLWESwitchingKeyDegreesMut,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
{
self.decompress_glwe_switching_key(&mut res.to_mut().0, &other.to_ref().0);
self.decompress_glwe_switching_key(res, other);
}
}
@@ -161,35 +159,21 @@ impl<B: Backend> LWEToGLWESwitchingKeyDecompress for Module<B> where Self: GLWES
impl<D: DataMut> LWEToGLWESwitchingKey<D> {
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
where
O: LWEToGLWESwitchingKeyCompressedToRef,
O: GGLWECompressedToRef + GLWESwitchingKeyDegrees,
M: LWEToGLWESwitchingKeyDecompress,
{
module.decompress_lwe_to_glwe_switching_key(self, other);
}
}
pub trait LWEToGLWESwitchingKeyCompressedToRef {
fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]>;
}
impl<D: DataRef> LWEToGLWESwitchingKeyCompressedToRef for LWEToGLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
{
fn to_ref(&self) -> LWEToGLWESwitchingKeyCompressed<&[u8]> {
LWEToGLWESwitchingKeyCompressed(self.0.to_ref())
impl<D: DataRef> GGLWECompressedToRef for LWEToGLWESwitchingKeyCompressed<D> {
fn to_ref(&self) -> GGLWECompressed<&[u8]> {
self.0.to_ref()
}
}
pub trait LWEToGLWESwitchingKeyCompressedToMut {
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]>;
}
impl<D: DataMut> LWEToGLWESwitchingKeyCompressedToMut for LWEToGLWESwitchingKeyCompressed<D>
where
GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
{
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyCompressed<&mut [u8]> {
LWEToGLWESwitchingKeyCompressed(self.0.to_mut())
impl<D: DataMut> GGLWECompressedToMut for LWEToGLWESwitchingKeyCompressed<D> {
fn to_mut(&mut self) -> GGLWECompressed<&mut [u8]> {
self.0.to_mut()
}
}

View File

@@ -4,8 +4,7 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef,
LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision,
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -24,7 +23,7 @@ pub struct AutomorphismKeyLayout {
#[derive(PartialEq, Eq, Clone)]
pub struct AutomorphismKey<D: Data> {
pub(crate) key: GLWESwitchingKey<D>,
pub(crate) key: GGLWE<D>,
pub(crate) p: i64,
}
@@ -161,7 +160,7 @@ impl AutomorphismKey<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
AutomorphismKey {
key: GLWESwitchingKey::alloc(n, base2k, k, rank, rank, dnum, dsize),
key: GGLWE::alloc(n, base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
@@ -186,39 +185,19 @@ impl AutomorphismKey<Vec<u8>> {
}
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
GLWESwitchingKey::bytes_of(n, base2k, k, rank, rank, dnum, dsize)
GGLWE::bytes_of(n, base2k, k, rank, rank, dnum, dsize)
}
}
pub trait AutomorphismKeyToMut {
fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]>;
}
impl<D: DataMut> AutomorphismKeyToMut for AutomorphismKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> AutomorphismKey<&mut [u8]> {
AutomorphismKey {
key: self.key.to_mut(),
p: self.p,
}
impl<D: DataMut> GGLWEToMut for AutomorphismKey<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
self.key.to_mut()
}
}
pub trait AutomorphismKeyToRef {
fn to_ref(&self) -> AutomorphismKey<&[u8]>;
}
impl<D: DataRef> AutomorphismKeyToRef for AutomorphismKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> AutomorphismKey<&[u8]> {
AutomorphismKey {
p: self.p,
key: self.key.to_ref(),
}
impl<D: DataMut> GGLWEToRef for AutomorphismKey<D> {
fn to_ref(&self) -> GGLWE<&[u8]> {
self.key.to_ref()
}
}

View File

@@ -62,37 +62,37 @@ impl GGLWEInfos for GLWESwitchingKeyLayout {
#[derive(PartialEq, Eq, Clone)]
pub struct GLWESwitchingKey<D: Data> {
pub(crate) key: GGLWE<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out
pub(crate) input_degree: Degree, // Degree of sk_in
pub(crate) output_degree: Degree, // Degree of sk_out
}
pub trait GLWESwitchingKeySetMetaData {
fn set_sk_in_n(&mut self, sk_in_n: usize);
fn set_sk_out_n(&mut self, sk_out_n: usize);
pub trait GLWESwitchingKeyDegrees {
fn input_degree(&self) -> &Degree;
fn output_degree(&self) -> &Degree;
}
impl<D: DataMut> GLWESwitchingKeySetMetaData for GLWESwitchingKey<D> {
fn set_sk_in_n(&mut self, sk_in_n: usize) {
self.sk_in_n = sk_in_n
impl<D: DataRef> GLWESwitchingKeyDegrees for GLWESwitchingKey<D> {
fn output_degree(&self) -> &Degree {
&self.output_degree
}
fn set_sk_out_n(&mut self, sk_out_n: usize) {
self.sk_out_n = sk_out_n
fn input_degree(&self) -> &Degree {
&self.input_degree
}
}
pub trait GLWESwtichingKeyGetMetaData {
fn sk_in_n(&self) -> usize;
fn sk_out_n(&self) -> usize;
pub trait GLWESwitchingKeyDegreesMut {
fn input_degree(&mut self) -> &mut Degree;
fn output_degree(&mut self) -> &mut Degree;
}
impl<D: DataRef> GLWESwtichingKeyGetMetaData for GLWESwitchingKey<D> {
fn sk_in_n(&self) -> usize {
self.sk_in_n
impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWESwitchingKey<D> {
fn output_degree(&mut self) -> &mut Degree {
&mut self.output_degree
}
fn sk_out_n(&self) -> usize {
self.sk_out_n
fn input_degree(&mut self) -> &mut Degree {
&mut self.input_degree
}
}
@@ -149,8 +149,8 @@ impl<D: DataRef> fmt::Display for GLWESwitchingKey<D> {
write!(
f,
"(GLWESwitchingKey: sk_in_n={} sk_out_n={}) {}",
self.sk_in_n,
self.sk_out_n,
self.input_degree,
self.output_degree,
self.key.data()
)
}
@@ -181,8 +181,8 @@ impl GLWESwitchingKey<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
GLWESwitchingKey {
key: GGLWE::alloc(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
input_degree: Degree(0),
output_degree: Degree(0),
}
}
@@ -214,37 +214,15 @@ impl GLWESwitchingKey<Vec<u8>> {
}
}
pub trait GLWESwitchingKeyToMut {
fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> GLWESwitchingKeyToMut for GLWESwitchingKey<D>
where
GGLWE<D>: GGLWEToMut,
{
fn to_mut(&mut self) -> GLWESwitchingKey<&mut [u8]> {
GLWESwitchingKey {
key: self.key.to_mut(),
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
}
impl<D: DataMut> GGLWEToMut for GLWESwitchingKey<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
self.key.to_mut()
}
}
pub trait GLWESwitchingKeyToRef {
fn to_ref(&self) -> GLWESwitchingKey<&[u8]>;
}
impl<D: DataRef> GLWESwitchingKeyToRef for GLWESwitchingKey<D>
where
GGLWE<D>: GGLWEToRef,
{
fn to_ref(&self) -> GLWESwitchingKey<&[u8]> {
GLWESwitchingKey {
key: self.key.to_ref(),
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
}
impl<D: DataRef> GGLWEToRef for GLWESwitchingKey<D> {
fn to_ref(&self) -> GGLWE<&[u8]> {
self.key.to_ref()
}
}
@@ -262,16 +240,16 @@ impl<D: DataMut> GLWESwitchingKey<D> {
impl<D: DataMut> ReaderFrom for GLWESwitchingKey<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.sk_in_n = reader.read_u64::<LittleEndian>()? as usize;
self.sk_out_n = reader.read_u64::<LittleEndian>()? as usize;
self.input_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.output_degree = Degree(reader.read_u32::<LittleEndian>()? as u32);
self.key.read_from(reader)
}
}
impl<D: DataRef> WriterTo for GLWESwitchingKey<D> {
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
writer.write_u64::<LittleEndian>(self.sk_in_n as u64)?;
writer.write_u64::<LittleEndian>(self.sk_out_n as u64)?;
writer.write_u32::<LittleEndian>(self.input_degree.into())?;
writer.write_u32::<LittleEndian>(self.output_degree.into())?;
self.key.write_to(writer)
}
}

View File

@@ -4,8 +4,7 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -23,7 +22,7 @@ pub struct TensorKeyLayout {
#[derive(PartialEq, Eq, Clone)]
pub struct TensorKey<D: Data> {
pub(crate) keys: Vec<GLWESwitchingKey<D>>,
pub(crate) keys: Vec<GGLWE<D>>,
}
impl<D: Data> LWEInfos for TensorKey<D> {
@@ -116,7 +115,7 @@ impl<D: DataMut> FillUniform for TensorKey<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.keys
.iter_mut()
.for_each(|key: &mut GLWESwitchingKey<D>| key.fill_uniform(log_bound, source))
.for_each(|key: &mut GGLWE<D>| key.fill_uniform(log_bound, source))
}
}
@@ -154,7 +153,7 @@ impl TensorKey<Vec<u8>> {
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
TensorKey {
keys: (0..pairs)
.map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.map(|_| GGLWE::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
}
@@ -180,13 +179,13 @@ impl TensorKey<Vec<u8>> {
pub fn bytes_of(n: Degree, 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 * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize)
pairs * GGLWE::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize)
}
}
impl<D: DataMut> TensorKey<D> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKey<D> {
// Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWE<D> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -196,8 +195,8 @@ impl<D: DataMut> TensorKey<D> {
}
impl<D: DataRef> TensorKey<D> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKey<D> {
// Returns a reference to GGLWE_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE<D> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -238,7 +237,7 @@ pub trait TensorKeyToRef {
impl<D: DataRef> TensorKeyToRef for TensorKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
GGLWE<D>: GGLWEToRef,
{
fn to_ref(&self) -> TensorKey<&[u8]> {
TensorKey {
@@ -253,7 +252,7 @@ pub trait TensorKeyToMut {
impl<D: DataMut> TensorKeyToMut for TensorKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
GGLWE<D>: GGLWEToMut,
{
fn to_mut(&mut self) -> TensorKey<&mut [u8]> {
TensorKey {

View File

@@ -4,8 +4,8 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey,
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
};
use std::fmt;
@@ -196,28 +196,24 @@ impl GLWEToLWESwitchingKey<Vec<u8>> {
}
}
pub trait GLWEToLWESwitchingKeyToRef {
fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]>;
}
impl<D: DataRef> GLWEToLWESwitchingKeyToRef for GLWEToLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> GLWEToLWESwitchingKey<&[u8]> {
GLWEToLWESwitchingKey(self.0.to_ref())
impl<D: DataRef> GGLWEToRef for GLWEToLWESwitchingKey<D> {
fn to_ref(&self) -> GGLWE<&[u8]> {
self.0.to_ref()
}
}
pub trait GLWEToLWESwitchingKeyToMut {
fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> GLWEToLWESwitchingKeyToMut for GLWEToLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> GLWEToLWESwitchingKey<&mut [u8]> {
GLWEToLWESwitchingKey(self.0.to_mut())
impl<D: DataMut> GGLWEToMut for GLWEToLWESwitchingKey<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
self.0.to_mut()
}
}
impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKey<D> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
}
}

View File

@@ -6,8 +6,8 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -193,28 +193,34 @@ impl<D: DataRef> WriterTo for LWESwitchingKey<D> {
}
}
pub trait LWESwitchingKeyToRef {
fn to_ref(&self) -> LWESwitchingKey<&[u8]>;
}
impl<D: DataRef> LWESwitchingKeyToRef for LWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWESwitchingKey<&[u8]> {
LWESwitchingKey(self.0.to_ref())
impl<D: DataRef> GGLWEToRef for LWESwitchingKey<D> {
fn to_ref(&self) -> GGLWE<&[u8]> {
self.0.to_ref()
}
}
pub trait LWESwitchingKeyToMut {
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> LWESwitchingKeyToMut for LWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> LWESwitchingKey<&mut [u8]> {
LWESwitchingKey(self.0.to_mut())
impl<D: DataMut> GGLWEToMut for LWESwitchingKey<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
self.0.to_mut()
}
}
impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWESwitchingKey<D> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
}
}
impl<D: DataRef> GLWESwitchingKeyDegrees for LWESwitchingKey<D> {
fn input_degree(&self) -> &Degree {
&self.0.input_degree
}
fn output_degree(&self) -> &Degree {
&self.0.output_degree
}
}

View File

@@ -6,8 +6,8 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey,
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -196,28 +196,24 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
}
}
pub trait LWEToGLWESwitchingKeyToRef {
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]>;
}
impl<D: DataRef> LWEToGLWESwitchingKeyToRef for LWEToGLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToRef,
{
fn to_ref(&self) -> LWEToGLWESwitchingKey<&[u8]> {
LWEToGLWESwitchingKey(self.0.to_ref())
impl<D: DataRef> GGLWEToRef for LWEToGLWESwitchingKey<D> {
fn to_ref(&self) -> GGLWE<&[u8]> {
self.0.to_ref()
}
}
pub trait LWEToGLWESwitchingKeyToMut {
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> LWEToGLWESwitchingKeyToMut for LWEToGLWESwitchingKey<D>
where
GLWESwitchingKey<D>: GLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> LWEToGLWESwitchingKey<&mut [u8]> {
LWEToGLWESwitchingKey(self.0.to_mut())
impl<D: DataMut> GGLWEToMut for LWEToGLWESwitchingKey<D> {
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
self.0.to_mut()
}
}
impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey<D> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
}
}

View File

@@ -1,16 +1,13 @@
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, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
};
#[derive(PartialEq, Eq)]
pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GLWESwitchingKeyPrepared<D, B>,
pub(crate) key: GGLWEPrepared<D, B>,
pub(crate) p: i64,
}
@@ -78,7 +75,7 @@ impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
pub trait AutomorphismKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
Self: GGLWEPreparedAlloc<B>,
{
fn alloc_automorphism_key_prepared(
&self,
@@ -89,7 +86,7 @@ where
dsize: Dsize,
) -> AutomorphismKeyPrepared<Vec<u8>, B> {
AutomorphismKeyPrepared::<Vec<u8>, B> {
key: self.alloc_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize),
key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
@@ -120,7 +117,7 @@ where
dnum: Dnum,
dsize: Dsize,
) -> usize {
self.bytes_of_glwe_switching_key_prepared(base2k, k, rank, rank, dnum, dsize)
self.bytes_of_gglwe_prepared(base2k, k, rank, rank, dnum, dsize)
}
fn bytes_of_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> usize
@@ -130,7 +127,7 @@ where
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
"rank_in != rank_out is not supported for AutomorphismKeyPrepared"
);
self.bytes_of_automorphism_key_prepared(
infos.base2k(),
@@ -142,7 +139,7 @@ where
}
}
impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {}
impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
@@ -178,26 +175,26 @@ impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub trait PrepareAutomorphismKey<B: Backend>
where
Self: GLWESwitchingKeyPrepare<B>,
Self: GGLWEPrepare<B>,
{
fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.prepare_glwe_switching_key_tmp_bytes(infos)
self.prepare_gglwe_tmp_bytes(infos)
}
fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: AutomorphismKeyPreparedToMut<B> + SetAutomorphismGaloisElement,
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
R: GGLWEPreparedToMut<B> + SetAutomorphismGaloisElement,
O: GGLWEToRef + GetAutomorphismGaloisElement,
{
self.prepare_glwe_switching(&mut res.to_mut().key, &other.to_ref().key, scratch);
self.prepare_gglwe(res, other, scratch);
res.set_p(other.p());
}
}
impl<B: Backend> PrepareAutomorphismKey<B> for Module<B> where Module<B>: GLWESwitchingKeyPrepare<B> {}
impl<B: Backend> PrepareAutomorphismKey<B> for Module<B> where Module<B>: GGLWEPrepare<B> {}
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
@@ -211,35 +208,21 @@ impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: AutomorphismKeyToRef + GetAutomorphismGaloisElement,
O: GGLWEToRef + GetAutomorphismGaloisElement,
M: PrepareAutomorphismKey<B>,
{
module.prepare_automorphism_key(self, other, scratch);
}
}
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(),
}
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
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(),
}
impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for AutomorphismKeyPrepared<D, BE> {
fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
self.key.to_ref()
}
}

View File

@@ -1,35 +1,35 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToRef,
GLWESwtichingKeyGetMetaData, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut,
LWEInfos, Rank, TorusPrecision,
prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef},
};
#[derive(PartialEq, Eq)]
pub struct GLWESwitchingKeyPrepared<D: Data, B: Backend> {
pub(crate) key: GGLWEPrepared<D, B>,
pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out
pub(crate) input_degree: Degree, // Degree of sk_in
pub(crate) output_degree: Degree, // Degree of sk_out
}
impl<D: DataMut, B: Backend> GLWESwitchingKeySetMetaData for GLWESwitchingKeyPrepared<D, B> {
fn set_sk_in_n(&mut self, sk_in_n: usize) {
self.sk_in_n = sk_in_n
impl<D: DataRef, BE: Backend> GLWESwitchingKeyDegrees for GLWESwitchingKeyPrepared<D, BE> {
fn output_degree(&self) -> &Degree {
&self.output_degree
}
fn set_sk_out_n(&mut self, sk_out_n: usize) {
self.sk_out_n = sk_out_n
fn input_degree(&self) -> &Degree {
&self.input_degree
}
}
impl<D: DataRef, B: Backend> GLWESwtichingKeyGetMetaData for GLWESwitchingKeyPrepared<D, B> {
fn sk_in_n(&self) -> usize {
self.sk_in_n
impl<D: DataMut, BE: Backend> GLWESwitchingKeyDegreesMut for GLWESwitchingKeyPrepared<D, BE> {
fn output_degree(&mut self) -> &mut Degree {
&mut self.output_degree
}
fn sk_out_n(&self) -> usize {
self.sk_out_n
fn input_degree(&mut self) -> &mut Degree {
&mut self.input_degree
}
}
@@ -90,8 +90,8 @@ where
) -> GLWESwitchingKeyPrepared<Vec<u8>, B> {
GLWESwitchingKeyPrepared::<Vec<u8>, B> {
key: self.alloc_gglwe_prepared(base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
input_degree: Degree(0),
output_degree: Degree(0),
}
}
@@ -199,12 +199,12 @@ where
fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GLWESwitchingKeyPreparedToMut<B> + GLWESwitchingKeySetMetaData,
O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
{
self.prepare_gglwe(&mut res.to_mut().key, &other.to_ref().key, scratch);
res.set_sk_in_n(other.sk_in_n());
res.set_sk_out_n(other.sk_out_n());
self.prepare_gglwe(res, other, scratch);
*res.input_degree() = *other.input_degree();
*res.output_degree() = *other.output_degree();
}
}
@@ -213,7 +213,7 @@ impl<B: Backend> GLWESwitchingKeyPrepare<B> for Module<B> where Self: GGLWEPrepa
impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: GLWESwitchingKeyToRef + GLWESwtichingKeyGetMetaData,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: GLWESwitchingKeyPrepare<B>,
{
module.prepare_glwe_switching(self, other, scratch);
@@ -229,30 +229,20 @@ impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
}
}
pub trait GLWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyPreparedToMut<B> for GLWESwitchingKeyPrepared<D, B> {
fn to_mut(&mut self) -> GLWESwitchingKeyPrepared<&mut [u8], B> {
GLWESwitchingKeyPrepared {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_mut(),
}
impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for GLWESwitchingKeyPrepared<D, BE>
where
GGLWEPrepared<D, BE>: GGLWEPreparedToRef<BE>,
{
fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
self.key.to_ref()
}
}
pub trait GLWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWESwitchingKeyPreparedToRef<B> for GLWESwitchingKeyPrepared<D, B> {
fn to_ref(&self) -> GLWESwitchingKeyPrepared<&[u8], B> {
GLWESwitchingKeyPrepared {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_ref(),
}
impl<D: DataRef, BE: Backend> GGLWEPreparedToMut<BE> for GLWESwitchingKeyPrepared<D, BE>
where
GGLWEPrepared<D, BE>: GGLWEPreparedToMut<BE>,
{
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], BE> {
self.key.to_mut()
}
}

View File

@@ -1,16 +1,13 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
};
#[derive(PartialEq, Eq)]
pub struct TensorKeyPrepared<D: Data, B: Backend> {
pub(crate) keys: Vec<GLWESwitchingKeyPrepared<D, B>>,
pub(crate) keys: Vec<GGLWEPrepared<D, B>>,
}
impl<D: Data, B: Backend> LWEInfos for TensorKeyPrepared<D, B> {
@@ -57,7 +54,7 @@ impl<D: Data, B: Backend> GGLWEInfos for TensorKeyPrepared<D, B> {
pub trait TensorKeyPreparedAlloc<B: Backend>
where
Self: GLWESwitchingKeyPreparedAlloc<B>,
Self: GGLWEPreparedAlloc<B>,
{
fn alloc_tensor_key_prepared(
&self,
@@ -70,7 +67,7 @@ where
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyPrepared {
keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize))
.map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
}
@@ -82,7 +79,7 @@ where
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKeyPrepared"
"rank_in != rank_out is not supported for TensorKeyPrepared"
);
self.alloc_tensor_key_prepared(
infos.base2k(),
@@ -95,7 +92,7 @@ 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_glwe_switching_key_prepared(base2k, k, Rank(1), rank, dnum, dsize)
pairs * self.bytes_of_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize)
}
fn bytes_of_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> usize
@@ -112,7 +109,7 @@ where
}
}
impl<B: Backend> TensorKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {}
impl<B: Backend> TensorKeyPreparedAlloc<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
@@ -147,8 +144,8 @@ impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
}
impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
// Returns a mutable reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GLWESwitchingKeyPrepared<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);
};
@@ -158,8 +155,8 @@ impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
}
impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
// Returns a reference to GLWESwitchingKey_{s}(s[i] * s[j])
pub fn at(&self, mut i: usize, mut j: usize) -> &GLWESwitchingKeyPrepared<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);
};
@@ -170,13 +167,13 @@ impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
pub trait TensorKeyPrepare<B: Backend>
where
Self: GLWESwitchingKeyPrepare<B>,
Self: GGLWEPrepare<B>,
{
fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.prepare_glwe_switching_key_tmp_bytes(infos)
self.prepare_gglwe_tmp_bytes(infos)
}
fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
@@ -190,12 +187,12 @@ where
assert_eq!(res.keys.len(), other.keys.len());
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
self.prepare_glwe_switching(a, b, scratch);
self.prepare_gglwe(a, b, scratch);
}
}
}
impl<B: Backend> TensorKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
impl<B: Backend> TensorKeyPrepare<B> for Module<B> where Self: GGLWEPrepare<B> {}
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
@@ -223,7 +220,7 @@ pub trait TensorKeyPreparedToMut<B: Backend> {
impl<D: DataMut, B: Backend> TensorKeyPreparedToMut<B> for TensorKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
{
fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> {
TensorKeyPrepared {
@@ -238,7 +235,7 @@ pub trait TensorKeyPreparedToRef<B: Backend> {
impl<D: DataRef, B: Backend> TensorKeyPreparedToRef<B> for TensorKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> {
TensorKeyPrepared {

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
};
#[derive(PartialEq, Eq)]
@@ -152,10 +150,10 @@ where
fn prepare_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: GLWEToLWESwitchingKeyPreparedToMut<B>,
O: GLWEToLWESwitchingKeyToRef,
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
{
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
self.prepare_glwe_switching(res, other, scratch);
}
}
@@ -174,35 +172,37 @@ impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: GLWEToLWESwitchingKeyToRef,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: GLWEToLWESwitchingKeyPrepare<B>,
{
module.prepare_glwe_to_lwe_switching_key(self, other, scratch);
}
}
pub trait GLWEToLWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWEToLWESwitchingKeyPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> {
GLWEToLWESwitchingKeyPrepared(self.0.to_ref())
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
self.0.to_ref()
}
}
pub trait GLWEToLWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> {
GLWEToLWESwitchingKeyPrepared(self.0.to_mut())
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
self.0.to_mut()
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
}
}

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKeyToRef, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
};
#[derive(PartialEq, Eq)]
@@ -160,10 +158,10 @@ where
}
fn prepare_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: LWESwitchingKeyPreparedToMut<B>,
O: LWESwitchingKeyToRef,
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
{
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
self.prepare_glwe_switching(res, other, scratch);
}
}
@@ -182,35 +180,37 @@ impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
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,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
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>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> LWESwitchingKeyPrepared<&[u8], B> {
LWESwitchingKeyPrepared(self.0.to_ref())
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
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>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
{
fn to_mut(&mut self) -> LWESwitchingKeyPrepared<&mut [u8], B> {
LWESwitchingKeyPrepared(self.0.to_mut())
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
self.0.to_mut()
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for LWESwitchingKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
}
}

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKeyToRef, Rank, TorusPrecision,
prepared::{
GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
GLWESwitchingKeyPreparedToRef,
},
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
};
/// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE].
@@ -159,10 +157,10 @@ where
fn prepare_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
where
R: LWEToGLWESwitchingKeyPreparedToMut<B>,
O: LWEToGLWESwitchingKeyToRef,
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
{
self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
self.prepare_glwe_switching(res, other, scratch);
}
}
@@ -181,35 +179,37 @@ impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
where
O: LWEToGLWESwitchingKeyToRef,
O: GGLWEToRef + GLWESwitchingKeyDegrees,
M: LWEToGLWESwitchingKeyPrepare<B>,
{
module.prepare_lwe_to_glwe_switching_key(self, other, scratch);
}
}
pub trait LWEToGLWESwitchingKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> LWEToGLWESwitchingKeyPreparedToRef<B> for LWEToGLWESwitchingKeyPrepared<D, B>
impl<D: DataRef, B: Backend> GGLWEPreparedToRef<B> for LWEToGLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToRef<B>,
{
fn to_ref(&self) -> LWEToGLWESwitchingKeyPrepared<&[u8], B> {
LWEToGLWESwitchingKeyPrepared(self.0.to_ref())
fn to_ref(&self) -> GGLWEPrepared<&[u8], B> {
self.0.to_ref()
}
}
pub trait LWEToGLWESwitchingKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPreparedToMut<B> for LWEToGLWESwitchingKeyPrepared<D, B>
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for LWEToGLWESwitchingKeyPrepared<D, B>
where
GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
GLWESwitchingKeyPrepared<D, B>: GGLWEPreparedToMut<B>,
{
fn to_mut(&mut self) -> LWEToGLWESwitchingKeyPrepared<&mut [u8], B> {
LWEToGLWESwitchingKeyPrepared(self.0.to_mut())
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
self.0.to_mut()
}
}
impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKeyPrepared<D, B> {
fn input_degree(&mut self) -> &mut Degree {
&mut self.0.input_degree
}
fn output_degree(&mut self) -> &mut Degree {
&mut self.0.output_degree
}
}