mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
wip
This commit is contained in:
@@ -297,8 +297,6 @@ where
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use super::{AddNormal, FillUniform};
|
use super::{AddNormal, FillUniform};
|
||||||
use crate::vec_znx_ops::*;
|
use crate::vec_znx_ops::*;
|
||||||
use crate::znx_base::*;
|
use crate::znx_base::*;
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ use crate::ffi::svp;
|
|||||||
use crate::ffi::vec_znx_dft::vec_znx_dft_t;
|
use crate::ffi::vec_znx_dft::vec_znx_dft_t;
|
||||||
use crate::znx_base::{ZnxInfos, ZnxView, ZnxViewMut};
|
use crate::znx_base::{ZnxInfos, ZnxView, ZnxViewMut};
|
||||||
use crate::{
|
use crate::{
|
||||||
Backend, FFT64, Module, ScalarToRef, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef, VecZnx,
|
Backend, FFT64, Module, ScalarToRef, ScalarZnxDft, ScalarZnxDftOwned, ScalarZnxDftToMut, ScalarZnxDftToRef,
|
||||||
VecZnxDft, VecZnxDftToMut, VecZnxDftToRef, VecZnxToRef, ZnxSliceSize,
|
VecZnxDft, VecZnxDftToMut, VecZnxDftToRef,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait ScalarZnxDftAlloc<B: Backend> {
|
pub trait ScalarZnxDftAlloc<B: Backend> {
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
use base2k::{
|
use base2k::{
|
||||||
Backend, DataView, DataViewMut, MatZnxDft, MatZnxDftAlloc, MatZnxDftToMut, MatZnxDftToRef, Module, VecZnx, VecZnxAlloc,
|
Backend, DataView, DataViewMut, MatZnxDft, MatZnxDftAlloc, MatZnxDftToMut, MatZnxDftToRef, Module, ScalarZnxDftToRef, VecZnx,
|
||||||
VecZnxDft, VecZnxDftAlloc, VecZnxDftToMut, VecZnxDftToRef, VecZnxToMut, VecZnxToRef, ZnxInfos,
|
VecZnxAlloc, VecZnxDft, VecZnxDftAlloc, VecZnxDftToMut, VecZnxDftToRef, VecZnxToMut, VecZnxToRef, ZnxInfos,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait Infos<T>
|
pub trait Infos {
|
||||||
where
|
type Inner: ZnxInfos;
|
||||||
T: ZnxInfos,
|
|
||||||
{
|
fn inner(&self) -> &Self::Inner;
|
||||||
fn inner(&self) -> &T;
|
|
||||||
|
|
||||||
/// Returns the ring degree of the polynomials.
|
/// Returns the ring degree of the polynomials.
|
||||||
fn n(&self) -> usize {
|
fn n(&self) -> usize {
|
||||||
@@ -48,17 +47,16 @@ where
|
|||||||
fn log_q(&self) -> usize;
|
fn log_q(&self) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Ciphertext<T> {
|
pub struct RLWECt<C>{
|
||||||
data: T,
|
data: VecZnx<C>,
|
||||||
log_base2k: usize,
|
log_base2k: usize,
|
||||||
log_q: usize,
|
log_q: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Infos<T> for Ciphertext<T>
|
impl<T: ZnxInfos> Infos for RLWECt<T> {
|
||||||
where
|
type Inner = T;
|
||||||
T: ZnxInfos,
|
|
||||||
{
|
fn inner(&self) -> &Self::Inner {
|
||||||
fn inner(&self) -> &T {
|
|
||||||
&self.data
|
&self.data
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,11 +88,10 @@ pub struct Plaintext<T> {
|
|||||||
log_q: usize,
|
log_q: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Infos<T> for Plaintext<T>
|
impl<T: ZnxInfos> Infos for Plaintext<T> {
|
||||||
where
|
type Inner = T;
|
||||||
T: ZnxInfos,
|
|
||||||
{
|
fn inner(&self) -> &Self::Inner {
|
||||||
fn inner(&self) -> &T {
|
|
||||||
&self.data
|
&self.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,16 @@ use base2k::{
|
|||||||
|
|
||||||
use sampling::source::Source;
|
use sampling::source::Source;
|
||||||
|
|
||||||
use crate::{elem::Infos, keys::SecretKey};
|
use crate::{
|
||||||
|
elem::{Ciphertext, Infos, Plaintext},
|
||||||
|
keys::SecretKey,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait EncryptSk<B: Backend, D, P> {
|
pub trait EncryptSk<B: Backend, C, P> {
|
||||||
fn encrypt<S>(
|
fn encrypt<S>(
|
||||||
module: &Module<B>,
|
module: &Module<B>,
|
||||||
res: &mut D,
|
res: &mut Ciphertext<C>,
|
||||||
pt: Option<&P>,
|
pt: Option<&Plaintext<P>>,
|
||||||
sk: &SecretKey<S>,
|
sk: &SecretKey<S>,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
@@ -22,20 +25,18 @@ pub trait EncryptSk<B: Backend, D, P> {
|
|||||||
) where
|
) where
|
||||||
S: ScalarZnxDftToRef<B>;
|
S: ScalarZnxDftToRef<B>;
|
||||||
|
|
||||||
fn encrypt_tmp_bytes(module: &Module<B>, size: usize) -> usize {
|
fn encrypt_scratch_bytes(module: &Module<B>, size: usize) -> usize;
|
||||||
(module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_big(1, size)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C, P> EncryptSk<FFT64, C, P> for C
|
impl<C, P> EncryptSk<FFT64, C, P> for Ciphertext<C>
|
||||||
where
|
where
|
||||||
C: VecZnxToMut + ZnxInfos + Infos<C>,
|
C: VecZnxToMut + ZnxInfos,
|
||||||
P: VecZnxToRef,
|
P: VecZnxToRef + ZnxInfos,
|
||||||
{
|
{
|
||||||
fn encrypt<S>(
|
fn encrypt<S>(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
ct: &mut C,
|
ct: &mut Ciphertext<C>,
|
||||||
pt: Option<&P>,
|
pt: Option<&Plaintext<P>>,
|
||||||
sk: &SecretKey<S>,
|
sk: &SecretKey<S>,
|
||||||
source_xa: &mut Source,
|
source_xa: &mut Source,
|
||||||
source_xe: &mut Source,
|
source_xe: &mut Source,
|
||||||
@@ -76,6 +77,41 @@ where
|
|||||||
// c0 = norm(c0_big = -as + m + e)
|
// c0 = norm(c0_big = -as + m + e)
|
||||||
module.vec_znx_big_normalize(log_base2k, &mut ct_mut, 0, &c0_big, 0, scratch_1);
|
module.vec_znx_big_normalize(log_base2k, &mut ct_mut, 0, &c0_big, 0, scratch_1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn encrypt_scratch_bytes(module: &Module<FFT64>, size: usize) -> usize {
|
||||||
|
(module.vec_znx_big_normalize_tmp_bytes() | module.bytes_of_vec_znx_dft(1, size)) + module.bytes_of_vec_znx_big(1, size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<C> Ciphertext<C>
|
||||||
|
where
|
||||||
|
C: VecZnxToMut + ZnxInfos,
|
||||||
|
{
|
||||||
|
pub fn encrypt_sk<P, S>(
|
||||||
|
&mut self,
|
||||||
|
module: &Module<FFT64>,
|
||||||
|
pt: Option<&Plaintext<P>>,
|
||||||
|
sk: &SecretKey<S>,
|
||||||
|
source_xa: &mut Source,
|
||||||
|
source_xe: &mut Source,
|
||||||
|
scratch: &mut Scratch,
|
||||||
|
sigma: f64,
|
||||||
|
bound: f64,
|
||||||
|
) where
|
||||||
|
P: VecZnxToRef + ZnxInfos,
|
||||||
|
S: ScalarZnxDftToRef<FFT64>,
|
||||||
|
{
|
||||||
|
<Self as EncryptSk<FFT64, _, _>>::encrypt(
|
||||||
|
module, self, pt, sk, source_xa, source_xe, scratch, sigma, bound,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn encrypt_sk_scratch_bytes<P>(module: &Module<FFT64>, size: usize) -> usize
|
||||||
|
where
|
||||||
|
Self: EncryptSk<FFT64, C, P>,
|
||||||
|
{
|
||||||
|
<Self as EncryptSk<FFT64, C, P>>::encrypt_scratch_bytes(module, size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait EncryptZeroSk<B: Backend, D> {
|
pub trait EncryptZeroSk<B: Backend, D> {
|
||||||
@@ -91,17 +127,12 @@ pub trait EncryptZeroSk<B: Backend, D> {
|
|||||||
) where
|
) where
|
||||||
S: ScalarZnxDftToRef<B>;
|
S: ScalarZnxDftToRef<B>;
|
||||||
|
|
||||||
fn encrypt_zero_tmp_bytes(module: &Module<B>, size: usize) -> usize {
|
fn encrypt_zero_scratch_bytes(module: &Module<B>, size: usize) -> usize;
|
||||||
(module.bytes_of_vec_znx(1, size) | module.bytes_of_vec_znx_dft(1, size))
|
|
||||||
+ module.bytes_of_vec_znx_big(1, size)
|
|
||||||
+ module.bytes_of_vec_znx(1, size)
|
|
||||||
+ module.vec_znx_big_normalize_tmp_bytes()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C> EncryptZeroSk<FFT64, C> for C
|
impl<C> EncryptZeroSk<FFT64, C> for C
|
||||||
where
|
where
|
||||||
C: VecZnxDftToMut<FFT64> + ZnxInfos + Infos<C>,
|
C: VecZnxDftToMut<FFT64> + ZnxInfos + Infos,
|
||||||
{
|
{
|
||||||
fn encrypt_zero<S>(
|
fn encrypt_zero<S>(
|
||||||
module: &Module<FFT64>,
|
module: &Module<FFT64>,
|
||||||
@@ -146,4 +177,53 @@ where
|
|||||||
// ct[0] = DFT(-as + e)
|
// ct[0] = DFT(-as + e)
|
||||||
module.vec_znx_dft(&mut ct_mut, 0, &tmp_znx, 0);
|
module.vec_znx_dft(&mut ct_mut, 0, &tmp_znx, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn encrypt_zero_scratch_bytes(module: &Module<FFT64>, size: usize) -> usize{
|
||||||
|
(module.bytes_of_vec_znx(1, size) | module.bytes_of_vec_znx_dft(1, size))
|
||||||
|
+ module.bytes_of_vec_znx_big(1, size)
|
||||||
|
+ module.bytes_of_vec_znx(1, size)
|
||||||
|
+ module.vec_znx_big_normalize_tmp_bytes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use base2k::{FFT64, Module, ScratchOwned, VecZnx, Scalar};
|
||||||
|
use sampling::source::Source;
|
||||||
|
|
||||||
|
use crate::{elem::{Ciphertext, Infos, Plaintext}, keys::SecretKey};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn encrypt_sk_vec_znx_fft64() {
|
||||||
|
let module: Module<FFT64> = Module::<FFT64>::new(32);
|
||||||
|
let log_base2k: usize = 8;
|
||||||
|
let log_q: usize = 54;
|
||||||
|
|
||||||
|
let sigma: f64 = 3.2;
|
||||||
|
let bound: f64 = sigma * 6;
|
||||||
|
|
||||||
|
let mut ct: Ciphertext<VecZnx<Vec<u8>>> = Ciphertext::<VecZnx<Vec<u8>>>::new(&module, log_base2k, log_q, 2);
|
||||||
|
let mut pt: Plaintext<VecZnx<Vec<u8>>> = Plaintext::<VecZnx<Vec<u8>>>::new(&module, log_base2k, log_q);
|
||||||
|
|
||||||
|
let mut source_xe = Source::new([0u8; 32]);
|
||||||
|
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||||
|
|
||||||
|
|
||||||
|
let mut scratch: ScratchOwned = ScratchOwned::new(ct.encrypt_encsk_scratch_bytes(&module, ct.size()));
|
||||||
|
|
||||||
|
let mut sk: SecretKey<Scalar<Vec<u8>>> = SecretKey::new(&module);
|
||||||
|
let mut sk_prep
|
||||||
|
sk.svp_prepare(&module, &mut sk_prep);
|
||||||
|
|
||||||
|
ct.encrypt_sk(
|
||||||
|
&module,
|
||||||
|
Some(&pt),
|
||||||
|
&sk_prep,
|
||||||
|
&mut source_xa,
|
||||||
|
&mut source_xe,
|
||||||
|
scratch.borrow(),
|
||||||
|
sigma,
|
||||||
|
bound,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use base2k::{
|
use base2k::{
|
||||||
Backend, FFT64, Module, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftOps, ScalarZnxDftToMut, Scratch, VecZnxDft,
|
Backend, Module, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScalarZnxDftToMut, Scratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftToMut, ZnxInfos, FFT64
|
||||||
VecZnxDftAlloc, VecZnxDftToMut,
|
|
||||||
};
|
};
|
||||||
use sampling::source::Source;
|
use sampling::source::Source;
|
||||||
|
|
||||||
@@ -43,6 +42,16 @@ impl SecretKey<Scalar<Vec<u8>>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SecretKeyPrep<C, B> = SecretKey<ScalarZnxDft<C, B>>;
|
||||||
|
|
||||||
|
impl<B: Backend> SecretKey<ScalarZnxDft<Vec<u8>, B>> {
|
||||||
|
pub fn new(module: &Module<B>) -> Self{
|
||||||
|
Self{
|
||||||
|
data: module.new_scalar_znx_dft(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct PublicKey<D, B: Backend> {
|
pub struct PublicKey<D, B: Backend> {
|
||||||
data: VecZnxDft<D, B>,
|
data: VecZnxDft<D, B>,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user