Removed traits on structs not dependent on Module API

This commit is contained in:
Pro7ech
2025-10-20 10:32:02 +02:00
parent 60fbd3e625
commit 53bc78f421
50 changed files with 858 additions and 2010 deletions

View File

@@ -6,8 +6,7 @@ use poulpy_hal::{
use crate::layouts::{
AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
};
@@ -86,32 +85,34 @@ impl<D: DataRef> fmt::Display for AutomorphismKeyCompressed<D> {
}
}
impl<BE: Backend> AutomorphismKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {}
impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub trait AutomorphismKeyCompressedAlloc
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn alloc_automorphism_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
AutomorphismKeyCompressed {
key: self.alloc_glwe_switching_key_compressed(base2k, k, rank, rank, dnum, dsize),
key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
fn alloc_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> AutomorphismKeyCompressed<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(infos.rank_in(), infos.rank_out());
self.alloc_automorphism_key_compressed(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
@@ -120,61 +121,8 @@ where
)
}
fn bytes_of_automorphism_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, rank, dnum, dsize)
}
fn bytes_of_automorphism_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(infos.rank_in(), infos.rank_out());
self.bytes_of_automorphism_key_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: AutomorphismKeyCompressedAlloc,
{
module.alloc_automorphism_key_compressed_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
M: AutomorphismKeyCompressedAlloc,
{
module.alloc_automorphism_key_compressed(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: AutomorphismKeyCompressedAlloc,
{
module.bytes_of_automorphism_key_compressed_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: AutomorphismKeyCompressedAlloc,
{
module.bytes_of_automorphism_key_compressed(base2k, k, rank, dnum, dsize)
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)
}
}

View File

@@ -7,7 +7,7 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{GLWECompressed, GLWEDecompress},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -105,19 +105,23 @@ impl<D: DataRef> fmt::Display for GGLWECompressed<D> {
}
}
pub trait GGLWECompressedAlloc
where
Self: GetDegree,
{
fn alloc_gglwe_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GGLWECompressed<Vec<u8>> {
impl GGLWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -134,7 +138,7 @@ where
GGLWECompressed {
data: MatZnx::alloc(
self.ring_degree().into(),
n.into(),
dnum.into(),
rank_in.into(),
1,
@@ -148,22 +152,21 @@ where
}
}
fn alloc_gglwe_compressed_from_infos<A>(&self, infos: &A) -> GGLWECompressed<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(infos.n(), self.ring_degree());
self.alloc_gglwe_compressed(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
fn bytes_of_gglwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize {
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -179,69 +182,13 @@ where
);
MatZnx::bytes_of(
self.ring_degree().into(),
n.into(),
dnum.into(),
rank_in.into(),
1,
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_gglwe_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(infos.n(), self.ring_degree());
self.bytes_of_gglwe_compressed(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<B: Backend> GGLWECompressedAlloc for Module<B> where Self: GetDegree {}
impl GGLWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: GGLWECompressedAlloc,
{
module.alloc_gglwe_compressed_from_infos(infos)
}
pub fn alloc<M>(
module: &M,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
M: GGLWECompressedAlloc,
{
module.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: GGLWECompressedAlloc,
{
module.bytes_of_gglwe_compressed_from_infos(infos)
}
pub fn byte_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: GGLWECompressedAlloc,
{
module.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize)
}
}
impl<D: DataRef> GGLWECompressed<D> {

View File

@@ -6,7 +6,7 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeySetMetaData, GLWESwitchingKeyToMut,
LWEInfos, Rank, TorusPrecision,
compressed::{GGLWECompressed, GGLWECompressedAlloc, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress},
compressed::{GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
@@ -81,33 +81,13 @@ impl<D: DataRef> fmt::Display for GLWESwitchingKeyCompressed<D> {
}
}
impl<BE: Backend> GLWESwitchingKeyCompressedAlloc for Module<BE> where Self: GGLWECompressedAlloc {}
pub trait GLWESwitchingKeyCompressedAlloc
where
Self: GGLWECompressedAlloc,
{
fn alloc_glwe_switching_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GLWESwitchingKeyCompressed<Vec<u8>> {
GLWESwitchingKeyCompressed {
key: self.alloc_gglwe_compressed(base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
}
}
fn alloc_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> GLWESwitchingKeyCompressed<Vec<u8>>
impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
self.alloc_glwe_switching_key_compressed(
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -117,62 +97,24 @@ where
)
}
fn bytes_of_glwe_switching_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize {
self.bytes_of_gglwe_compressed(base2k, k, rank_in, dnum, dsize)
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,
}
}
fn bytes_of_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_gglwe_compressed_from_infos(infos)
}
}
impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: GLWESwitchingKeyCompressedAlloc,
{
module.alloc_glwe_switching_key_compressed_from_infos(infos)
GGLWECompressed::bytes_of_from_infos(infos)
}
pub fn alloc<M>(
module: &M,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
M: GLWESwitchingKeyCompressedAlloc,
{
module.alloc_glwe_switching_key_compressed(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: GLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_glwe_switching_key_compressed_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: GLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, dsize)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum, dsize: Dsize) -> usize
where {
GGLWECompressed::bytes_of(n, base2k, k, rank_in, dnum, dsize)
}
}

View File

@@ -6,8 +6,7 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -82,38 +81,36 @@ impl<D: DataRef> fmt::Display for TensorKeyCompressed<D> {
}
}
impl<BE: Backend> TensorKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {}
impl TensorKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub trait TensorKeyCompressedAlloc
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn alloc_tensor_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> TensorKeyCompressed<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
TensorKeyCompressed {
keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank, dnum, dsize))
.map(|_| GLWESwitchingKeyCompressed::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
}
fn alloc_tensor_key_compressed_from_infos<A>(&self, infos: &A) -> TensorKeyCompressed<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKeyCompressed"
);
self.alloc_tensor_key_compressed(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
@@ -122,54 +119,9 @@ where
)
}
fn bytes_of_tensor_key_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
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 * self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, dsize)
}
fn bytes_of_tensor_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_tensor_key_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl TensorKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: TensorKeyCompressedAlloc,
{
module.alloc_tensor_key_compressed_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
M: TensorKeyCompressedAlloc,
{
module.alloc_tensor_key_compressed(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: TensorKeyCompressedAlloc,
{
module.bytes_of_tensor_key_compressed_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: TensorKeyCompressedAlloc,
{
module.bytes_of_tensor_key_compressed(base2k, k, rank, dnum, dsize)
pairs * GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, dsize)
}
}

View File

@@ -6,7 +6,7 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToMut, GLWEInfos, LWEInfos, Rank, TorusPrecision,
compressed::{GLWECompressed, GLWEDecompress},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -96,20 +96,22 @@ impl<D: DataMut> FillUniform for GGSWCompressed<D> {
}
}
impl<BE: Backend> GGSWCompressedAlloc for Module<BE> where Self: GetDegree {}
impl GGSWCompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGSWInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub trait GGSWCompressedAlloc
where
Self: GetDegree,
{
fn alloc_ggsw_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GGSWCompressed<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let size: usize = k.0.div_ceil(base2k.0) as usize;
assert!(
size as u32 > dsize.0,
@@ -126,7 +128,7 @@ where
GGSWCompressed {
data: MatZnx::alloc(
self.ring_degree().into(),
n.into(),
dnum.into(),
(rank + 1).into(),
1,
@@ -140,11 +142,12 @@ where
}
}
fn alloc_ggsw_compressed_from_infos<A>(&self, infos: &A) -> GGSWCompressed<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGSWInfos,
{
self.alloc_ggsw_compressed(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
@@ -153,7 +156,7 @@ where
)
}
fn bytes_of_ggsw_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let size: usize = k.0.div_ceil(base2k.0) as usize;
assert!(
size as u32 > dsize.0,
@@ -169,58 +172,13 @@ where
);
MatZnx::bytes_of(
self.ring_degree().into(),
n.into(),
dnum.into(),
(rank + 1).into(),
1,
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_ggsw_compressed_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGSWInfos,
{
self.bytes_of_ggsw_compressed(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl GGSWCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGSWInfos,
M: GGSWCompressedAlloc,
{
module.alloc_ggsw_compressed_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
M: GGSWCompressedAlloc,
{
module.alloc_ggsw_compressed(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGSWInfos,
M: GGSWCompressedAlloc,
{
module.bytes_of_ggsw_compressed_key_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: GGSWCompressedAlloc,
{
module.bytes_of_ggsw_compressed(base2k, k, rank, dnum, dsize)
}
}
impl<D: DataRef> GGSWCompressed<D> {

View File

@@ -88,17 +88,17 @@ impl<D: DataMut> FillUniform for GLWECompressed<D> {
}
}
pub trait GLWECompressedAlloc
where
Self: GetDegree,
{
fn alloc_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWECompressed<Vec<u8>> {
impl GLWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
GLWECompressed {
data: VecZnx::alloc(
self.ring_degree().into(),
1,
k.0.div_ceil(base2k.0) as usize,
),
data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize),
base2k,
k,
rank,
@@ -106,62 +106,15 @@ where
}
}
fn alloc_glwe_compressed_from_infos<A>(&self, infos: &A) -> GLWECompressed<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GLWEInfos,
{
assert_eq!(self.ring_degree(), infos.n());
self.alloc_glwe_compressed(infos.base2k(), infos.k(), infos.rank())
Self::bytes_of(infos.n(), infos.base2k(), infos.k())
}
fn bytes_of_glwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::bytes_of(
self.ring_degree().into(),
1,
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_glwe_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
assert_eq!(self.ring_degree(), infos.n());
self.bytes_of_glwe_compressed(infos.base2k(), infos.k())
}
}
impl<B: Backend> GLWECompressedAlloc for Module<B> where Self: GetDegree {}
impl GLWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GLWEInfos,
M: GLWECompressedAlloc,
{
module.alloc_glwe_compressed_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
where
M: GLWECompressedAlloc,
{
module.alloc_glwe_compressed(base2k, k, rank)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWECompressedAlloc,
{
module.bytes_of_glwe_compressed_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> usize
where
M: GLWECompressedAlloc,
{
module.bytes_of_glwe_compressed(base2k, k)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize)
}
}

View File

@@ -9,8 +9,7 @@ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyToMut, LWEInfos, Rank,
TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
};
@@ -88,96 +87,61 @@ impl<D: DataRef> WriterTo for GLWEToLWESwitchingKeyCompressed<D> {
}
}
pub trait GLWEToLWESwitchingKeyCompressedAlloc
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn alloc_glwe_to_lwe_switching_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
) -> GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
GLWEToLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
}
fn alloc_glwe_to_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyCompressed<Vec<u8>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
self.alloc_glwe_to_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
fn bytes_of_glwe_to_lwe_switching_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, rank_in, dnum, Dsize(1))
}
fn bytes_of_glwe_to_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
self.bytes_of_glwe_switching_key_compressed_from_infos(infos)
}
}
impl<B: Backend> GLWEToLWESwitchingKeyCompressedAlloc for Module<B> where Self: GLWESwitchingKeyCompressedAlloc {}
impl GLWEToLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyCompressedAlloc,
{
module.alloc_glwe_to_lwe_switching_key_compressed_from_infos(infos)
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self
where
M: GLWEToLWESwitchingKeyCompressedAlloc,
{
module.alloc_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum)
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
GLWEToLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
n,
base2k,
k,
rank_in,
Rank(1),
dnum,
Dsize(1),
))
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_glwe_to_lwe_switching_key_compressed_from_infos(infos)
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is unsupported for GLWEToLWESwitchingKeyCompressed"
);
GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize
where
M: GLWEToLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_glwe_to_lwe_switching_key_compressed(base2k, k, rank_in, dnum)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_in: Rank) -> usize {
GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank_in, dnum, Dsize(1))
}
}

View File

@@ -62,8 +62,15 @@ impl<D: DataMut> FillUniform for LWECompressed<D> {
}
}
pub trait LWECompressedAlloc {
fn alloc_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> LWECompressed<Vec<u8>> {
impl LWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: LWEInfos,
{
Self::alloc(infos.base2k(), infos.k())
}
pub fn alloc(base2k: Base2K, k: TorusPrecision) -> Self {
LWECompressed {
data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize),
k,
@@ -72,57 +79,16 @@ pub trait LWECompressedAlloc {
}
}
fn alloc_lwe_compressed_from_infos<A>(&self, infos: &A) -> LWECompressed<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: LWEInfos,
{
self.alloc_lwe_compressed(infos.base2k(), infos.k())
Self::bytes_of(infos.base2k(), infos.k())
}
fn bytes_of_lwe_compressed(&self, base2k: Base2K, k: TorusPrecision) -> usize {
pub fn bytes_of(base2k: Base2K, k: TorusPrecision) -> usize {
Zn::bytes_of(1, 1, k.0.div_ceil(base2k.0) as usize)
}
fn bytes_of_lwe_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: LWEInfos,
{
self.bytes_of_lwe_compressed(infos.base2k(), infos.k())
}
}
impl<B: Backend> LWECompressedAlloc for Module<B> {}
impl LWECompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: LWEInfos,
M: LWECompressedAlloc,
{
module.alloc_lwe_compressed_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self
where
M: LWECompressedAlloc,
{
module.alloc_lwe_compressed(base2k, k)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: LWEInfos,
M: LWECompressedAlloc,
{
module.bytes_of_lwe_compressed_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> usize
where
M: LWECompressedAlloc,
{
module.bytes_of_lwe_compressed(base2k, k)
}
}
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

View File

@@ -6,8 +6,7 @@ use poulpy_hal::{
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWESwitchingKey, LWESwitchingKeyToMut, Rank, TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
};
use std::fmt;
@@ -85,99 +84,65 @@ impl<D: DataRef> WriterTo for LWESwitchingKeyCompressed<D> {
}
}
pub trait LWESwitchingKeyCompressedAlloc
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn alloc_lwe_switching_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
dnum: Dnum,
) -> LWESwitchingKeyCompressed<Vec<u8>> {
LWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)))
}
fn alloc_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> LWESwitchingKeyCompressed<Vec<u8>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
self.alloc_lwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum())
}
fn bytes_of_lwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1))
}
fn bytes_of_lwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
self.bytes_of_glwe_switching_key_compressed_from_infos(infos)
}
}
impl<B: Backend> LWESwitchingKeyCompressedAlloc for Module<B> where Self: GLWESwitchingKeyCompressedAlloc {}
impl LWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
M: LWESwitchingKeyCompressedAlloc,
{
module.alloc_lwe_switching_key_compressed_from_infos(infos)
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self
where
M: LWESwitchingKeyCompressedAlloc,
{
module.alloc_lwe_switching_key_compressed(base2k, k, dnum)
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
LWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
n,
base2k,
k,
Rank(1),
Rank(1),
dnum,
Dsize(1),
))
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
M: LWESwitchingKeyCompressedAlloc,
{
module.bytes_of_lwe_switching_key_compressed_from_infos(infos)
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKeyCompressed"
);
GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize
where
M: LWESwitchingKeyCompressedAlloc,
{
module.bytes_of_lwe_switching_key_compressed(base2k, k, dnum)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1))
}
}

View File

@@ -7,8 +7,7 @@ use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyToMut, Rank,
TorusPrecision,
compressed::{
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedAlloc, GLWESwitchingKeyCompressedToMut,
GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
},
};
use std::fmt;
@@ -86,90 +85,61 @@ impl<D: DataRef> WriterTo for LWEToGLWESwitchingKeyCompressed<D> {
}
}
impl<BE: Backend> LWEToGLWESwitchingKeyCompressedAlloc for Module<BE> where Self: GLWESwitchingKeyCompressedAlloc {}
pub trait LWEToGLWESwitchingKeyCompressedAlloc
where
Self: GLWESwitchingKeyCompressedAlloc,
{
fn alloc_lwe_to_glwe_switching_key_compressed(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_out: Rank,
dnum: Dnum,
) -> LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
LWEToGLWESwitchingKeyCompressed(self.alloc_glwe_switching_key_compressed(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
}
fn alloc_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKeyCompressed<Vec<u8>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
self.alloc_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
fn bytes_of_lwe_to_glwe_switching_key_compressed(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key_compressed(base2k, k, Rank(1), dnum, Dsize(1))
}
fn bytes_of_lwe_to_glwe_switching_key_compressed_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
self.bytes_of_lwe_to_glwe_switching_key_compressed(infos.base2k(), infos.k(), infos.dnum())
}
}
impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
pub fn alloc<A, M>(module: &M, infos: &A) -> Self
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyCompressedAlloc,
{
module.alloc_lwe_to_glwe_switching_key_compressed_from_infos(infos)
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.dnum(),
)
}
pub fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self
where
M: LWEToGLWESwitchingKeyCompressedAlloc,
{
module.alloc_lwe_to_glwe_switching_key_compressed(base2k, k, rank_out, dnum)
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
LWEToGLWESwitchingKeyCompressed(GLWESwitchingKeyCompressed::alloc(
n,
base2k,
k,
Rank(1),
rank_out,
dnum,
Dsize(1),
))
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_lwe_to_glwe_switching_key_compressed_from_infos(infos)
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
);
GLWESwitchingKeyCompressed::bytes_of_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize
where
M: LWEToGLWESwitchingKeyCompressedAlloc,
{
module.bytes_of_lwe_to_glwe_switching_key_compressed(base2k, k, dnum)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, Rank(1), dnum, Dsize(1))
}
}

View File

@@ -1,11 +1,11 @@
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWE, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef,
LWEInfos, Rank, TorusPrecision,
prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -144,31 +144,13 @@ impl<D: DataRef> fmt::Display for AutomorphismKey<D> {
}
}
impl<B: Backend> AutomorphismKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
pub trait AutomorphismKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_automorphism_key(
&self,
base2k: Base2K,
k: TorusPrecision,
rank: Rank,
dnum: Dnum,
dsize: Dsize,
) -> AutomorphismKey<Vec<u8>> {
AutomorphismKey {
key: self.alloc_glwe_switching_key(base2k, k, rank, rank, dnum, dsize),
p: 0,
}
}
fn alloc_automorphism_key_from_infos<A>(&self, infos: &A) -> AutomorphismKey<Vec<u8>>
impl AutomorphismKey<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
self.alloc_automorphism_key(
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
@@ -177,11 +159,14 @@ where
)
}
fn bytes_of_automorphism_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, rank, rank, dnum, dsize)
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),
p: 0,
}
}
fn bytes_of_automorphism_key_from_infos<A>(&self, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -190,7 +175,8 @@ where
infos.rank_out(),
"rank_in != rank_out is not supported for AutomorphismKey"
);
self.bytes_of_automorphism_key(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
@@ -198,37 +184,9 @@ where
infos.dsize(),
)
}
}
impl AutomorphismKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: AutomorphismKeyAlloc,
{
module.alloc_automorphism_key_from_infos(infos)
}
pub fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
M: AutomorphismKeyAlloc,
{
module.alloc_automorphism_key(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: AutomorphismKeyAlloc,
{
module.bytes_of_automorphism_key_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: AutomorphismKeyAlloc,
{
module.bytes_of_automorphism_key(base2k, k, rank, dnum, dsize)
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)
}
}

View File

@@ -1,11 +1,9 @@
use poulpy_hal::{
layouts::{
Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos,
},
layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos},
source::Source,
};
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision};
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
@@ -191,19 +189,23 @@ impl<D: DataMut> GGLWE<D> {
}
}
pub trait GGLWEAlloc
where
Self: GetDegree,
{
fn alloc_gglwe(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GGLWE<Vec<u8>> {
impl GGLWE<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, rank_out: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -220,7 +222,7 @@ where
GGLWE {
data: MatZnx::alloc(
self.ring_degree().into(),
n.into(),
dnum.into(),
rank_in.into(),
(rank_out + 1).into(),
@@ -232,11 +234,12 @@ where
}
}
fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GGLWE<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
self.alloc_gglwe(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -246,8 +249,8 @@ where
)
}
fn bytes_of_gglwe(
&self,
pub fn bytes_of(
n: Degree,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
@@ -270,77 +273,13 @@ where
);
MatZnx::bytes_of(
self.ring_degree().into(),
n.into(),
dnum.into(),
rank_in.into(),
(rank_out + 1).into(),
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_gglwe_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_gglwe(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<B: Backend> GGLWEAlloc for Module<B> where Self: GetDegree {}
impl GGLWE<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: GGLWEAlloc,
{
module.alloc_glwe_from_infos(infos)
}
pub fn alloc<M>(
module: &M,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
M: GGLWEAlloc,
{
module.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: GGLWEAlloc,
{
module.bytes_of_gglwe_from_infos(infos)
}
pub fn bytes_of<M>(
module: &M,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
M: GGLWEAlloc,
{
module.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize)
}
}
pub trait GGLWEToMut {

View File

@@ -1,11 +1,10 @@
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEAlloc, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank,
TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -163,31 +162,13 @@ impl<D: DataMut> FillUniform for GLWESwitchingKey<D> {
}
}
pub trait GLWESwitchingKeyAlloc
where
Self: GGLWEAlloc,
{
fn alloc_glwe_switching_key(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> GLWESwitchingKey<Vec<u8>> {
GLWESwitchingKey {
key: self.alloc_gglwe(base2k, k, rank_in, rank_out, dnum, dsize),
sk_in_n: 0,
sk_out_n: 0,
}
}
fn alloc_glwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWESwitchingKey<Vec<u8>>
impl GLWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
self.alloc_glwe_switching_key(
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
@@ -197,8 +178,31 @@ where
)
}
fn bytes_of_glwe_switching_key(
&self,
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,
}
}
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
pub fn bytes_of(
n: Degree,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
@@ -206,71 +210,7 @@ where
dnum: Dnum,
dsize: Dsize,
) -> usize {
self.bytes_of_gglwe(base2k, k, rank_in, rank_out, dnum, dsize)
}
fn bytes_of_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
self.bytes_of_glwe_switching_key(
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.rank_out(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<B: Backend> GLWESwitchingKeyAlloc for Module<B> where Self: GGLWEAlloc {}
impl GLWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: GLWESwitchingKeyAlloc,
{
module.alloc_glwe_switching_key_from_infos(infos)
}
pub fn alloc<M>(
module: &M,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> Self
where
M: GLWESwitchingKeyAlloc,
{
module.alloc_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: GLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_switching_key_from_infos(infos)
}
pub fn bytes_of<M>(
module: &M,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
rank_out: Rank,
dnum: Dnum,
dsize: Dsize,
) -> usize
where
M: GLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_switching_key(base2k, k, rank_in, rank_out, dnum, dsize)
GGLWE::bytes_of(n, base2k, k, rank_in, rank_out, dnum, dsize)
}
}

View File

@@ -1,11 +1,11 @@
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -130,20 +130,36 @@ impl<D: DataRef> fmt::Display for TensorKey<D> {
}
}
pub trait TensorKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> TensorKey<Vec<u8>> {
impl TensorKey<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in(),
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKey"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let pairs: u32 = (((rank.0 + 1) * rank.0) >> 1).max(1);
TensorKey {
keys: (0..pairs)
.map(|_| self.alloc_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize))
.map(|_| GLWESwitchingKey::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
.collect(),
}
}
fn alloc_tensor_key_from_infos<A>(&self, infos: &A) -> TensorKey<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
{
@@ -152,7 +168,8 @@ where
infos.rank_out(),
"rank_in != rank_out is not supported for GGLWETensorKey"
);
self.alloc_tensor_key(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
@@ -161,61 +178,9 @@ where
)
}
fn bytes_of_tensor_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
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 * self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank, dnum, dsize)
}
fn bytes_of_tensor_key_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 GGLWETensorKey"
);
self.bytes_of_tensor_key(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl<B: Backend> TensorKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl TensorKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGLWEInfos,
M: TensorKeyAlloc,
{
module.alloc_tensor_key_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
M: TensorKeyAlloc,
{
module.alloc_tensor_key(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: TensorKeyAlloc,
{
module.bytes_of_tensor_key_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: TensorKeyAlloc,
{
module.bytes_of_tensor_key(base2k, k, rank, dnum, dsize)
pairs * GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank, dnum, dsize)
}
}

View File

@@ -1,12 +1,10 @@
use poulpy_hal::{
layouts::{
Backend, Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, Module, ReaderFrom, WriterTo, ZnxInfos,
},
layouts::{Data, DataMut, DataRef, FillUniform, MatZnx, MatZnxToMut, MatZnxToRef, ReaderFrom, WriterTo, ZnxInfos},
source::Source,
};
use std::fmt;
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision};
use crate::layouts::{Base2K, Degree, Dnum, Dsize, GLWE, GLWEInfos, LWEInfos, Rank, TorusPrecision};
pub trait GGSWInfos
where
@@ -152,13 +150,22 @@ impl<D: DataMut> GGSW<D> {
}
}
impl<B: Backend> GGSWAlloc for Module<B> where Self: GetDegree {}
impl GGSW<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGSWInfos,
{
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
pub trait GGSWAlloc
where
Self: GetDegree,
{
fn alloc_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> GGSW<Vec<u8>> {
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -175,7 +182,7 @@ where
GGSW {
data: MatZnx::alloc(
self.ring_degree().into(),
n.into(),
dnum.into(),
(rank + 1).into(),
(rank + 1).into(),
@@ -187,11 +194,12 @@ where
}
}
fn alloc_ggsw_from_infos<A>(&self, infos: &A) -> GGSW<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGSWInfos,
{
self.alloc_ggsw(
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank(),
@@ -200,7 +208,7 @@ where
)
}
fn bytes_of_ggsw(&self, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
let size: usize = k.0.div_ceil(base2k.0) as usize;
debug_assert!(
size as u32 > dsize.0,
@@ -216,58 +224,13 @@ where
);
MatZnx::bytes_of(
self.ring_degree().into(),
n.into(),
dnum.into(),
(rank + 1).into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_ggsw_from_infos<A>(&self, infos: &A) -> usize
where
A: GGSWInfos,
{
self.bytes_of_ggsw(
infos.base2k(),
infos.k(),
infos.rank(),
infos.dnum(),
infos.dsize(),
)
}
}
impl GGSW<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GGSWInfos,
M: GGSWAlloc,
{
module.alloc_ggsw_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
where
M: GGSWAlloc,
{
module.alloc_ggsw(base2k, k, rank, dnum, dsize)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GGSWInfos,
M: GGSWAlloc,
{
module.bytes_of_ggsw_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
where
M: GGSWAlloc,
{
module.bytes_of_ggsw(base2k, k, rank, dnum, dsize)
}
}
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

View File

@@ -1,12 +1,11 @@
use poulpy_hal::{
layouts::{
Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef,
WriterTo, ZnxInfos,
Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos,
},
source::Source,
};
use crate::layouts::{Base2K, Degree, GetDegree, LWEInfos, Rank, TorusPrecision};
use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::fmt;
@@ -146,76 +145,31 @@ impl<D: DataMut> FillUniform for GLWE<D> {
}
}
pub trait GLWEAlloc
where
Self: GetDegree,
{
fn alloc_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWE<Vec<u8>> {
impl GLWE<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
GLWE {
data: VecZnx::alloc(
self.ring_degree().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
),
data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
base2k,
k,
}
}
fn alloc_glwe_from_infos<A>(&self, infos: &A) -> GLWE<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GLWEInfos,
{
self.alloc_glwe(infos.base2k(), infos.k(), infos.rank())
Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
fn bytes_of_glwe(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
VecZnx::bytes_of(
self.ring_degree().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_glwe_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe(infos.base2k(), infos.k(), infos.rank())
}
}
impl<B: Backend> GLWEAlloc for Module<B> where Self: GetDegree {}
impl GLWE<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GLWEInfos,
M: GLWEAlloc,
{
module.alloc_glwe_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
where
M: GLWEAlloc,
{
module.alloc_glwe(base2k, k, rank)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWEAlloc,
{
module.bytes_of_glwe_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize
where
M: GLWEAlloc,
{
module.bytes_of_glwe(base2k, k, rank)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
}
}

View File

@@ -1,11 +1,9 @@
use poulpy_hal::layouts::{
Backend, Data, DataMut, DataRef, Module, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos,
};
use poulpy_hal::layouts::{Data, DataMut, DataRef, ReaderFrom, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos};
use crate::{
GetDistribution, GetDistributionMut,
dist::Distribution,
layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision},
layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision},
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
@@ -85,77 +83,32 @@ impl GLWEInfos for GLWEPublicKeyLayout {
}
}
pub trait GLWEPublicKeyAlloc
where
Self: GetDegree,
{
fn alloc_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKey<Vec<u8>> {
impl GLWEPublicKey<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
GLWEPublicKey {
data: VecZnx::alloc(
self.ring_degree().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
),
data: VecZnx::alloc(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
base2k,
k,
dist: Distribution::NONE,
}
}
fn alloc_glwe_public_key_from_infos<A>(&self, infos: &A) -> GLWEPublicKey<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GLWEInfos,
{
self.alloc_glwe_public_key(infos.base2k(), infos.k(), infos.rank())
Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
fn bytes_of_glwe_public_key(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
VecZnx::bytes_of(
self.ring_degree().into(),
(rank + 1).into(),
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_glwe_public_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe_public_key(infos.base2k(), infos.k(), infos.rank())
}
}
impl<B: Backend> GLWEPublicKeyAlloc for Module<B> where Self: GetDegree {}
impl GLWEPublicKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GLWEInfos,
M: GLWEPublicKeyAlloc,
{
module.alloc_glwe_public_key_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
where
M: GLWEPublicKeyAlloc,
{
module.alloc_glwe_public_key(base2k, k, rank)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWEPublicKeyAlloc,
{
module.bytes_of_glwe_public_key_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize
where
M: GLWEPublicKeyAlloc,
{
module.bytes_of_glwe_public_key(base2k, k, rank)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
VecZnx::bytes_of(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
}
}

View File

@@ -1,10 +1,8 @@
use std::fmt;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos};
use poulpy_hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos};
use crate::layouts::{
Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision,
};
use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, LWEInfos, Rank, SetGLWEInfos, TorusPrecision};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct GLWEPlaintextLayout {
@@ -85,76 +83,31 @@ impl<D: DataRef> fmt::Display for GLWEPlaintext<D> {
}
}
pub trait GLWEPlaintextAlloc
where
Self: GetDegree,
{
fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext<Vec<u8>> {
impl GLWEPlaintext<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc(infos.n(), infos.base2k(), infos.k())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self {
GLWEPlaintext {
data: VecZnx::alloc(
self.ring_degree().into(),
1,
k.0.div_ceil(base2k.0) as usize,
),
data: VecZnx::alloc(n.into(), 1, k.0.div_ceil(base2k.0) as usize),
base2k,
k,
}
}
fn alloc_glwe_plaintext_from_infos<A>(&self, infos: &A) -> GLWEPlaintext<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GLWEInfos,
{
self.alloc_glwe_plaintext(infos.base2k(), infos.k())
Self::bytes_of(infos.n(), infos.base2k(), infos.k())
}
fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::bytes_of(
self.ring_degree().into(),
1,
k.0.div_ceil(base2k.0) as usize,
)
}
fn bytes_of_glwe_plaintext_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe_plaintext(infos.base2k(), infos.k())
}
}
impl<B: Backend> GLWEPlaintextAlloc for Module<B> where Self: GetDegree {}
impl GLWEPlaintext<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GLWEInfos,
M: GLWEPlaintextAlloc,
{
module.alloc_glwe_plaintext_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self
where
M: GLWEPlaintextAlloc,
{
module.alloc_glwe_plaintext(base2k, k)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWEPlaintextAlloc,
{
module.bytes_of_glwe_plaintext_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> usize
where
M: GLWEPlaintextAlloc,
{
module.bytes_of_glwe_plaintext(base2k, k)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
VecZnx::bytes_of(n.into(), 1, k.0.div_ceil(base2k.0) as usize)
}
}

View File

@@ -1,15 +1,12 @@
use poulpy_hal::{
layouts::{
Backend, Data, DataMut, DataRef, Module, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos,
ZnxZero,
},
layouts::{Data, DataMut, DataRef, ReaderFrom, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, WriterTo, ZnxInfos, ZnxZero},
source::Source,
};
use crate::{
GetDistribution,
dist::Distribution,
layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision},
layouts::{Base2K, Degree, GLWEInfos, LWEInfos, Rank, TorusPrecision},
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -77,67 +74,30 @@ impl<D: Data> GLWEInfos for GLWESecret<D> {
}
}
pub trait GLWESecretAlloc
where
Self: GetDegree,
{
fn alloc_glwe_secret(&self, rank: Rank) -> GLWESecret<Vec<u8>> {
impl GLWESecret<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc(infos.n(), infos.rank())
}
pub fn alloc(n: Degree, rank: Rank) -> Self {
GLWESecret {
data: ScalarZnx::alloc(self.ring_degree().into(), rank.into()),
data: ScalarZnx::alloc(n.into(), rank.into()),
dist: Distribution::NONE,
}
}
fn alloc_glwe_secret_from_infos<A>(&self, infos: &A) -> GLWESecret<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GLWEInfos,
{
self.alloc_glwe_secret(infos.rank())
Self::bytes_of(infos.n(), infos.rank())
}
fn bytes_of_glwe_secret(&self, rank: Rank) -> usize {
ScalarZnx::bytes_of(self.ring_degree().into(), rank.into())
}
fn bytes_of_glwe_secret_from_infos<A>(&self, infos: &A) -> usize
where
A: GLWEInfos,
{
self.bytes_of_glwe_secret(infos.rank())
}
}
impl<B: Backend> GLWESecretAlloc for Module<B> where Self: GetDegree {}
impl GLWESecret<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: GLWEInfos,
M: GLWESecretAlloc,
{
module.alloc_glwe_secret_from_infos(infos)
}
pub fn alloc<M>(module: &M, rank: Rank) -> Self
where
M: GLWESecretAlloc,
{
module.alloc_glwe_secret(rank)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: GLWEInfos,
M: GLWESecretAlloc,
{
module.bytes_of_glwe_secret_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, rank: Rank) -> usize
where
M: GLWESecretAlloc,
{
module.bytes_of_glwe_secret(rank)
pub fn bytes_of(n: Degree, rank: Rank) -> usize {
ScalarZnx::bytes_of(n.into(), rank.into())
}
}

View File

@@ -1,11 +1,11 @@
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
use std::fmt;
@@ -132,90 +132,67 @@ impl<D: DataRef> WriterTo for GLWEToLWESwitchingKey<D> {
}
}
pub trait GLWEToLWESwitchingKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_glwe_to_lwe_switching_key(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_in: Rank,
dnum: Dnum,
) -> GLWEToLWESwitchingKey<Vec<u8>> {
GLWEToLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
}
fn alloc_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKey<Vec<u8>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
self.alloc_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
fn bytes_of_glwe_to_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
}
fn bytes_of_glwe_to_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
self.bytes_of_glwe_to_lwe_switching_key(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
}
}
impl<B: Backend> GLWEToLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyAlloc,
{
module.alloc_glwe_to_lwe_switching_key_from_infos(infos)
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self
where
M: GLWEToLWESwitchingKeyAlloc,
{
module.alloc_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum)
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
GLWEToLWESwitchingKey(GLWESwitchingKey::alloc(
n,
base2k,
k,
rank_in,
Rank(1),
dnum,
Dsize(1),
))
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_to_lwe_switching_key_from_infos(infos)
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for GLWEToLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for GLWEToLWESwitchingKey"
);
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_in(),
infos.dnum(),
)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize
where
M: GLWEToLWESwitchingKeyAlloc,
{
module.bytes_of_glwe_to_lwe_switching_key(base2k, k, rank_in, dnum)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
GLWESwitchingKey::bytes_of(n, base2k, k, rank_in, Rank(1), dnum, Dsize(1))
}
}

View File

@@ -1,7 +1,7 @@
use std::fmt;
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos},
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo, Zn, ZnToMut, ZnToRef, ZnxInfos},
source::Source,
};
@@ -125,8 +125,15 @@ where
}
}
pub trait LWEAlloc {
fn alloc_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> LWE<Vec<u8>> {
impl LWE<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: LWEInfos,
{
Self::alloc(infos.n(), infos.base2k(), infos.k())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision) -> Self {
LWE {
data: Zn::alloc((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize),
k,
@@ -134,57 +141,16 @@ pub trait LWEAlloc {
}
}
fn alloc_lwe_from_infos<A>(&self, infos: &A) -> LWE<Vec<u8>>
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: LWEInfos,
{
self.alloc_lwe(infos.n(), infos.base2k(), infos.k())
Self::bytes_of(infos.n(), infos.base2k(), infos.k())
}
fn bytes_of_lwe(&self, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision) -> usize {
Zn::bytes_of((n + 1).into(), 1, k.0.div_ceil(base2k.0) as usize)
}
fn bytes_of_lwe_from_infos<A>(&self, infos: &A) -> usize
where
A: LWEInfos,
{
self.bytes_of_lwe(infos.n(), infos.base2k(), infos.k())
}
}
impl<B: Backend> LWEAlloc for Module<B> {}
impl LWE<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: LWEInfos,
M: LWEAlloc,
{
module.alloc_lwe_from_infos(infos)
}
pub fn alloc<M>(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> Self
where
M: LWEAlloc,
{
module.alloc_lwe(n, base2k, k)
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
where
A: LWEInfos,
M: LWEAlloc,
{
module.bytes_of_lwe_from_infos(infos)
}
pub fn bytes_of<M>(module: &M, n: Degree, base2k: Base2K, k: TorusPrecision) -> usize
where
M: LWEAlloc,
{
module.bytes_of_lwe(n, base2k, k)
}
}
pub trait LWEToRef {

View File

@@ -1,13 +1,13 @@
use std::fmt;
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -101,94 +101,65 @@ impl<D: Data> GGLWEInfos for LWESwitchingKey<D> {
}
}
pub trait LWESwitchingKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> LWESwitchingKey<Vec<u8>> {
LWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1)))
}
fn alloc_lwe_switching_key_from_infos<A>(&self, infos: &A) -> LWESwitchingKey<Vec<u8>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
self.alloc_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum())
}
fn bytes_of_lwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
}
fn bytes_of_lwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
self.bytes_of_lwe_switching_key(infos.base2k(), infos.k(), infos.dnum())
}
}
impl<B: Backend> LWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl LWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
M: LWESwitchingKeyAlloc,
{
module.alloc_lwe_switching_key_from_infos(infos)
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.dnum())
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self
where
M: LWESwitchingKeyAlloc,
{
module.alloc_lwe_switching_key(base2k, k, dnum)
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> Self {
LWESwitchingKey(GLWESwitchingKey::alloc(
n,
base2k,
k,
Rank(1),
Rank(1),
dnum,
Dsize(1),
))
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
M: LWESwitchingKeyAlloc,
{
module.bytes_of_glwe_switching_key_from_infos(infos)
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWESwitchingKey"
);
assert_eq!(
infos.rank_out().0,
1,
"rank_out > 1 is not supported for LWESwitchingKey"
);
Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.dnum())
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize
where
M: LWESwitchingKeyAlloc,
{
module.bytes_of_lwe_switching_key(base2k, k, dnum)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), Rank(1), dnum, Dsize(1))
}
}

View File

@@ -1,6 +1,6 @@
use std::fmt;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Zn, ZnToMut, ZnToRef, ZnxInfos};
use poulpy_hal::layouts::{Data, DataMut, DataRef, Zn, ZnToMut, ZnToRef, ZnxInfos};
use crate::layouts::{Base2K, Degree, LWEInfos, TorusPrecision};
@@ -52,40 +52,21 @@ impl<D: Data> LWEInfos for LWEPlaintext<D> {
}
}
pub trait LWEPlaintextAlloc {
fn alloc_lwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> LWEPlaintext<Vec<u8>> {
impl LWEPlaintext<Vec<u8>> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: LWEInfos,
{
Self::alloc(infos.base2k(), infos.k())
}
pub fn alloc(base2k: Base2K, k: TorusPrecision) -> Self {
LWEPlaintext {
data: Zn::alloc(1, 1, k.0.div_ceil(base2k.0) as usize),
k,
base2k,
}
}
fn alloc_lwe_plaintext_from_infos<A>(&self, infos: &A) -> LWEPlaintext<Vec<u8>>
where
A: LWEInfos,
{
self.alloc_lwe_plaintext(infos.base2k(), infos.k())
}
}
impl<B: Backend> LWEPlaintextAlloc for Module<B> {}
impl LWEPlaintext<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
A: LWEInfos,
M: LWEPlaintextAlloc,
{
module.alloc_lwe_plaintext_from_infos(infos)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision) -> Self
where
M: LWEPlaintextAlloc,
{
module.alloc_lwe_plaintext(base2k, k)
}
}
impl<D: DataRef> fmt::Display for LWEPlaintext<D> {

View File

@@ -1,5 +1,5 @@
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero},
layouts::{Data, DataMut, DataRef, ScalarZnx, ScalarZnxToMut, ScalarZnxToRef, ZnxInfos, ZnxView, ZnxZero},
source::Source,
};
@@ -13,8 +13,8 @@ pub struct LWESecret<D: Data> {
pub(crate) dist: Distribution,
}
pub trait LWESecretAlloc {
fn alloc_lwe_secret(&self, n: Degree) -> LWESecret<Vec<u8>> {
impl LWESecret<Vec<u8>> {
pub fn alloc(n: Degree) -> Self {
LWESecret {
data: ScalarZnx::alloc(n.into(), 1),
dist: Distribution::NONE,
@@ -22,17 +22,6 @@ pub trait LWESecretAlloc {
}
}
impl<B: Backend> LWESecretAlloc for Module<B> {}
impl LWESecret<Vec<u8>> {
pub fn alloc<M>(module: &M, n: Degree) -> Self
where
M: LWESecretAlloc,
{
module.alloc_lwe_secret(n)
}
}
impl<D: DataRef> LWESecret<D> {
pub fn raw(&self) -> &[i64] {
self.data.at(0, 0)

View File

@@ -1,13 +1,13 @@
use std::fmt;
use poulpy_hal::{
layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyAlloc, GLWESwitchingKeyToMut,
GLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyToMut, GLWESwitchingKeyToRef, LWEInfos,
Rank, TorusPrecision,
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -131,91 +131,68 @@ impl<D: DataRef> WriterTo for LWEToGLWESwitchingKey<D> {
}
}
pub trait LWEToGLWESwitchingKeyAlloc
where
Self: GLWESwitchingKeyAlloc,
{
fn alloc_lwe_to_glwe_switching_key(
&self,
base2k: Base2K,
k: TorusPrecision,
rank_out: Rank,
dnum: Dnum,
) -> LWEToGLWESwitchingKey<Vec<u8>> {
LWEToGLWESwitchingKey(self.alloc_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1)))
}
fn alloc_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> LWEToGLWESwitchingKey<Vec<u8>>
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
self.alloc_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
fn bytes_of_lwe_to_glwe_switching_key(&self, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
self.bytes_of_glwe_switching_key(base2k, k, Rank(1), rank_out, dnum, Dsize(1))
}
fn bytes_of_lwe_to_glwe_switching_key_from_infos<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos,
{
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
self.bytes_of_lwe_to_glwe_switching_key(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
}
}
impl<B: Backend> LWEToGLWESwitchingKeyAlloc for Module<B> where Self: GLWESwitchingKeyAlloc {}
impl LWEToGLWESwitchingKey<Vec<u8>> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyAlloc,
{
module.alloc_lwe_to_glwe_switching_key_from_infos(infos)
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
Self::alloc(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.dnum(),
)
}
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self
where
M: LWEToGLWESwitchingKeyAlloc,
{
module.alloc_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum)
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
LWEToGLWESwitchingKey(GLWESwitchingKey::alloc(
n,
base2k,
k,
Rank(1),
rank_out,
dnum,
Dsize(1),
))
}
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyAlloc,
{
module.bytes_of_lwe_to_glwe_switching_key_from_infos(infos)
assert_eq!(
infos.rank_in().0,
1,
"rank_in > 1 is not supported for LWEToGLWESwitchingKey"
);
assert_eq!(
infos.dsize().0,
1,
"dsize > 1 is not supported for LWEToGLWESwitchingKey"
);
Self::bytes_of(
infos.n(),
infos.base2k(),
infos.k(),
infos.rank_out(),
infos.dnum(),
)
}
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, rank_out: Rank) -> usize
where
M: LWEToGLWESwitchingKeyAlloc,
{
module.bytes_of_lwe_to_glwe_switching_key(base2k, k, rank_out, dnum)
pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> usize {
GLWESwitchingKey::bytes_of(n, base2k, k, Rank(1), rank_out, dnum, Dsize(1))
}
}