mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 21:26:41 +01:00
Traits reduction, file + structs renaming
This commit is contained in:
@@ -57,9 +57,9 @@ impl<D: Data, B: Backend> GGLWEInfos for GGLWEPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GGLWEPreparedAlloc<B: Backend>
|
||||
pub trait GGLWEPreparedAlloc<BE: Backend>
|
||||
where
|
||||
Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf,
|
||||
Self: GetDegree + VmpPMatAlloc<BE> + VmpPMatBytesOf + VmpPrepare<BE> + VmpPrepareTmpBytes,
|
||||
{
|
||||
fn alloc_gglwe_prepared(
|
||||
&self,
|
||||
@@ -69,7 +69,7 @@ where
|
||||
rank_out: Rank,
|
||||
dnum: Dnum,
|
||||
dsize: Dsize,
|
||||
) -> GGLWEPrepared<Vec<u8>, B> {
|
||||
) -> GGLWEPrepared<Vec<u8>, BE> {
|
||||
let size: usize = k.0.div_ceil(base2k.0) as usize;
|
||||
debug_assert!(
|
||||
size as u32 > dsize.0,
|
||||
@@ -92,7 +92,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn alloc_gglwe_prepared_from_infos<A>(&self, infos: &A) -> GGLWEPrepared<Vec<u8>, B>
|
||||
fn alloc_gglwe_prepared_from_infos<A>(&self, infos: &A) -> GGLWEPrepared<Vec<u8>, BE>
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
@@ -147,9 +147,41 @@ where
|
||||
infos.dsize(),
|
||||
)
|
||||
}
|
||||
|
||||
fn prepare_gglwe_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.vmp_prepare_tmp_bytes(
|
||||
infos.dnum().into(),
|
||||
infos.rank_in().into(),
|
||||
(infos.rank() + 1).into(),
|
||||
infos.size(),
|
||||
)
|
||||
}
|
||||
|
||||
fn prepare_gglwe<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<BE>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<BE>,
|
||||
O: GGLWEToRef,
|
||||
{
|
||||
let mut res: GGLWEPrepared<&mut [u8], BE> = res.to_mut();
|
||||
let other: GGLWE<&[u8]> = other.to_ref();
|
||||
|
||||
assert_eq!(res.n(), self.ring_degree());
|
||||
assert_eq!(other.n(), self.ring_degree());
|
||||
assert_eq!(res.base2k, other.base2k);
|
||||
assert_eq!(res.k, other.k);
|
||||
assert_eq!(res.dsize, other.dsize);
|
||||
|
||||
self.vmp_prepare(&mut res.data, &other.data, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GGLWEPreparedAlloc<B> for Module<B> where Module<B>: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf {}
|
||||
impl<BE: Backend> GGLWEPreparedAlloc<BE> for Module<BE> where
|
||||
Module<BE>: GetDegree + VmpPMatAlloc<BE> + VmpPMatBytesOf + VmpPrepare<BE> + VmpPrepareTmpBytes
|
||||
{
|
||||
}
|
||||
|
||||
impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
@@ -199,47 +231,11 @@ impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GGLWEPrepare<B: Backend>
|
||||
where
|
||||
Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B>,
|
||||
{
|
||||
fn prepare_gglwe_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.vmp_prepare_tmp_bytes(
|
||||
infos.dnum().into(),
|
||||
infos.rank_in().into(),
|
||||
(infos.rank() + 1).into(),
|
||||
infos.size(),
|
||||
)
|
||||
}
|
||||
|
||||
fn prepare_gglwe<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B>,
|
||||
O: GGLWEToRef,
|
||||
{
|
||||
let mut res: GGLWEPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: GGLWE<&[u8]> = other.to_ref();
|
||||
|
||||
assert_eq!(res.n(), self.ring_degree());
|
||||
assert_eq!(other.n(), self.ring_degree());
|
||||
assert_eq!(res.base2k, other.base2k);
|
||||
assert_eq!(res.k, other.k);
|
||||
assert_eq!(res.dsize, other.dsize);
|
||||
|
||||
self.vmp_prepare(&mut res.data, &other.data, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GGLWEPrepare<B> for Module<B> where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B> {}
|
||||
|
||||
impl<D: DataMut, B: Backend> GGLWEPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: GGLWEToRef,
|
||||
M: GGLWEPrepare<B>,
|
||||
M: GGLWEPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_gglwe(self, other, scratch);
|
||||
}
|
||||
@@ -248,7 +244,7 @@ impl<D: DataMut, B: Backend> GGLWEPrepared<D, B> {
|
||||
impl<B: Backend> GGLWEPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
|
||||
where
|
||||
M: GGLWEPrepare<B>,
|
||||
M: GGLWEPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_gglwe_tmp_bytes(self)
|
||||
}
|
||||
@@ -51,7 +51,7 @@ impl<D: Data, B: Backend> GGSWInfos for GGSWPrepared<D, B> {
|
||||
|
||||
pub trait GGSWPreparedAlloc<B: Backend>
|
||||
where
|
||||
Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf,
|
||||
Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare<B>,
|
||||
{
|
||||
fn alloc_ggsw_prepared(
|
||||
&self,
|
||||
@@ -133,9 +133,39 @@ where
|
||||
infos.rank(),
|
||||
)
|
||||
}
|
||||
|
||||
fn ggsw_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGSWInfos,
|
||||
{
|
||||
assert_eq!(self.ring_degree(), infos.n());
|
||||
self.vmp_prepare_tmp_bytes(
|
||||
infos.dnum().into(),
|
||||
(infos.rank() + 1).into(),
|
||||
(infos.rank() + 1).into(),
|
||||
infos.size(),
|
||||
)
|
||||
}
|
||||
fn ggsw_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGSWPreparedToMut<B>,
|
||||
O: GGSWToRef,
|
||||
{
|
||||
let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: GGSW<&[u8]> = other.to_ref();
|
||||
assert_eq!(res.n(), self.ring_degree());
|
||||
assert_eq!(other.n(), self.ring_degree());
|
||||
assert_eq!(res.k, other.k);
|
||||
assert_eq!(res.base2k, other.base2k);
|
||||
assert_eq!(res.dsize, other.dsize);
|
||||
self.vmp_prepare(&mut res.data, &other.data, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GGSWPreparedAlloc<B> for Module<B> where Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf {}
|
||||
impl<B: Backend> GGSWPreparedAlloc<B> for Module<B> where
|
||||
Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare<B>
|
||||
{
|
||||
}
|
||||
|
||||
impl<B: Backend> GGSWPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
@@ -175,45 +205,11 @@ impl<D: DataRef, B: Backend> GGSWPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GGSWPrepare<B: Backend>
|
||||
where
|
||||
Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B>,
|
||||
{
|
||||
fn ggsw_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGSWInfos,
|
||||
{
|
||||
assert_eq!(self.ring_degree(), infos.n());
|
||||
self.vmp_prepare_tmp_bytes(
|
||||
infos.dnum().into(),
|
||||
(infos.rank() + 1).into(),
|
||||
(infos.rank() + 1).into(),
|
||||
infos.size(),
|
||||
)
|
||||
}
|
||||
fn ggsw_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGSWPreparedToMut<B>,
|
||||
O: GGSWToRef,
|
||||
{
|
||||
let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: GGSW<&[u8]> = other.to_ref();
|
||||
assert_eq!(res.n(), self.ring_degree());
|
||||
assert_eq!(other.n(), self.ring_degree());
|
||||
assert_eq!(res.k, other.k);
|
||||
assert_eq!(res.base2k, other.base2k);
|
||||
assert_eq!(res.dsize, other.dsize);
|
||||
self.vmp_prepare(&mut res.data, &other.data, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GGSWPrepare<B> for Module<B> where Self: GetDegree + VmpPrepareTmpBytes + VmpPrepare<B> {}
|
||||
|
||||
impl<B: Backend> GGSWPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GGSWInfos,
|
||||
M: GGSWPrepare<B>,
|
||||
M: GGSWPreparedAlloc<B>,
|
||||
{
|
||||
module.ggsw_prepare_tmp_bytes(infos)
|
||||
}
|
||||
@@ -223,7 +219,7 @@ impl<D: DataMut, B: Backend> GGSWPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: GGSWToRef,
|
||||
M: GGSWPrepare<B>,
|
||||
M: GGSWPreparedAlloc<B>,
|
||||
{
|
||||
module.ggsw_prepare(self, other, scratch);
|
||||
}
|
||||
161
poulpy-core/src/layouts/prepared/glwe.rs
Normal file
161
poulpy-core/src/layouts/prepared/glwe.rs
Normal file
@@ -0,0 +1,161 @@
|
||||
use poulpy_hal::{
|
||||
api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf},
|
||||
layouts::{Backend, Data, DataMut, DataRef, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos},
|
||||
};
|
||||
|
||||
use crate::layouts::{Base2K, Degree, GLWE, GLWEInfos, GLWEToRef, GetDegree, LWEInfos, Rank, TorusPrecision};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct GLWEPrepared<D: Data, B: Backend> {
|
||||
pub(crate) data: VecZnxDft<D, B>,
|
||||
pub(crate) base2k: Base2K,
|
||||
pub(crate) k: TorusPrecision,
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> LWEInfos for GLWEPrepared<D, B> {
|
||||
fn base2k(&self) -> Base2K {
|
||||
self.base2k
|
||||
}
|
||||
|
||||
fn k(&self) -> TorusPrecision {
|
||||
self.k
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
self.data.size()
|
||||
}
|
||||
|
||||
fn n(&self) -> Degree {
|
||||
Degree(self.data.n() as u32)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GLWEInfos for GLWEPrepared<D, B> {
|
||||
fn rank(&self) -> Rank {
|
||||
Rank(self.data.cols() as u32 - 1)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWEPreparedAlloc<B: Backend>
|
||||
where
|
||||
Self: GetDegree + VecZnxDftAlloc<B> + VecZnxDftBytesOf + VecZnxDftApply<B>,
|
||||
{
|
||||
fn alloc_glwe_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPrepared<Vec<u8>, B> {
|
||||
GLWEPrepared {
|
||||
data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
|
||||
base2k,
|
||||
k,
|
||||
}
|
||||
}
|
||||
|
||||
fn alloc_glwe_prepared_from_infos<A>(&self, infos: &A) -> GLWEPrepared<Vec<u8>, B>
|
||||
where
|
||||
A: GLWEInfos,
|
||||
{
|
||||
self.alloc_glwe_prepared(infos.base2k(), infos.k(), infos.rank())
|
||||
}
|
||||
|
||||
fn bytes_of_glwe_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
|
||||
self.bytes_of_vec_znx_dft((rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
|
||||
}
|
||||
|
||||
fn bytes_of_glwe_prepared_from_infos<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GLWEInfos,
|
||||
{
|
||||
self.bytes_of_glwe_prepared(infos.base2k(), infos.k(), infos.rank())
|
||||
}
|
||||
|
||||
fn prepare_glwe<R, O>(&self, res: &mut R, other: &O)
|
||||
where
|
||||
R: GLWEPreparedToMut<B>,
|
||||
O: GLWEToRef,
|
||||
{
|
||||
{
|
||||
let mut res: GLWEPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: GLWE<&[u8]> = other.to_ref();
|
||||
|
||||
assert_eq!(res.n(), self.ring_degree());
|
||||
assert_eq!(other.n(), self.ring_degree());
|
||||
assert_eq!(res.size(), other.size());
|
||||
assert_eq!(res.k(), other.k());
|
||||
assert_eq!(res.base2k(), other.base2k());
|
||||
|
||||
for i in 0..(res.rank() + 1).into() {
|
||||
self.vec_znx_dft_apply(1, 0, &mut res.data, i, &other.data, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWEPreparedAlloc<B> for Module<B> where Self: VecZnxDftAlloc<B> + VecZnxDftBytesOf + VecZnxDftApply<B> {}
|
||||
|
||||
impl<B: Backend> GLWEPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
where
|
||||
A: GLWEInfos,
|
||||
M: GLWEPreparedAlloc<B>,
|
||||
{
|
||||
module.alloc_glwe_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self
|
||||
where
|
||||
M: GLWEPreparedAlloc<B>,
|
||||
{
|
||||
module.alloc_glwe_prepared(base2k, k, rank)
|
||||
}
|
||||
|
||||
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GLWEInfos,
|
||||
M: GLWEPreparedAlloc<B>,
|
||||
{
|
||||
module.bytes_of_glwe_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize
|
||||
where
|
||||
M: GLWEPreparedAlloc<B>,
|
||||
{
|
||||
module.bytes_of_glwe_prepared(base2k, k, rank)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> GLWEPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O)
|
||||
where
|
||||
O: GLWEToRef,
|
||||
M: GLWEPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_glwe(self, other);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWEPreparedToMut<B: Backend> {
|
||||
fn to_mut(&mut self) -> GLWEPrepared<&mut [u8], B>;
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> GLWEPreparedToMut<B> for GLWEPrepared<D, B> {
|
||||
fn to_mut(&mut self) -> GLWEPrepared<&mut [u8], B> {
|
||||
GLWEPrepared {
|
||||
k: self.k,
|
||||
base2k: self.base2k,
|
||||
data: self.data.to_mut(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWEPreparedToRef<B: Backend> {
|
||||
fn to_ref(&self) -> GLWEPrepared<&[u8], B>;
|
||||
}
|
||||
|
||||
impl<D: DataRef, B: Backend> GLWEPreparedToRef<B> for GLWEPrepared<D, B> {
|
||||
fn to_ref(&self) -> GLWEPrepared<&[u8], B> {
|
||||
GLWEPrepared {
|
||||
data: self.data.to_ref(),
|
||||
k: self.k,
|
||||
base2k: self.base2k,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
|
||||
GGLWEPreparedToRef, GGLWEToRef, GLWEInfos, LWEInfos, Rank, TorusPrecision,
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef,
|
||||
GGLWEToRef, GLWEInfos, GetGaloisElement, LWEInfos, Rank, SetGaloisElement, TorusPrecision,
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct AutomorphismKeyPrepared<D: Data, B: Backend> {
|
||||
pub struct GLWEAutomorphismKeyPrepared<D: Data, B: Backend> {
|
||||
pub(crate) key: GGLWEPrepared<D, B>,
|
||||
pub(crate) p: i64,
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> LWEInfos for AutomorphismKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> LWEInfos for GLWEAutomorphismKeyPrepared<D, B> {
|
||||
fn n(&self) -> Degree {
|
||||
self.key.n()
|
||||
}
|
||||
@@ -29,33 +29,25 @@ impl<D: Data, B: Backend> LWEInfos for AutomorphismKeyPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GetAutomorphismGaloisElement {
|
||||
fn p(&self) -> i64;
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> GetGaloisElement for GLWEAutomorphismKeyPrepared<D, B> {
|
||||
fn p(&self) -> i64 {
|
||||
self.p
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SetAutomorphismGaloisElement {
|
||||
fn set_p(&mut self, p: i64);
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> SetAutomorphismGaloisElement for AutomorphismKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> SetGaloisElement for GLWEAutomorphismKeyPrepared<D, B> {
|
||||
fn set_p(&mut self, p: i64) {
|
||||
self.p = p
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GLWEInfos for AutomorphismKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> GLWEInfos for GLWEAutomorphismKeyPrepared<D, B> {
|
||||
fn rank(&self) -> Rank {
|
||||
self.rank_out()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> GGLWEInfos for GLWEAutomorphismKeyPrepared<D, B> {
|
||||
fn rank_in(&self) -> Rank {
|
||||
self.key.rank_in()
|
||||
}
|
||||
@@ -73,7 +65,7 @@ impl<D: Data, B: Backend> GGLWEInfos for AutomorphismKeyPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AutomorphismKeyPreparedAlloc<B: Backend>
|
||||
pub trait GLWEAutomorphismKeyPreparedApi<B: Backend>
|
||||
where
|
||||
Self: GGLWEPreparedAlloc<B>,
|
||||
{
|
||||
@@ -84,14 +76,14 @@ where
|
||||
rank: Rank,
|
||||
dnum: Dnum,
|
||||
dsize: Dsize,
|
||||
) -> AutomorphismKeyPrepared<Vec<u8>, B> {
|
||||
AutomorphismKeyPrepared::<Vec<u8>, B> {
|
||||
) -> GLWEAutomorphismKeyPrepared<Vec<u8>, B> {
|
||||
GLWEAutomorphismKeyPrepared::<Vec<u8>, B> {
|
||||
key: self.alloc_gglwe_prepared(base2k, k, rank, rank, dnum, dsize),
|
||||
p: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn alloc_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> AutomorphismKeyPrepared<Vec<u8>, B>
|
||||
fn alloc_automorphism_key_prepared_from_infos<A>(&self, infos: &A) -> GLWEAutomorphismKeyPrepared<Vec<u8>, B>
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
@@ -137,46 +129,7 @@ where
|
||||
infos.dsize(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> AutomorphismKeyPreparedAlloc<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
|
||||
|
||||
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: AutomorphismKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.alloc_automorphism_key_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
|
||||
where
|
||||
M: AutomorphismKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
|
||||
}
|
||||
|
||||
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: AutomorphismKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.bytes_of_automorphism_key_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
|
||||
where
|
||||
M: AutomorphismKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AutomorphismKeyPrepare<B: Backend>
|
||||
where
|
||||
Self: GGLWEPrepare<B>,
|
||||
{
|
||||
fn prepare_automorphism_key_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
@@ -186,42 +139,74 @@ where
|
||||
|
||||
fn prepare_automorphism_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + SetAutomorphismGaloisElement,
|
||||
O: GGLWEToRef + GetAutomorphismGaloisElement,
|
||||
R: GGLWEPreparedToMut<B> + SetGaloisElement,
|
||||
O: GGLWEToRef + GetGaloisElement,
|
||||
{
|
||||
self.prepare_gglwe(res, other, scratch);
|
||||
res.set_p(other.p());
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> AutomorphismKeyPrepare<B> for Module<B> where Module<B>: GGLWEPrepare<B> {}
|
||||
impl<B: Backend> GLWEAutomorphismKeyPreparedApi<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
|
||||
|
||||
impl<B: Backend> AutomorphismKeyPrepared<Vec<u8>, B> {
|
||||
impl<B: Backend> GLWEAutomorphismKeyPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: GLWEAutomorphismKeyPreparedApi<B>,
|
||||
{
|
||||
module.alloc_automorphism_key_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
|
||||
where
|
||||
M: GLWEAutomorphismKeyPreparedApi<B>,
|
||||
{
|
||||
module.alloc_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
|
||||
}
|
||||
|
||||
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: GLWEAutomorphismKeyPreparedApi<B>,
|
||||
{
|
||||
module.bytes_of_automorphism_key_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
|
||||
where
|
||||
M: GLWEAutomorphismKeyPreparedApi<B>,
|
||||
{
|
||||
module.bytes_of_automorphism_key_prepared(base2k, k, rank, dnum, dsize)
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWEAutomorphismKeyPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
|
||||
where
|
||||
M: AutomorphismKeyPrepare<B>,
|
||||
M: GLWEAutomorphismKeyPreparedApi<B>,
|
||||
{
|
||||
module.prepare_automorphism_key_tmp_bytes(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> AutomorphismKeyPrepared<D, B> {
|
||||
impl<D: DataMut, B: Backend> GLWEAutomorphismKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: GGLWEToRef + GetAutomorphismGaloisElement,
|
||||
M: AutomorphismKeyPrepare<B>,
|
||||
O: GGLWEToRef + GetGaloisElement,
|
||||
M: GLWEAutomorphismKeyPreparedApi<B>,
|
||||
{
|
||||
module.prepare_automorphism_key(self, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for AutomorphismKeyPrepared<D, B> {
|
||||
impl<D: DataMut, B: Backend> GGLWEPreparedToMut<B> for GLWEAutomorphismKeyPrepared<D, B> {
|
||||
fn to_mut(&mut self) -> GGLWEPrepared<&mut [u8], B> {
|
||||
self.key.to_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for AutomorphismKeyPrepared<D, BE> {
|
||||
impl<D: DataRef, BE: Backend> GGLWEPreparedToRef<BE> for GLWEAutomorphismKeyPrepared<D, BE> {
|
||||
fn to_ref(&self) -> GGLWEPrepared<&[u8], BE> {
|
||||
self.key.to_ref()
|
||||
}
|
||||
@@ -1,19 +1,20 @@
|
||||
use poulpy_hal::{
|
||||
api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf},
|
||||
layouts::{Backend, Data, DataMut, DataRef, Module, VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, ZnxInfos},
|
||||
layouts::{Backend, Data, DataMut, DataRef, Module},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GetDistribution, GetDistributionMut,
|
||||
dist::Distribution,
|
||||
layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, LWEInfos, Rank, TorusPrecision},
|
||||
layouts::{
|
||||
Base2K, Degree, GLWEInfos, GLWEPrepared, GLWEPreparedAlloc, GLWEPreparedToMut, GLWEPreparedToRef, GLWEToRef, GetDegree,
|
||||
LWEInfos, Rank, TorusPrecision,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct GLWEPublicKeyPrepared<D: Data, B: Backend> {
|
||||
pub(crate) data: VecZnxDft<D, B>,
|
||||
pub(crate) base2k: Base2K,
|
||||
pub(crate) k: TorusPrecision,
|
||||
pub(crate) key: GLWEPrepared<D, B>,
|
||||
pub(crate) dist: Distribution,
|
||||
}
|
||||
|
||||
@@ -31,37 +32,35 @@ impl<D: DataMut, BE: Backend> GetDistributionMut for GLWEPublicKeyPrepared<D, BE
|
||||
|
||||
impl<D: Data, B: Backend> LWEInfos for GLWEPublicKeyPrepared<D, B> {
|
||||
fn base2k(&self) -> Base2K {
|
||||
self.base2k
|
||||
self.key.base2k()
|
||||
}
|
||||
|
||||
fn k(&self) -> TorusPrecision {
|
||||
self.k
|
||||
self.key.k()
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
self.data.size()
|
||||
self.key.size()
|
||||
}
|
||||
|
||||
fn n(&self) -> Degree {
|
||||
Degree(self.data.n() as u32)
|
||||
self.key.n()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GLWEInfos for GLWEPublicKeyPrepared<D, B> {
|
||||
fn rank(&self) -> Rank {
|
||||
Rank(self.data.cols() as u32 - 1)
|
||||
self.key.rank()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWEPublicKeyPreparedAlloc<B: Backend>
|
||||
where
|
||||
Self: GetDegree + VecZnxDftAlloc<B> + VecZnxDftBytesOf,
|
||||
Self: GetDegree + GLWEPreparedAlloc<B>,
|
||||
{
|
||||
fn alloc_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPublicKeyPrepared<Vec<u8>, B> {
|
||||
GLWEPublicKeyPrepared {
|
||||
data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
|
||||
base2k,
|
||||
k,
|
||||
key: self.alloc_glwe_prepared(base2k, k, rank),
|
||||
dist: Distribution::NONE,
|
||||
}
|
||||
}
|
||||
@@ -74,7 +73,7 @@ where
|
||||
}
|
||||
|
||||
fn bytes_of_glwe_public_key_prepared(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
|
||||
self.bytes_of_vec_znx_dft((rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
|
||||
self.bytes_of_glwe_prepared(base2k, k, rank)
|
||||
}
|
||||
|
||||
fn bytes_of_glwe_public_key_prepared_from_infos<A>(&self, infos: &A) -> usize
|
||||
@@ -83,9 +82,18 @@ where
|
||||
{
|
||||
self.bytes_of_glwe_public_key_prepared(infos.base2k(), infos.k(), infos.rank())
|
||||
}
|
||||
|
||||
fn prepare_glwe_public_key<R, O>(&self, res: &mut R, other: &O)
|
||||
where
|
||||
R: GLWEPreparedToMut<B> + GetDistributionMut,
|
||||
O: GLWEToRef + GetDistribution,
|
||||
{
|
||||
self.prepare_glwe(res, other);
|
||||
*res.dist_mut() = *other.dist();
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWEPublicKeyPreparedAlloc<B> for Module<B> where Self: VecZnxDftAlloc<B> + VecZnxDftBytesOf {}
|
||||
impl<B: Backend> GLWEPublicKeyPreparedAlloc<B> for Module<B> where Self: VecZnxDftAlloc<B> + VecZnxDftBytesOf + VecZnxDftApply<B> {}
|
||||
|
||||
impl<B: Backend> GLWEPublicKeyPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
@@ -119,72 +127,30 @@ impl<B: Backend> GLWEPublicKeyPrepared<Vec<u8>, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWEPublicKeyPrepare<B: Backend>
|
||||
where
|
||||
Self: GetDegree + VecZnxDftApply<B>,
|
||||
{
|
||||
fn prepare_glwe_public_key<R, O>(&self, res: &mut R, other: &O)
|
||||
where
|
||||
R: GLWEPublicKeyPreparedToMut<B> + GetDistributionMut,
|
||||
O: GLWEPublicKeyToRef + GetDistribution,
|
||||
{
|
||||
{
|
||||
let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: GLWEPublicKey<&[u8]> = other.to_ref();
|
||||
|
||||
assert_eq!(res.n(), self.ring_degree());
|
||||
assert_eq!(other.n(), self.ring_degree());
|
||||
assert_eq!(res.size(), other.size());
|
||||
assert_eq!(res.k(), other.k());
|
||||
assert_eq!(res.base2k(), other.base2k());
|
||||
|
||||
for i in 0..(res.rank() + 1).into() {
|
||||
self.vec_znx_dft_apply(1, 0, &mut res.data, i, &other.data, i);
|
||||
}
|
||||
}
|
||||
|
||||
*res.dist_mut() = *other.dist();
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWEPublicKeyPrepare<B> for Module<B> where Self: GetDegree + VecZnxDftApply<B> {}
|
||||
|
||||
impl<D: DataMut, B: Backend> GLWEPublicKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O)
|
||||
where
|
||||
O: GLWEPublicKeyToRef + GetDistribution,
|
||||
M: GLWEPublicKeyPrepare<B>,
|
||||
O: GLWEToRef + GetDistribution,
|
||||
M: GLWEPublicKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_glwe_public_key(self, other);
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
impl<D: DataMut, B: Backend> GLWEPreparedToMut<B> for GLWEPublicKeyPrepared<D, B>
|
||||
where
|
||||
GLWEPrepared<D, B>: GLWEPreparedToMut<B>,
|
||||
{
|
||||
fn to_mut(&mut self) -> GLWEPrepared<&mut [u8], B> {
|
||||
self.key.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,
|
||||
}
|
||||
impl<D: DataRef, B: Backend> GLWEPreparedToRef<B> for GLWEPublicKeyPrepared<D, B>
|
||||
where
|
||||
GLWEPrepared<D, B>: GLWEPreparedToRef<B>,
|
||||
{
|
||||
fn to_ref(&self) -> GLWEPrepared<&[u8], B> {
|
||||
self.key.to_ref()
|
||||
}
|
||||
}
|
||||
@@ -49,9 +49,9 @@ impl<D: Data, B: Backend> GLWEInfos for GLWESecretPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWESecretPreparedAlloc<B: Backend>
|
||||
pub trait GLWESecretPreparedApi<B: Backend>
|
||||
where
|
||||
Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc<B>,
|
||||
Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc<B> + SvpPrepare<B>,
|
||||
{
|
||||
fn alloc_glwe_secret_prepared(&self, rank: Rank) -> GLWESecretPrepared<Vec<u8>, B> {
|
||||
GLWESecretPrepared {
|
||||
@@ -77,56 +77,7 @@ where
|
||||
assert_eq!(self.ring_degree(), infos.n());
|
||||
self.bytes_of_glwe_secret_prepared(infos.rank())
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWESecretPreparedAlloc<B> for Module<B> where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc<B> {}
|
||||
|
||||
impl<B: Backend> GLWESecretPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
where
|
||||
A: GLWEInfos,
|
||||
M: GLWESecretPreparedAlloc<B>,
|
||||
{
|
||||
module.alloc_glwe_secret_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn alloc<M>(module: &M, rank: Rank) -> Self
|
||||
where
|
||||
M: GLWESecretPreparedAlloc<B>,
|
||||
{
|
||||
module.alloc_glwe_secret_prepared(rank)
|
||||
}
|
||||
|
||||
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GLWEInfos,
|
||||
M: GLWESecretPreparedAlloc<B>,
|
||||
{
|
||||
module.bytes_of_glwe_secret_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn bytes_of<M>(module: &M, rank: Rank) -> usize
|
||||
where
|
||||
M: GLWESecretPreparedAlloc<B>,
|
||||
{
|
||||
module.bytes_of_glwe_secret_prepared(rank)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GLWESecretPrepared<D, B> {
|
||||
pub fn n(&self) -> Degree {
|
||||
Degree(self.data.n() as u32)
|
||||
}
|
||||
|
||||
pub fn rank(&self) -> Rank {
|
||||
Rank(self.data.cols() as u32)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWESecretPrepare<B: Backend>
|
||||
where
|
||||
Self: SvpPrepare<B>,
|
||||
{
|
||||
fn prepare_glwe_secret<R, O>(&self, res: &mut R, other: &O)
|
||||
where
|
||||
R: GLWESecretPreparedToMut<B> + GetDistributionMut,
|
||||
@@ -145,12 +96,54 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWESecretPrepare<B> for Module<B> where Self: SvpPrepare<B> {}
|
||||
impl<B: Backend> GLWESecretPreparedApi<B> for Module<B> where Self: GetDegree + SvpPPolBytesOf + SvpPPolAlloc<B> + SvpPrepare<B> {}
|
||||
|
||||
impl<B: Backend> GLWESecretPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
where
|
||||
A: GLWEInfos,
|
||||
M: GLWESecretPreparedApi<B>,
|
||||
{
|
||||
module.alloc_glwe_secret_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn alloc<M>(module: &M, rank: Rank) -> Self
|
||||
where
|
||||
M: GLWESecretPreparedApi<B>,
|
||||
{
|
||||
module.alloc_glwe_secret_prepared(rank)
|
||||
}
|
||||
|
||||
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GLWEInfos,
|
||||
M: GLWESecretPreparedApi<B>,
|
||||
{
|
||||
module.bytes_of_glwe_secret_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn bytes_of<M>(module: &M, rank: Rank) -> usize
|
||||
where
|
||||
M: GLWESecretPreparedApi<B>,
|
||||
{
|
||||
module.bytes_of_glwe_secret_prepared(rank)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GLWESecretPrepared<D, B> {
|
||||
pub fn n(&self) -> Degree {
|
||||
Degree(self.data.n() as u32)
|
||||
}
|
||||
|
||||
pub fn rank(&self) -> Rank {
|
||||
Rank(self.data.cols() as u32)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> GLWESecretPrepared<D, B> {
|
||||
pub fn prepare<M, O>(&mut self, module: &M, other: &O)
|
||||
where
|
||||
M: GLWESecretPrepare<B>,
|
||||
M: GLWESecretPreparedApi<B>,
|
||||
O: GLWESecretToRef + GetDistribution,
|
||||
{
|
||||
module.prepare_glwe_secret(self, other);
|
||||
@@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEToRef, GLWEInfos, GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut,
|
||||
LWEInfos, Rank, TorusPrecision,
|
||||
prepared::{GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef},
|
||||
prepared::{GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef},
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
@@ -134,6 +134,23 @@ where
|
||||
infos.dsize(),
|
||||
)
|
||||
}
|
||||
|
||||
fn prepare_glwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_gglwe_tmp_bytes(infos)
|
||||
}
|
||||
|
||||
fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_gglwe(res, other, scratch);
|
||||
*res.input_degree() = *other.input_degree();
|
||||
*res.output_degree() = *other.output_degree();
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GGLWEPreparedAlloc<B> {}
|
||||
@@ -186,35 +203,11 @@ impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWESwitchingKeyPrepare<B: Backend>
|
||||
where
|
||||
Self: GGLWEPrepare<B>,
|
||||
{
|
||||
fn prepare_glwe_switching_key_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_gglwe_tmp_bytes(infos)
|
||||
}
|
||||
|
||||
fn prepare_glwe_switching<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_gglwe(res, other, scratch);
|
||||
*res.input_degree() = *other.input_degree();
|
||||
*res.output_degree() = *other.output_degree();
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWESwitchingKeyPrepare<B> for Module<B> where Self: GGLWEPrepare<B> {}
|
||||
|
||||
impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
M: GLWESwitchingKeyPrepare<B>,
|
||||
M: GLWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_glwe_switching(self, other, scratch);
|
||||
}
|
||||
@@ -223,7 +216,7 @@ impl<D: DataMut, B: Backend> GLWESwitchingKeyPrepared<D, B> {
|
||||
impl<B: Backend> GLWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<M>(&self, module: &M) -> usize
|
||||
where
|
||||
M: GLWESwitchingKeyPrepare<B>,
|
||||
M: GLWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_glwe_switching_key_tmp_bytes(self)
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepare, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut,
|
||||
GGLWEPreparedToRef, GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedAlloc, GGLWEPreparedToMut, GGLWEPreparedToRef,
|
||||
GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
@@ -107,6 +107,28 @@ where
|
||||
infos.dsize(),
|
||||
)
|
||||
}
|
||||
|
||||
fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_gglwe_tmp_bytes(infos)
|
||||
}
|
||||
|
||||
fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: TensorKeyPreparedToMut<B>,
|
||||
O: TensorKeyToRef,
|
||||
{
|
||||
let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: TensorKey<&[u8]> = other.to_ref();
|
||||
|
||||
assert_eq!(res.keys.len(), other.keys.len());
|
||||
|
||||
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
|
||||
self.prepare_gglwe(a, b, scratch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> TensorKeyPreparedAlloc<B> for Module<B> where Module<B>: GGLWEPreparedAlloc<B> {}
|
||||
@@ -165,40 +187,11 @@ impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyPrepare<B: Backend>
|
||||
where
|
||||
Self: GGLWEPrepare<B>,
|
||||
{
|
||||
fn prepare_tensor_key_tmp_bytes<A>(&self, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_gglwe_tmp_bytes(infos)
|
||||
}
|
||||
|
||||
fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: TensorKeyPreparedToMut<B>,
|
||||
O: TensorKeyToRef,
|
||||
{
|
||||
let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: TensorKey<&[u8]> = other.to_ref();
|
||||
|
||||
assert_eq!(res.keys.len(), other.keys.len());
|
||||
|
||||
for (a, b) in res.keys.iter_mut().zip(other.keys.iter()) {
|
||||
self.prepare_gglwe(a, b, scratch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> TensorKeyPrepare<B> for Module<B> where Self: GGLWEPrepare<B> {}
|
||||
|
||||
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: TensorKeyPrepare<B>,
|
||||
M: TensorKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_tensor_key_tmp_bytes(infos)
|
||||
}
|
||||
@@ -208,7 +201,7 @@ impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: TensorKeyToRef,
|
||||
M: TensorKeyPrepare<B>,
|
||||
M: TensorKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_tensor_key(self, other, scratch);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
|
||||
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
|
||||
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
|
||||
prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
@@ -101,6 +101,21 @@ where
|
||||
);
|
||||
self.bytes_of_glwe_to_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
|
||||
}
|
||||
|
||||
fn prepare_glwe_to_lwe_switching_key_tmp_bytes<A>(&self, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_glwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
|
||||
fn prepare_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_glwe_switching(res, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWEToLWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GLWESwitchingKeyPreparedAlloc<B> {}
|
||||
@@ -137,33 +152,11 @@ impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWEToLWESwitchingKeyPrepare<B: Backend>
|
||||
where
|
||||
Self: GLWESwitchingKeyPrepare<B>,
|
||||
{
|
||||
fn prepare_glwe_to_lwe_switching_key_tmp_bytes<A>(&self, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_glwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
|
||||
fn prepare_glwe_to_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_glwe_switching(res, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWEToLWESwitchingKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
|
||||
|
||||
impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: GLWEToLWESwitchingKeyPrepare<B>,
|
||||
M: GLWEToLWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_glwe_to_lwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
@@ -173,7 +166,7 @@ impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
M: GLWEToLWESwitchingKeyPrepare<B>,
|
||||
M: GLWEToLWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_glwe_to_lwe_switching_key(self, other, scratch);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
|
||||
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
|
||||
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
|
||||
prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
@@ -110,6 +110,20 @@ where
|
||||
);
|
||||
self.bytes_of_lwe_switching_key_prepared(infos.base2k(), infos.k(), infos.dnum())
|
||||
}
|
||||
|
||||
fn prepare_lwe_switching_key_tmp_bytes<A>(&self, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_glwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
fn prepare_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_glwe_switching(res, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> LWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GLWESwitchingKeyPreparedAlloc<B> {}
|
||||
@@ -146,32 +160,11 @@ impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait LWESwitchingKeyPrepare<B: Backend>
|
||||
where
|
||||
Self: GLWESwitchingKeyPrepare<B>,
|
||||
{
|
||||
fn prepare_lwe_switching_key_tmp_bytes<A>(&self, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_glwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
fn prepare_lwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_glwe_switching(res, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> LWESwitchingKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
|
||||
|
||||
impl<B: Backend> LWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: LWESwitchingKeyPrepare<B>,
|
||||
M: LWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_lwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
@@ -181,7 +174,7 @@ impl<D: DataMut, B: Backend> LWESwitchingKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
M: LWESwitchingKeyPrepare<B>,
|
||||
M: LWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_lwe_switching_key(self, other, scratch);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedToMut, GGLWEPreparedToRef, GGLWEToRef, GLWEInfos,
|
||||
GLWESwitchingKeyDegrees, GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
|
||||
prepared::{GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
|
||||
prepared::{GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc},
|
||||
};
|
||||
|
||||
/// A special [GLWESwitchingKey] required to for the conversion from [LWE] to [GLWE].
|
||||
@@ -108,6 +108,21 @@ where
|
||||
);
|
||||
self.bytes_of_lwe_to_glwe_switching_key_prepared(infos.base2k(), infos.k(), infos.rank_out(), infos.dnum())
|
||||
}
|
||||
|
||||
fn prepare_lwe_to_glwe_switching_key_tmp_bytes<A>(&self, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_glwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
|
||||
fn prepare_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_glwe_switching(res, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> LWEToGLWESwitchingKeyPreparedAlloc<B> for Module<B> where Self: GLWESwitchingKeyPreparedAlloc<B> {}
|
||||
@@ -144,33 +159,11 @@ impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait LWEToGLWESwitchingKeyPrepare<B: Backend>
|
||||
where
|
||||
Self: GLWESwitchingKeyPrepare<B>,
|
||||
{
|
||||
fn prepare_lwe_to_glwe_switching_key_tmp_bytes<A>(&self, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
self.prepare_glwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
|
||||
fn prepare_lwe_to_glwe_switching_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: GGLWEPreparedToMut<B> + GLWESwitchingKeyDegreesMut,
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
{
|
||||
self.prepare_glwe_switching(res, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> LWEToGLWESwitchingKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
|
||||
|
||||
impl<B: Backend> LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A)
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: LWEToGLWESwitchingKeyPrepare<B>,
|
||||
M: LWEToGLWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_lwe_to_glwe_switching_key_tmp_bytes(infos);
|
||||
}
|
||||
@@ -180,7 +173,7 @@ impl<D: DataMut, B: Backend> LWEToGLWESwitchingKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: GGLWEToRef + GLWESwitchingKeyDegrees,
|
||||
M: LWEToGLWESwitchingKeyPrepare<B>,
|
||||
M: LWEToGLWESwitchingKeyPreparedAlloc<B>,
|
||||
{
|
||||
module.prepare_lwe_to_glwe_switching_key(self, other, scratch);
|
||||
}
|
||||
@@ -1,21 +1,23 @@
|
||||
mod gglwe_atk;
|
||||
mod gglwe_ct;
|
||||
mod gglwe_ksk;
|
||||
mod gglwe_tsk;
|
||||
mod ggsw_ct;
|
||||
mod glwe_pk;
|
||||
mod glwe_sk;
|
||||
mod glwe_to_lwe_ksk;
|
||||
mod lwe_ksk;
|
||||
mod lwe_to_glwe_ksk;
|
||||
mod gglwe;
|
||||
mod ggsw;
|
||||
mod glwe;
|
||||
mod glwe_automorphism_key;
|
||||
mod glwe_public_key;
|
||||
mod glwe_secret;
|
||||
mod glwe_switching_key;
|
||||
mod glwe_tensor_key;
|
||||
mod glwe_to_lwe_switching_key;
|
||||
mod lwe_switching_key;
|
||||
mod lwe_to_glwe_switching_key;
|
||||
|
||||
pub use gglwe_atk::*;
|
||||
pub use gglwe_ct::*;
|
||||
pub use gglwe_ksk::*;
|
||||
pub use gglwe_tsk::*;
|
||||
pub use ggsw_ct::*;
|
||||
pub use glwe_pk::*;
|
||||
pub use glwe_sk::*;
|
||||
pub use glwe_to_lwe_ksk::*;
|
||||
pub use lwe_ksk::*;
|
||||
pub use lwe_to_glwe_ksk::*;
|
||||
pub use gglwe::*;
|
||||
pub use ggsw::*;
|
||||
pub use glwe::*;
|
||||
pub use glwe_automorphism_key::*;
|
||||
pub use glwe_public_key::*;
|
||||
pub use glwe_secret::*;
|
||||
pub use glwe_switching_key::*;
|
||||
pub use glwe_tensor_key::*;
|
||||
pub use glwe_to_lwe_switching_key::*;
|
||||
pub use lwe_switching_key::*;
|
||||
pub use lwe_to_glwe_switching_key::*;
|
||||
|
||||
Reference in New Issue
Block a user