mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
updated sampling traits
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use base2k::{
|
||||
Encoding, FFT64, Module, Sampling, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScratchOwned,
|
||||
VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps,
|
||||
VecZnxOps, ZnxInfos,
|
||||
AddNormal, Encoding, FFT64, FillUniform, Module, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps,
|
||||
ScratchOwned, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc,
|
||||
VecZnxDftOps, VecZnxOps, ZnxInfos,
|
||||
};
|
||||
use itertools::izip;
|
||||
use sampling::source::Source;
|
||||
@@ -36,7 +36,7 @@ fn main() {
|
||||
);
|
||||
|
||||
// Fill the second column with random values: ct = (0, a)
|
||||
module.fill_uniform(log_base2k, &mut ct, 1, ct_size, &mut source);
|
||||
ct.fill_uniform(log_base2k, 1, ct_size, &mut source);
|
||||
|
||||
let mut buf_dft: VecZnxDft<Vec<u8>, FFT64> = module.new_vec_znx_dft(1, ct_size);
|
||||
|
||||
@@ -88,9 +88,8 @@ fn main() {
|
||||
|
||||
// Add noise to ct[0]
|
||||
// ct[0] <- ct[0] + e
|
||||
module.add_normal(
|
||||
ct.add_normal(
|
||||
log_base2k,
|
||||
&mut ct,
|
||||
0, // Selects the first column of ct (ct[0])
|
||||
log_base2k * ct_size, // Scaling of the noise: 2^{-log_base2k * limbs}
|
||||
&mut source,
|
||||
|
||||
Submodule base2k/spqlios-arithmetic updated: e3d3247335...8135d85e7a
@@ -151,7 +151,7 @@ impl<D: AsRef<[u8]>> MatZnxDft<D, FFT64> {
|
||||
}
|
||||
}
|
||||
|
||||
pub type MatZnxDftAllocOwned<B> = MatZnxDft<Vec<u8>, B>;
|
||||
pub type MatZnxDftOwned<B> = MatZnxDft<Vec<u8>, B>;
|
||||
|
||||
pub trait MatZnxDftToRef<B: Backend> {
|
||||
fn to_ref(&self) -> MatZnxDft<&[u8], B>;
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::ffi::vec_znx_dft::vec_znx_dft_t;
|
||||
use crate::ffi::vmp;
|
||||
use crate::znx_base::{ZnxInfos, ZnxView, ZnxViewMut};
|
||||
use crate::{
|
||||
Backend, FFT64, MatZnxDft, MatZnxDftAllocOwned, MatZnxDftToMut, MatZnxDftToRef, Module, Scratch, VecZnxDft, VecZnxDftToMut,
|
||||
Backend, FFT64, MatZnxDft, MatZnxDftOwned, MatZnxDftToMut, MatZnxDftToRef, Module, Scratch, VecZnxDft, VecZnxDftToMut,
|
||||
VecZnxDftToRef,
|
||||
};
|
||||
|
||||
@@ -13,7 +13,7 @@ pub trait MatZnxDftAlloc<B: Backend> {
|
||||
///
|
||||
/// * `rows`: number of rows (number of [VecZnxDft]).
|
||||
/// * `size`: number of size (number of size of each [VecZnxDft]).
|
||||
fn new_mat_znx_dft(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> MatZnxDftAllocOwned<B>;
|
||||
fn new_mat_znx_dft(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> MatZnxDftOwned<B>;
|
||||
|
||||
fn bytes_of_mat_znx_dft(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize;
|
||||
|
||||
@@ -24,7 +24,7 @@ pub trait MatZnxDftAlloc<B: Backend> {
|
||||
cols_out: usize,
|
||||
size: usize,
|
||||
bytes: Vec<u8>,
|
||||
) -> MatZnxDftAllocOwned<B>;
|
||||
) -> MatZnxDftOwned<B>;
|
||||
}
|
||||
|
||||
pub trait MatZnxDftScratch {
|
||||
@@ -103,11 +103,11 @@ pub trait MatZnxDftOps<BACKEND: Backend> {
|
||||
|
||||
impl<B: Backend> MatZnxDftAlloc<B> for Module<B> {
|
||||
fn bytes_of_mat_znx_dft(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> usize {
|
||||
MatZnxDftAllocOwned::bytes_of(self, rows, cols_in, cols_out, size)
|
||||
MatZnxDftOwned::bytes_of(self, rows, cols_in, cols_out, size)
|
||||
}
|
||||
|
||||
fn new_mat_znx_dft(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> MatZnxDftAllocOwned<B> {
|
||||
MatZnxDftAllocOwned::new(self, rows, cols_in, cols_out, size)
|
||||
fn new_mat_znx_dft(&self, rows: usize, cols_in: usize, cols_out: usize, size: usize) -> MatZnxDftOwned<B> {
|
||||
MatZnxDftOwned::new(self, rows, cols_in, cols_out, size)
|
||||
}
|
||||
|
||||
fn new_mat_znx_dft_from_bytes(
|
||||
@@ -117,8 +117,8 @@ impl<B: Backend> MatZnxDftAlloc<B> for Module<B> {
|
||||
cols_out: usize,
|
||||
size: usize,
|
||||
bytes: Vec<u8>,
|
||||
) -> MatZnxDftAllocOwned<B> {
|
||||
MatZnxDftAllocOwned::new_from_bytes(self, rows, cols_in, cols_out, size, bytes)
|
||||
) -> MatZnxDftOwned<B> {
|
||||
MatZnxDftOwned::new_from_bytes(self, rows, cols_in, cols_out, size, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,8 +305,8 @@ impl MatZnxDftOps<FFT64> for Module<FFT64> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
Encoding, FFT64, MatZnxDft, MatZnxDftOps, Module, Sampling, ScratchOwned, VecZnx, VecZnxAlloc, VecZnxBig, VecZnxBigAlloc,
|
||||
VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, ZnxInfos, ZnxView, ZnxViewMut,
|
||||
Encoding, FFT64, FillUniform, MatZnxDft, MatZnxDftOps, Module, ScratchOwned, VecZnx, VecZnxAlloc, VecZnxBig,
|
||||
VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, ZnxInfos, ZnxView, ZnxViewMut,
|
||||
};
|
||||
use sampling::source::Source;
|
||||
|
||||
@@ -329,7 +329,7 @@ mod tests {
|
||||
for row_i in 0..mat_rows {
|
||||
let mut source: Source = Source::new([0u8; 32]);
|
||||
(0..mat_cols_out).for_each(|col_out| {
|
||||
module.fill_uniform(log_base2k, &mut a, col_out, mat_size, &mut source);
|
||||
a.fill_uniform(log_base2k, col_out, mat_size, &mut source);
|
||||
module.vec_znx_dft(&mut a_dft, col_out, &a, col_out);
|
||||
});
|
||||
module.vmp_prepare_row(&mut mat, row_i, col_in, &a_dft);
|
||||
|
||||
@@ -1,47 +1,53 @@
|
||||
use crate::znx_base::ZnxViewMut;
|
||||
use crate::{Backend, Module, VecZnx, VecZnxToMut};
|
||||
use crate::{FFT64, VecZnx, VecZnxBig, VecZnxBigToMut, VecZnxToMut};
|
||||
use rand_distr::{Distribution, Normal};
|
||||
use sampling::source::Source;
|
||||
|
||||
pub trait Sampling {
|
||||
pub trait FillUniform {
|
||||
/// Fills the first `size` size with uniform values in \[-2^{log_base2k-1}, 2^{log_base2k-1}\]
|
||||
fn fill_uniform<A>(&self, log_base2k: usize, a: &mut A, col_i: usize, size: usize, source: &mut Source)
|
||||
where
|
||||
A: VecZnxToMut;
|
||||
fn fill_uniform(&mut self, log_base2k: usize, col_i: usize, size: usize, source: &mut Source);
|
||||
}
|
||||
|
||||
/// Adds vector sampled according to the provided distribution, scaled by 2^{-log_k} and bounded to \[-bound, bound\].
|
||||
fn add_dist_f64<A, D: Distribution<f64>>(
|
||||
&self,
|
||||
pub trait FillDistF64 {
|
||||
fn fill_dist_f64<D: Distribution<f64>>(
|
||||
&mut self,
|
||||
log_base2k: usize,
|
||||
a: &mut A,
|
||||
col_i: usize,
|
||||
log_k: usize,
|
||||
source: &mut Source,
|
||||
dist: D,
|
||||
bound: f64,
|
||||
) where
|
||||
A: VecZnxToMut;
|
||||
);
|
||||
}
|
||||
|
||||
/// Adds a discrete normal vector scaled by 2^{-log_k} with the provided standard deviation and bounded to \[-bound, bound\].
|
||||
fn add_normal<A>(
|
||||
&self,
|
||||
pub trait AddDistF64 {
|
||||
/// Adds vector sampled according to the provided distribution, scaled by 2^{-log_k} and bounded to \[-bound, bound\].
|
||||
fn add_dist_f64<D: Distribution<f64>>(
|
||||
&mut self,
|
||||
log_base2k: usize,
|
||||
a: &mut A,
|
||||
col_i: usize,
|
||||
log_k: usize,
|
||||
source: &mut Source,
|
||||
sigma: f64,
|
||||
dist: D,
|
||||
bound: f64,
|
||||
) where
|
||||
A: VecZnxToMut;
|
||||
);
|
||||
}
|
||||
|
||||
impl<B: Backend> Sampling for Module<B> {
|
||||
fn fill_uniform<A>(&self, log_base2k: usize, a: &mut A, col_i: usize, size: usize, source: &mut Source)
|
||||
where
|
||||
A: VecZnxToMut,
|
||||
{
|
||||
let mut a: VecZnx<&mut [u8]> = a.to_mut();
|
||||
pub trait FillNormal {
|
||||
fn fill_normal(&mut self, log_base2k: usize, col_i: usize, log_k: usize, source: &mut Source, sigma: f64, bound: f64);
|
||||
}
|
||||
|
||||
pub trait AddNormal {
|
||||
/// Adds a discrete normal vector scaled by 2^{-log_k} with the provided standard deviation and bounded to \[-bound, bound\].
|
||||
fn add_normal(&mut self, log_base2k: usize, col_i: usize, log_k: usize, source: &mut Source, sigma: f64, bound: f64);
|
||||
}
|
||||
|
||||
impl<T> FillUniform for VecZnx<T>
|
||||
where
|
||||
VecZnx<T>: VecZnxToMut,
|
||||
{
|
||||
fn fill_uniform(&mut self, log_base2k: usize, col_i: usize, size: usize, source: &mut Source) {
|
||||
let mut a: VecZnx<&mut [u8]> = self.to_mut();
|
||||
let base2k: u64 = 1 << log_base2k;
|
||||
let mask: u64 = base2k - 1;
|
||||
let base2k_half: i64 = (base2k >> 1) as i64;
|
||||
@@ -51,20 +57,65 @@ impl<B: Backend> Sampling for Module<B> {
|
||||
.for_each(|x| *x = (source.next_u64n(base2k, mask) as i64) - base2k_half);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn add_dist_f64<A, D: Distribution<f64>>(
|
||||
&self,
|
||||
impl<T> FillDistF64 for VecZnx<T>
|
||||
where
|
||||
VecZnx<T>: VecZnxToMut,
|
||||
{
|
||||
fn fill_dist_f64<D: Distribution<f64>>(
|
||||
&mut self,
|
||||
log_base2k: usize,
|
||||
a: &mut A,
|
||||
col_i: usize,
|
||||
log_k: usize,
|
||||
source: &mut Source,
|
||||
dist: D,
|
||||
bound: f64,
|
||||
) where
|
||||
A: VecZnxToMut,
|
||||
{
|
||||
let mut a: VecZnx<&mut [u8]> = a.to_mut();
|
||||
) {
|
||||
let mut a: VecZnx<&mut [u8]> = self.to_mut();
|
||||
assert!(
|
||||
(bound.log2().ceil() as i64) < 64,
|
||||
"invalid bound: ceil(log2(bound))={} > 63",
|
||||
(bound.log2().ceil() as i64)
|
||||
);
|
||||
|
||||
let limb: usize = (log_k + log_base2k - 1) / log_base2k - 1;
|
||||
let log_base2k_rem: usize = log_k % log_base2k;
|
||||
|
||||
if log_base2k_rem != 0 {
|
||||
a.at_mut(col_i, limb).iter_mut().for_each(|a| {
|
||||
let mut dist_f64: f64 = dist.sample(source);
|
||||
while dist_f64.abs() > bound {
|
||||
dist_f64 = dist.sample(source)
|
||||
}
|
||||
*a = (dist_f64.round() as i64) << log_base2k_rem;
|
||||
});
|
||||
} else {
|
||||
a.at_mut(col_i, limb).iter_mut().for_each(|a| {
|
||||
let mut dist_f64: f64 = dist.sample(source);
|
||||
while dist_f64.abs() > bound {
|
||||
dist_f64 = dist.sample(source)
|
||||
}
|
||||
*a = dist_f64.round() as i64
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AddDistF64 for VecZnx<T>
|
||||
where
|
||||
VecZnx<T>: VecZnxToMut,
|
||||
{
|
||||
fn add_dist_f64<D: Distribution<f64>>(
|
||||
&mut self,
|
||||
log_base2k: usize,
|
||||
col_i: usize,
|
||||
log_k: usize,
|
||||
source: &mut Source,
|
||||
dist: D,
|
||||
bound: f64,
|
||||
) {
|
||||
let mut a: VecZnx<&mut [u8]> = self.to_mut();
|
||||
assert!(
|
||||
(bound.log2().ceil() as i64) < 64,
|
||||
"invalid bound: ceil(log2(bound))={} > 63",
|
||||
@@ -92,14 +143,149 @@ impl<B: Backend> Sampling for Module<B> {
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_normal<A>(&self, log_base2k: usize, a: &mut A, col_i: usize, log_k: usize, source: &mut Source, sigma: f64, bound: f64)
|
||||
where
|
||||
A: VecZnxToMut,
|
||||
{
|
||||
impl<T> FillNormal for VecZnx<T>
|
||||
where
|
||||
VecZnx<T>: VecZnxToMut,
|
||||
{
|
||||
fn fill_normal(&mut self, log_base2k: usize, col_i: usize, log_k: usize, source: &mut Source, sigma: f64, bound: f64) {
|
||||
self.fill_dist_f64(
|
||||
log_base2k,
|
||||
col_i,
|
||||
log_k,
|
||||
source,
|
||||
Normal::new(0.0, sigma).unwrap(),
|
||||
bound,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AddNormal for VecZnx<T>
|
||||
where
|
||||
VecZnx<T>: VecZnxToMut,
|
||||
{
|
||||
fn add_normal(&mut self, log_base2k: usize, col_i: usize, log_k: usize, source: &mut Source, sigma: f64, bound: f64) {
|
||||
self.add_dist_f64(
|
||||
log_base2k,
|
||||
col_i,
|
||||
log_k,
|
||||
source,
|
||||
Normal::new(0.0, sigma).unwrap(),
|
||||
bound,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FillDistF64 for VecZnxBig<T, FFT64>
|
||||
where
|
||||
VecZnxBig<T, FFT64>: VecZnxBigToMut<FFT64>,
|
||||
{
|
||||
fn fill_dist_f64<D: Distribution<f64>>(
|
||||
&mut self,
|
||||
log_base2k: usize,
|
||||
col_i: usize,
|
||||
log_k: usize,
|
||||
source: &mut Source,
|
||||
dist: D,
|
||||
bound: f64,
|
||||
) {
|
||||
let mut a: VecZnxBig<&mut [u8], FFT64> = self.to_mut();
|
||||
assert!(
|
||||
(bound.log2().ceil() as i64) < 64,
|
||||
"invalid bound: ceil(log2(bound))={} > 63",
|
||||
(bound.log2().ceil() as i64)
|
||||
);
|
||||
|
||||
let limb: usize = (log_k + log_base2k - 1) / log_base2k - 1;
|
||||
let log_base2k_rem: usize = log_k % log_base2k;
|
||||
|
||||
if log_base2k_rem != 0 {
|
||||
a.at_mut(col_i, limb).iter_mut().for_each(|a| {
|
||||
let mut dist_f64: f64 = dist.sample(source);
|
||||
while dist_f64.abs() > bound {
|
||||
dist_f64 = dist.sample(source)
|
||||
}
|
||||
*a = (dist_f64.round() as i64) << log_base2k_rem;
|
||||
});
|
||||
} else {
|
||||
a.at_mut(col_i, limb).iter_mut().for_each(|a| {
|
||||
let mut dist_f64: f64 = dist.sample(source);
|
||||
while dist_f64.abs() > bound {
|
||||
dist_f64 = dist.sample(source)
|
||||
}
|
||||
*a = dist_f64.round() as i64
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AddDistF64 for VecZnxBig<T, FFT64>
|
||||
where
|
||||
VecZnxBig<T, FFT64>: VecZnxBigToMut<FFT64>,
|
||||
{
|
||||
fn add_dist_f64<D: Distribution<f64>>(
|
||||
&mut self,
|
||||
log_base2k: usize,
|
||||
col_i: usize,
|
||||
log_k: usize,
|
||||
source: &mut Source,
|
||||
dist: D,
|
||||
bound: f64,
|
||||
) {
|
||||
let mut a: VecZnxBig<&mut [u8], FFT64> = self.to_mut();
|
||||
assert!(
|
||||
(bound.log2().ceil() as i64) < 64,
|
||||
"invalid bound: ceil(log2(bound))={} > 63",
|
||||
(bound.log2().ceil() as i64)
|
||||
);
|
||||
|
||||
let limb: usize = (log_k + log_base2k - 1) / log_base2k - 1;
|
||||
let log_base2k_rem: usize = log_k % log_base2k;
|
||||
|
||||
if log_base2k_rem != 0 {
|
||||
a.at_mut(col_i, limb).iter_mut().for_each(|a| {
|
||||
let mut dist_f64: f64 = dist.sample(source);
|
||||
while dist_f64.abs() > bound {
|
||||
dist_f64 = dist.sample(source)
|
||||
}
|
||||
*a += (dist_f64.round() as i64) << log_base2k_rem;
|
||||
});
|
||||
} else {
|
||||
a.at_mut(col_i, limb).iter_mut().for_each(|a| {
|
||||
let mut dist_f64: f64 = dist.sample(source);
|
||||
while dist_f64.abs() > bound {
|
||||
dist_f64 = dist.sample(source)
|
||||
}
|
||||
*a += dist_f64.round() as i64
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FillNormal for VecZnxBig<T, FFT64>
|
||||
where
|
||||
VecZnxBig<T, FFT64>: VecZnxBigToMut<FFT64>,
|
||||
{
|
||||
fn fill_normal(&mut self, log_base2k: usize, col_i: usize, log_k: usize, source: &mut Source, sigma: f64, bound: f64) {
|
||||
self.fill_dist_f64(
|
||||
log_base2k,
|
||||
col_i,
|
||||
log_k,
|
||||
source,
|
||||
Normal::new(0.0, sigma).unwrap(),
|
||||
bound,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AddNormal for VecZnxBig<T, FFT64>
|
||||
where
|
||||
VecZnxBig<T, FFT64>: VecZnxBigToMut<FFT64>,
|
||||
{
|
||||
fn add_normal(&mut self, log_base2k: usize, col_i: usize, log_k: usize, source: &mut Source, sigma: f64, bound: f64) {
|
||||
self.add_dist_f64(
|
||||
log_base2k,
|
||||
a,
|
||||
col_i,
|
||||
log_k,
|
||||
source,
|
||||
@@ -111,14 +297,16 @@ impl<B: Backend> Sampling for Module<B> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Sampling;
|
||||
use std::fmt::Display;
|
||||
|
||||
use super::{AddNormal, FillUniform};
|
||||
use crate::vec_znx_ops::*;
|
||||
use crate::znx_base::*;
|
||||
use crate::{FFT64, Module, Stats, VecZnx};
|
||||
use sampling::source::Source;
|
||||
|
||||
#[test]
|
||||
fn fill_uniform() {
|
||||
fn vec_znx_fill_uniform() {
|
||||
let n: usize = 4096;
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(n);
|
||||
let log_base2k: usize = 17;
|
||||
@@ -129,7 +317,7 @@ mod tests {
|
||||
let one_12_sqrt: f64 = 0.28867513459481287;
|
||||
(0..cols).for_each(|col_i| {
|
||||
let mut a: VecZnx<_> = module.new_vec_znx(cols, size);
|
||||
module.fill_uniform(log_base2k, &mut a, col_i, size, &mut source);
|
||||
a.fill_uniform(log_base2k, col_i, size, &mut source);
|
||||
(0..cols).for_each(|col_j| {
|
||||
if col_j != col_i {
|
||||
(0..size).for_each(|limb_i| {
|
||||
@@ -149,7 +337,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_normal() {
|
||||
fn vec_znx_add_normal() {
|
||||
let n: usize = 4096;
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(n);
|
||||
let log_base2k: usize = 17;
|
||||
@@ -163,7 +351,7 @@ mod tests {
|
||||
let k_f64: f64 = (1u64 << log_k as u64) as f64;
|
||||
(0..cols).for_each(|col_i| {
|
||||
let mut a: VecZnx<_> = module.new_vec_znx(cols, size);
|
||||
module.add_normal(log_base2k, &mut a, col_i, log_k, &mut source, sigma, bound);
|
||||
a.add_normal(log_base2k, col_i, log_k, &mut source, sigma, bound);
|
||||
(0..cols).for_each(|col_j| {
|
||||
if col_j != col_i {
|
||||
(0..size).for_each(|limb_i| {
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
use base2k::{BACKEND, Module, Sampling, ScalarZnxDftOps, VecZnx, VecZnxDft, VecZnxDftOps, VecZnxOps, MatZnxDft, alloc_aligned_u8};
|
||||
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
|
||||
use rlwe::{
|
||||
ciphertext::{Ciphertext, new_gadget_ciphertext},
|
||||
elem::ElemCommon,
|
||||
encryptor::{encrypt_grlwe_sk, encrypt_grlwe_sk_tmp_bytes},
|
||||
gadget_product::{gadget_product_core, gadget_product_core_tmp_bytes},
|
||||
keys::SecretKey,
|
||||
parameters::{Parameters, ParametersLiteral},
|
||||
};
|
||||
use sampling::source::Source;
|
||||
|
||||
fn bench_gadget_product_inplace(c: &mut Criterion) {
|
||||
fn runner<'a>(
|
||||
module: &'a Module,
|
||||
res_dft_0: &'a mut VecZnxDft,
|
||||
res_dft_1: &'a mut VecZnxDft,
|
||||
a: &'a VecZnx,
|
||||
b: &'a Ciphertext<MatZnxDft>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &'a mut [u8],
|
||||
) -> Box<dyn FnMut() + 'a> {
|
||||
Box::new(move || {
|
||||
gadget_product_core(module, res_dft_0, res_dft_1, a, b, b_cols, tmp_bytes);
|
||||
})
|
||||
}
|
||||
|
||||
let mut b: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime> = c.benchmark_group("gadget_product_inplace");
|
||||
|
||||
for log_n in 10..11 {
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: BACKEND::FFT64,
|
||||
log_n: log_n,
|
||||
log_q: 32,
|
||||
log_p: 0,
|
||||
log_base2k: 16,
|
||||
log_scale: 20,
|
||||
xe: 3.2,
|
||||
xs: 128,
|
||||
};
|
||||
|
||||
let params: Parameters = Parameters::new(¶ms_lit);
|
||||
|
||||
let mut tmp_bytes: Vec<u8> = alloc_aligned_u8(
|
||||
params.encrypt_rlwe_sk_tmp_bytes(params.log_q())
|
||||
| gadget_product_core_tmp_bytes(
|
||||
params.module(),
|
||||
params.log_base2k(),
|
||||
params.log_q(),
|
||||
params.log_q(),
|
||||
params.cols_q(),
|
||||
params.log_qp(),
|
||||
)
|
||||
| encrypt_grlwe_sk_tmp_bytes(
|
||||
params.module(),
|
||||
params.log_base2k(),
|
||||
params.cols_qp(),
|
||||
params.log_qp(),
|
||||
),
|
||||
);
|
||||
|
||||
let mut source: Source = Source::new([3; 32]);
|
||||
|
||||
let mut sk0: SecretKey = SecretKey::new(params.module());
|
||||
let mut sk1: SecretKey = SecretKey::new(params.module());
|
||||
sk0.fill_ternary_hw(params.xs(), &mut source);
|
||||
sk1.fill_ternary_hw(params.xs(), &mut source);
|
||||
|
||||
let mut source_xe: Source = Source::new([4; 32]);
|
||||
let mut source_xa: Source = Source::new([5; 32]);
|
||||
|
||||
let mut sk0_svp_ppol: base2k::ScalarZnxDft = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk0_svp_ppol, &sk0.0);
|
||||
|
||||
let mut sk1_svp_ppol: base2k::ScalarZnxDft = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk1_svp_ppol, &sk1.0);
|
||||
|
||||
let mut gadget_ct: Ciphertext<MatZnxDft> = new_gadget_ciphertext(
|
||||
params.module(),
|
||||
params.log_base2k(),
|
||||
params.cols_q(),
|
||||
params.log_qp(),
|
||||
);
|
||||
|
||||
encrypt_grlwe_sk(
|
||||
params.module(),
|
||||
&mut gadget_ct,
|
||||
&sk0.0,
|
||||
&sk1_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
params.xe(),
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(params.log_q());
|
||||
|
||||
params.encrypt_rlwe_sk(
|
||||
&mut ct,
|
||||
None,
|
||||
&sk0_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
let mut res_dft_0: VecZnxDft = params.module().new_vec_znx_dft(1, gadget_ct.cols());
|
||||
let mut res_dft_1: VecZnxDft = params.module().new_vec_znx_dft(1, gadget_ct.cols());
|
||||
|
||||
let mut a: VecZnx = params.module().new_vec_znx(0, params.cols_q());
|
||||
params
|
||||
.module()
|
||||
.fill_uniform(params.log_base2k(), &mut a, params.cols_q(), &mut source_xa);
|
||||
|
||||
let b_cols: usize = gadget_ct.cols();
|
||||
|
||||
let runners: [(String, Box<dyn FnMut()>); 1] = [(format!("gadget_product"), {
|
||||
runner(
|
||||
params.module(),
|
||||
&mut res_dft_0,
|
||||
&mut res_dft_1,
|
||||
&mut a,
|
||||
&gadget_ct,
|
||||
b_cols,
|
||||
&mut tmp_bytes,
|
||||
)
|
||||
})];
|
||||
|
||||
for (name, mut runner) in runners {
|
||||
let id: BenchmarkId = BenchmarkId::new(name, format!("n={}", 1 << log_n));
|
||||
b.bench_with_input(id, &(), |b: &mut criterion::Bencher<'_>, _| {
|
||||
b.iter(&mut runner)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_gadget_product_inplace);
|
||||
criterion_main!(benches);
|
||||
@@ -1,76 +0,0 @@
|
||||
use base2k::{Encoding, ScalarZnxDftOps, VecZnx, alloc_aligned};
|
||||
use rlwe::{
|
||||
ciphertext::Ciphertext,
|
||||
elem::ElemCommon,
|
||||
keys::SecretKey,
|
||||
parameters::{Parameters, ParametersLiteral},
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use sampling::source::Source;
|
||||
|
||||
fn main() {
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: base2k::BACKEND::FFT64,
|
||||
log_n: 10,
|
||||
log_q: 54,
|
||||
log_p: 0,
|
||||
log_base2k: 17,
|
||||
log_scale: 20,
|
||||
xe: 3.2,
|
||||
xs: 128,
|
||||
};
|
||||
|
||||
let params: Parameters = Parameters::new(¶ms_lit);
|
||||
|
||||
let mut tmp_bytes: Vec<u8> =
|
||||
alloc_aligned(params.decrypt_rlwe_tmp_byte(params.log_q()) | params.encrypt_rlwe_sk_tmp_bytes(params.log_q()));
|
||||
|
||||
let mut source: Source = Source::new([0; 32]);
|
||||
let mut sk: SecretKey = SecretKey::new(params.module());
|
||||
sk.fill_ternary_hw(params.xs(), &mut source);
|
||||
|
||||
let mut want = vec![i64::default(); params.n()];
|
||||
|
||||
want.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
|
||||
|
||||
let mut pt: Plaintext = params.new_plaintext(params.log_q());
|
||||
|
||||
let log_base2k = pt.log_base2k();
|
||||
|
||||
let log_k: usize = params.log_q() - 20;
|
||||
|
||||
pt.0.value[0].encode_vec_i64(0, log_base2k, log_k, &want, 32);
|
||||
pt.0.value[0].normalize(log_base2k, &mut tmp_bytes);
|
||||
|
||||
println!("log_k: {}", log_k);
|
||||
pt.0.value[0].print(0, pt.cols(), 16);
|
||||
println!();
|
||||
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(params.log_q());
|
||||
|
||||
let mut source_xe: Source = Source::new([1; 32]);
|
||||
let mut source_xa: Source = Source::new([2; 32]);
|
||||
|
||||
let mut sk_svp_ppol: base2k::ScalarZnxDft = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk_svp_ppol, &sk.0);
|
||||
|
||||
params.encrypt_rlwe_sk(
|
||||
&mut ct,
|
||||
Some(&pt),
|
||||
&sk_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
params.decrypt_rlwe(&mut pt, &ct, &sk_svp_ppol, &mut tmp_bytes);
|
||||
pt.0.value[0].print(0, pt.cols(), 16);
|
||||
|
||||
let mut have = vec![i64::default(); params.n()];
|
||||
|
||||
println!("pt: {}", log_k);
|
||||
pt.0.value[0].decode_vec_i64(0, pt.log_base2k(), log_k, &mut have);
|
||||
|
||||
println!("want: {:?}", &want[..16]);
|
||||
println!("have: {:?}", &have[..16]);
|
||||
}
|
||||
@@ -1,349 +0,0 @@
|
||||
use crate::{
|
||||
ciphertext::{Ciphertext, new_gadget_ciphertext},
|
||||
elem::ElemCommon,
|
||||
encryptor::{encrypt_grlwe_sk, encrypt_grlwe_sk_tmp_bytes},
|
||||
key_switching::{key_switch_rlwe, key_switch_rlwe_inplace, key_switch_tmp_bytes},
|
||||
keys::SecretKey,
|
||||
parameters::Parameters,
|
||||
};
|
||||
use base2k::{
|
||||
MatZnxDft, MatZnxDftOps, Module, Scalar, ScalarAlloc, ScalarZnxDft, ScalarZnxDftOps, VecZnx, VecZnxBig, VecZnxBigOps,
|
||||
VecZnxDft, VecZnxDftOps, VecZnxOps, assert_alignement,
|
||||
};
|
||||
use sampling::source::Source;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Stores DFT([-A*AUTO(s, -p) + 2^{-K*i}*s + E, A]) where AUTO(X, p): X^{i} -> X^{i*p}
|
||||
pub struct AutomorphismKey {
|
||||
pub value: Ciphertext<MatZnxDft>,
|
||||
pub p: i64,
|
||||
}
|
||||
|
||||
pub fn automorphis_key_new_tmp_bytes(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> usize {
|
||||
module.bytes_of_scalar() + module.bytes_of_scalar_znx_dft() + encrypt_grlwe_sk_tmp_bytes(module, log_base2k, rows, log_q)
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn automorphism_key_new_tmp_bytes(&self, rows: usize, log_q: usize) -> usize {
|
||||
automorphis_key_new_tmp_bytes(self.module(), self.log_base2k(), rows, log_q)
|
||||
}
|
||||
|
||||
pub fn automorphism_tmp_bytes(&self, res_logq: usize, in_logq: usize, gct_logq: usize) -> usize {
|
||||
automorphism_tmp_bytes(
|
||||
self.module(),
|
||||
self.log_base2k(),
|
||||
res_logq,
|
||||
in_logq,
|
||||
gct_logq,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl AutomorphismKey {
|
||||
pub fn new(
|
||||
module: &Module,
|
||||
p: i64,
|
||||
sk: &SecretKey,
|
||||
log_base2k: usize,
|
||||
rows: usize,
|
||||
log_q: usize,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) -> Self {
|
||||
Self::new_many_core(
|
||||
module,
|
||||
&vec![p],
|
||||
sk,
|
||||
log_base2k,
|
||||
rows,
|
||||
log_q,
|
||||
source_xa,
|
||||
source_xe,
|
||||
sigma,
|
||||
tmp_bytes,
|
||||
)
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn new_many(
|
||||
module: &Module,
|
||||
p: &Vec<i64>,
|
||||
sk: &SecretKey,
|
||||
log_base2k: usize,
|
||||
rows: usize,
|
||||
log_q: usize,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) -> HashMap<i64, AutomorphismKey> {
|
||||
Self::new_many_core(
|
||||
module, p, sk, log_base2k, rows, log_q, source_xa, source_xe, sigma, tmp_bytes,
|
||||
)
|
||||
.into_iter()
|
||||
.zip(p.iter().cloned())
|
||||
.map(|(key, pi)| (pi, key))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn new_many_core(
|
||||
module: &Module,
|
||||
p: &Vec<i64>,
|
||||
sk: &SecretKey,
|
||||
log_base2k: usize,
|
||||
rows: usize,
|
||||
log_q: usize,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) -> Vec<Self> {
|
||||
let (sk_auto_bytes, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_scalar());
|
||||
let (sk_out_bytes, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_scalar_znx_dft());
|
||||
|
||||
let sk_auto: Scalar = module.new_scalar_from_bytes_borrow(sk_auto_bytes);
|
||||
let mut sk_out: ScalarZnxDft = module.new_scalar_znx_dft_from_bytes_borrow(sk_out_bytes);
|
||||
|
||||
let mut keys: Vec<AutomorphismKey> = Vec::new();
|
||||
|
||||
p.iter().for_each(|pi| {
|
||||
let mut value: Ciphertext<MatZnxDft> = new_gadget_ciphertext(module, log_base2k, rows, log_q);
|
||||
|
||||
let p_inv: i64 = module.galois_element_inv(*pi);
|
||||
|
||||
module.vec_znx_automorphism(p_inv, &mut sk_auto.as_vec_znx(), &sk.0.as_vec_znx());
|
||||
module.scalar_znx_dft_prepare(&mut sk_out, &sk_auto);
|
||||
encrypt_grlwe_sk(
|
||||
module, &mut value, &sk.0, &sk_out, source_xa, source_xe, sigma, tmp_bytes,
|
||||
);
|
||||
|
||||
keys.push(Self {
|
||||
value: value,
|
||||
p: *pi,
|
||||
})
|
||||
});
|
||||
|
||||
keys
|
||||
}
|
||||
}
|
||||
|
||||
pub fn automorphism_tmp_bytes(module: &Module, log_base2k: usize, res_logq: usize, in_logq: usize, gct_logq: usize) -> usize {
|
||||
key_switch_tmp_bytes(module, log_base2k, res_logq, in_logq, gct_logq)
|
||||
}
|
||||
|
||||
pub fn automorphism(
|
||||
module: &Module,
|
||||
c: &mut Ciphertext<VecZnx>,
|
||||
a: &Ciphertext<VecZnx>,
|
||||
b: &AutomorphismKey,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
key_switch_rlwe(module, c, a, &b.value, b_cols, tmp_bytes);
|
||||
// c[0] = AUTO([-b*AUTO(s, -p) + m + e], p) = [-AUTO(b, p)*s + AUTO(m, p) + AUTO(b, e)]
|
||||
module.vec_znx_automorphism_inplace(b.p, c.at_mut(0));
|
||||
// c[1] = AUTO(b, p)
|
||||
module.vec_znx_automorphism_inplace(b.p, c.at_mut(1));
|
||||
}
|
||||
|
||||
pub fn automorphism_inplace_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize {
|
||||
return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols)
|
||||
+ 2 * module.bytes_of_vec_znx_dft(1, std::cmp::min(c_cols, a_cols));
|
||||
}
|
||||
|
||||
pub fn automorphism_inplace(
|
||||
module: &Module,
|
||||
a: &mut Ciphertext<VecZnx>,
|
||||
b: &AutomorphismKey,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
key_switch_rlwe_inplace(module, a, &b.value, b_cols, tmp_bytes);
|
||||
// a[0] = AUTO([-b*AUTO(s, -p) + m + e], p) = [-AUTO(b, p)*s + AUTO(m, p) + AUTO(b, e)]
|
||||
module.vec_znx_automorphism_inplace(b.p, a.at_mut(0));
|
||||
// a[1] = AUTO(b, p)
|
||||
module.vec_znx_automorphism_inplace(b.p, a.at_mut(1));
|
||||
}
|
||||
|
||||
pub fn automorphism_big(
|
||||
module: &Module,
|
||||
c: &mut Ciphertext<VecZnxBig>,
|
||||
a: &Ciphertext<VecZnx>,
|
||||
b: &AutomorphismKey,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let cols = std::cmp::min(c.cols(), a.cols());
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(tmp_bytes.len() >= automorphism_tmp_bytes(module, c.cols(), a.cols(), b.value.rows(), b.value.cols()));
|
||||
assert_alignement(tmp_bytes.as_ptr());
|
||||
}
|
||||
|
||||
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
|
||||
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
|
||||
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_res_dft);
|
||||
|
||||
// a1_dft = DFT(a[1])
|
||||
module.vec_znx_dft(&mut a1_dft, a.at(1));
|
||||
|
||||
// res_dft = IDFT(<DFT(a), DFT([-A*AUTO(s, -p) + 2^{-K*i}*s + E])>) = [-b*AUTO(s, -p) + a * s + e]
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, b.value.at(0), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(c.at_mut(0), &mut res_dft);
|
||||
|
||||
// res_dft = [-b*AUTO(s, -p) + a * s + e] + [-a * s + m + e] = [-b*AUTO(s, -p) + m + e]
|
||||
module.vec_znx_big_add_small_inplace(c.at_mut(0), a.at(0));
|
||||
|
||||
// c[0] = AUTO([-b*AUTO(s, -p) + m + e], p) = [-AUTO(b, p)*s + AUTO(m, p) + AUTO(b, e)]
|
||||
module.vec_znx_big_automorphism_inplace(b.p, c.at_mut(0));
|
||||
|
||||
// res_dft = IDFT(<DFT(a), DFT([A])>) = [b]
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, b.value.at(1), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(c.at_mut(1), &mut res_dft);
|
||||
|
||||
// c[1] = AUTO(b, p)
|
||||
module.vec_znx_big_automorphism_inplace(b.p, c.at_mut(1));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{AutomorphismKey, automorphism};
|
||||
use crate::{
|
||||
ciphertext::Ciphertext,
|
||||
decryptor::decrypt_rlwe,
|
||||
elem::ElemCommon,
|
||||
encryptor::encrypt_rlwe_sk,
|
||||
keys::SecretKey,
|
||||
parameters::{Parameters, ParametersLiteral},
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use base2k::{BACKEND, Encoding, Module, ScalarZnxDft, ScalarZnxDftOps, VecZnx, VecZnxOps, alloc_aligned};
|
||||
use sampling::source::{Source, new_seed};
|
||||
|
||||
#[test]
|
||||
fn test_automorphism() {
|
||||
let log_base2k: usize = 10;
|
||||
let log_q: usize = 50;
|
||||
let log_p: usize = 15;
|
||||
|
||||
// Basic parameters with enough limbs to test edge cases
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: BACKEND::FFT64,
|
||||
log_n: 12,
|
||||
log_q: log_q,
|
||||
log_p: log_p,
|
||||
log_base2k: log_base2k,
|
||||
log_scale: 20,
|
||||
xe: 3.2,
|
||||
xs: 1 << 11,
|
||||
};
|
||||
|
||||
let params: Parameters = Parameters::new(¶ms_lit);
|
||||
|
||||
let module: &Module = params.module();
|
||||
let log_q: usize = params.log_q();
|
||||
let log_qp: usize = params.log_qp();
|
||||
let gct_rows: usize = params.cols_q();
|
||||
let gct_cols: usize = params.cols_qp();
|
||||
|
||||
// scratch space
|
||||
let mut tmp_bytes: Vec<u8> = alloc_aligned(
|
||||
params.decrypt_rlwe_tmp_byte(log_q)
|
||||
| params.encrypt_rlwe_sk_tmp_bytes(log_q)
|
||||
| params.automorphism_key_new_tmp_bytes(gct_rows, log_qp)
|
||||
| params.automorphism_tmp_bytes(log_q, log_q, log_qp),
|
||||
);
|
||||
|
||||
// Samplers for public and private randomness
|
||||
let mut source_xe: Source = Source::new(new_seed());
|
||||
let mut source_xa: Source = Source::new(new_seed());
|
||||
let mut source_xs: Source = Source::new(new_seed());
|
||||
|
||||
let mut sk: SecretKey = SecretKey::new(module);
|
||||
sk.fill_ternary_hw(params.xs(), &mut source_xs);
|
||||
let mut sk_svp_ppol: ScalarZnxDft = module.new_svp_ppol();
|
||||
module.svp_prepare(&mut sk_svp_ppol, &sk.0);
|
||||
|
||||
let p: i64 = -5;
|
||||
|
||||
let auto_key: AutomorphismKey = AutomorphismKey::new(
|
||||
module,
|
||||
p,
|
||||
&sk,
|
||||
log_base2k,
|
||||
gct_rows,
|
||||
log_qp,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
params.xe(),
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
let mut data: Vec<i64> = vec![0i64; params.n()];
|
||||
|
||||
data.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
|
||||
|
||||
let log_k: usize = 2 * log_base2k;
|
||||
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(log_q);
|
||||
let mut pt: Plaintext = params.new_plaintext(log_q);
|
||||
let mut pt_auto: Plaintext = params.new_plaintext(log_q);
|
||||
|
||||
pt.at_mut(0).encode_vec_i64(0, log_base2k, log_k, &data, 32);
|
||||
module.vec_znx_automorphism(p, pt_auto.at_mut(0), pt.at(0));
|
||||
|
||||
encrypt_rlwe_sk(
|
||||
module,
|
||||
&mut ct.elem_mut(),
|
||||
Some(pt.at(0)),
|
||||
&sk_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
params.xe(),
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
let mut ct_auto: Ciphertext<VecZnx> = params.new_ciphertext(log_q);
|
||||
|
||||
// ct <- AUTO(ct)
|
||||
automorphism(
|
||||
module,
|
||||
&mut ct_auto,
|
||||
&ct,
|
||||
&auto_key,
|
||||
gct_cols,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
// pt = dec(auto(ct)) - auto(pt)
|
||||
decrypt_rlwe(
|
||||
module,
|
||||
pt.elem_mut(),
|
||||
ct_auto.elem(),
|
||||
&sk_svp_ppol,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
module.vec_znx_sub_ba_inplace(pt.at_mut(0), pt_auto.at(0));
|
||||
|
||||
// pt.at(0).print(pt.cols(), 16);
|
||||
|
||||
let noise_have: f64 = pt.at(0).std(0, log_base2k).log2();
|
||||
|
||||
let var_msg: f64 = (params.xs() as f64) / params.n() as f64;
|
||||
let var_a_err: f64 = 1f64 / 12f64;
|
||||
|
||||
let noise_pred: f64 = params.noise_grlwe_product(var_msg, var_a_err, ct_auto.log_q(), auto_key.value.log_q());
|
||||
|
||||
println!("noise_pred: {}", noise_pred);
|
||||
println!("noise_have: {}", noise_have);
|
||||
|
||||
assert!(noise_have <= noise_pred + 1.0);
|
||||
}
|
||||
}
|
||||
@@ -1,93 +1,4 @@
|
||||
use crate::elem::{Elem, ElemCommon};
|
||||
use crate::parameters::Parameters;
|
||||
use base2k::{ZnxInfos, Layout, Module, VecZnx, MatZnxDft};
|
||||
|
||||
pub struct Ciphertext<T>(pub Elem<T>);
|
||||
|
||||
impl Parameters {
|
||||
pub fn new_ciphertext(&self, log_q: usize) -> Ciphertext<VecZnx> {
|
||||
Ciphertext::new(self.module(), self.log_base2k(), log_q, 2)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ElemCommon<T> for Ciphertext<T>
|
||||
where
|
||||
T: ZnxInfos,
|
||||
{
|
||||
fn n(&self) -> usize {
|
||||
self.elem().n()
|
||||
}
|
||||
|
||||
fn log_n(&self) -> usize {
|
||||
self.elem().log_n()
|
||||
}
|
||||
|
||||
fn log_q(&self) -> usize {
|
||||
self.elem().log_q()
|
||||
}
|
||||
|
||||
fn elem(&self) -> &Elem<T> {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn elem_mut(&mut self) -> &mut Elem<T> {
|
||||
&mut self.0
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
self.elem().size()
|
||||
}
|
||||
|
||||
fn layout(&self) -> Layout {
|
||||
self.elem().layout()
|
||||
}
|
||||
|
||||
fn rows(&self) -> usize {
|
||||
self.elem().rows()
|
||||
}
|
||||
|
||||
fn cols(&self) -> usize {
|
||||
self.elem().cols()
|
||||
}
|
||||
|
||||
fn at(&self, i: usize) -> &T {
|
||||
self.elem().at(i)
|
||||
}
|
||||
|
||||
fn at_mut(&mut self, i: usize) -> &mut T {
|
||||
self.elem_mut().at_mut(i)
|
||||
}
|
||||
|
||||
fn log_base2k(&self) -> usize {
|
||||
self.elem().log_base2k()
|
||||
}
|
||||
|
||||
fn log_scale(&self) -> usize {
|
||||
self.elem().log_scale()
|
||||
}
|
||||
}
|
||||
|
||||
impl Ciphertext<VecZnx> {
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize, rows: usize) -> Self {
|
||||
Self(Elem::<VecZnx>::new(module, log_base2k, log_q, rows))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_rlwe_ciphertext(module: &Module, log_base2k: usize, log_q: usize) -> Ciphertext<VecZnx> {
|
||||
let rows: usize = 2;
|
||||
Ciphertext::<VecZnx>::new(module, log_base2k, log_q, rows)
|
||||
}
|
||||
|
||||
pub fn new_gadget_ciphertext(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> Ciphertext<MatZnxDft> {
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let mut elem: Elem<MatZnxDft> = Elem::<MatZnxDft>::new(module, log_base2k, 2, rows, cols);
|
||||
elem.log_q = log_q;
|
||||
Ciphertext(elem)
|
||||
}
|
||||
|
||||
pub fn new_rgsw_ciphertext(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> Ciphertext<MatZnxDft> {
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let mut elem: Elem<MatZnxDft> = Elem::<MatZnxDft>::new(module, log_base2k, 4, rows, cols);
|
||||
elem.log_q = log_q;
|
||||
Ciphertext(elem)
|
||||
pub struct Ciphertext{
|
||||
x
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
use crate::{
|
||||
ciphertext::Ciphertext,
|
||||
elem::{Elem, ElemCommon},
|
||||
keys::SecretKey,
|
||||
parameters::Parameters,
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use base2k::{Module, ScalarZnxDft, ScalarZnxDftOps, VecZnx, VecZnxBigOps, VecZnxDft, VecZnxDftOps};
|
||||
use std::cmp::min;
|
||||
|
||||
pub struct Decryptor {
|
||||
sk: ScalarZnxDft,
|
||||
}
|
||||
|
||||
impl Decryptor {
|
||||
pub fn new(params: &Parameters, sk: &SecretKey) -> Self {
|
||||
let mut sk_svp_ppol: ScalarZnxDft = params.module().new_svp_ppol();
|
||||
sk.prepare(params.module(), &mut sk_svp_ppol);
|
||||
Self { sk: sk_svp_ppol }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrypt_rlwe_tmp_byte(module: &Module, cols: usize) -> usize {
|
||||
module.bytes_of_vec_znx_dft(1, cols) + module.vec_znx_big_normalize_tmp_bytes()
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn decrypt_rlwe_tmp_byte(&self, log_q: usize) -> usize {
|
||||
decrypt_rlwe_tmp_byte(
|
||||
self.module(),
|
||||
(log_q + self.log_base2k() - 1) / self.log_base2k(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn decrypt_rlwe(&self, res: &mut Plaintext, ct: &Ciphertext<VecZnx>, sk: &ScalarZnxDft, tmp_bytes: &mut [u8]) {
|
||||
decrypt_rlwe(self.module(), &mut res.0, &ct.0, sk, tmp_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrypt_rlwe(module: &Module, res: &mut Elem<VecZnx>, a: &Elem<VecZnx>, sk: &ScalarZnxDft, tmp_bytes: &mut [u8]) {
|
||||
let cols: usize = a.cols();
|
||||
|
||||
assert!(
|
||||
tmp_bytes.len() >= decrypt_rlwe_tmp_byte(module, cols),
|
||||
"invalid tmp_bytes: tmp_bytes.len()={} < decrypt_rlwe_tmp_byte={}",
|
||||
tmp_bytes.len(),
|
||||
decrypt_rlwe_tmp_byte(module, cols)
|
||||
);
|
||||
|
||||
let (tmp_bytes_vec_znx_dft, tmp_bytes_normalize) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
|
||||
let mut res_dft: VecZnxDft = VecZnxDft::from_bytes_borrow(module, 1, cols, tmp_bytes_vec_znx_dft);
|
||||
let mut res_big: base2k::VecZnxBig = res_dft.as_vec_znx_big();
|
||||
|
||||
// res_dft <- DFT(ct[1]) * DFT(sk)
|
||||
module.svp_apply_dft(&mut res_dft, sk, a.at(1));
|
||||
// res_big <- ct[1] x sk
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft);
|
||||
// res_big <- ct[1] x sk + ct[0]
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, a.at(0));
|
||||
// res <- normalize(ct[1] x sk + ct[0])
|
||||
module.vec_znx_big_normalize(a.log_base2k(), res.at_mut(0), &res_big, tmp_bytes_normalize);
|
||||
|
||||
res.log_base2k = a.log_base2k();
|
||||
res.log_q = min(res.log_q(), a.log_q());
|
||||
res.log_scale = a.log_scale();
|
||||
}
|
||||
168
rlwe/src/elem.rs
168
rlwe/src/elem.rs
@@ -1,168 +0,0 @@
|
||||
use base2k::{ZnxInfos, Layout, Module, VecZnx, VecZnxOps, MatZnxDft, MatZnxDftOps};
|
||||
|
||||
pub struct Elem<T> {
|
||||
pub value: Vec<T>,
|
||||
pub log_base2k: usize,
|
||||
pub log_q: usize,
|
||||
pub log_scale: usize,
|
||||
}
|
||||
|
||||
pub trait ElemVecZnx {
|
||||
fn from_bytes(module: &Module, log_base2k: usize, log_q: usize, size: usize, bytes: &mut [u8]) -> Elem<VecZnx>;
|
||||
fn from_bytes_borrow(module: &Module, log_base2k: usize, log_q: usize, size: usize, bytes: &mut [u8]) -> Elem<VecZnx>;
|
||||
fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, size: usize) -> usize;
|
||||
fn zero(&mut self);
|
||||
}
|
||||
|
||||
impl ElemVecZnx for Elem<VecZnx> {
|
||||
fn bytes_of(module: &Module, log_base2k: usize, log_q: usize, size: usize) -> usize {
|
||||
let cols = (log_q + log_base2k - 1) / log_base2k;
|
||||
module.n() * cols * size * 8
|
||||
}
|
||||
|
||||
fn from_bytes(module: &Module, log_base2k: usize, log_q: usize, size: usize, bytes: &mut [u8]) -> Elem<VecZnx> {
|
||||
assert!(size > 0);
|
||||
let n: usize = module.n();
|
||||
assert!(bytes.len() >= Self::bytes_of(module, log_base2k, log_q, size));
|
||||
let mut value: Vec<VecZnx> = Vec::new();
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let elem_size = VecZnx::bytes_of(n, size, cols);
|
||||
let mut ptr: usize = 0;
|
||||
(0..size).for_each(|_| {
|
||||
value.push(VecZnx::from_bytes(n, 1, cols, &mut bytes[ptr..]));
|
||||
ptr += elem_size
|
||||
});
|
||||
Self {
|
||||
value,
|
||||
log_q,
|
||||
log_base2k,
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_bytes_borrow(module: &Module, log_base2k: usize, log_q: usize, size: usize, bytes: &mut [u8]) -> Elem<VecZnx> {
|
||||
assert!(size > 0);
|
||||
let n: usize = module.n();
|
||||
assert!(bytes.len() >= Self::bytes_of(module, log_base2k, log_q, size));
|
||||
let mut value: Vec<VecZnx> = Vec::new();
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let elem_size = VecZnx::bytes_of(n, 1, cols);
|
||||
let mut ptr: usize = 0;
|
||||
(0..size).for_each(|_| {
|
||||
value.push(VecZnx::from_bytes_borrow(n, 1, cols, &mut bytes[ptr..]));
|
||||
ptr += elem_size
|
||||
});
|
||||
Self {
|
||||
value,
|
||||
log_q,
|
||||
log_base2k,
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn zero(&mut self) {
|
||||
self.value.iter_mut().for_each(|i| i.zero());
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ElemCommon<T> {
|
||||
fn n(&self) -> usize;
|
||||
fn log_n(&self) -> usize;
|
||||
fn elem(&self) -> &Elem<T>;
|
||||
fn elem_mut(&mut self) -> &mut Elem<T>;
|
||||
fn size(&self) -> usize;
|
||||
fn layout(&self) -> Layout;
|
||||
fn rows(&self) -> usize;
|
||||
fn cols(&self) -> usize;
|
||||
fn log_base2k(&self) -> usize;
|
||||
fn log_q(&self) -> usize;
|
||||
fn log_scale(&self) -> usize;
|
||||
fn at(&self, i: usize) -> &T;
|
||||
fn at_mut(&mut self, i: usize) -> &mut T;
|
||||
}
|
||||
|
||||
impl<T: ZnxInfos> ElemCommon<T> for Elem<T> {
|
||||
fn n(&self) -> usize {
|
||||
self.value[0].n()
|
||||
}
|
||||
|
||||
fn log_n(&self) -> usize {
|
||||
self.value[0].log_n()
|
||||
}
|
||||
|
||||
fn elem(&self) -> &Elem<T> {
|
||||
self
|
||||
}
|
||||
|
||||
fn elem_mut(&mut self) -> &mut Elem<T> {
|
||||
self
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
self.value.len()
|
||||
}
|
||||
|
||||
fn layout(&self) -> Layout {
|
||||
self.value[0].layout()
|
||||
}
|
||||
|
||||
fn rows(&self) -> usize {
|
||||
self.value[0].rows()
|
||||
}
|
||||
|
||||
fn cols(&self) -> usize {
|
||||
self.value[0].cols()
|
||||
}
|
||||
|
||||
fn log_base2k(&self) -> usize {
|
||||
self.log_base2k
|
||||
}
|
||||
|
||||
fn log_q(&self) -> usize {
|
||||
self.log_q
|
||||
}
|
||||
|
||||
fn log_scale(&self) -> usize {
|
||||
self.log_scale
|
||||
}
|
||||
|
||||
fn at(&self, i: usize) -> &T {
|
||||
assert!(i < self.size());
|
||||
&self.value[i]
|
||||
}
|
||||
|
||||
fn at_mut(&mut self, i: usize) -> &mut T {
|
||||
assert!(i < self.size());
|
||||
&mut self.value[i]
|
||||
}
|
||||
}
|
||||
|
||||
impl Elem<VecZnx> {
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize, rows: usize) -> Self {
|
||||
assert!(rows > 0);
|
||||
let cols: usize = (log_q + log_base2k - 1) / log_base2k;
|
||||
let mut value: Vec<VecZnx> = Vec::new();
|
||||
(0..rows).for_each(|_| value.push(module.new_vec_znx(1, cols)));
|
||||
Self {
|
||||
value,
|
||||
log_q,
|
||||
log_base2k,
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Elem<MatZnxDft> {
|
||||
pub fn new(module: &Module, log_base2k: usize, size: usize, rows: usize, cols: usize) -> Self {
|
||||
assert!(rows > 0);
|
||||
assert!(cols > 0);
|
||||
let mut value: Vec<MatZnxDft> = Vec::new();
|
||||
(0..size).for_each(|_| value.push(module.new_vmp_pmat(1, rows, cols)));
|
||||
Self {
|
||||
value: value,
|
||||
log_q: 0,
|
||||
log_base2k: log_base2k,
|
||||
log_scale: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,369 +0,0 @@
|
||||
use crate::ciphertext::Ciphertext;
|
||||
use crate::elem::{Elem, ElemCommon, ElemVecZnx};
|
||||
use crate::keys::SecretKey;
|
||||
use crate::parameters::Parameters;
|
||||
use crate::plaintext::Plaintext;
|
||||
use base2k::sampling::Sampling;
|
||||
use base2k::{
|
||||
ZnxInfos, Module, Scalar, ScalarZnxDft, ScalarZnxDftOps, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps, MatZnxDft,
|
||||
MatZnxDftOps,
|
||||
};
|
||||
|
||||
use sampling::source::{Source, new_seed};
|
||||
|
||||
impl Parameters {
|
||||
pub fn encrypt_rlwe_sk_tmp_bytes(&self, log_q: usize) -> usize {
|
||||
encrypt_rlwe_sk_tmp_bytes(self.module(), self.log_base2k(), log_q)
|
||||
}
|
||||
pub fn encrypt_rlwe_sk(
|
||||
&self,
|
||||
ct: &mut Ciphertext<VecZnx>,
|
||||
pt: Option<&Plaintext>,
|
||||
sk: &ScalarZnxDft,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
encrypt_rlwe_sk(
|
||||
self.module(),
|
||||
&mut ct.0,
|
||||
pt.map(|pt| pt.at(0)),
|
||||
sk,
|
||||
source_xa,
|
||||
source_xe,
|
||||
self.xe(),
|
||||
tmp_bytes,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EncryptorSk {
|
||||
sk: ScalarZnxDft,
|
||||
source_xa: Source,
|
||||
source_xe: Source,
|
||||
initialized: bool,
|
||||
tmp_bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
impl EncryptorSk {
|
||||
pub fn new(params: &Parameters, sk: Option<&SecretKey>) -> Self {
|
||||
let mut sk_svp_ppol: ScalarZnxDft = params.module().new_svp_ppol();
|
||||
let mut initialized: bool = false;
|
||||
if let Some(sk) = sk {
|
||||
sk.prepare(params.module(), &mut sk_svp_ppol);
|
||||
initialized = true;
|
||||
}
|
||||
Self {
|
||||
sk: sk_svp_ppol,
|
||||
initialized,
|
||||
source_xa: Source::new(new_seed()),
|
||||
source_xe: Source::new(new_seed()),
|
||||
tmp_bytes: vec![0u8; params.encrypt_rlwe_sk_tmp_bytes(params.cols_qp())],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_sk(&mut self, module: &Module, sk: &SecretKey) {
|
||||
sk.prepare(module, &mut self.sk);
|
||||
self.initialized = true;
|
||||
}
|
||||
|
||||
pub fn seed_source_xa(&mut self, seed: [u8; 32]) {
|
||||
self.source_xa = Source::new(seed)
|
||||
}
|
||||
|
||||
pub fn seed_source_xe(&mut self, seed: [u8; 32]) {
|
||||
self.source_xe = Source::new(seed)
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk(&mut self, params: &Parameters, ct: &mut Ciphertext<VecZnx>, pt: Option<&Plaintext>) {
|
||||
assert!(
|
||||
self.initialized == true,
|
||||
"invalid call to [EncryptorSk.encrypt_rlwe_sk]: [EncryptorSk] has not been initialized with a [SecretKey]"
|
||||
);
|
||||
params.encrypt_rlwe_sk(
|
||||
ct,
|
||||
pt,
|
||||
&self.sk,
|
||||
&mut self.source_xa,
|
||||
&mut self.source_xe,
|
||||
&mut self.tmp_bytes,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk_core(
|
||||
&self,
|
||||
params: &Parameters,
|
||||
ct: &mut Ciphertext<VecZnx>,
|
||||
pt: Option<&Plaintext>,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
assert!(
|
||||
self.initialized == true,
|
||||
"invalid call to [EncryptorSk.encrypt_rlwe_sk]: [EncryptorSk] has not been initialized with a [SecretKey]"
|
||||
);
|
||||
params.encrypt_rlwe_sk(ct, pt, &self.sk, source_xa, source_xe, tmp_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk_tmp_bytes(module: &Module, log_base2k: usize, log_q: usize) -> usize {
|
||||
module.bytes_of_vec_znx_dft(1, (log_q + log_base2k - 1) / log_base2k) + module.vec_znx_big_normalize_tmp_bytes()
|
||||
}
|
||||
pub fn encrypt_rlwe_sk(
|
||||
module: &Module,
|
||||
ct: &mut Elem<VecZnx>,
|
||||
pt: Option<&VecZnx>,
|
||||
sk: &ScalarZnxDft,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
encrypt_rlwe_sk_core::<0>(module, ct, pt, sk, source_xa, source_xe, sigma, tmp_bytes)
|
||||
}
|
||||
|
||||
fn encrypt_rlwe_sk_core<const PT_POS: u8>(
|
||||
module: &Module,
|
||||
ct: &mut Elem<VecZnx>,
|
||||
pt: Option<&VecZnx>,
|
||||
sk: &ScalarZnxDft,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let cols: usize = ct.cols();
|
||||
let log_base2k: usize = ct.log_base2k();
|
||||
let log_q: usize = ct.log_q();
|
||||
|
||||
assert!(
|
||||
tmp_bytes.len() >= encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q),
|
||||
"invalid tmp_bytes: tmp_bytes={} < encrypt_rlwe_sk_tmp_bytes={}",
|
||||
tmp_bytes.len(),
|
||||
encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q)
|
||||
);
|
||||
|
||||
let log_q: usize = ct.log_q();
|
||||
let log_base2k: usize = ct.log_base2k();
|
||||
let c1: &mut VecZnx = ct.at_mut(1);
|
||||
|
||||
// c1 <- Z_{2^prec}[X]/(X^{N}+1)
|
||||
module.fill_uniform(log_base2k, c1, cols, source_xa);
|
||||
|
||||
let (tmp_bytes_vec_znx_dft, tmp_bytes_normalize) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
|
||||
// Scratch space for DFT values
|
||||
let mut buf_dft: VecZnxDft = VecZnxDft::from_bytes_borrow(module, 1, cols, tmp_bytes_vec_znx_dft);
|
||||
|
||||
// Applies buf_dft <- DFT(s) * DFT(c1)
|
||||
module.svp_apply_dft(&mut buf_dft, sk, c1);
|
||||
|
||||
// Alias scratch space
|
||||
let mut buf_big: VecZnxBig = buf_dft.as_vec_znx_big();
|
||||
|
||||
// buf_big = s x c1
|
||||
module.vec_znx_idft_tmp_a(&mut buf_big, &mut buf_dft);
|
||||
|
||||
match PT_POS {
|
||||
// c0 <- -s x c1 + m
|
||||
0 => {
|
||||
let c0: &mut VecZnx = ct.at_mut(0);
|
||||
if let Some(pt) = pt {
|
||||
module.vec_znx_big_sub_small_a_inplace(&mut buf_big, pt);
|
||||
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, tmp_bytes_normalize);
|
||||
} else {
|
||||
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, tmp_bytes_normalize);
|
||||
module.vec_znx_negate_inplace(c0);
|
||||
}
|
||||
}
|
||||
// c1 <- c1 + m
|
||||
1 => {
|
||||
if let Some(pt) = pt {
|
||||
module.vec_znx_add_inplace(c1, pt);
|
||||
c1.normalize(log_base2k, tmp_bytes_normalize);
|
||||
}
|
||||
let c0: &mut VecZnx = ct.at_mut(0);
|
||||
module.vec_znx_big_normalize(log_base2k, c0, &buf_big, tmp_bytes_normalize);
|
||||
module.vec_znx_negate_inplace(c0);
|
||||
}
|
||||
_ => panic!("PT_POS must be 1 or 2"),
|
||||
}
|
||||
|
||||
// c0 <- -s x c1 + m + e
|
||||
module.add_normal(
|
||||
log_base2k,
|
||||
ct.at_mut(0),
|
||||
log_q,
|
||||
source_xe,
|
||||
sigma,
|
||||
(sigma * 6.0).ceil(),
|
||||
);
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn encrypt_grlwe_sk_tmp_bytes(&self, rows: usize, log_q: usize) -> usize {
|
||||
encrypt_grlwe_sk_tmp_bytes(self.module(), self.log_base2k(), rows, log_q)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encrypt_grlwe_sk_tmp_bytes(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> usize {
|
||||
let cols = (log_q + log_base2k - 1) / log_base2k;
|
||||
Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 2)
|
||||
+ Plaintext::bytes_of(module, log_base2k, log_q)
|
||||
+ encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q)
|
||||
+ module.vmp_prepare_tmp_bytes(rows, cols)
|
||||
}
|
||||
|
||||
pub fn encrypt_grlwe_sk(
|
||||
module: &Module,
|
||||
ct: &mut Ciphertext<MatZnxDft>,
|
||||
m: &Scalar,
|
||||
sk: &ScalarZnxDft,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let log_q: usize = ct.log_q();
|
||||
let log_base2k: usize = ct.log_base2k();
|
||||
let (left, right) = ct.0.value.split_at_mut(1);
|
||||
encrypt_grlwe_sk_core::<0>(
|
||||
module,
|
||||
log_base2k,
|
||||
[&mut left[0], &mut right[0]],
|
||||
log_q,
|
||||
m,
|
||||
sk,
|
||||
source_xa,
|
||||
source_xe,
|
||||
sigma,
|
||||
tmp_bytes,
|
||||
)
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn encrypt_rgsw_sk_tmp_bytes(&self, rows: usize, log_q: usize) -> usize {
|
||||
encrypt_rgsw_sk_tmp_bytes(self.module(), self.log_base2k(), rows, log_q)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encrypt_rgsw_sk_tmp_bytes(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> usize {
|
||||
let cols = (log_q + log_base2k - 1) / log_base2k;
|
||||
Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 2)
|
||||
+ Plaintext::bytes_of(module, log_base2k, log_q)
|
||||
+ encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q)
|
||||
+ module.vmp_prepare_tmp_bytes(rows, cols)
|
||||
}
|
||||
|
||||
pub fn encrypt_rgsw_sk(
|
||||
module: &Module,
|
||||
ct: &mut Ciphertext<MatZnxDft>,
|
||||
m: &Scalar,
|
||||
sk: &ScalarZnxDft,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let log_q: usize = ct.log_q();
|
||||
let log_base2k: usize = ct.log_base2k();
|
||||
|
||||
let (left, right) = ct.0.value.split_at_mut(2);
|
||||
let (ll, lr) = left.split_at_mut(1);
|
||||
let (rl, rr) = right.split_at_mut(1);
|
||||
|
||||
encrypt_grlwe_sk_core::<0>(
|
||||
module,
|
||||
log_base2k,
|
||||
[&mut ll[0], &mut lr[0]],
|
||||
log_q,
|
||||
m,
|
||||
sk,
|
||||
source_xa,
|
||||
source_xe,
|
||||
sigma,
|
||||
tmp_bytes,
|
||||
);
|
||||
encrypt_grlwe_sk_core::<1>(
|
||||
module,
|
||||
log_base2k,
|
||||
[&mut rl[0], &mut rr[0]],
|
||||
log_q,
|
||||
m,
|
||||
sk,
|
||||
source_xa,
|
||||
source_xe,
|
||||
sigma,
|
||||
tmp_bytes,
|
||||
);
|
||||
}
|
||||
|
||||
fn encrypt_grlwe_sk_core<const PT_POS: u8>(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
mut ct: [&mut MatZnxDft; 2],
|
||||
log_q: usize,
|
||||
m: &Scalar,
|
||||
sk: &ScalarZnxDft,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let rows: usize = ct[0].rows();
|
||||
|
||||
let min_tmp_bytes_len = encrypt_grlwe_sk_tmp_bytes(module, log_base2k, rows, log_q);
|
||||
|
||||
assert!(
|
||||
tmp_bytes.len() >= min_tmp_bytes_len,
|
||||
"invalid tmp_bytes: tmp_bytes.len()={} < encrypt_grlwe_sk_tmp_bytes={}",
|
||||
tmp_bytes.len(),
|
||||
min_tmp_bytes_len
|
||||
);
|
||||
|
||||
let bytes_of_elem: usize = Elem::<VecZnx>::bytes_of(module, log_base2k, log_q, 2);
|
||||
let bytes_of_pt: usize = Plaintext::bytes_of(module, log_base2k, log_q);
|
||||
let bytes_of_enc_sk: usize = encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q);
|
||||
|
||||
let (tmp_bytes_pt, tmp_bytes) = tmp_bytes.split_at_mut(bytes_of_pt);
|
||||
let (tmp_bytes_enc_sk, tmp_bytes) = tmp_bytes.split_at_mut(bytes_of_enc_sk);
|
||||
let (tmp_bytes_elem, tmp_bytes_vmp_prepare_row) = tmp_bytes.split_at_mut(bytes_of_elem);
|
||||
|
||||
let mut tmp_elem: Elem<VecZnx> = Elem::<VecZnx>::from_bytes_borrow(module, log_base2k, log_q, 2, tmp_bytes_elem);
|
||||
let mut tmp_pt: Plaintext = Plaintext::from_bytes_borrow(module, log_base2k, log_q, tmp_bytes_pt);
|
||||
|
||||
(0..rows).for_each(|row_i| {
|
||||
// Sets the i-th row of the RLWE sample to m (i.e. m * 2^{-log_base2k*i})
|
||||
tmp_pt.at_mut(0).at_mut(row_i).copy_from_slice(&m.raw());
|
||||
|
||||
// Encrypts RLWE(m * 2^{-log_base2k*i})
|
||||
encrypt_rlwe_sk_core::<PT_POS>(
|
||||
module,
|
||||
&mut tmp_elem,
|
||||
Some(tmp_pt.at(0)),
|
||||
sk,
|
||||
source_xa,
|
||||
source_xe,
|
||||
sigma,
|
||||
tmp_bytes_enc_sk,
|
||||
);
|
||||
|
||||
// Zeroes the ith-row of tmp_pt
|
||||
tmp_pt.at_mut(0).at_mut(row_i).fill(0);
|
||||
|
||||
// GRLWE[row_i][0||1] = [-as + m * 2^{-i*log_base2k} + e*2^{-log_q} || a]
|
||||
module.vmp_prepare_row(
|
||||
ct[0],
|
||||
tmp_elem.at(0).raw(),
|
||||
row_i,
|
||||
tmp_bytes_vmp_prepare_row,
|
||||
);
|
||||
module.vmp_prepare_row(
|
||||
&mut ct[1],
|
||||
tmp_elem.at(1).raw(),
|
||||
row_i,
|
||||
tmp_bytes_vmp_prepare_row,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1,383 +0,0 @@
|
||||
use crate::{ciphertext::Ciphertext, elem::ElemCommon, parameters::Parameters};
|
||||
use base2k::{Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, MatZnxDft, MatZnxDftOps};
|
||||
use std::cmp::min;
|
||||
|
||||
pub fn gadget_product_core_tmp_bytes(
|
||||
module: &Module,
|
||||
log_base2k: usize,
|
||||
res_log_q: usize,
|
||||
in_log_q: usize,
|
||||
gct_rows: usize,
|
||||
gct_log_q: usize,
|
||||
) -> usize {
|
||||
let gct_cols: usize = (gct_log_q + log_base2k - 1) / log_base2k;
|
||||
let in_cols: usize = (in_log_q + log_base2k - 1) / log_base2k;
|
||||
let out_cols: usize = (res_log_q + log_base2k - 1) / log_base2k;
|
||||
module.vmp_apply_dft_to_dft_tmp_bytes(out_cols, in_cols, gct_rows, gct_cols)
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn gadget_product_tmp_bytes(&self, res_log_q: usize, in_log_q: usize, gct_rows: usize, gct_log_q: usize) -> usize {
|
||||
gadget_product_core_tmp_bytes(
|
||||
self.module(),
|
||||
self.log_base2k(),
|
||||
res_log_q,
|
||||
in_log_q,
|
||||
gct_rows,
|
||||
gct_log_q,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gadget_product_core(
|
||||
module: &Module,
|
||||
res_dft_0: &mut VecZnxDft,
|
||||
res_dft_1: &mut VecZnxDft,
|
||||
a: &VecZnx,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
assert!(b_cols <= b.cols());
|
||||
module.vec_znx_dft(res_dft_1, a);
|
||||
module.vmp_apply_dft_to_dft(res_dft_0, res_dft_1, b.at(0), tmp_bytes);
|
||||
module.vmp_apply_dft_to_dft_inplace(res_dft_1, b.at(1), tmp_bytes);
|
||||
}
|
||||
|
||||
pub fn gadget_product_big_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize {
|
||||
return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols)
|
||||
+ 2 * module.bytes_of_vec_znx_dft(1, min(c_cols, a_cols));
|
||||
}
|
||||
|
||||
/// Evaluates the gadget product: c.at(i) = IDFT(<DFT(a.at(i)), b.at(i)>)
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `module`: backend support for operations mod (X^N + 1).
|
||||
/// * `c`: a [Ciphertext<VecZnxBig>] with cols_c cols.
|
||||
/// * `a`: a [Ciphertext<VecZnx>] with cols_a cols.
|
||||
/// * `b`: a [Ciphertext<VmpPMat>] with at least min(cols_c, cols_a) rows.
|
||||
pub fn gadget_product_big(
|
||||
module: &Module,
|
||||
c: &mut Ciphertext<VecZnxBig>,
|
||||
a: &Ciphertext<VecZnx>,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let cols: usize = min(c.cols(), a.cols());
|
||||
|
||||
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
|
||||
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
|
||||
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_res_dft);
|
||||
|
||||
// a1_dft = DFT(a[1])
|
||||
module.vec_znx_dft(&mut a1_dft, a.at(1));
|
||||
|
||||
// c[i] = IDFT(DFT(a[1]) * b[i])
|
||||
(0..2).for_each(|i| {
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, b.at(i), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(c.at_mut(i), &mut res_dft);
|
||||
})
|
||||
}
|
||||
|
||||
/// Evaluates the gadget product: c.at(i) = NORMALIZE(IDFT(<DFT(a.at(i)), b.at(i)>)
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `module`: backend support for operations mod (X^N + 1).
|
||||
/// * `c`: a [Ciphertext<VecZnx>] with cols_c cols.
|
||||
/// * `a`: a [Ciphertext<VecZnx>] with cols_a cols.
|
||||
/// * `b`: a [Ciphertext<VmpPMat>] with at least min(cols_c, cols_a) rows.
|
||||
pub fn gadget_product(
|
||||
module: &Module,
|
||||
c: &mut Ciphertext<VecZnx>,
|
||||
a: &Ciphertext<VecZnx>,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let cols: usize = min(c.cols(), a.cols());
|
||||
|
||||
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
|
||||
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
|
||||
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_res_dft);
|
||||
let mut res_big: VecZnxBig = res_dft.as_vec_znx_big();
|
||||
|
||||
// a1_dft = DFT(a[1])
|
||||
module.vec_znx_dft(&mut a1_dft, a.at(1));
|
||||
|
||||
// c[i] = IDFT(DFT(a[1]) * b[i])
|
||||
(0..2).for_each(|i| {
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, b.at(i), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft);
|
||||
module.vec_znx_big_normalize(c.log_base2k(), c.at_mut(i), &mut res_big, tmp_bytes);
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{
|
||||
ciphertext::{Ciphertext, new_gadget_ciphertext},
|
||||
decryptor::decrypt_rlwe,
|
||||
elem::{Elem, ElemCommon, ElemVecZnx},
|
||||
encryptor::encrypt_grlwe_sk,
|
||||
gadget_product::gadget_product_core,
|
||||
keys::SecretKey,
|
||||
parameters::{Parameters, ParametersLiteral},
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use base2k::{
|
||||
BACKEND, ZnxInfos, Sampling, ScalarZnxDftOps, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, VecZnxOps, MatZnxDft,
|
||||
alloc_aligned_u8,
|
||||
};
|
||||
use sampling::source::{Source, new_seed};
|
||||
|
||||
#[test]
|
||||
fn test_gadget_product_core() {
|
||||
let log_base2k: usize = 10;
|
||||
let q_cols: usize = 7;
|
||||
let p_cols: usize = 1;
|
||||
|
||||
// Basic parameters with enough limbs to test edge cases
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: BACKEND::FFT64,
|
||||
log_n: 12,
|
||||
log_q: q_cols * log_base2k,
|
||||
log_p: p_cols * log_base2k,
|
||||
log_base2k: log_base2k,
|
||||
log_scale: 20,
|
||||
xe: 3.2,
|
||||
xs: 1 << 11,
|
||||
};
|
||||
|
||||
let params: Parameters = Parameters::new(¶ms_lit);
|
||||
|
||||
// scratch space
|
||||
let mut tmp_bytes: Vec<u8> = alloc_aligned_u8(
|
||||
params.decrypt_rlwe_tmp_byte(params.log_qp())
|
||||
| params.gadget_product_tmp_bytes(
|
||||
params.log_qp(),
|
||||
params.log_qp(),
|
||||
params.cols_qp(),
|
||||
params.log_qp(),
|
||||
)
|
||||
| params.encrypt_grlwe_sk_tmp_bytes(params.cols_qp(), params.log_qp()),
|
||||
);
|
||||
|
||||
// Samplers for public and private randomness
|
||||
let mut source_xe: Source = Source::new(new_seed());
|
||||
let mut source_xa: Source = Source::new(new_seed());
|
||||
let mut source_xs: Source = Source::new(new_seed());
|
||||
|
||||
// Two secret keys
|
||||
let mut sk0: SecretKey = SecretKey::new(params.module());
|
||||
sk0.fill_ternary_hw(params.xs(), &mut source_xs);
|
||||
let mut sk0_svp_ppol: base2k::ScalarZnxDft = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk0_svp_ppol, &sk0.0);
|
||||
|
||||
let mut sk1: SecretKey = SecretKey::new(params.module());
|
||||
sk1.fill_ternary_hw(params.xs(), &mut source_xs);
|
||||
let mut sk1_svp_ppol: base2k::ScalarZnxDft = params.module().new_svp_ppol();
|
||||
params.module().svp_prepare(&mut sk1_svp_ppol, &sk1.0);
|
||||
|
||||
// The gadget ciphertext
|
||||
let mut gadget_ct: Ciphertext<MatZnxDft> = new_gadget_ciphertext(
|
||||
params.module(),
|
||||
log_base2k,
|
||||
params.cols_qp(),
|
||||
params.log_qp(),
|
||||
);
|
||||
|
||||
// gct = [-b*sk1 + g(sk0) + e, b]
|
||||
encrypt_grlwe_sk(
|
||||
params.module(),
|
||||
&mut gadget_ct,
|
||||
&sk0.0,
|
||||
&sk1_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
params.xe(),
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
// Intermediate buffers
|
||||
|
||||
// Input polynopmial, uniformly distributed
|
||||
let mut a: VecZnx = params.module().new_vec_znx(1, params.cols_q());
|
||||
params
|
||||
.module()
|
||||
.fill_uniform(log_base2k, &mut a, params.cols_q(), &mut source_xa);
|
||||
|
||||
// res = g^-1(a) * gct
|
||||
let mut elem_res: Elem<VecZnx> = Elem::<VecZnx>::new(params.module(), log_base2k, params.log_qp(), 2);
|
||||
|
||||
// Ideal output = a * s
|
||||
let mut a_dft: VecZnxDft = params.module().new_vec_znx_dft(1, a.cols());
|
||||
let mut a_big: VecZnxBig = a_dft.as_vec_znx_big();
|
||||
let mut a_times_s: VecZnx = params.module().new_vec_znx(1, a.cols());
|
||||
|
||||
// a * sk0
|
||||
params.module().svp_apply_dft(&mut a_dft, &sk0_svp_ppol, &a);
|
||||
params.module().vec_znx_idft_tmp_a(&mut a_big, &mut a_dft);
|
||||
params
|
||||
.module()
|
||||
.vec_znx_big_normalize(params.log_base2k(), &mut a_times_s, &a_big, &mut tmp_bytes);
|
||||
|
||||
// Plaintext for decrypted output of gadget product
|
||||
let mut pt: Plaintext = Plaintext::new(params.module(), params.log_base2k(), params.log_qp());
|
||||
|
||||
// Iterates over all possible cols values for input/output polynomials and gadget ciphertext.
|
||||
|
||||
(1..a.cols() + 1).for_each(|a_cols| {
|
||||
let mut a_trunc: VecZnx = params.module().new_vec_znx(1, a_cols);
|
||||
a_trunc.copy_from(&a);
|
||||
|
||||
(1..gadget_ct.cols() + 1).for_each(|b_cols| {
|
||||
let mut res_dft_0: VecZnxDft = params.module().new_vec_znx_dft(1, b_cols);
|
||||
let mut res_dft_1: VecZnxDft = params.module().new_vec_znx_dft(1, b_cols);
|
||||
let mut res_big_0: VecZnxBig = res_dft_0.as_vec_znx_big();
|
||||
let mut res_big_1: VecZnxBig = res_dft_1.as_vec_znx_big();
|
||||
|
||||
pt.elem_mut().zero();
|
||||
elem_res.zero();
|
||||
|
||||
// let b_cols: usize = min(a_cols+1, gadget_ct.cols());
|
||||
|
||||
println!("a_cols: {} b_cols: {}", a_cols, b_cols);
|
||||
|
||||
// res_dft_0 = DFT(gct_[0] * ct[1] = a * (-bs' + s + e) = -cs' + as + e')
|
||||
// res_dft_1 = DFT(gct_[1] * ct[1] = a * b = c)
|
||||
gadget_product_core(
|
||||
params.module(),
|
||||
&mut res_dft_0,
|
||||
&mut res_dft_1,
|
||||
&a_trunc,
|
||||
&gadget_ct,
|
||||
b_cols,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
// res_big_0 = IDFT(res_dft_0)
|
||||
params
|
||||
.module()
|
||||
.vec_znx_idft_tmp_a(&mut res_big_0, &mut res_dft_0);
|
||||
// res_big_1 = IDFT(res_dft_1);
|
||||
params
|
||||
.module()
|
||||
.vec_znx_idft_tmp_a(&mut res_big_1, &mut res_dft_1);
|
||||
|
||||
// res_big_0 = normalize(res_big_0)
|
||||
params
|
||||
.module()
|
||||
.vec_znx_big_normalize(log_base2k, elem_res.at_mut(0), &res_big_0, &mut tmp_bytes);
|
||||
|
||||
// res_big_1 = normalize(res_big_1)
|
||||
params
|
||||
.module()
|
||||
.vec_znx_big_normalize(log_base2k, elem_res.at_mut(1), &res_big_1, &mut tmp_bytes);
|
||||
|
||||
// <(-c*sk1 + a*sk0 + e, a), (1, sk1)> = a*sk0 + e
|
||||
decrypt_rlwe(
|
||||
params.module(),
|
||||
pt.elem_mut(),
|
||||
&elem_res,
|
||||
&sk1_svp_ppol,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
// a * sk0 + e - a*sk0 = e
|
||||
params
|
||||
.module()
|
||||
.vec_znx_sub_ab_inplace(pt.at_mut(0), &mut a_times_s);
|
||||
pt.at_mut(0).normalize(log_base2k, &mut tmp_bytes);
|
||||
|
||||
// pt.at(0).print(pt.elem().cols(), 16);
|
||||
|
||||
let noise_have: f64 = pt.at(0).std(0, log_base2k).log2();
|
||||
|
||||
let var_a_err: f64;
|
||||
|
||||
if a_cols < a.cols() {
|
||||
var_a_err = 1f64 / 12f64;
|
||||
} else {
|
||||
var_a_err = 0f64;
|
||||
}
|
||||
|
||||
let a_logq: usize = a_cols * log_base2k;
|
||||
let b_logq: usize = b_cols * log_base2k;
|
||||
let var_msg: f64 = (params.xs() as f64) / params.n() as f64;
|
||||
|
||||
println!("{} {} {} {}", var_msg, var_a_err, a_logq, b_logq);
|
||||
|
||||
let noise_pred: f64 = params.noise_grlwe_product(var_msg, var_a_err, a_logq, b_logq);
|
||||
|
||||
println!("noise_pred: {}", noise_pred);
|
||||
println!("noise_have: {}", noise_have);
|
||||
|
||||
// assert!(noise_have <= noise_pred + 1.0);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn noise_grlwe_product(&self, var_msg: f64, var_a_err: f64, a_logq: usize, b_logq: usize) -> f64 {
|
||||
let n: f64 = self.n() as f64;
|
||||
let var_xs: f64 = self.xs() as f64;
|
||||
|
||||
let var_gct_err_lhs: f64;
|
||||
let var_gct_err_rhs: f64;
|
||||
if b_logq < self.log_qp() {
|
||||
let var_round: f64 = 1f64 / 12f64;
|
||||
var_gct_err_lhs = var_round;
|
||||
var_gct_err_rhs = var_round;
|
||||
} else {
|
||||
var_gct_err_lhs = self.xe() * self.xe();
|
||||
var_gct_err_rhs = 0f64;
|
||||
}
|
||||
|
||||
noise_grlwe_product(
|
||||
n,
|
||||
self.log_base2k(),
|
||||
var_xs,
|
||||
var_msg,
|
||||
var_a_err,
|
||||
var_gct_err_lhs,
|
||||
var_gct_err_rhs,
|
||||
a_logq,
|
||||
b_logq,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn noise_grlwe_product(
|
||||
n: f64,
|
||||
log_base2k: usize,
|
||||
var_xs: f64,
|
||||
var_msg: f64,
|
||||
var_a_err: f64,
|
||||
var_gct_err_lhs: f64,
|
||||
var_gct_err_rhs: f64,
|
||||
a_logq: usize,
|
||||
b_logq: usize,
|
||||
) -> f64 {
|
||||
let a_logq: usize = min(a_logq, b_logq);
|
||||
let a_cols: usize = (a_logq + log_base2k - 1) / log_base2k;
|
||||
|
||||
let b_scale = 2.0f64.powi(b_logq as i32);
|
||||
let a_scale: f64 = 2.0f64.powi((b_logq - a_logq) as i32);
|
||||
|
||||
let base: f64 = (1 << (log_base2k)) as f64;
|
||||
let var_base: f64 = base * base / 12f64;
|
||||
|
||||
// lhs = a_cols * n * (var_base * var_gct_err_lhs + var_e_a * var_msg * p^2)
|
||||
// rhs = a_cols * n * var_base * var_gct_err_rhs * var_xs
|
||||
let mut noise: f64 = (a_cols as f64) * n * var_base * (var_gct_err_lhs + var_xs * var_gct_err_rhs);
|
||||
noise += var_msg * var_a_err * a_scale * a_scale * n;
|
||||
noise = noise.sqrt();
|
||||
noise /= b_scale;
|
||||
noise.log2().min(-1.0) // max noise is [-2^{-1}, 2^{-1}]
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
use crate::encryptor::{encrypt_grlwe_sk, encrypt_grlwe_sk_tmp_bytes};
|
||||
use crate::keys::{PublicKey, SecretKey, SwitchingKey};
|
||||
use crate::parameters::Parameters;
|
||||
use base2k::{Module, ScalarZnxDft};
|
||||
use sampling::source::Source;
|
||||
|
||||
pub struct KeyGenerator {}
|
||||
|
||||
impl KeyGenerator {
|
||||
pub fn gen_secret_key_thread_safe(&self, params: &Parameters, source: &mut Source) -> SecretKey {
|
||||
let mut sk: SecretKey = SecretKey::new(params.module());
|
||||
sk.fill_ternary_hw(params.xs(), source);
|
||||
sk
|
||||
}
|
||||
|
||||
pub fn gen_public_key_thread_safe(
|
||||
&self,
|
||||
params: &Parameters,
|
||||
sk_ppol: &ScalarZnxDft,
|
||||
source: &mut Source,
|
||||
tmp_bytes: &mut [u8],
|
||||
) -> PublicKey {
|
||||
let mut xa_source: Source = source.branch();
|
||||
let mut xe_source: Source = source.branch();
|
||||
let mut pk: PublicKey = PublicKey::new(params.module(), params.log_base2k(), params.log_qp());
|
||||
pk.gen_thread_safe(
|
||||
params.module(),
|
||||
sk_ppol,
|
||||
params.xe(),
|
||||
&mut xa_source,
|
||||
&mut xe_source,
|
||||
tmp_bytes,
|
||||
);
|
||||
pk
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_switching_key_tmp_bytes(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> usize {
|
||||
encrypt_grlwe_sk_tmp_bytes(module, log_base2k, rows, log_q)
|
||||
}
|
||||
|
||||
pub fn gen_switching_key(
|
||||
module: &Module,
|
||||
swk: &mut SwitchingKey,
|
||||
sk_in: &SecretKey,
|
||||
sk_out: &ScalarZnxDft,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
encrypt_grlwe_sk(
|
||||
module, &mut swk.0, &sk_in.0, sk_out, source_xa, source_xe, sigma, tmp_bytes,
|
||||
);
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
use crate::ciphertext::Ciphertext;
|
||||
use crate::elem::ElemCommon;
|
||||
use base2k::{Module, VecZnx, VecZnxBigOps, VecZnxDftOps, MatZnxDft, MatZnxDftOps, assert_alignement};
|
||||
use std::cmp::min;
|
||||
|
||||
pub fn key_switch_tmp_bytes(module: &Module, log_base2k: usize, res_logq: usize, in_logq: usize, gct_logq: usize) -> usize {
|
||||
let gct_cols: usize = (gct_logq + log_base2k - 1) / log_base2k;
|
||||
let in_cols: usize = (in_logq + log_base2k - 1) / log_base2k;
|
||||
let res_cols: usize = (res_logq + log_base2k - 1) / log_base2k;
|
||||
return module.vmp_apply_dft_to_dft_tmp_bytes(res_cols, in_cols, in_cols, gct_cols)
|
||||
+ module.bytes_of_vec_znx_dft(1, std::cmp::min(res_cols, in_cols))
|
||||
+ module.bytes_of_vec_znx_dft(1, gct_cols);
|
||||
}
|
||||
|
||||
pub fn key_switch_rlwe(
|
||||
module: &Module,
|
||||
c: &mut Ciphertext<VecZnx>,
|
||||
a: &Ciphertext<VecZnx>,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
key_switch_rlwe_core(module, c, a, b, b_cols, tmp_bytes);
|
||||
}
|
||||
|
||||
pub fn key_switch_rlwe_inplace(
|
||||
module: &Module,
|
||||
a: &mut Ciphertext<VecZnx>,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
key_switch_rlwe_core(module, a, a, b, b_cols, tmp_bytes);
|
||||
}
|
||||
|
||||
fn key_switch_rlwe_core(
|
||||
module: &Module,
|
||||
c: *mut Ciphertext<VecZnx>,
|
||||
a: *const Ciphertext<VecZnx>,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
// SAFETY WARNING: must ensure `c` and `a` are valid for read/write
|
||||
let c: &mut Ciphertext<VecZnx> = unsafe { &mut *c };
|
||||
let a: &Ciphertext<VecZnx> = unsafe { &*a };
|
||||
|
||||
let cols: usize = min(min(c.cols(), a.cols()), b.rows());
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(b_cols <= b.cols());
|
||||
assert!(tmp_bytes.len() >= key_switch_tmp_bytes(module, c.cols(), a.cols(), b.rows(), b.cols()));
|
||||
assert_alignement(tmp_bytes.as_ptr());
|
||||
}
|
||||
|
||||
let (tmp_bytes_a1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
|
||||
|
||||
let mut a1_dft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_a1_dft);
|
||||
let mut res_dft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_res_dft);
|
||||
let mut res_big = res_dft.as_vec_znx_big();
|
||||
|
||||
module.vec_znx_dft(&mut a1_dft, a.at(1));
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, b.at(0), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft);
|
||||
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, a.at(0));
|
||||
module.vec_znx_big_normalize(c.log_base2k(), c.at_mut(0), &mut res_big, tmp_bytes);
|
||||
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, b.at(1), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft);
|
||||
|
||||
module.vec_znx_big_normalize(c.log_base2k(), c.at_mut(1), &mut res_big, tmp_bytes);
|
||||
}
|
||||
|
||||
pub fn key_switch_grlwe(module: &Module, c: &mut Ciphertext<VecZnx>, a: &Ciphertext<VecZnx>, b: &Ciphertext<MatZnxDft>) {}
|
||||
|
||||
pub fn key_switch_rgsw(module: &Module, c: &mut Ciphertext<VecZnx>, a: &Ciphertext<VecZnx>, b: &Ciphertext<MatZnxDft>) {}
|
||||
@@ -1,82 +0,0 @@
|
||||
use crate::ciphertext::{Ciphertext, new_gadget_ciphertext};
|
||||
use crate::elem::{Elem, ElemCommon};
|
||||
use crate::encryptor::{encrypt_rlwe_sk, encrypt_rlwe_sk_tmp_bytes};
|
||||
use base2k::{Module, Scalar, ScalarZnxDft, ScalarZnxDftOps, VecZnx, MatZnxDft};
|
||||
use sampling::source::Source;
|
||||
|
||||
pub struct SecretKey(pub Scalar);
|
||||
|
||||
impl SecretKey {
|
||||
pub fn new(module: &Module) -> Self {
|
||||
SecretKey(Scalar::new(module.n()))
|
||||
}
|
||||
|
||||
pub fn fill_ternary_prob(&mut self, prob: f64, source: &mut Source) {
|
||||
self.0.fill_ternary_prob(prob, source);
|
||||
}
|
||||
|
||||
pub fn fill_ternary_hw(&mut self, hw: usize, source: &mut Source) {
|
||||
self.0.fill_ternary_hw(hw, source);
|
||||
}
|
||||
|
||||
pub fn prepare(&self, module: &Module, sk_ppol: &mut ScalarZnxDft) {
|
||||
module.scalar_znx_dft_prepare(sk_ppol, &self.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PublicKey(pub Elem<VecZnx>);
|
||||
|
||||
impl PublicKey {
|
||||
pub fn new(module: &Module, log_base2k: usize, log_q: usize) -> PublicKey {
|
||||
PublicKey(Elem::<VecZnx>::new(module, log_base2k, log_q, 2))
|
||||
}
|
||||
|
||||
pub fn gen_thread_safe(
|
||||
&mut self,
|
||||
module: &Module,
|
||||
sk: &ScalarZnxDft,
|
||||
xe: f64,
|
||||
xa_source: &mut Source,
|
||||
xe_source: &mut Source,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
encrypt_rlwe_sk(
|
||||
module,
|
||||
&mut self.0,
|
||||
None,
|
||||
sk,
|
||||
xa_source,
|
||||
xe_source,
|
||||
xe,
|
||||
tmp_bytes,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn gen_thread_safe_tmp_bytes(module: &Module, log_base2k: usize, log_q: usize) -> usize {
|
||||
encrypt_rlwe_sk_tmp_bytes(module, log_base2k, log_q)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SwitchingKey(pub Ciphertext<MatZnxDft>);
|
||||
|
||||
impl SwitchingKey {
|
||||
pub fn new(module: &Module, log_base2k: usize, rows: usize, log_q: usize) -> SwitchingKey {
|
||||
SwitchingKey(new_gadget_ciphertext(module, log_base2k, rows, log_q))
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize {
|
||||
self.0.n()
|
||||
}
|
||||
|
||||
pub fn rows(&self) -> usize {
|
||||
self.0.rows()
|
||||
}
|
||||
|
||||
pub fn cols(&self) -> usize {
|
||||
self.0.cols()
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.0.log_base2k()
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1 @@
|
||||
pub mod automorphism;
|
||||
pub mod ciphertext;
|
||||
pub mod decryptor;
|
||||
pub mod elem;
|
||||
pub mod encryptor;
|
||||
pub mod gadget_product;
|
||||
pub mod key_generator;
|
||||
pub mod key_switching;
|
||||
pub mod keys;
|
||||
pub mod parameters;
|
||||
pub mod plaintext;
|
||||
pub mod rgsw_product;
|
||||
pub mod trace;
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
use base2k::module::{BACKEND, Module};
|
||||
|
||||
pub const DEFAULT_SIGMA: f64 = 3.2;
|
||||
|
||||
pub struct ParametersLiteral {
|
||||
pub backend: BACKEND,
|
||||
pub log_n: usize,
|
||||
pub log_q: usize,
|
||||
pub log_p: usize,
|
||||
pub log_base2k: usize,
|
||||
pub log_scale: usize,
|
||||
pub xe: f64,
|
||||
pub xs: usize,
|
||||
}
|
||||
|
||||
pub struct Parameters {
|
||||
log_n: usize,
|
||||
log_q: usize,
|
||||
log_p: usize,
|
||||
log_scale: usize,
|
||||
log_base2k: usize,
|
||||
xe: f64,
|
||||
xs: usize,
|
||||
module: Module,
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn new(p: &ParametersLiteral) -> Self {
|
||||
assert!(
|
||||
p.log_n + 2 * p.log_base2k <= 53,
|
||||
"invalid parameters: p.log_n + 2*p.log_base2k > 53"
|
||||
);
|
||||
Self {
|
||||
log_n: p.log_n,
|
||||
log_q: p.log_q,
|
||||
log_p: p.log_p,
|
||||
log_scale: p.log_scale,
|
||||
log_base2k: p.log_base2k,
|
||||
xe: p.xe,
|
||||
xs: p.xs,
|
||||
module: Module::new(1 << p.log_n, p.backend),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize {
|
||||
1 << self.log_n
|
||||
}
|
||||
|
||||
pub fn log_scale(&self) -> usize {
|
||||
self.log_scale
|
||||
}
|
||||
|
||||
pub fn log_q(&self) -> usize {
|
||||
self.log_q
|
||||
}
|
||||
|
||||
pub fn log_p(&self) -> usize {
|
||||
self.log_p
|
||||
}
|
||||
|
||||
pub fn log_qp(&self) -> usize {
|
||||
self.log_q + self.log_p
|
||||
}
|
||||
|
||||
pub fn cols_q(&self) -> usize {
|
||||
(self.log_q + self.log_base2k - 1) / self.log_base2k
|
||||
}
|
||||
|
||||
pub fn cols_qp(&self) -> usize {
|
||||
(self.log_q + self.log_p + self.log_base2k - 1) / self.log_base2k
|
||||
}
|
||||
|
||||
pub fn log_base2k(&self) -> usize {
|
||||
self.log_base2k
|
||||
}
|
||||
|
||||
pub fn module(&self) -> &Module {
|
||||
&self.module
|
||||
}
|
||||
|
||||
pub fn xe(&self) -> f64 {
|
||||
self.xe
|
||||
}
|
||||
|
||||
pub fn xs(&self) -> usize {
|
||||
self.xs
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
use crate::ciphertext::Ciphertext;
|
||||
use crate::elem::{Elem, ElemCommon, ElemVecZnx};
|
||||
use crate::parameters::Parameters;
|
||||
use base2k::{Layout, Module, VecZnx};
|
||||
|
||||
pub struct Plaintext(pub Elem<VecZnx>);
|
||||
|
||||
impl Parameters {
|
||||
pub fn new_plaintext(&self, log_q: usize) -> Plaintext {
|
||||
Plaintext::new(self.module(), self.log_base2k(), log_q)
|
||||
}
|
||||
|
||||
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(&self, log_q: usize, bytes: &mut [u8]) -> Plaintext {
|
||||
Plaintext(Elem::<VecZnx>::from_bytes(
|
||||
self.module(),
|
||||
self.log_base2k(),
|
||||
log_q,
|
||||
1,
|
||||
bytes,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
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 Plaintext {
|
||||
pub fn bytes_of(module: &Module, log_base2k: usize, log_q: usize) -> usize {
|
||||
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::<VecZnx>::from_bytes(
|
||||
module, log_base2k, log_q, 1, bytes,
|
||||
))
|
||||
}
|
||||
|
||||
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 ElemCommon<VecZnx> for Plaintext {
|
||||
fn n(&self) -> usize {
|
||||
self.0.n()
|
||||
}
|
||||
|
||||
fn log_n(&self) -> usize {
|
||||
self.elem().log_n()
|
||||
}
|
||||
|
||||
fn log_q(&self) -> usize {
|
||||
self.0.log_q
|
||||
}
|
||||
|
||||
fn elem(&self) -> &Elem<VecZnx> {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn elem_mut(&mut self) -> &mut Elem<VecZnx> {
|
||||
&mut self.0
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
self.elem().size()
|
||||
}
|
||||
|
||||
fn layout(&self) -> Layout {
|
||||
self.elem().layout()
|
||||
}
|
||||
|
||||
fn rows(&self) -> usize {
|
||||
self.0.rows()
|
||||
}
|
||||
|
||||
fn cols(&self) -> usize {
|
||||
self.0.cols()
|
||||
}
|
||||
|
||||
fn at(&self, i: usize) -> &VecZnx {
|
||||
self.0.at(i)
|
||||
}
|
||||
|
||||
fn at_mut(&mut self, i: usize) -> &mut VecZnx {
|
||||
self.0.at_mut(i)
|
||||
}
|
||||
|
||||
fn log_base2k(&self) -> usize {
|
||||
self.0.log_base2k()
|
||||
}
|
||||
|
||||
fn log_scale(&self) -> usize {
|
||||
self.0.log_scale()
|
||||
}
|
||||
}
|
||||
@@ -1,300 +0,0 @@
|
||||
use crate::{ciphertext::Ciphertext, elem::ElemCommon, parameters::Parameters};
|
||||
use base2k::{Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, MatZnxDft, MatZnxDftOps, assert_alignement};
|
||||
use std::cmp::min;
|
||||
|
||||
impl Parameters {
|
||||
pub fn rgsw_product_tmp_bytes(&self, res_logq: usize, in_logq: usize, gct_logq: usize) -> usize {
|
||||
rgsw_product_tmp_bytes(
|
||||
self.module(),
|
||||
self.log_base2k(),
|
||||
res_logq,
|
||||
in_logq,
|
||||
gct_logq,
|
||||
)
|
||||
}
|
||||
}
|
||||
pub fn rgsw_product_tmp_bytes(module: &Module, log_base2k: usize, res_logq: usize, in_logq: usize, gct_logq: usize) -> usize {
|
||||
let gct_cols: usize = (gct_logq + log_base2k - 1) / log_base2k;
|
||||
let in_cols: usize = (in_logq + log_base2k - 1) / log_base2k;
|
||||
let res_cols: usize = (res_logq + log_base2k - 1) / log_base2k;
|
||||
return module.vmp_apply_dft_to_dft_tmp_bytes(res_cols, in_cols, in_cols, gct_cols)
|
||||
+ module.bytes_of_vec_znx_dft(1, std::cmp::min(res_cols, in_cols))
|
||||
+ 2 * module.bytes_of_vec_znx_dft(1, gct_cols);
|
||||
}
|
||||
|
||||
pub fn rgsw_product(
|
||||
module: &Module,
|
||||
c: &mut Ciphertext<VecZnx>,
|
||||
a: &Ciphertext<VecZnx>,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(b_cols <= b.cols());
|
||||
assert_eq!(c.size(), 2);
|
||||
assert_eq!(a.size(), 2);
|
||||
assert_eq!(b.size(), 4);
|
||||
assert!(tmp_bytes.len() >= rgsw_product_tmp_bytes(module, c.cols(), a.cols(), min(b.rows(), a.cols()), b_cols));
|
||||
assert_alignement(tmp_bytes.as_ptr());
|
||||
}
|
||||
|
||||
let (tmp_bytes_ai_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, a.cols()));
|
||||
let (tmp_bytes_c0_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
|
||||
let (tmp_bytes_c1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
|
||||
|
||||
let mut ai_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, a.cols(), tmp_bytes_ai_dft);
|
||||
let mut c0_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c0_dft);
|
||||
let mut c1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c1_dft);
|
||||
|
||||
let mut c0_big: VecZnxBig = c0_dft.as_vec_znx_big();
|
||||
let mut c1_big: VecZnxBig = c1_dft.as_vec_znx_big();
|
||||
|
||||
module.vec_znx_dft(&mut ai_dft, a.at(0));
|
||||
module.vmp_apply_dft_to_dft(&mut c0_dft, &ai_dft, b.at(0), tmp_bytes);
|
||||
module.vmp_apply_dft_to_dft(&mut c1_dft, &ai_dft, b.at(1), tmp_bytes);
|
||||
|
||||
module.vec_znx_dft(&mut ai_dft, a.at(1));
|
||||
module.vmp_apply_dft_to_dft_add(&mut c0_dft, &ai_dft, b.at(2), tmp_bytes);
|
||||
module.vmp_apply_dft_to_dft_add(&mut c1_dft, &ai_dft, b.at(3), tmp_bytes);
|
||||
|
||||
module.vec_znx_idft_tmp_a(&mut c0_big, &mut c0_dft);
|
||||
module.vec_znx_idft_tmp_a(&mut c1_big, &mut c1_dft);
|
||||
|
||||
module.vec_znx_big_normalize(c.log_base2k(), c.at_mut(0), &mut c0_big, tmp_bytes);
|
||||
module.vec_znx_big_normalize(c.log_base2k(), c.at_mut(1), &mut c1_big, tmp_bytes);
|
||||
}
|
||||
|
||||
pub fn rgsw_product_inplace(
|
||||
module: &Module,
|
||||
a: &mut Ciphertext<VecZnx>,
|
||||
b: &Ciphertext<MatZnxDft>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(b_cols <= b.cols());
|
||||
assert_eq!(a.size(), 2);
|
||||
assert_eq!(b.size(), 4);
|
||||
assert!(tmp_bytes.len() >= rgsw_product_tmp_bytes(module, a.cols(), a.cols(), min(b.rows(), a.cols()), b_cols));
|
||||
assert_alignement(tmp_bytes.as_ptr());
|
||||
}
|
||||
|
||||
let (tmp_bytes_ai_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, a.cols()));
|
||||
let (tmp_bytes_c0_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
|
||||
let (tmp_bytes_c1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
|
||||
|
||||
let mut ai_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, a.cols(), tmp_bytes_ai_dft);
|
||||
let mut c0_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c0_dft);
|
||||
let mut c1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_c1_dft);
|
||||
|
||||
let mut c0_big: VecZnxBig = c0_dft.as_vec_znx_big();
|
||||
let mut c1_big: VecZnxBig = c1_dft.as_vec_znx_big();
|
||||
|
||||
module.vec_znx_dft(&mut ai_dft, a.at(0));
|
||||
module.vmp_apply_dft_to_dft(&mut c0_dft, &ai_dft, b.at(0), tmp_bytes);
|
||||
module.vmp_apply_dft_to_dft(&mut c1_dft, &ai_dft, b.at(1), tmp_bytes);
|
||||
|
||||
module.vec_znx_dft(&mut ai_dft, a.at(1));
|
||||
module.vmp_apply_dft_to_dft_add(&mut c0_dft, &ai_dft, b.at(2), tmp_bytes);
|
||||
module.vmp_apply_dft_to_dft_add(&mut c1_dft, &ai_dft, b.at(3), tmp_bytes);
|
||||
|
||||
module.vec_znx_idft_tmp_a(&mut c0_big, &mut c0_dft);
|
||||
module.vec_znx_idft_tmp_a(&mut c1_big, &mut c1_dft);
|
||||
|
||||
module.vec_znx_big_normalize(a.log_base2k(), a.at_mut(0), &mut c0_big, tmp_bytes);
|
||||
module.vec_znx_big_normalize(a.log_base2k(), a.at_mut(1), &mut c1_big, tmp_bytes);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{
|
||||
ciphertext::{Ciphertext, new_rgsw_ciphertext},
|
||||
decryptor::decrypt_rlwe,
|
||||
elem::ElemCommon,
|
||||
encryptor::{encrypt_rgsw_sk, encrypt_rlwe_sk},
|
||||
keys::SecretKey,
|
||||
parameters::{DEFAULT_SIGMA, Parameters, ParametersLiteral},
|
||||
plaintext::Plaintext,
|
||||
rgsw_product::rgsw_product_inplace,
|
||||
};
|
||||
use base2k::{BACKEND, Encoding, Module, Scalar, ScalarZnxDft, ScalarZnxDftOps, VecZnx, VecZnxOps, MatZnxDft, alloc_aligned};
|
||||
use sampling::source::{Source, new_seed};
|
||||
|
||||
#[test]
|
||||
fn test_rgsw_product() {
|
||||
let log_base2k: usize = 10;
|
||||
let log_q: usize = 50;
|
||||
let log_p: usize = 15;
|
||||
|
||||
// Basic parameters with enough limbs to test edge cases
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: BACKEND::FFT64,
|
||||
log_n: 12,
|
||||
log_q: log_q,
|
||||
log_p: log_p,
|
||||
log_base2k: log_base2k,
|
||||
log_scale: 20,
|
||||
xe: 3.2,
|
||||
xs: 1 << 11,
|
||||
};
|
||||
|
||||
let params: Parameters = Parameters::new(¶ms_lit);
|
||||
|
||||
let module: &Module = params.module();
|
||||
let log_q: usize = params.log_q();
|
||||
let log_qp: usize = params.log_qp();
|
||||
let gct_rows: usize = params.cols_q();
|
||||
let gct_cols: usize = params.cols_qp();
|
||||
|
||||
// scratch space
|
||||
let mut tmp_bytes: Vec<u8> = alloc_aligned(
|
||||
params.decrypt_rlwe_tmp_byte(log_q)
|
||||
| params.encrypt_rlwe_sk_tmp_bytes(log_q)
|
||||
| params.rgsw_product_tmp_bytes(log_q, log_q, log_qp)
|
||||
| params.encrypt_rgsw_sk_tmp_bytes(gct_rows, log_qp),
|
||||
);
|
||||
|
||||
// Samplers for public and private randomness
|
||||
let mut source_xe: Source = Source::new(new_seed());
|
||||
let mut source_xa: Source = Source::new(new_seed());
|
||||
let mut source_xs: Source = Source::new(new_seed());
|
||||
|
||||
let mut sk: SecretKey = SecretKey::new(module);
|
||||
sk.fill_ternary_hw(params.xs(), &mut source_xs);
|
||||
let mut sk_svp_ppol: ScalarZnxDft = module.new_svp_ppol();
|
||||
module.svp_prepare(&mut sk_svp_ppol, &sk.0);
|
||||
|
||||
let mut ct_rgsw: Ciphertext<MatZnxDft> = new_rgsw_ciphertext(module, log_base2k, gct_rows, log_qp);
|
||||
|
||||
let k: i64 = 3;
|
||||
|
||||
// X^k
|
||||
let m: Scalar = module.new_scalar();
|
||||
let data: &mut [i64] = m.raw_mut();
|
||||
data[k as usize] = 1;
|
||||
|
||||
encrypt_rgsw_sk(
|
||||
module,
|
||||
&mut ct_rgsw,
|
||||
&m,
|
||||
&sk_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
DEFAULT_SIGMA,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
let log_k: usize = 2 * log_base2k;
|
||||
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(log_q);
|
||||
let mut pt: Plaintext = params.new_plaintext(log_q);
|
||||
let mut pt_rotate: Plaintext = params.new_plaintext(log_q);
|
||||
|
||||
pt.at_mut(0).encode_vec_i64(0, log_base2k, log_k, &data, 32);
|
||||
|
||||
module.vec_znx_rotate(k, pt_rotate.at_mut(0), pt.at_mut(0));
|
||||
|
||||
encrypt_rlwe_sk(
|
||||
module,
|
||||
&mut ct.elem_mut(),
|
||||
Some(pt.at(0)),
|
||||
&sk_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
params.xe(),
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
rgsw_product_inplace(module, &mut ct, &ct_rgsw, gct_cols, &mut tmp_bytes);
|
||||
|
||||
decrypt_rlwe(
|
||||
module,
|
||||
pt.elem_mut(),
|
||||
ct.elem(),
|
||||
&sk_svp_ppol,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
module.vec_znx_sub_ba_inplace(pt.at_mut(0), pt_rotate.at(0));
|
||||
|
||||
// pt.at(0).print(pt.cols(), 16);
|
||||
|
||||
let noise_have: f64 = pt.at(0).std(0, log_base2k).log2();
|
||||
|
||||
let var_msg: f64 = 1f64 / params.n() as f64; // X^{k}
|
||||
let var_a0_err: f64 = params.xe() * params.xe();
|
||||
let var_a1_err: f64 = 1f64 / 12f64;
|
||||
|
||||
let noise_pred: f64 = params.noise_rgsw_product(var_msg, var_a0_err, var_a1_err, ct.log_q(), ct_rgsw.log_q());
|
||||
|
||||
println!("noise_pred: {}", noise_pred);
|
||||
println!("noise_have: {}", noise_have);
|
||||
|
||||
assert!(noise_have <= noise_pred + 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn noise_rgsw_product(&self, var_msg: f64, var_a0_err: f64, var_a1_err: f64, a_logq: usize, b_logq: usize) -> f64 {
|
||||
let n: f64 = self.n() as f64;
|
||||
let var_xs: f64 = self.xs() as f64;
|
||||
|
||||
let var_gct_err_lhs: f64;
|
||||
let var_gct_err_rhs: f64;
|
||||
if b_logq < self.log_qp() {
|
||||
let var_round: f64 = 1f64 / 12f64;
|
||||
var_gct_err_lhs = var_round;
|
||||
var_gct_err_rhs = var_round;
|
||||
} else {
|
||||
var_gct_err_lhs = self.xe() * self.xe();
|
||||
var_gct_err_rhs = 0f64;
|
||||
}
|
||||
|
||||
noise_rgsw_product(
|
||||
n,
|
||||
self.log_base2k(),
|
||||
var_xs,
|
||||
var_msg,
|
||||
var_a0_err,
|
||||
var_a1_err,
|
||||
var_gct_err_lhs,
|
||||
var_gct_err_rhs,
|
||||
a_logq,
|
||||
b_logq,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn noise_rgsw_product(
|
||||
n: f64,
|
||||
log_base2k: usize,
|
||||
var_xs: f64,
|
||||
var_msg: f64,
|
||||
var_a0_err: f64,
|
||||
var_a1_err: f64,
|
||||
var_gct_err_lhs: f64,
|
||||
var_gct_err_rhs: f64,
|
||||
a_logq: usize,
|
||||
b_logq: usize,
|
||||
) -> f64 {
|
||||
let a_logq: usize = min(a_logq, b_logq);
|
||||
let a_cols: usize = (a_logq + log_base2k - 1) / log_base2k;
|
||||
|
||||
let b_scale = 2.0f64.powi(b_logq as i32);
|
||||
let a_scale: f64 = 2.0f64.powi((b_logq - a_logq) as i32);
|
||||
|
||||
let base: f64 = (1 << (log_base2k)) as f64;
|
||||
let var_base: f64 = base * base / 12f64;
|
||||
|
||||
// lhs = a_cols * n * (var_base * var_gct_err_lhs + var_e_a * var_msg * p^2)
|
||||
// rhs = a_cols * n * var_base * var_gct_err_rhs * var_xs
|
||||
let mut noise: f64 = 2.0 * (a_cols as f64) * n * var_base * (var_gct_err_lhs + var_xs * var_gct_err_rhs);
|
||||
noise += var_msg * var_a0_err * a_scale * a_scale * n;
|
||||
noise += var_msg * var_a1_err * a_scale * a_scale * n * var_xs;
|
||||
noise = noise.sqrt();
|
||||
noise /= b_scale;
|
||||
noise.log2().min(-1.0) // max noise is [-2^{-1}, 2^{-1}]
|
||||
}
|
||||
113
rlwe/src/test.rs
113
rlwe/src/test.rs
@@ -1,113 +0,0 @@
|
||||
use base2k::{alloc_aligned, SvpPPol, SvpPPolOps, VecZnx, BACKEND};
|
||||
use sampling::source::{Source, new_seed};
|
||||
use crate::{ciphertext::Ciphertext, decryptor::decrypt_rlwe, elem::ElemCommon, encryptor::encrypt_rlwe_sk, keys::SecretKey, parameters::{Parameters, ParametersLiteral, DEFAULT_SIGMA}, plaintext::Plaintext};
|
||||
|
||||
|
||||
|
||||
pub struct Context{
|
||||
pub params: Parameters,
|
||||
pub sk0: SecretKey,
|
||||
pub sk0_ppol:SvpPPol,
|
||||
pub sk1: SecretKey,
|
||||
pub sk1_ppol: SvpPPol,
|
||||
pub tmp_bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Context{
|
||||
pub fn new(log_n: usize, log_base2k: usize, log_q: usize, log_p: usize) -> Self{
|
||||
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: BACKEND::FFT64,
|
||||
log_n: log_n,
|
||||
log_q: log_q,
|
||||
log_p: log_p,
|
||||
log_base2k: log_base2k,
|
||||
log_scale: 20,
|
||||
xe: DEFAULT_SIGMA,
|
||||
xs: 1 << (log_n-1),
|
||||
};
|
||||
|
||||
let params: Parameters =Parameters::new(¶ms_lit);
|
||||
let module = params.module();
|
||||
|
||||
let log_q: usize = params.log_q();
|
||||
|
||||
let mut source_xs: Source = Source::new(new_seed());
|
||||
|
||||
let mut sk0: SecretKey = SecretKey::new(module);
|
||||
sk0.fill_ternary_hw(params.xs(), &mut source_xs);
|
||||
let mut sk0_ppol: base2k::SvpPPol = module.new_svp_ppol();
|
||||
module.svp_prepare(&mut sk0_ppol, &sk0.0);
|
||||
|
||||
let mut sk1: SecretKey = SecretKey::new(module);
|
||||
sk1.fill_ternary_hw(params.xs(), &mut source_xs);
|
||||
let mut sk1_ppol: base2k::SvpPPol = module.new_svp_ppol();
|
||||
module.svp_prepare(&mut sk1_ppol, &sk1.0);
|
||||
|
||||
let tmp_bytes: Vec<u8> = alloc_aligned(params.decrypt_rlwe_tmp_byte(log_q)| params.encrypt_rlwe_sk_tmp_bytes(log_q));
|
||||
|
||||
Context{
|
||||
params: params,
|
||||
sk0: sk0,
|
||||
sk0_ppol: sk0_ppol,
|
||||
sk1: sk1,
|
||||
sk1_ppol: sk1_ppol,
|
||||
tmp_bytes: tmp_bytes,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk0(&mut self, pt: &Plaintext, ct: &mut Ciphertext<VecZnx>){
|
||||
|
||||
let mut source_xe: Source = Source::new(new_seed());
|
||||
let mut source_xa: Source = Source::new(new_seed());
|
||||
|
||||
encrypt_rlwe_sk(
|
||||
self.params.module(),
|
||||
ct.elem_mut(),
|
||||
Some(pt.elem()),
|
||||
&self.sk0_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
self.params.xe(),
|
||||
&mut self.tmp_bytes,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn encrypt_rlwe_sk1(&mut self, ct: &mut Ciphertext<VecZnx>, pt: &Plaintext){
|
||||
|
||||
let mut source_xe: Source = Source::new(new_seed());
|
||||
let mut source_xa: Source = Source::new(new_seed());
|
||||
|
||||
encrypt_rlwe_sk(
|
||||
self.params.module(),
|
||||
ct.elem_mut(),
|
||||
Some(pt.elem()),
|
||||
&self.sk1_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
self.params.xe(),
|
||||
&mut self.tmp_bytes,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn decrypt_sk0(&mut self, pt: &mut Plaintext, ct: &Ciphertext<VecZnx>){
|
||||
decrypt_rlwe(
|
||||
self.params.module(),
|
||||
pt.elem_mut(),
|
||||
ct.elem(),
|
||||
&self.sk0_ppol,
|
||||
&mut self.tmp_bytes,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn decrypt_sk1(&mut self, pt: &mut Plaintext, ct: &Ciphertext<VecZnx>){
|
||||
decrypt_rlwe(
|
||||
self.params.module(),
|
||||
pt.elem_mut(),
|
||||
ct.elem(),
|
||||
&self.sk1_ppol,
|
||||
&mut self.tmp_bytes,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,236 +0,0 @@
|
||||
use crate::{automorphism::AutomorphismKey, ciphertext::Ciphertext, elem::ElemCommon, parameters::Parameters};
|
||||
use base2k::{Module, VecZnx, VecZnxBig, VecZnxBigOps, VecZnxDft, VecZnxDftOps, MatZnxDftOps, assert_alignement};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn trace_galois_elements(module: &Module) -> Vec<i64> {
|
||||
let mut gal_els: Vec<i64> = Vec::new();
|
||||
(0..module.log_n()).for_each(|i| {
|
||||
if i == 0 {
|
||||
gal_els.push(-1);
|
||||
} else {
|
||||
gal_els.push(module.galois_element(1 << (i - 1)));
|
||||
}
|
||||
});
|
||||
gal_els
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn trace_tmp_bytes(&self, res_logq: usize, in_logq: usize, gct_logq: usize) -> usize {
|
||||
self.automorphism_tmp_bytes(res_logq, in_logq, gct_logq)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trace_tmp_bytes(module: &Module, c_cols: usize, a_cols: usize, b_rows: usize, b_cols: usize) -> usize {
|
||||
return module.vmp_apply_dft_to_dft_tmp_bytes(c_cols, a_cols, b_rows, b_cols)
|
||||
+ 2 * module.bytes_of_vec_znx_dft(1, std::cmp::min(c_cols, a_cols));
|
||||
}
|
||||
|
||||
pub fn trace_inplace(
|
||||
module: &Module,
|
||||
a: &mut Ciphertext<VecZnx>,
|
||||
start: usize,
|
||||
end: usize,
|
||||
b: &HashMap<i64, AutomorphismKey>,
|
||||
b_cols: usize,
|
||||
tmp_bytes: &mut [u8],
|
||||
) {
|
||||
let cols: usize = a.cols();
|
||||
|
||||
let b_rows: usize;
|
||||
|
||||
if let Some((_, key)) = b.iter().next() {
|
||||
b_rows = key.value.rows();
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
println!("{} {}", b_cols, key.value.cols());
|
||||
assert!(b_cols <= key.value.cols())
|
||||
}
|
||||
} else {
|
||||
panic!("b: HashMap<i64, AutomorphismKey>, is empty")
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert!(start <= end);
|
||||
assert!(end <= module.n());
|
||||
assert!(tmp_bytes.len() >= trace_tmp_bytes(module, cols, cols, b_rows, b_cols));
|
||||
assert_alignement(tmp_bytes.as_ptr());
|
||||
}
|
||||
|
||||
let cols: usize = std::cmp::min(b_cols, a.cols());
|
||||
|
||||
let (tmp_bytes_b1_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, cols));
|
||||
let (tmp_bytes_res_dft, tmp_bytes) = tmp_bytes.split_at_mut(module.bytes_of_vec_znx_dft(1, b_cols));
|
||||
|
||||
let mut a1_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, cols, tmp_bytes_b1_dft);
|
||||
let mut res_dft: VecZnxDft = module.new_vec_znx_dft_from_bytes_borrow(1, b_cols, tmp_bytes_res_dft);
|
||||
let mut res_big: VecZnxBig = res_dft.as_vec_znx_big();
|
||||
|
||||
let log_base2k: usize = a.log_base2k();
|
||||
|
||||
(start..end).for_each(|i| {
|
||||
a.at_mut(0).rsh(log_base2k, 1, tmp_bytes);
|
||||
a.at_mut(1).rsh(log_base2k, 1, tmp_bytes);
|
||||
|
||||
let p: i64;
|
||||
if i == 0 {
|
||||
p = -1;
|
||||
} else {
|
||||
p = module.galois_element(1 << (i - 1));
|
||||
}
|
||||
|
||||
if let Some(key) = b.get(&p) {
|
||||
module.vec_znx_dft(&mut a1_dft, a.at(1));
|
||||
|
||||
// a[0] = NORMALIZE(a[0] + AUTO(a[0] + IDFT(<DFT(a[1]), key[0]>)))
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, key.value.at(0), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft);
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, a.at(0));
|
||||
module.vec_znx_big_automorphism_inplace(p, &mut res_big);
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, a.at(0));
|
||||
module.vec_znx_big_normalize(a.log_base2k(), a.at_mut(0), &mut res_big, tmp_bytes);
|
||||
|
||||
// a[1] = NORMALIZE(a[1] + AUTO(IDFT(<DFT(a[1]), key[1]>)))
|
||||
module.vmp_apply_dft_to_dft(&mut res_dft, &a1_dft, key.value.at(1), tmp_bytes);
|
||||
module.vec_znx_idft_tmp_a(&mut res_big, &mut res_dft);
|
||||
module.vec_znx_big_automorphism_inplace(p, &mut res_big);
|
||||
module.vec_znx_big_add_small_inplace(&mut res_big, a.at(1));
|
||||
module.vec_znx_big_normalize(a.log_base2k(), a.at_mut(1), &mut res_big, tmp_bytes);
|
||||
} else {
|
||||
panic!("b[{}] is empty", p)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{trace_galois_elements, trace_inplace};
|
||||
use crate::{
|
||||
automorphism::AutomorphismKey,
|
||||
ciphertext::Ciphertext,
|
||||
decryptor::decrypt_rlwe,
|
||||
elem::ElemCommon,
|
||||
encryptor::encrypt_rlwe_sk,
|
||||
keys::SecretKey,
|
||||
parameters::{DEFAULT_SIGMA, Parameters, ParametersLiteral},
|
||||
plaintext::Plaintext,
|
||||
};
|
||||
use base2k::{BACKEND, Encoding, Module, ScalarZnxDft, ScalarZnxDftOps, VecZnx, alloc_aligned};
|
||||
use sampling::source::{Source, new_seed};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn test_trace_inplace() {
|
||||
let log_base2k: usize = 10;
|
||||
let log_q: usize = 50;
|
||||
let log_p: usize = 15;
|
||||
|
||||
// Basic parameters with enough limbs to test edge cases
|
||||
let params_lit: ParametersLiteral = ParametersLiteral {
|
||||
backend: BACKEND::FFT64,
|
||||
log_n: 12,
|
||||
log_q: log_q,
|
||||
log_p: log_p,
|
||||
log_base2k: log_base2k,
|
||||
log_scale: 20,
|
||||
xe: 3.2,
|
||||
xs: 1 << 11,
|
||||
};
|
||||
|
||||
let params: Parameters = Parameters::new(¶ms_lit);
|
||||
|
||||
let module: &Module = params.module();
|
||||
let log_q: usize = params.log_q();
|
||||
let log_qp: usize = params.log_qp();
|
||||
let gct_rows: usize = params.cols_q();
|
||||
let gct_cols: usize = params.cols_qp();
|
||||
|
||||
// scratch space
|
||||
let mut tmp_bytes: Vec<u8> = alloc_aligned(
|
||||
params.decrypt_rlwe_tmp_byte(log_q)
|
||||
| params.encrypt_rlwe_sk_tmp_bytes(log_q)
|
||||
| params.automorphism_key_new_tmp_bytes(gct_rows, log_qp)
|
||||
| params.automorphism_tmp_bytes(log_q, log_q, log_qp),
|
||||
);
|
||||
|
||||
// Samplers for public and private randomness
|
||||
let mut source_xe: Source = Source::new(new_seed());
|
||||
let mut source_xa: Source = Source::new(new_seed());
|
||||
let mut source_xs: Source = Source::new(new_seed());
|
||||
|
||||
let mut sk: SecretKey = SecretKey::new(module);
|
||||
sk.fill_ternary_hw(params.xs(), &mut source_xs);
|
||||
let mut sk_svp_ppol: ScalarZnxDft = module.new_svp_ppol();
|
||||
module.svp_prepare(&mut sk_svp_ppol, &sk.0);
|
||||
|
||||
let gal_els: Vec<i64> = trace_galois_elements(module);
|
||||
|
||||
let auto_keys: HashMap<i64, AutomorphismKey> = AutomorphismKey::new_many(
|
||||
module,
|
||||
&gal_els,
|
||||
&sk,
|
||||
log_base2k,
|
||||
gct_rows,
|
||||
log_qp,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
DEFAULT_SIGMA,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
let mut data: Vec<i64> = vec![0i64; params.n()];
|
||||
|
||||
data.iter_mut()
|
||||
.enumerate()
|
||||
.for_each(|(i, x)| *x = 1 + i as i64);
|
||||
|
||||
let log_k: usize = 2 * log_base2k;
|
||||
|
||||
let mut ct: Ciphertext<VecZnx> = params.new_ciphertext(log_q);
|
||||
let mut pt: Plaintext = params.new_plaintext(log_q);
|
||||
|
||||
pt.at_mut(0).encode_vec_i64(0, log_base2k, log_k, &data, 32);
|
||||
pt.at_mut(0).normalize(log_base2k, &mut tmp_bytes);
|
||||
|
||||
pt.at(0).decode_vec_i64(0, log_base2k, log_k, &mut data);
|
||||
|
||||
pt.at(0).print(0, pt.cols(), 16);
|
||||
|
||||
encrypt_rlwe_sk(
|
||||
module,
|
||||
&mut ct.elem_mut(),
|
||||
Some(pt.at(0)),
|
||||
&sk_svp_ppol,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
params.xe(),
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
trace_inplace(module, &mut ct, 0, 4, &auto_keys, gct_cols, &mut tmp_bytes);
|
||||
trace_inplace(
|
||||
module,
|
||||
&mut ct,
|
||||
4,
|
||||
module.log_n(),
|
||||
&auto_keys,
|
||||
gct_cols,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
// pt = dec(auto(ct)) - auto(pt)
|
||||
decrypt_rlwe(
|
||||
module,
|
||||
pt.elem_mut(),
|
||||
ct.elem(),
|
||||
&sk_svp_ppol,
|
||||
&mut tmp_bytes,
|
||||
);
|
||||
|
||||
pt.at(0).print(0, pt.cols(), 16);
|
||||
|
||||
pt.at(0).decode_vec_i64(0, log_base2k, log_k, &mut data);
|
||||
|
||||
println!("trace: {:?}", &data[..16]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user