Various improvement to memory management and API

[module]: added enum for backend
[VecZnx, VecZnxDft, VecZnxBig, VmpPMat]: added ptr to data
[VecZnxBorrow]: removed
[VecZnxAPI]: removed
This commit is contained in:
Jean-Philippe Bossuat
2025-03-17 12:07:40 +01:00
parent 97a1559bf2
commit 46c577409e
28 changed files with 896 additions and 1064 deletions

View File

@@ -1,61 +1,65 @@
use crate::ciphertext::Ciphertext;
use crate::elem::{Elem, ElemCommon, ElemVecZnx, VecZnxCommon};
use crate::elem::{Elem, ElemCommon, ElemVecZnx};
use crate::parameters::Parameters;
use base2k::{Module, VecZnx};
pub struct Plaintext<T>(pub Elem<T>);
pub struct Plaintext(pub Elem<VecZnx>);
impl Parameters {
pub fn new_plaintext(&self, log_q: usize) -> Plaintext<VecZnx> {
pub fn new_plaintext(&self, log_q: usize) -> Plaintext {
Plaintext::new(self.module(), self.log_base2k(), log_q)
}
pub fn bytes_of_plaintext<T>(&self, log_q: usize) -> usize
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
Elem::<T>::bytes_of(self.module(), self.log_base2k(), log_q, 1)
pub fn bytes_of_plaintext(&self, log_q: usize) -> usize
where {
Elem::<VecZnx>::bytes_of(self.module(), self.log_base2k(), log_q, 1)
}
pub fn plaintext_from_bytes<T>(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext<T>
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
Plaintext::<T>(self.elem_from_bytes::<T>(log_q, 1, bytes))
pub fn plaintext_from_bytes(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext {
Plaintext(Elem::<VecZnx>::from_bytes(
self.module(),
self.log_base2k(),
log_q,
1,
bytes,
))
}
}
impl Plaintext<VecZnx> {
impl Plaintext {
pub fn new(module: &Module, log_base2k: usize, log_q: usize) -> Self {
Self(Elem::<VecZnx>::new(module, log_base2k, log_q, 1))
}
}
impl<T> Plaintext<T>
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
impl Plaintext {
pub fn bytes_of(module: &Module, log_base2k: usize, log_q: usize) -> usize {
Elem::<T>::bytes_of(module, log_base2k, log_q, 1)
Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 1)
}
pub fn from_bytes(module: &Module, log_base2k: usize, log_q: usize, bytes: &mut [u8]) -> Self {
Self(Elem::<T>::from_bytes(module, log_base2k, log_q, 1, bytes))
Self(Elem::<VecZnx>::from_bytes(
module, log_base2k, log_q, 1, bytes,
))
}
pub fn as_ciphertext(&self) -> Ciphertext<T> {
unsafe { Ciphertext::<T>(std::ptr::read(&self.0)) }
pub fn from_bytes_borrow(
module: &Module,
log_base2k: usize,
log_q: usize,
bytes: &mut [u8],
) -> Self {
Self(Elem::<VecZnx>::from_bytes_borrow(
module, log_base2k, log_q, 1, bytes,
))
}
pub fn as_ciphertext(&self) -> Ciphertext<VecZnx> {
unsafe { Ciphertext::<VecZnx>(std::ptr::read(&self.0)) }
}
}
impl<T> ElemCommon<T> for Plaintext<T>
where
T: VecZnxCommon<Owned = T>,
Elem<T>: ElemVecZnx<T>,
{
impl ElemCommon<VecZnx> for Plaintext {
fn n(&self) -> usize {
self.0.n()
}
@@ -68,11 +72,11 @@ where
self.0.log_q
}
fn elem(&self) -> &Elem<T> {
fn elem(&self) -> &Elem<VecZnx> {
&self.0
}
fn elem_mut(&mut self) -> &mut Elem<T> {
fn elem_mut(&mut self) -> &mut Elem<VecZnx> {
&mut self.0
}
@@ -88,11 +92,11 @@ where
self.0.cols()
}
fn at(&self, i: usize) -> &T {
fn at(&self, i: usize) -> &VecZnx {
self.0.at(i)
}
fn at_mut(&mut self, i: usize) -> &mut T {
fn at_mut(&mut self, i: usize) -> &mut VecZnx {
self.0.at_mut(i)
}