This commit is contained in:
Pro7ech
2025-10-12 21:34:10 +02:00
parent f72363cc4b
commit 662e533eac
32 changed files with 1594 additions and 787 deletions

View File

@@ -6,14 +6,14 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GGLWESwitchingKeyCompressed},
compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct GGLWEAutomorphismKeyCompressed<D: Data> {
pub(crate) key: GGLWESwitchingKeyCompressed<D>,
pub(crate) key: GGLWEKeyCompressed<D>,
pub(crate) p: i64,
}
@@ -83,14 +83,14 @@ impl GGLWEAutomorphismKeyCompressed<Vec<u8>> {
{
debug_assert_eq!(infos.rank_in(), infos.rank_out());
Self {
key: GGLWESwitchingKeyCompressed::alloc(infos),
key: GGLWEKeyCompressed::alloc(infos),
p: 0,
}
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
Self {
key: GGLWESwitchingKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize),
key: GGLWEKeyCompressed::alloc_with(n, base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
@@ -100,11 +100,11 @@ impl GGLWEAutomorphismKeyCompressed<Vec<u8>> {
A: GGLWEInfos,
{
debug_assert_eq!(infos.rank_in(), infos.rank_out());
GGLWESwitchingKeyCompressed::alloc_bytes(infos)
GGLWEKeyCompressed::alloc_bytes(infos)
}
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize)
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank, dnum, dsize)
}
}
@@ -131,3 +131,35 @@ where
self.p = other.p;
}
}
pub trait GGLWEAutomorphismKeyCompressedToRef {
fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]>;
}
impl<D: DataRef> GGLWEAutomorphismKeyCompressedToRef for GGLWEAutomorphismKeyCompressed<D>
where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToRef,
{
fn to_ref(&self) -> GGLWEAutomorphismKeyCompressed<&[u8]> {
GGLWEAutomorphismKeyCompressed {
key: self.key.to_ref(),
p: self.p,
}
}
}
pub trait GGLWEAutomorphismKeyCompressedToMut {
fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]>;
}
impl<D: DataRef> GGLWEAutomorphismKeyCompressedToMut for GGLWEAutomorphismKeyCompressed<D>
where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToMut,
{
fn to_mut(&mut self) -> GGLWEAutomorphismKeyCompressed<&mut [u8]> {
GGLWEAutomorphismKeyCompressed {
p: self.p,
key: self.key.to_mut(),
}
}
}

View File

@@ -1,6 +1,8 @@
use poulpy_hal::{
api::{VecZnxCopy, VecZnxFillUniform},
layouts::{Backend, Data, DataMut, DataRef, FillUniform, MatZnx, Module, ReaderFrom, WriterTo, ZnxInfos},
layouts::{
Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos,
},
source::Source,
};
@@ -289,3 +291,37 @@ where
});
}
}
pub trait GGLWECiphertextCompressedToMut {
fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]>;
}
impl<D: DataMut> GGLWECiphertextCompressedToMut for GGLWECiphertextCompressed<D> {
fn to_mut(&mut self) -> GGLWECiphertextCompressed<&mut [u8]> {
GGLWECiphertextCompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
seed: self.seed.clone(),
rank_out: self.rank_out,
data: self.data.to_mut(),
}
}
}
pub trait GGLWECiphertextCompressedToRef {
fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]>;
}
impl<D: DataMut> GGLWECiphertextCompressedToRef for GGLWECiphertextCompressed<D> {
fn to_ref(&self) -> GGLWECiphertextCompressed<&[u8]> {
GGLWECiphertextCompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
seed: self.seed.clone(),
rank_out: self.rank_out,
data: self.data.to_ref(),
}
}
}

View File

@@ -6,19 +6,19 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GGLWECiphertextCompressed},
compressed::{Decompress, GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut, GGLWECiphertextCompressedToRef},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct GGLWESwitchingKeyCompressed<D: Data> {
pub struct GGLWEKeyCompressed<D: Data> {
pub(crate) key: GGLWECiphertextCompressed<D>,
pub(crate) sk_in_n: usize, // Degree of sk_in
pub(crate) sk_out_n: usize, // Degree of sk_out
}
impl<D: Data> LWEInfos for GGLWESwitchingKeyCompressed<D> {
impl<D: Data> LWEInfos for GGLWEKeyCompressed<D> {
fn n(&self) -> Degree {
self.key.n()
}
@@ -35,13 +35,13 @@ impl<D: Data> LWEInfos for GGLWESwitchingKeyCompressed<D> {
self.key.size()
}
}
impl<D: Data> GLWEInfos for GGLWESwitchingKeyCompressed<D> {
impl<D: Data> GLWEInfos for GGLWEKeyCompressed<D> {
fn rank(&self) -> Rank {
self.rank_out()
}
}
impl<D: Data> GGLWEInfos for GGLWESwitchingKeyCompressed<D> {
impl<D: Data> GGLWEInfos for GGLWEKeyCompressed<D> {
fn rank_in(&self) -> Rank {
self.key.rank_in()
}
@@ -59,19 +59,19 @@ impl<D: Data> GGLWEInfos for GGLWESwitchingKeyCompressed<D> {
}
}
impl<D: DataRef> fmt::Debug for GGLWESwitchingKeyCompressed<D> {
impl<D: DataRef> fmt::Debug for GGLWEKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self}")
}
}
impl<D: DataMut> FillUniform for GGLWESwitchingKeyCompressed<D> {
impl<D: DataMut> FillUniform for GGLWEKeyCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.key.fill_uniform(log_bound, source);
}
}
impl<D: DataRef> fmt::Display for GGLWESwitchingKeyCompressed<D> {
impl<D: DataRef> fmt::Display for GGLWEKeyCompressed<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
@@ -81,12 +81,12 @@ impl<D: DataRef> fmt::Display for GGLWESwitchingKeyCompressed<D> {
}
}
impl GGLWESwitchingKeyCompressed<Vec<u8>> {
impl GGLWEKeyCompressed<Vec<u8>> {
pub fn alloc<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
GGLWESwitchingKeyCompressed {
GGLWEKeyCompressed {
key: GGLWECiphertextCompressed::alloc(infos),
sk_in_n: 0,
sk_out_n: 0,
@@ -102,7 +102,7 @@ impl GGLWESwitchingKeyCompressed<Vec<u8>> {
dnum: Dnum,
dsize: Dsize,
) -> Self {
GGLWESwitchingKeyCompressed {
GGLWEKeyCompressed {
key: GGLWECiphertextCompressed::alloc_with(n, base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
@@ -121,7 +121,7 @@ impl GGLWESwitchingKeyCompressed<Vec<u8>> {
}
}
impl<D: DataMut> ReaderFrom for GGLWESwitchingKeyCompressed<D> {
impl<D: DataMut> ReaderFrom for GGLWEKeyCompressed<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;
@@ -129,7 +129,7 @@ impl<D: DataMut> ReaderFrom for GGLWESwitchingKeyCompressed<D> {
}
}
impl<D: DataRef> WriterTo for GGLWESwitchingKeyCompressed<D> {
impl<D: DataRef> WriterTo for GGLWEKeyCompressed<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)?;
@@ -137,13 +137,47 @@ impl<D: DataRef> WriterTo for GGLWESwitchingKeyCompressed<D> {
}
}
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWESwitchingKeyCompressed<DR>> for GGLWESwitchingKey<D>
impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, GGLWEKeyCompressed<DR>> for GGLWESwitchingKey<D>
where
Module<B>: VecZnxFillUniform + VecZnxCopy,
{
fn decompress(&mut self, module: &Module<B>, other: &GGLWESwitchingKeyCompressed<DR>) {
fn decompress(&mut self, module: &Module<B>, other: &GGLWEKeyCompressed<DR>) {
self.key.decompress(module, &other.key);
self.sk_in_n = other.sk_in_n;
self.sk_out_n = other.sk_out_n;
}
}
pub trait GGLWEKeyCompressedToMut {
fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]>;
}
impl<D: DataMut> GGLWEKeyCompressedToMut for GGLWEKeyCompressed<D>
where
GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToMut,
{
fn to_mut(&mut self) -> GGLWEKeyCompressed<&mut [u8]> {
GGLWEKeyCompressed {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_mut(),
}
}
}
pub trait GGLWEKeyCompressedToRef {
fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]>;
}
impl<D: DataMut> GGLWEKeyCompressedToRef for GGLWEKeyCompressed<D>
where
GGLWECiphertextCompressed<D>: GGLWECiphertextCompressedToRef,
{
fn to_ref(&self) -> GGLWEKeyCompressed<&[u8]> {
GGLWEKeyCompressed {
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
key: self.key.to_ref(),
}
}
}

View File

@@ -6,14 +6,14 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWETensorKey, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{Decompress, GGLWESwitchingKeyCompressed},
compressed::{Decompress, GGLWEKeyCompressed, GGLWEKeyCompressedToMut, GGLWEKeyCompressedToRef},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct GGLWETensorKeyCompressed<D: Data> {
pub(crate) keys: Vec<GGLWESwitchingKeyCompressed<D>>,
pub(crate) keys: Vec<GGLWEKeyCompressed<D>>,
}
impl<D: Data> LWEInfos for GGLWETensorKeyCompressed<D> {
@@ -66,7 +66,7 @@ impl<D: DataMut> FillUniform for GGLWETensorKeyCompressed<D> {
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
self.keys
.iter_mut()
.for_each(|key: &mut GGLWESwitchingKeyCompressed<D>| key.fill_uniform(log_bound, source))
.for_each(|key: &mut GGLWEKeyCompressed<D>| key.fill_uniform(log_bound, source))
}
}
@@ -101,10 +101,10 @@ impl GGLWETensorKeyCompressed<Vec<u8>> {
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let mut keys: Vec<GGLWESwitchingKeyCompressed<Vec<u8>>> = Vec::new();
let mut keys: Vec<GGLWEKeyCompressed<Vec<u8>>> = Vec::new();
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
(0..pairs).for_each(|_| {
keys.push(GGLWESwitchingKeyCompressed::alloc_with(
keys.push(GGLWEKeyCompressed::alloc_with(
n,
base2k,
k,
@@ -129,7 +129,7 @@ impl GGLWETensorKeyCompressed<Vec<u8>> {
let rank_out: usize = infos.rank_out().into();
let pairs: usize = (((rank_out + 1) * rank_out) >> 1).max(1);
pairs
* GGLWESwitchingKeyCompressed::alloc_bytes_with(
* GGLWEKeyCompressed::alloc_bytes_with(
infos.n(),
infos.base2k(),
infos.k(),
@@ -141,7 +141,7 @@ impl GGLWETensorKeyCompressed<Vec<u8>> {
pub fn alloc_bytes_with(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 * GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize)
pairs * GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, dsize)
}
}
@@ -172,7 +172,7 @@ impl<D: DataRef> WriterTo for GGLWETensorKeyCompressed<D> {
}
impl<D: DataMut> GGLWETensorKeyCompressed<D> {
pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWESwitchingKeyCompressed<D> {
pub(crate) fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEKeyCompressed<D> {
if i > j {
std::mem::swap(&mut i, &mut j);
};
@@ -205,3 +205,33 @@ where
});
}
}
pub trait GGLWETensorKeyCompressedToMut {
fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]>;
}
impl<D: DataMut> GGLWETensorKeyCompressedToMut for GGLWETensorKeyCompressed<D>
where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToMut,
{
fn to_mut(&mut self) -> GGLWETensorKeyCompressed<&mut [u8]> {
GGLWETensorKeyCompressed {
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
}
}
}
pub trait GGLWETensorKeyCompressedToRef {
fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]>;
}
impl<D: DataMut> GGLWETensorKeyCompressedToRef for GGLWETensorKeyCompressed<D>
where
GGLWEKeyCompressed<D>: GGLWEKeyCompressedToRef,
{
fn to_ref(&self) -> GGLWETensorKeyCompressed<&[u8]> {
GGLWETensorKeyCompressed {
keys: self.keys.iter().map(|c| c.to_ref()).collect(),
}
}
}

View File

@@ -1,6 +1,8 @@
use poulpy_hal::{
api::{VecZnxCopy, VecZnxFillUniform},
layouts::{Backend, Data, DataMut, DataRef, FillUniform, MatZnx, Module, ReaderFrom, WriterTo, ZnxInfos},
layouts::{
Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos,
},
source::Source,
};
@@ -235,3 +237,37 @@ where
});
}
}
pub trait GGSWCiphertextCompressedToMut {
fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]>;
}
impl<D: DataMut> GGSWCiphertextCompressedToMut for GGSWCiphertextCompressed<D> {
fn to_mut(&mut self) -> GGSWCiphertextCompressed<&mut [u8]> {
GGSWCiphertextCompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
rank: self.rank(),
seed: self.seed.clone(),
data: self.data.to_mut(),
}
}
}
pub trait GGSWCiphertextCompressedToRef {
fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]>;
}
impl<D: DataRef> GGSWCiphertextCompressedToRef for GGSWCiphertextCompressed<D> {
fn to_ref(&self) -> GGSWCiphertextCompressed<&[u8]> {
GGSWCiphertextCompressed {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
rank: self.rank(),
seed: self.seed.clone(),
data: self.data.to_ref(),
}
}
}

View File

@@ -6,11 +6,11 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWESwitchingKeyCompressed,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, compressed::GGLWEKeyCompressed,
};
#[derive(PartialEq, Eq, Clone)]
pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
pub struct GLWEToLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWEKeyCompressed<D>);
impl<D: Data> LWEInfos for GLWEToLWESwitchingKeyCompressed<D> {
fn base2k(&self) -> Base2K {
@@ -98,11 +98,11 @@ impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
Self(GGLWESwitchingKeyCompressed::alloc(infos))
Self(GGLWEKeyCompressed::alloc(infos))
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
Self(GGLWESwitchingKeyCompressed::alloc_with(
Self(GGLWEKeyCompressed::alloc_with(
n,
base2k,
k,
@@ -127,10 +127,10 @@ impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
GGLWESwitchingKeyCompressed::alloc_bytes(infos)
GGLWEKeyCompressed::alloc_bytes(infos)
}
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize {
GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1))
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, rank_in, dnum, Dsize(1))
}
}

View File

@@ -6,12 +6,12 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, Rank, TorusPrecision,
compressed::{Decompress, GGLWESwitchingKeyCompressed},
compressed::{Decompress, GGLWEKeyCompressed},
};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
pub struct LWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWEKeyCompressed<D>);
impl<D: Data> LWEInfos for LWESwitchingKeyCompressed<D> {
fn base2k(&self) -> Base2K {
@@ -103,11 +103,11 @@ impl LWESwitchingKeyCompressed<Vec<u8>> {
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
Self(GGLWESwitchingKeyCompressed::alloc(infos))
Self(GGLWEKeyCompressed::alloc(infos))
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
Self(GGLWESwitchingKeyCompressed::alloc_with(
Self(GGLWEKeyCompressed::alloc_with(
n,
base2k,
k,
@@ -137,11 +137,11 @@ impl LWESwitchingKeyCompressed<Vec<u8>> {
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
GGLWESwitchingKeyCompressed::alloc_bytes(infos)
GGLWEKeyCompressed::alloc_bytes(infos)
}
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
}
}

View File

@@ -6,12 +6,12 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision,
compressed::{Decompress, GGLWESwitchingKeyCompressed},
compressed::{Decompress, GGLWEKeyCompressed},
};
use std::fmt;
#[derive(PartialEq, Eq, Clone)]
pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWEKeyCompressed<D>);
impl<D: Data> LWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
fn n(&self) -> Degree {
@@ -98,11 +98,11 @@ impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
Self(GGLWESwitchingKeyCompressed::alloc(infos))
Self(GGLWEKeyCompressed::alloc(infos))
}
pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
Self(GGLWESwitchingKeyCompressed::alloc_with(
Self(GGLWEKeyCompressed::alloc_with(
n,
base2k,
k,
@@ -127,11 +127,11 @@ impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
GGLWESwitchingKeyCompressed::alloc_bytes(infos)
GGLWEKeyCompressed::alloc_bytes(infos)
}
pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GGLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
GGLWEKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
}
}

View File

@@ -4,7 +4,8 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWESwitchingKey, GGLWESwitchingKeyToMut, GLWECiphertext, GLWEInfos, LWEInfos,
Rank, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -170,6 +171,26 @@ impl GGLWEAutomorphismKey<Vec<u8>> {
}
}
pub trait GGLWEAutomorphismKeyToMut {
fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]>;
}
impl<D: DataMut> GGLWEAutomorphismKeyToMut for GGLWEAutomorphismKey<D>
where
GGLWESwitchingKey<D>: GGLWESwitchingKeyToMut,
{
fn to_mut(&mut self) -> GGLWEAutomorphismKey<&mut [u8]> {
GGLWEAutomorphismKey {
key: self.key.to_mut(),
p: self.p,
}
}
}
pub trait GGLWEAutomorphismKeyToRef {
fn to_ref(&self) -> GGLWEAutomorphismKey<&[u8]>;
}
impl<D: DataRef> GGLWEAutomorphismKey<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
self.key.at(row, col)

View File

@@ -1,5 +1,5 @@
use poulpy_hal::{
layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, ReaderFrom, WriterTo, ZnxInfos},
layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos},
source::Source,
};
@@ -389,6 +389,36 @@ impl GGLWECiphertext<Vec<u8>> {
}
}
pub trait GGLWECiphertextToMut {
fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]>;
}
impl<D: DataMut> GGLWECiphertextToMut for GGLWECiphertext<D> {
fn to_mut(&mut self) -> GGLWECiphertext<&mut [u8]> {
GGLWECiphertext {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
data: self.data.to_mut(),
}
}
}
pub trait GGLWECiphertextToRef {
fn to_ref(&self) -> GGLWECiphertext<&[u8]>;
}
impl<D: DataMut> GGLWECiphertextToRef for GGLWECiphertext<D> {
fn to_ref(&self) -> GGLWECiphertext<&[u8]> {
GGLWECiphertext {
k: self.k(),
base2k: self.base2k(),
dsize: self.dsize(),
data: self.data.to_ref(),
}
}
}
impl<D: DataMut> ReaderFrom for GGLWECiphertext<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);

View File

@@ -4,7 +4,8 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWECiphertext, GGLWECiphertextToMut, GGLWEInfos, GLWECiphertext, GLWEInfos, LWEInfos, Rank,
TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -180,6 +181,23 @@ impl GGLWESwitchingKey<Vec<u8>> {
}
}
pub trait GGLWESwitchingKeyToMut {
fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]>;
}
impl<D: DataMut> GGLWESwitchingKeyToMut for GGLWESwitchingKey<D>
where
GGLWECiphertext<D>: GGLWECiphertextToMut,
{
fn to_mut(&mut self) -> GGLWESwitchingKey<&mut [u8]> {
GGLWESwitchingKey {
key: self.key.to_mut(),
sk_in_n: self.sk_in_n,
sk_out_n: self.sk_out_n,
}
}
}
impl<D: DataRef> GGLWESwitchingKey<D> {
pub fn at(&self, row: usize, col: usize) -> GLWECiphertext<&[u8]> {
self.key.at(row, col)

View File

@@ -1,5 +1,5 @@
use poulpy_hal::{
layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, ReaderFrom, WriterTo, ZnxInfos},
layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos},
source::Source,
};
use std::fmt;
@@ -370,3 +370,35 @@ impl<D: DataRef> WriterTo for GGSWCiphertext<D> {
self.data.write_to(writer)
}
}
pub trait GGSWCiphertextToMut {
fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]>;
}
impl<D: DataMut> GGSWCiphertextToMut for GGSWCiphertext<D> {
fn to_mut(&mut self) -> GGSWCiphertext<&mut [u8]> {
GGSWCiphertext::builder()
.base2k(self.base2k())
.dsize(self.dsize())
.k(self.k())
.data(self.data.to_mut())
.build()
.unwrap()
}
}
pub trait GGSWCiphertextToRef {
fn to_ref(&self) -> GGSWCiphertext<&[u8]>;
}
impl<D: DataRef> GGSWCiphertextToRef for GGSWCiphertext<D> {
fn to_ref(&self) -> GGSWCiphertext<&[u8]> {
GGSWCiphertext::builder()
.base2k(self.base2k())
.dsize(self.dsize())
.k(self.k())
.data(self.data.to_ref())
.build()
.unwrap()
}
}

View File

@@ -1,4 +1,4 @@
use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, WriterTo, ZnxInfos};
use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos};
use crate::{
dist::Distribution,
@@ -207,3 +207,33 @@ impl<D: DataRef> WriterTo for GLWEPublicKey<D> {
self.data.write_to(writer)
}
}
pub trait GLWEPublicKeyToRef {
fn to_ref(&self) -> GLWEPublicKey<&[u8]>;
}
impl<D: DataRef> GLWEPublicKeyToRef for GLWEPublicKey<D> {
fn to_ref(&self) -> GLWEPublicKey<&[u8]> {
GLWEPublicKey {
data: self.data.to_ref(),
base2k: self.base2k,
k: self.k,
dist: self.dist,
}
}
}
pub trait GLWEPublicKeyToMut {
fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]>;
}
impl<D: DataMut> GLWEPublicKeyToMut for GLWEPublicKey<D> {
fn to_mut(&mut self) -> GLWEPublicKey<&mut [u8]> {
GLWEPublicKey {
base2k: self.base2k,
k: self.k,
dist: self.dist,
data: self.data.to_mut(),
}
}
}

View File

@@ -200,3 +200,31 @@ impl<D: DataMut> GLWECiphertextToMut for GLWEPlaintext<D> {
.unwrap()
}
}
pub trait GLWEPlaintextToRef {
fn to_ref(&self) -> GLWEPlaintext<&[u8]>;
}
impl<D: DataRef> GLWEPlaintextToRef for GLWEPlaintext<D> {
fn to_ref(&self) -> GLWEPlaintext<&[u8]> {
GLWEPlaintext {
data: self.data.to_ref(),
base2k: self.base2k,
k: self.k,
}
}
}
pub trait GLWEPlaintextToMut {
fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]>;
}
impl<D: DataMut> GLWEPlaintextToMut for GLWEPlaintext<D> {
fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]> {
GLWEPlaintext {
base2k: self.base2k,
k: self.k,
data: self.data.to_mut(),
}
}
}

View File

@@ -1,5 +1,5 @@
use poulpy_hal::{
layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, WriterTo, ZnxInfos, ZnxZero},
layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero},
source::Source,
};
@@ -136,6 +136,32 @@ impl<D: DataMut> GLWESecret<D> {
}
}
pub trait GLWESecretToMut {
fn to_mut(&mut self) -> GLWESecret<&mut [u8]>;
}
impl<D: DataMut> GLWESecretToMut for GLWESecret<D> {
fn to_mut(&mut self) -> GLWESecret<&mut [u8]> {
GLWESecret {
dist: self.dist,
data: self.data.to_mut(),
}
}
}
pub trait GLWESecretToRef {
fn to_ref(&self) -> GLWESecret<&[u8]>;
}
impl<D: DataRef> GLWESecretToRef for GLWESecret<D> {
fn to_ref(&self) -> GLWESecret<&[u8]> {
GLWESecret {
data: self.data.to_ref(),
dist: self.dist,
}
}
}
impl<D: DataMut> ReaderFrom for GLWESecret<D> {
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
match Distribution::read_from(reader) {

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{
api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare, VmpPrepareTmpBytes},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToRef, ZnxInfos},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos},
oep::VmpPMatAllocBytesImpl,
};
@@ -295,6 +295,22 @@ where
}
}
pub trait GGSWCiphertextPreparedToMut<B: Backend> {
fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GGSWCiphertextPreparedToMut<B> for GGSWCiphertextPrepared<D, B> {
fn to_ref(&mut self) -> GGSWCiphertextPrepared<&mut [u8], B> {
GGSWCiphertextPrepared::builder()
.base2k(self.base2k())
.dsize(self.dsize())
.k(self.k())
.data(self.data.to_mut())
.build()
.unwrap()
}
}
pub trait GGSWCiphertextPreparedToRef<B: Backend> {
fn to_ref(&self) -> GGSWCiphertextPrepared<&[u8], B>;
}

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{
api::{VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VecZnxDft, ZnxInfos},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos},
oep::VecZnxDftAllocBytesImpl,
};
@@ -205,3 +205,33 @@ where
self.dist = other.dist;
}
}
pub trait GLWEPublicKeyPreparedToMut<B: Backend> {
fn to_mut(&mut self) -> GLWEPublicKeyPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWEPublicKeyPreparedToMut<B> for GLWEPublicKeyPrepared<D, B> {
fn to_mut(&mut self) -> GLWEPublicKeyPrepared<&mut [u8], B> {
GLWEPublicKeyPrepared {
dist: self.dist,
k: self.k,
base2k: self.base2k,
data: self.data.to_mut(),
}
}
}
pub trait GLWEPublicKeyPreparedToRef<B: Backend> {
fn to_ref(&self) -> GLWEPublicKeyPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWEPublicKeyPreparedToRef<B> for GLWEPublicKeyPrepared<D, B> {
fn to_ref(&self) -> GLWEPublicKeyPrepared<&[u8], B> {
GLWEPublicKeyPrepared {
data: self.data.to_ref(),
dist: self.dist,
k: self.k,
base2k: self.base2k,
}
}
}

View File

@@ -1,6 +1,6 @@
use poulpy_hal::{
api::{SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, SvpPPol, ZnxInfos},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, SvpPPol, SvpPPolToMut, SvpPPolToRef, ZnxInfos},
};
use crate::{
@@ -113,3 +113,29 @@ where
self.dist = other.dist
}
}
pub trait GLWESecretPreparedToRef<B: Backend> {
fn to_ref(&self) -> GLWESecretPrepared<&[u8], B>;
}
impl<D: DataRef, B: Backend> GLWESecretPreparedToRef<B> for GLWESecretPrepared<D, B> {
fn to_ref(&self) -> GLWESecretPrepared<&[u8], B> {
GLWESecretPrepared {
data: self.data.to_ref(),
dist: self.dist,
}
}
}
pub trait GLWESecretPreparedToMut<B: Backend> {
fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B>;
}
impl<D: DataMut, B: Backend> GLWESecretPreparedToMut<B> for GLWESecretPrepared<D, B> {
fn to_ref(&mut self) -> GLWESecretPrepared<&mut [u8], B> {
GLWESecretPrepared {
dist: self.dist,
data: self.data.to_mut(),
}
}
}