mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-09 20:56:47 +01:00
* Add bivariate-convolution * Add pair-wise convolution + tests + benches * Add take_cnv_pvec_[left/right] to Scratch & updated CHANGELOG.md * cross-base2k normalization with positive offset * clippy & fix CI doctest avx compile error * more streamlined bounds derivation for normalization * Working cross-base2k normalization with pos/neg offset * Update normalization API & tests * Add glwe tensoring test * Add relinearization + preliminary test * Fix GGLWEToGGSW key infos * Add (X,Y) convolution by const (1, Y) poly * Faster normalization test + add bench for cnv_by_const * Update changelog
213 lines
4.8 KiB
Rust
213 lines
4.8 KiB
Rust
use poulpy_hal::{
|
|
layouts::{
|
|
Data, DataMut, DataRef, FillUniform, ReaderFrom, ToOwnedDeep, VecZnx, VecZnxToMut, VecZnxToRef, WriterTo, ZnxInfos,
|
|
},
|
|
source::Source,
|
|
};
|
|
|
|
use crate::layouts::{Base2K, Degree, LWEInfos, Rank, TorusPrecision};
|
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
|
use std::fmt;
|
|
|
|
pub trait GLWEInfos
|
|
where
|
|
Self: LWEInfos,
|
|
{
|
|
fn rank(&self) -> Rank;
|
|
fn glwe_layout(&self) -> GLWELayout {
|
|
GLWELayout {
|
|
n: self.n(),
|
|
base2k: self.base2k(),
|
|
k: self.k(),
|
|
rank: self.rank(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait SetGLWEInfos {
|
|
fn set_k(&mut self, k: TorusPrecision);
|
|
fn set_base2k(&mut self, base2k: Base2K);
|
|
}
|
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
|
pub struct GLWELayout {
|
|
pub n: Degree,
|
|
pub base2k: Base2K,
|
|
pub k: TorusPrecision,
|
|
pub rank: Rank,
|
|
}
|
|
|
|
impl LWEInfos for GLWELayout {
|
|
fn n(&self) -> Degree {
|
|
self.n
|
|
}
|
|
|
|
fn base2k(&self) -> Base2K {
|
|
self.base2k
|
|
}
|
|
|
|
fn k(&self) -> TorusPrecision {
|
|
self.k
|
|
}
|
|
}
|
|
|
|
impl GLWEInfos for GLWELayout {
|
|
fn rank(&self) -> Rank {
|
|
self.rank
|
|
}
|
|
}
|
|
|
|
#[derive(PartialEq, Eq, Clone)]
|
|
pub struct GLWE<D: Data> {
|
|
pub(crate) data: VecZnx<D>,
|
|
pub(crate) base2k: Base2K,
|
|
pub(crate) k: TorusPrecision,
|
|
}
|
|
|
|
impl<D: DataMut> SetGLWEInfos for GLWE<D> {
|
|
fn set_base2k(&mut self, base2k: Base2K) {
|
|
self.base2k = base2k
|
|
}
|
|
|
|
fn set_k(&mut self, k: TorusPrecision) {
|
|
self.k = k
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> GLWE<D> {
|
|
pub fn data(&self) -> &VecZnx<D> {
|
|
&self.data
|
|
}
|
|
}
|
|
|
|
impl<D: DataMut> GLWE<D> {
|
|
pub fn data_mut(&mut self) -> &mut VecZnx<D> {
|
|
&mut self.data
|
|
}
|
|
}
|
|
|
|
impl<D: Data> LWEInfos for GLWE<D> {
|
|
fn base2k(&self) -> Base2K {
|
|
self.base2k
|
|
}
|
|
|
|
fn k(&self) -> TorusPrecision {
|
|
self.k
|
|
}
|
|
|
|
fn n(&self) -> Degree {
|
|
Degree(self.data.n() as u32)
|
|
}
|
|
|
|
fn size(&self) -> usize {
|
|
self.data.size()
|
|
}
|
|
}
|
|
|
|
impl<D: Data> GLWEInfos for GLWE<D> {
|
|
fn rank(&self) -> Rank {
|
|
Rank(self.data.cols() as u32 - 1)
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> ToOwnedDeep for GLWE<D> {
|
|
type Owned = GLWE<Vec<u8>>;
|
|
fn to_owned_deep(&self) -> Self::Owned {
|
|
GLWE {
|
|
data: self.data.to_owned_deep(),
|
|
k: self.k,
|
|
base2k: self.base2k,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> fmt::Debug for GLWE<D> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{self}")
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> fmt::Display for GLWE<D> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "GLWE: base2k={} k={}: {}", self.base2k().0, self.k().0, self.data)
|
|
}
|
|
}
|
|
|
|
impl<D: DataMut> FillUniform for GLWE<D> {
|
|
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
|
|
self.data.fill_uniform(log_bound, source);
|
|
}
|
|
}
|
|
|
|
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(n.into(), (rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
|
|
base2k,
|
|
k,
|
|
}
|
|
}
|
|
|
|
pub fn bytes_of_from_infos<A>(infos: &A) -> usize
|
|
where
|
|
A: GLWEInfos,
|
|
{
|
|
Self::bytes_of(infos.n(), infos.base2k(), infos.k(), infos.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)
|
|
}
|
|
}
|
|
|
|
impl<D: DataMut> ReaderFrom for GLWE<D> {
|
|
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
|
|
self.k = TorusPrecision(reader.read_u32::<LittleEndian>()?);
|
|
self.base2k = Base2K(reader.read_u32::<LittleEndian>()?);
|
|
self.data.read_from(reader)
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> WriterTo for GLWE<D> {
|
|
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
|
|
writer.write_u32::<LittleEndian>(self.k.0)?;
|
|
writer.write_u32::<LittleEndian>(self.base2k.0)?;
|
|
self.data.write_to(writer)
|
|
}
|
|
}
|
|
|
|
pub trait GLWEToRef: Sized {
|
|
fn to_ref(&self) -> GLWE<&[u8]>;
|
|
}
|
|
|
|
impl<D: DataRef> GLWEToRef for GLWE<D> {
|
|
fn to_ref(&self) -> GLWE<&[u8]> {
|
|
GLWE {
|
|
k: self.k,
|
|
base2k: self.base2k,
|
|
data: self.data.to_ref(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait GLWEToMut: GLWEToRef {
|
|
fn to_mut(&mut self) -> GLWE<&mut [u8]>;
|
|
}
|
|
|
|
impl<D: DataMut> GLWEToMut for GLWE<D> {
|
|
fn to_mut(&mut self) -> GLWE<&mut [u8]> {
|
|
GLWE {
|
|
k: self.k,
|
|
base2k: self.base2k,
|
|
data: self.data.to_mut(),
|
|
}
|
|
}
|
|
}
|