Files
poulpy/core/src/layouts/lwe_pt.rs
Jean-Philippe Bossuat 3a828740cc Traits cleaning, CBT example & bug fixes (#72)
* Some cleaning, CBT example, fix mod switch and add LUT correctness test to BR test

* finished trait cleaning

* removed trait aliastoutside of backend
2025-08-16 18:23:22 +02:00

96 lines
2.0 KiB
Rust

use std::fmt;
use backend::hal::layouts::{Data, DataMut, DataRef, VecZnx, VecZnxToMut, VecZnxToRef};
use crate::layouts::{Infos, SetMetaData};
pub struct LWEPlaintext<D: Data> {
pub(crate) data: VecZnx<D>,
pub(crate) k: usize,
pub(crate) basek: usize,
}
impl LWEPlaintext<Vec<u8>> {
pub fn alloc(basek: usize, k: usize) -> Self {
Self {
data: VecZnx::alloc(1, 1, k.div_ceil(basek)),
k: k,
basek: basek,
}
}
}
impl<D: DataRef> fmt::Display for LWEPlaintext<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"LWEPlaintext: basek={} k={}: {}",
self.basek(),
self.k(),
self.data
)
}
}
impl<D: Data> Infos for LWEPlaintext<D> {
type Inner = VecZnx<D>;
fn inner(&self) -> &Self::Inner {
&self.data
}
fn basek(&self) -> usize {
self.basek
}
fn k(&self) -> usize {
self.k
}
}
impl<D: DataMut> SetMetaData for LWEPlaintext<D> {
fn set_k(&mut self, k: usize) {
self.k = k
}
fn set_basek(&mut self, basek: usize) {
self.basek = basek
}
}
pub trait LWEPlaintextToRef {
#[allow(dead_code)]
fn to_ref(&self) -> LWEPlaintext<&[u8]>;
}
impl<D: DataRef> LWEPlaintextToRef for LWEPlaintext<D> {
fn to_ref(&self) -> LWEPlaintext<&[u8]> {
LWEPlaintext {
data: self.data.to_ref(),
basek: self.basek,
k: self.k,
}
}
}
pub trait LWEPlaintextToMut {
#[allow(dead_code)]
fn to_mut(&mut self) -> LWEPlaintext<&mut [u8]>;
}
impl<D: DataMut> LWEPlaintextToMut for LWEPlaintext<D> {
fn to_mut(&mut self) -> LWEPlaintext<&mut [u8]> {
LWEPlaintext {
data: self.data.to_mut(),
basek: self.basek,
k: self.k,
}
}
}
impl<D: DataMut> LWEPlaintext<D> {
pub fn data_mut(&mut self) -> &mut VecZnx<D> {
&mut self.data
}
}