mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
compiling CBT but failing tests
This commit is contained in:
@@ -4,8 +4,8 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::layouts::{
|
||||
AutomorphismKey, Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut,
|
||||
GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWEDecompress, GLWEInfos, GetGaloisElement, LWEInfos, Rank,
|
||||
Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWECompressedToRef,
|
||||
GGLWEDecompress, GGLWEInfos, GGLWEToMut, GLWEAutomorphismKey, GLWEDecompress, GLWEInfos, GetGaloisElement, LWEInfos, Rank,
|
||||
SetGaloisElement, TorusPrecision,
|
||||
};
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
@@ -154,7 +154,7 @@ where
|
||||
|
||||
impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWEDecompress {}
|
||||
|
||||
impl<D: DataMut> AutomorphismKey<D>
|
||||
impl<D: DataMut> GLWEAutomorphismKey<D>
|
||||
where
|
||||
Self: SetGaloisElement,
|
||||
{
|
||||
|
||||
@@ -5,7 +5,7 @@ use poulpy_hal::{
|
||||
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWECompressed, GGLWECompressedToMut, GGLWECompressedToRef, GGLWEDecompress, GGLWEInfos,
|
||||
GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToMut, TorusPrecision,
|
||||
GLWEInfos, GLWETensorKey, GLWETensorKeyToMut, LWEInfos, Rank, TorusPrecision,
|
||||
};
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use std::fmt;
|
||||
@@ -149,11 +149,11 @@ impl<D: DataRef> WriterTo for GLWETensorKeyCompressed<D> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyCompressedAtRef<D: DataRef> {
|
||||
pub trait GLWETensorKeyCompressedAtRef<D: DataRef> {
|
||||
fn at(&self, i: usize, j: usize) -> &GGLWECompressed<D>;
|
||||
}
|
||||
|
||||
impl<D: DataRef> TensorKeyCompressedAtRef<D> for GLWETensorKeyCompressed<D> {
|
||||
impl<D: DataRef> GLWETensorKeyCompressedAtRef<D> for GLWETensorKeyCompressed<D> {
|
||||
fn at(&self, mut i: usize, mut j: usize) -> &GGLWECompressed<D> {
|
||||
if i > j {
|
||||
std::mem::swap(&mut i, &mut j);
|
||||
@@ -163,11 +163,11 @@ impl<D: DataRef> TensorKeyCompressedAtRef<D> for GLWETensorKeyCompressed<D> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyCompressedAtMut<D: DataMut> {
|
||||
pub trait GLWETensorKeyCompressedAtMut<D: DataMut> {
|
||||
fn at_mut(&mut self, i: usize, j: usize) -> &mut GGLWECompressed<D>;
|
||||
}
|
||||
|
||||
impl<D: DataMut> TensorKeyCompressedAtMut<D> for GLWETensorKeyCompressed<D> {
|
||||
impl<D: DataMut> GLWETensorKeyCompressedAtMut<D> for GLWETensorKeyCompressed<D> {
|
||||
fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWECompressed<D> {
|
||||
if i > j {
|
||||
std::mem::swap(&mut i, &mut j);
|
||||
@@ -177,16 +177,16 @@ impl<D: DataMut> TensorKeyCompressedAtMut<D> for GLWETensorKeyCompressed<D> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyDecompress
|
||||
pub trait GLWETensorKeyDecompress
|
||||
where
|
||||
Self: GGLWEDecompress,
|
||||
{
|
||||
fn decompress_tensor_key<R, O>(&self, res: &mut R, other: &O)
|
||||
where
|
||||
R: TensorKeyToMut,
|
||||
O: TensorKeyCompressedToRef,
|
||||
R: GLWETensorKeyToMut,
|
||||
O: GLWETensorKeyCompressedToRef,
|
||||
{
|
||||
let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut();
|
||||
let res: &mut GLWETensorKey<&mut [u8]> = &mut res.to_mut();
|
||||
let other: &GLWETensorKeyCompressed<&[u8]> = &other.to_ref();
|
||||
|
||||
assert_eq!(
|
||||
@@ -203,23 +203,23 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> TensorKeyDecompress for Module<B> where Self: GGLWEDecompress {}
|
||||
impl<B: Backend> GLWETensorKeyDecompress for Module<B> where Self: GGLWEDecompress {}
|
||||
|
||||
impl<D: DataMut> TensorKey<D> {
|
||||
impl<D: DataMut> GLWETensorKey<D> {
|
||||
pub fn decompress<O, M>(&mut self, module: &M, other: &O)
|
||||
where
|
||||
O: TensorKeyCompressedToRef,
|
||||
M: TensorKeyDecompress,
|
||||
O: GLWETensorKeyCompressedToRef,
|
||||
M: GLWETensorKeyDecompress,
|
||||
{
|
||||
module.decompress_tensor_key(self, other);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyCompressedToMut {
|
||||
pub trait GLWETensorKeyCompressedToMut {
|
||||
fn to_mut(&mut self) -> GLWETensorKeyCompressed<&mut [u8]>;
|
||||
}
|
||||
|
||||
impl<D: DataMut> TensorKeyCompressedToMut for GLWETensorKeyCompressed<D>
|
||||
impl<D: DataMut> GLWETensorKeyCompressedToMut for GLWETensorKeyCompressed<D>
|
||||
where
|
||||
GGLWECompressed<D>: GGLWECompressedToMut,
|
||||
{
|
||||
@@ -230,11 +230,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyCompressedToRef {
|
||||
pub trait GLWETensorKeyCompressedToRef {
|
||||
fn to_ref(&self) -> GLWETensorKeyCompressed<&[u8]>;
|
||||
}
|
||||
|
||||
impl<D: DataRef> TensorKeyCompressedToRef for GLWETensorKeyCompressed<D>
|
||||
impl<D: DataRef> GLWETensorKeyCompressedToRef for GLWETensorKeyCompressed<D>
|
||||
where
|
||||
GGLWECompressed<D>: GGLWECompressedToRef,
|
||||
{
|
||||
|
||||
@@ -11,7 +11,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
pub struct AutomorphismKeyLayout {
|
||||
pub struct GLWEAutomorphismKeyLayout {
|
||||
pub n: Degree,
|
||||
pub base2k: Base2K,
|
||||
pub k: TorusPrecision,
|
||||
@@ -21,7 +21,7 @@ pub struct AutomorphismKeyLayout {
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
pub struct AutomorphismKey<D: Data> {
|
||||
pub struct GLWEAutomorphismKey<D: Data> {
|
||||
pub(crate) key: GGLWE<D>,
|
||||
pub(crate) p: i64,
|
||||
}
|
||||
@@ -34,25 +34,25 @@ pub trait SetGaloisElement {
|
||||
fn set_p(&mut self, p: i64);
|
||||
}
|
||||
|
||||
impl<D: DataMut> SetGaloisElement for AutomorphismKey<D> {
|
||||
impl<D: DataMut> SetGaloisElement for GLWEAutomorphismKey<D> {
|
||||
fn set_p(&mut self, p: i64) {
|
||||
self.p = p
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> GetGaloisElement for AutomorphismKey<D> {
|
||||
impl<D: DataRef> GetGaloisElement for GLWEAutomorphismKey<D> {
|
||||
fn p(&self) -> i64 {
|
||||
self.p
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data> AutomorphismKey<D> {
|
||||
impl<D: Data> GLWEAutomorphismKey<D> {
|
||||
pub fn p(&self) -> i64 {
|
||||
self.p
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data> LWEInfos for AutomorphismKey<D> {
|
||||
impl<D: Data> LWEInfos for GLWEAutomorphismKey<D> {
|
||||
fn n(&self) -> Degree {
|
||||
self.key.n()
|
||||
}
|
||||
@@ -70,13 +70,13 @@ impl<D: Data> LWEInfos for AutomorphismKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data> GLWEInfos for AutomorphismKey<D> {
|
||||
impl<D: Data> GLWEInfos for GLWEAutomorphismKey<D> {
|
||||
fn rank(&self) -> Rank {
|
||||
self.rank_out()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data> GGLWEInfos for AutomorphismKey<D> {
|
||||
impl<D: Data> GGLWEInfos for GLWEAutomorphismKey<D> {
|
||||
fn rank_in(&self) -> Rank {
|
||||
self.key.rank_in()
|
||||
}
|
||||
@@ -94,7 +94,7 @@ impl<D: Data> GGLWEInfos for AutomorphismKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl LWEInfos for AutomorphismKeyLayout {
|
||||
impl LWEInfos for GLWEAutomorphismKeyLayout {
|
||||
fn base2k(&self) -> Base2K {
|
||||
self.base2k
|
||||
}
|
||||
@@ -108,13 +108,13 @@ impl LWEInfos for AutomorphismKeyLayout {
|
||||
}
|
||||
}
|
||||
|
||||
impl GLWEInfos for AutomorphismKeyLayout {
|
||||
impl GLWEInfos for GLWEAutomorphismKeyLayout {
|
||||
fn rank(&self) -> Rank {
|
||||
self.rank
|
||||
}
|
||||
}
|
||||
|
||||
impl GGLWEInfos for AutomorphismKeyLayout {
|
||||
impl GGLWEInfos for GLWEAutomorphismKeyLayout {
|
||||
fn rank_in(&self) -> Rank {
|
||||
self.rank
|
||||
}
|
||||
@@ -132,25 +132,25 @@ impl GGLWEInfos for AutomorphismKeyLayout {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> fmt::Debug for AutomorphismKey<D> {
|
||||
impl<D: DataRef> fmt::Debug for GLWEAutomorphismKey<D> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> FillUniform for AutomorphismKey<D> {
|
||||
impl<D: DataMut> FillUniform for GLWEAutomorphismKey<D> {
|
||||
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
|
||||
self.key.fill_uniform(log_bound, source);
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> fmt::Display for AutomorphismKey<D> {
|
||||
impl<D: DataRef> fmt::Display for GLWEAutomorphismKey<D> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "(AutomorphismKey: p={}) {}", self.p, self.key)
|
||||
}
|
||||
}
|
||||
|
||||
impl AutomorphismKey<Vec<u8>> {
|
||||
impl GLWEAutomorphismKey<Vec<u8>> {
|
||||
pub fn alloc_from_infos<A>(infos: &A) -> Self
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
@@ -166,7 +166,7 @@ impl AutomorphismKey<Vec<u8>> {
|
||||
}
|
||||
|
||||
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
|
||||
AutomorphismKey {
|
||||
GLWEAutomorphismKey {
|
||||
key: GGLWE::alloc(n, base2k, k, rank, rank, dnum, dsize),
|
||||
p: 0,
|
||||
}
|
||||
@@ -196,38 +196,38 @@ impl AutomorphismKey<Vec<u8>> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> GGLWEToMut for AutomorphismKey<D> {
|
||||
impl<D: DataMut> GGLWEToMut for GLWEAutomorphismKey<D> {
|
||||
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
|
||||
self.key.to_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> GGLWEToRef for AutomorphismKey<D> {
|
||||
impl<D: DataMut> GGLWEToRef for GLWEAutomorphismKey<D> {
|
||||
fn to_ref(&self) -> GGLWE<&[u8]> {
|
||||
self.key.to_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> AutomorphismKey<D> {
|
||||
impl<D: DataRef> GLWEAutomorphismKey<D> {
|
||||
pub fn at(&self, row: usize, col: usize) -> GLWE<&[u8]> {
|
||||
self.key.at(row, col)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> AutomorphismKey<D> {
|
||||
impl<D: DataMut> GLWEAutomorphismKey<D> {
|
||||
pub fn at_mut(&mut self, row: usize, col: usize) -> GLWE<&mut [u8]> {
|
||||
self.key.at_mut(row, col)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> ReaderFrom for AutomorphismKey<D> {
|
||||
impl<D: DataMut> ReaderFrom for GLWEAutomorphismKey<D> {
|
||||
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
|
||||
self.p = reader.read_u64::<LittleEndian>()? as i64;
|
||||
self.key.read_from(reader)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> WriterTo for AutomorphismKey<D> {
|
||||
impl<D: DataRef> WriterTo for GLWEAutomorphismKey<D> {
|
||||
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
|
||||
writer.write_u64::<LittleEndian>(self.p as u64)?;
|
||||
self.key.write_to(writer)
|
||||
|
||||
@@ -11,7 +11,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
pub struct TensorKeyLayout {
|
||||
pub struct GLWETensorKeyLayout {
|
||||
pub n: Degree,
|
||||
pub base2k: Base2K,
|
||||
pub k: TorusPrecision,
|
||||
@@ -21,11 +21,11 @@ pub struct TensorKeyLayout {
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Clone)]
|
||||
pub struct TensorKey<D: Data> {
|
||||
pub struct GLWETensorKey<D: Data> {
|
||||
pub(crate) keys: Vec<GGLWE<D>>,
|
||||
}
|
||||
|
||||
impl<D: Data> LWEInfos for TensorKey<D> {
|
||||
impl<D: Data> LWEInfos for GLWETensorKey<D> {
|
||||
fn n(&self) -> Degree {
|
||||
self.keys[0].n()
|
||||
}
|
||||
@@ -43,13 +43,13 @@ impl<D: Data> LWEInfos for TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data> GLWEInfos for TensorKey<D> {
|
||||
impl<D: Data> GLWEInfos for GLWETensorKey<D> {
|
||||
fn rank(&self) -> Rank {
|
||||
self.keys[0].rank_out()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data> GGLWEInfos for TensorKey<D> {
|
||||
impl<D: Data> GGLWEInfos for GLWETensorKey<D> {
|
||||
fn rank_in(&self) -> Rank {
|
||||
self.rank_out()
|
||||
}
|
||||
@@ -67,7 +67,7 @@ impl<D: Data> GGLWEInfos for TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl LWEInfos for TensorKeyLayout {
|
||||
impl LWEInfos for GLWETensorKeyLayout {
|
||||
fn n(&self) -> Degree {
|
||||
self.n
|
||||
}
|
||||
@@ -81,13 +81,13 @@ impl LWEInfos for TensorKeyLayout {
|
||||
}
|
||||
}
|
||||
|
||||
impl GLWEInfos for TensorKeyLayout {
|
||||
impl GLWEInfos for GLWETensorKeyLayout {
|
||||
fn rank(&self) -> Rank {
|
||||
self.rank_out()
|
||||
}
|
||||
}
|
||||
|
||||
impl GGLWEInfos for TensorKeyLayout {
|
||||
impl GGLWEInfos for GLWETensorKeyLayout {
|
||||
fn rank_in(&self) -> Rank {
|
||||
self.rank
|
||||
}
|
||||
@@ -105,13 +105,13 @@ impl GGLWEInfos for TensorKeyLayout {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> fmt::Debug for TensorKey<D> {
|
||||
impl<D: DataRef> fmt::Debug for GLWETensorKey<D> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> FillUniform for TensorKey<D> {
|
||||
impl<D: DataMut> FillUniform for GLWETensorKey<D> {
|
||||
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
|
||||
self.keys
|
||||
.iter_mut()
|
||||
@@ -119,7 +119,7 @@ impl<D: DataMut> FillUniform for TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> fmt::Display for TensorKey<D> {
|
||||
impl<D: DataRef> fmt::Display for GLWETensorKey<D> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
writeln!(f, "(GLWETensorKey)",)?;
|
||||
for (i, key) in self.keys.iter().enumerate() {
|
||||
@@ -129,7 +129,7 @@ impl<D: DataRef> fmt::Display for TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl TensorKey<Vec<u8>> {
|
||||
impl GLWETensorKey<Vec<u8>> {
|
||||
pub fn alloc_from_infos<A>(infos: &A) -> Self
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
@@ -151,7 +151,7 @@ impl TensorKey<Vec<u8>> {
|
||||
|
||||
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 {
|
||||
GLWETensorKey {
|
||||
keys: (0..pairs)
|
||||
.map(|_| GGLWE::alloc(n, base2k, k, Rank(1), rank, dnum, dsize))
|
||||
.collect(),
|
||||
@@ -183,7 +183,7 @@ impl TensorKey<Vec<u8>> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> TensorKey<D> {
|
||||
impl<D: DataMut> GLWETensorKey<D> {
|
||||
// Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
|
||||
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWE<D> {
|
||||
if i > j {
|
||||
@@ -194,7 +194,7 @@ impl<D: DataMut> TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> TensorKey<D> {
|
||||
impl<D: DataRef> GLWETensorKey<D> {
|
||||
// Returns a reference to GGLWE_{s}(s[i] * s[j])
|
||||
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWE<D> {
|
||||
if i > j {
|
||||
@@ -205,7 +205,7 @@ impl<D: DataRef> TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut> ReaderFrom for TensorKey<D> {
|
||||
impl<D: DataMut> ReaderFrom for GLWETensorKey<D> {
|
||||
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
|
||||
let len: usize = reader.read_u64::<LittleEndian>()? as usize;
|
||||
if self.keys.len() != len {
|
||||
@@ -221,7 +221,7 @@ impl<D: DataMut> ReaderFrom for TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef> WriterTo for TensorKey<D> {
|
||||
impl<D: DataRef> WriterTo for GLWETensorKey<D> {
|
||||
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
|
||||
writer.write_u64::<LittleEndian>(self.keys.len() as u64)?;
|
||||
for key in &self.keys {
|
||||
@@ -231,31 +231,31 @@ impl<D: DataRef> WriterTo for TensorKey<D> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyToRef {
|
||||
fn to_ref(&self) -> TensorKey<&[u8]>;
|
||||
pub trait GLWETensorKeyToRef {
|
||||
fn to_ref(&self) -> GLWETensorKey<&[u8]>;
|
||||
}
|
||||
|
||||
impl<D: DataRef> TensorKeyToRef for TensorKey<D>
|
||||
impl<D: DataRef> GLWETensorKeyToRef for GLWETensorKey<D>
|
||||
where
|
||||
GGLWE<D>: GGLWEToRef,
|
||||
{
|
||||
fn to_ref(&self) -> TensorKey<&[u8]> {
|
||||
TensorKey {
|
||||
fn to_ref(&self) -> GLWETensorKey<&[u8]> {
|
||||
GLWETensorKey {
|
||||
keys: self.keys.iter().map(|c| c.to_ref()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyToMut {
|
||||
fn to_mut(&mut self) -> TensorKey<&mut [u8]>;
|
||||
pub trait GLWETensorKeyToMut {
|
||||
fn to_mut(&mut self) -> GLWETensorKey<&mut [u8]>;
|
||||
}
|
||||
|
||||
impl<D: DataMut> TensorKeyToMut for TensorKey<D>
|
||||
impl<D: DataMut> GLWETensorKeyToMut for GLWETensorKey<D>
|
||||
where
|
||||
GGLWE<D>: GGLWEToMut,
|
||||
{
|
||||
fn to_mut(&mut self) -> TensorKey<&mut [u8]> {
|
||||
TensorKey {
|
||||
fn to_mut(&mut self) -> GLWETensorKey<&mut [u8]> {
|
||||
GLWETensorKey {
|
||||
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
|
||||
pub trait LWEInfos {
|
||||
fn n(&self) -> Degree;
|
||||
fn log_n(&self) -> usize {
|
||||
(u64::BITS - (self.n().as_usize() as u64 - 1).leading_zeros()) as usize
|
||||
}
|
||||
fn k(&self) -> TorusPrecision;
|
||||
fn max_k(&self) -> TorusPrecision {
|
||||
TorusPrecision(self.k().0 * self.size() as u32)
|
||||
|
||||
@@ -2,15 +2,15 @@ use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
|
||||
|
||||
use crate::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEInfos, GGLWEPrepared, GGLWEPreparedFactory, GGLWEPreparedToMut, GGLWEPreparedToRef,
|
||||
GLWEInfos, LWEInfos, Rank, TensorKey, TensorKeyToRef, TorusPrecision,
|
||||
GLWEInfos, GLWETensorKey, GLWETensorKeyToRef, LWEInfos, Rank, TorusPrecision,
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub struct TensorKeyPrepared<D: Data, B: Backend> {
|
||||
pub struct GLWETensorKeyPrepared<D: Data, B: Backend> {
|
||||
pub(crate) keys: Vec<GGLWEPrepared<D, B>>,
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> LWEInfos for TensorKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> LWEInfos for GLWETensorKeyPrepared<D, B> {
|
||||
fn n(&self) -> Degree {
|
||||
self.keys[0].n()
|
||||
}
|
||||
@@ -28,13 +28,13 @@ impl<D: Data, B: Backend> LWEInfos for TensorKeyPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GLWEInfos for TensorKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> GLWEInfos for GLWETensorKeyPrepared<D, B> {
|
||||
fn rank(&self) -> Rank {
|
||||
self.rank_out()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: Data, B: Backend> GGLWEInfos for TensorKeyPrepared<D, B> {
|
||||
impl<D: Data, B: Backend> GGLWEInfos for GLWETensorKeyPrepared<D, B> {
|
||||
fn rank_in(&self) -> Rank {
|
||||
self.rank_out()
|
||||
}
|
||||
@@ -52,7 +52,7 @@ impl<D: Data, B: Backend> GGLWEInfos for TensorKeyPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyPreparedFactory<B: Backend>
|
||||
pub trait GLWETensorKeyPreparedFactory<B: Backend>
|
||||
where
|
||||
Self: GGLWEPreparedFactory<B>,
|
||||
{
|
||||
@@ -63,16 +63,16 @@ where
|
||||
dnum: Dnum,
|
||||
dsize: Dsize,
|
||||
rank: Rank,
|
||||
) -> TensorKeyPrepared<Vec<u8>, B> {
|
||||
) -> GLWETensorKeyPrepared<Vec<u8>, B> {
|
||||
let pairs: u32 = (((rank.as_u32() + 1) * rank.as_u32()) >> 1).max(1);
|
||||
TensorKeyPrepared {
|
||||
GLWETensorKeyPrepared {
|
||||
keys: (0..pairs)
|
||||
.map(|_| self.alloc_gglwe_prepared(base2k, k, Rank(1), rank, dnum, dsize))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn alloc_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> TensorKeyPrepared<Vec<u8>, B>
|
||||
fn alloc_tensor_key_prepared_from_infos<A>(&self, infos: &A) -> GLWETensorKeyPrepared<Vec<u8>, B>
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
{
|
||||
@@ -117,11 +117,11 @@ where
|
||||
|
||||
fn prepare_tensor_key<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
R: TensorKeyPreparedToMut<B>,
|
||||
O: TensorKeyToRef,
|
||||
R: GLWETensorKeyPreparedToMut<B>,
|
||||
O: GLWETensorKeyToRef,
|
||||
{
|
||||
let mut res: TensorKeyPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: TensorKey<&[u8]> = other.to_ref();
|
||||
let mut res: GLWETensorKeyPrepared<&mut [u8], B> = res.to_mut();
|
||||
let other: GLWETensorKey<&[u8]> = other.to_ref();
|
||||
|
||||
assert_eq!(res.keys.len(), other.keys.len());
|
||||
|
||||
@@ -131,20 +131,20 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> TensorKeyPreparedFactory<B> for Module<B> where Module<B>: GGLWEPreparedFactory<B> {}
|
||||
impl<B: Backend> GLWETensorKeyPreparedFactory<B> for Module<B> where Module<B>: GGLWEPreparedFactory<B> {}
|
||||
|
||||
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
|
||||
impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> {
|
||||
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: TensorKeyPreparedFactory<B>,
|
||||
M: GLWETensorKeyPreparedFactory<B>,
|
||||
{
|
||||
module.alloc_tensor_key_prepared_from_infos(infos)
|
||||
}
|
||||
|
||||
pub fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
|
||||
where
|
||||
M: TensorKeyPreparedFactory<B>,
|
||||
M: GLWETensorKeyPreparedFactory<B>,
|
||||
{
|
||||
module.alloc_tensor_key_prepared(base2k, k, dnum, dsize, rank)
|
||||
}
|
||||
@@ -152,20 +152,20 @@ impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
|
||||
pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: TensorKeyPreparedFactory<B>,
|
||||
M: GLWETensorKeyPreparedFactory<B>,
|
||||
{
|
||||
module.bytes_of_tensor_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: TensorKeyPreparedFactory<B>,
|
||||
M: GLWETensorKeyPreparedFactory<B>,
|
||||
{
|
||||
module.bytes_of_tensor_key_prepared(base2k, k, rank, dnum, dsize)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
|
||||
impl<D: DataMut, B: Backend> GLWETensorKeyPrepared<D, B> {
|
||||
// Returns a mutable reference to GGLWE_{s}(s[i] * s[j])
|
||||
pub fn at_mut(&mut self, mut i: usize, mut j: usize) -> &mut GGLWEPrepared<D, B> {
|
||||
if i > j {
|
||||
@@ -176,7 +176,7 @@ impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
|
||||
impl<D: DataRef, B: Backend> GLWETensorKeyPrepared<D, B> {
|
||||
// Returns a reference to GGLWE_{s}(s[i] * s[j])
|
||||
pub fn at(&self, mut i: usize, mut j: usize) -> &GGLWEPrepared<D, B> {
|
||||
if i > j {
|
||||
@@ -187,51 +187,51 @@ impl<D: DataRef, B: Backend> TensorKeyPrepared<D, B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> TensorKeyPrepared<Vec<u8>, B> {
|
||||
impl<B: Backend> GLWETensorKeyPrepared<Vec<u8>, B> {
|
||||
pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
|
||||
where
|
||||
A: GGLWEInfos,
|
||||
M: TensorKeyPreparedFactory<B>,
|
||||
M: GLWETensorKeyPreparedFactory<B>,
|
||||
{
|
||||
module.prepare_tensor_key_tmp_bytes(infos)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> TensorKeyPrepared<D, B> {
|
||||
impl<D: DataMut, B: Backend> GLWETensorKeyPrepared<D, B> {
|
||||
pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
|
||||
where
|
||||
O: TensorKeyToRef,
|
||||
M: TensorKeyPreparedFactory<B>,
|
||||
O: GLWETensorKeyToRef,
|
||||
M: GLWETensorKeyPreparedFactory<B>,
|
||||
{
|
||||
module.prepare_tensor_key(self, other, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyPreparedToMut<B: Backend> {
|
||||
fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B>;
|
||||
pub trait GLWETensorKeyPreparedToMut<B: Backend> {
|
||||
fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B>;
|
||||
}
|
||||
|
||||
impl<D: DataMut, B: Backend> TensorKeyPreparedToMut<B> for TensorKeyPrepared<D, B>
|
||||
impl<D: DataMut, B: Backend> GLWETensorKeyPreparedToMut<B> for GLWETensorKeyPrepared<D, B>
|
||||
where
|
||||
GGLWEPrepared<D, B>: GGLWEPreparedToMut<B>,
|
||||
{
|
||||
fn to_mut(&mut self) -> TensorKeyPrepared<&mut [u8], B> {
|
||||
TensorKeyPrepared {
|
||||
fn to_mut(&mut self) -> GLWETensorKeyPrepared<&mut [u8], B> {
|
||||
GLWETensorKeyPrepared {
|
||||
keys: self.keys.iter_mut().map(|c| c.to_mut()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TensorKeyPreparedToRef<B: Backend> {
|
||||
fn to_ref(&self) -> TensorKeyPrepared<&[u8], B>;
|
||||
pub trait GLWETensorKeyPreparedToRef<B: Backend> {
|
||||
fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B>;
|
||||
}
|
||||
|
||||
impl<D: DataRef, B: Backend> TensorKeyPreparedToRef<B> for TensorKeyPrepared<D, B>
|
||||
impl<D: DataRef, B: Backend> GLWETensorKeyPreparedToRef<B> for GLWETensorKeyPrepared<D, B>
|
||||
where
|
||||
GGLWEPrepared<D, B>: GGLWEPreparedToRef<B>,
|
||||
{
|
||||
fn to_ref(&self) -> TensorKeyPrepared<&[u8], B> {
|
||||
TensorKeyPrepared {
|
||||
fn to_ref(&self) -> GLWETensorKeyPrepared<&[u8], B> {
|
||||
GLWETensorKeyPrepared {
|
||||
keys: self.keys.iter().map(|c| c.to_ref()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user