mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 21:26:41 +01:00
227 lines
5.1 KiB
Rust
227 lines
5.1 KiB
Rust
use std::fmt;
|
|
|
|
use poulpy_hal::{
|
|
layouts::{Data, DataMut, DataRef, FillUniform, ReaderFrom, WriterTo},
|
|
source::Source,
|
|
};
|
|
|
|
use crate::layouts::{
|
|
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
|
|
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
|
|
};
|
|
|
|
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
|
pub struct LWESwitchingKeyLayout {
|
|
pub n: Degree,
|
|
pub base2k: Base2K,
|
|
pub k: TorusPrecision,
|
|
pub dnum: Dnum,
|
|
}
|
|
|
|
impl LWEInfos for LWESwitchingKeyLayout {
|
|
fn n(&self) -> Degree {
|
|
self.n
|
|
}
|
|
|
|
fn base2k(&self) -> Base2K {
|
|
self.base2k
|
|
}
|
|
|
|
fn k(&self) -> TorusPrecision {
|
|
self.k
|
|
}
|
|
}
|
|
|
|
impl GLWEInfos for LWESwitchingKeyLayout {
|
|
fn rank(&self) -> Rank {
|
|
self.rank_out()
|
|
}
|
|
}
|
|
|
|
impl GGLWEInfos for LWESwitchingKeyLayout {
|
|
fn rank_in(&self) -> Rank {
|
|
Rank(1)
|
|
}
|
|
|
|
fn dsize(&self) -> Dsize {
|
|
Dsize(1)
|
|
}
|
|
|
|
fn rank_out(&self) -> Rank {
|
|
Rank(1)
|
|
}
|
|
|
|
fn dnum(&self) -> Dnum {
|
|
self.dnum
|
|
}
|
|
}
|
|
|
|
#[derive(PartialEq, Eq, Clone)]
|
|
pub struct LWESwitchingKey<D: Data>(pub(crate) GLWESwitchingKey<D>);
|
|
|
|
impl<D: Data> LWEInfos for LWESwitchingKey<D> {
|
|
fn base2k(&self) -> Base2K {
|
|
self.0.base2k()
|
|
}
|
|
|
|
fn k(&self) -> TorusPrecision {
|
|
self.0.k()
|
|
}
|
|
|
|
fn n(&self) -> Degree {
|
|
self.0.n()
|
|
}
|
|
|
|
fn size(&self) -> usize {
|
|
self.0.size()
|
|
}
|
|
}
|
|
|
|
impl<D: Data> GLWEInfos for LWESwitchingKey<D> {
|
|
fn rank(&self) -> Rank {
|
|
self.rank_out()
|
|
}
|
|
}
|
|
|
|
impl<D: Data> GGLWEInfos for LWESwitchingKey<D> {
|
|
fn dsize(&self) -> Dsize {
|
|
self.0.dsize()
|
|
}
|
|
|
|
fn rank_in(&self) -> Rank {
|
|
self.0.rank_in()
|
|
}
|
|
|
|
fn rank_out(&self) -> Rank {
|
|
self.0.rank_out()
|
|
}
|
|
|
|
fn dnum(&self) -> Dnum {
|
|
self.0.dnum()
|
|
}
|
|
}
|
|
|
|
impl LWESwitchingKey<Vec<u8>> {
|
|
pub fn alloc_from_infos<A>(infos: &A) -> Self
|
|
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(infos.n(), infos.base2k(), infos.k(), infos.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>(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(infos.n(), infos.base2k(), infos.k(), infos.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))
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> fmt::Debug for LWESwitchingKey<D> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{self}")
|
|
}
|
|
}
|
|
|
|
impl<D: DataMut> FillUniform for LWESwitchingKey<D> {
|
|
fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
|
|
self.0.fill_uniform(log_bound, source);
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> fmt::Display for LWESwitchingKey<D> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "(LWESwitchingKey) {}", self.0)
|
|
}
|
|
}
|
|
|
|
impl<D: DataMut> ReaderFrom for LWESwitchingKey<D> {
|
|
fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
|
|
self.0.read_from(reader)
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> WriterTo for LWESwitchingKey<D> {
|
|
fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
|
|
self.0.write_to(writer)
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> GGLWEToRef for LWESwitchingKey<D> {
|
|
fn to_ref(&self) -> GGLWE<&[u8]> {
|
|
self.0.to_ref()
|
|
}
|
|
}
|
|
|
|
impl<D: DataMut> GGLWEToMut for LWESwitchingKey<D> {
|
|
fn to_mut(&mut self) -> GGLWE<&mut [u8]> {
|
|
self.0.to_mut()
|
|
}
|
|
}
|
|
|
|
impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWESwitchingKey<D> {
|
|
fn input_degree(&mut self) -> &mut Degree {
|
|
&mut self.0.input_degree
|
|
}
|
|
|
|
fn output_degree(&mut self) -> &mut Degree {
|
|
&mut self.0.output_degree
|
|
}
|
|
}
|
|
|
|
impl<D: DataRef> GLWESwitchingKeyDegrees for LWESwitchingKey<D> {
|
|
fn input_degree(&self) -> &Degree {
|
|
&self.0.input_degree
|
|
}
|
|
|
|
fn output_degree(&self) -> &Degree {
|
|
&self.0.output_degree
|
|
}
|
|
}
|