mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 21:26:41 +01:00
wip on BR + added enc/dec for LWE
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use backend::{MatZnxDftOps, MatZnxDftScratch, Module, ScalarZnxDftOps, Scratch, VecZnxDftOps, VecZnxOps, ZnxView, ZnxViewMut, ZnxZero, FFT64};
|
||||
use itertools::izip;
|
||||
|
||||
use crate::{
|
||||
GGSWCiphertext, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEPlaintext, Infos, LWECiphertext,
|
||||
ScratchCore, blind_rotation::key::BlindRotationKeyCGGI, lwe::ciphertext::LWECiphertextToRef,
|
||||
};
|
||||
|
||||
pub fn cggi_blind_rotate_scratch_space(
|
||||
module: &Module<FFT64>,
|
||||
basek: usize,
|
||||
k_lut: usize,
|
||||
k_brk: usize,
|
||||
rows: usize,
|
||||
rank: usize,
|
||||
) -> usize {
|
||||
let size = k_brk.div_ceil(basek);
|
||||
GGSWCiphertext::<Vec<u8>, FFT64>::bytes_of(module, basek, k_brk, rows, 1, rank)
|
||||
+ (module.mat_znx_dft_mul_x_pow_minus_one_scratch_space(size, rank + 1)
|
||||
| GLWECiphertext::external_product_inplace_scratch_space(module, basek, k_lut, k_brk, 1, rank))
|
||||
}
|
||||
|
||||
pub fn cggi_blind_rotate<DataRes, DataIn, DataLUT>(
|
||||
module: &Module<FFT64>,
|
||||
res: &mut GLWECiphertext<DataRes>,
|
||||
lwe: &LWECiphertext<DataIn>,
|
||||
lut: &GLWEPlaintext<DataLUT>,
|
||||
brk: &BlindRotationKeyCGGI<FFT64>,
|
||||
scratch: &mut Scratch,
|
||||
) where
|
||||
DataRes: AsRef<[u8]> + AsMut<[u8]>,
|
||||
DataIn: AsRef<[u8]>,
|
||||
DataLUT: AsRef<[u8]>,
|
||||
{
|
||||
|
||||
println!("{}", lwe.n());
|
||||
|
||||
let mut lwe_2n: Vec<i64> = vec![0i64; lwe.n() + 1]; // TODO: from scratch space
|
||||
let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut();
|
||||
let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref();
|
||||
let lut_ref: GLWECiphertext<&[u8]> = lut.to_ref();
|
||||
|
||||
let cols = out_mut.rank()+1;
|
||||
|
||||
mod_switch_2n(module, &mut lwe_2n, &lwe_ref);
|
||||
|
||||
let a: &[i64] = &lwe_2n[1..];
|
||||
let b: i64 = lwe_2n[0];
|
||||
|
||||
out_mut.data.zero();
|
||||
|
||||
// Initialize out to X^{b} * LUT(X)
|
||||
module.vec_znx_rotate(b, &mut out_mut.data, 0, &lut_ref.data, 0);
|
||||
|
||||
let block_size: usize = brk.block_size();
|
||||
|
||||
// ACC + [sum DFT(X^ai -1) * (DFT(ACC) x BRKi)]
|
||||
|
||||
let (mut acc_dft, scratch1) = scratch.tmp_glwe_fourier(module, brk.basek(), out_mut.k(), out_mut.rank());
|
||||
let (mut acc_add_dft, scratch2) = scratch1.tmp_glwe_fourier(module, brk.basek(), out_mut.k(), out_mut.rank());
|
||||
let (mut vmp_res, scratch3) = scratch2.tmp_vec_znx_dft(module, acc_dft.rank()+1, acc_dft.size());
|
||||
let (mut xai_minus_one, scratch4) = scratch3.tmp_scalar_znx(module, 1);
|
||||
let (mut xai_minus_one_dft, scratch5) = scratch4.tmp_scalar_znx_dft(module, 1);
|
||||
|
||||
let start: Instant = Instant::now();
|
||||
izip!(
|
||||
a.chunks_exact(block_size),
|
||||
brk.data.chunks_exact(block_size)
|
||||
)
|
||||
.for_each(|(ai, ski)| {
|
||||
|
||||
out_mut.dft(module, &mut acc_dft);
|
||||
acc_add_dft.data.zero();
|
||||
|
||||
izip!(ai.iter(), ski.iter())
|
||||
.enumerate()
|
||||
.for_each(|(i, (aii, skii))| {
|
||||
|
||||
// vmp_res = DFT(acc) * BRK[i]
|
||||
module.vmp_apply(&mut vmp_res, &acc_dft.data, &skii.data, scratch5);
|
||||
|
||||
// DFT(X^ai -1)
|
||||
xai_minus_one.zero();
|
||||
xai_minus_one.at_mut(0, 0)[0] = 1;
|
||||
module.vec_znx_rotate_inplace(*aii, &mut xai_minus_one, 0);
|
||||
xai_minus_one.at_mut(0, 0)[0] -= 1;
|
||||
module.svp_prepare(&mut xai_minus_one_dft, 0, &xai_minus_one, 0);
|
||||
|
||||
// DFT(X^ai -1) * (DFT(acc) * BRK[i])
|
||||
(0..cols).for_each(|i|{
|
||||
module.svp_apply_inplace(&mut vmp_res, i, &xai_minus_one_dft, 0);
|
||||
module.vec_znx_dft_add_inplace(&mut acc_add_dft.data, i, &vmp_res, i);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
acc_add_dft.idft(module, &mut out_mut, scratch5);
|
||||
});
|
||||
let duration: std::time::Duration = start.elapsed();
|
||||
println!("external products: {} us", duration.as_micros());
|
||||
}
|
||||
|
||||
fn mod_switch_2n(module: &Module<FFT64>, res: &mut [i64], lwe: &LWECiphertext<&[u8]>) {
|
||||
let basek: usize = lwe.basek();
|
||||
|
||||
let log2n: usize = module.log_n() + 1;
|
||||
|
||||
res.copy_from_slice(&lwe.data.at(0, 0));
|
||||
|
||||
if basek > log2n {
|
||||
let diff: usize = basek - log2n;
|
||||
res.iter_mut().for_each(|x| {
|
||||
*x = div_signed_by_pow2(x, diff);
|
||||
})
|
||||
} else {
|
||||
let rem: usize = basek - (log2n % basek);
|
||||
let size: usize = log2n.div_ceil(basek);
|
||||
(1..size).for_each(|i| {
|
||||
if i == size - 1 && rem != basek {
|
||||
let k_rem: usize = basek - rem;
|
||||
izip!(lwe.data.at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| {
|
||||
*y = (*y << k_rem) + (x >> rem);
|
||||
});
|
||||
} else {
|
||||
izip!(lwe.data.at(0, i).iter(), res.iter_mut()).for_each(|(x, y)| {
|
||||
*y = (*y << basek) + x;
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_signed_by_pow2(x: &i64, k: usize) -> i64 {
|
||||
let bias: i64 = (1 << k) - 1;
|
||||
(x + ((x >> 63) & bias)) >> k
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
use backend::{Backend, FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut};
|
||||
use sampling::source::Source;
|
||||
|
||||
use crate::{Distribution, FourierGLWESecret, GGSWCiphertext, GLWEAutomorphismKey, LWESecret};
|
||||
use crate::{Distribution, FourierGLWESecret, GGSWCiphertext, Infos, LWESecret};
|
||||
|
||||
pub struct BlindRotationKeyCGGI<B: Backend> {
|
||||
pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>,
|
||||
pub(crate) dist: Distribution,
|
||||
}
|
||||
|
||||
pub struct BlindRotationKeyFHEW<B: Backend> {
|
||||
pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>,
|
||||
pub(crate) auto: Vec<GLWEAutomorphismKey<Vec<u8>, B>>,
|
||||
}
|
||||
// pub struct BlindRotationKeyFHEW<B: Backend> {
|
||||
// pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>,
|
||||
// pub(crate) auto: Vec<GLWEAutomorphismKey<Vec<u8>, B>>,
|
||||
//}
|
||||
|
||||
impl BlindRotationKeyCGGI<FFT64> {
|
||||
pub fn allocate(module: &Module<FFT64>, lwe_degree: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self {
|
||||
let mut data: Vec<GGSWCiphertext<Vec<u8>, FFT64>> = Vec::with_capacity(lwe_degree);
|
||||
(0..lwe_degree).for_each(|_| data.push(GGSWCiphertext::alloc(module, basek, k, rows, 1, rank)));
|
||||
pub fn allocate(module: &Module<FFT64>, n_lwe: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self {
|
||||
let mut data: Vec<GGSWCiphertext<Vec<u8>, FFT64>> = Vec::with_capacity(n_lwe);
|
||||
(0..n_lwe).for_each(|_| data.push(GGSWCiphertext::alloc(module, basek, k, rows, 1, rank)));
|
||||
Self {
|
||||
data,
|
||||
dist: Distribution::NONE,
|
||||
@@ -61,4 +61,27 @@ impl BlindRotationKeyCGGI<FFT64> {
|
||||
ggsw.encrypt_sk(module, &pt, sk_glwe, source_xa, source_xe, sigma, scratch);
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn block_size(&self) -> usize {
|
||||
match self.dist {
|
||||
Distribution::BinaryBlock(value) => value,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn rows(&self) -> usize {
|
||||
self.data[0].rows()
|
||||
}
|
||||
|
||||
pub(crate) fn k(&self) -> usize {
|
||||
self.data[0].k()
|
||||
}
|
||||
|
||||
pub(crate) fn rank(&self) -> usize {
|
||||
self.data[0].rank()
|
||||
}
|
||||
|
||||
pub(crate) fn basek(&self) -> usize {
|
||||
self.data[0].basek()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,6 @@
|
||||
// pub mod cggi;
|
||||
pub mod ccgi;
|
||||
pub mod key;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test_fft64;
|
||||
|
||||
85
core/src/blind_rotation/test_fft64/cggi.rs
Normal file
85
core/src/blind_rotation/test_fft64/cggi.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use core::time;
|
||||
use std::time::Instant;
|
||||
|
||||
use backend::{Encoding, Module, ScratchOwned, FFT64};
|
||||
use sampling::source::Source;
|
||||
|
||||
use crate::{
|
||||
blind_rotation::{ccgi::{cggi_blind_rotate, cggi_blind_rotate_scratch_space}, key::BlindRotationKeyCGGI}, lwe::LWEPlaintext, FourierGLWESecret, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWESecret
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn blind_rotation() {
|
||||
let module: Module<FFT64> = Module::<FFT64>::new(2048);
|
||||
let basek: usize = 17;
|
||||
|
||||
let n_lwe: usize = 1071;
|
||||
|
||||
let k_lwe: usize = 22;
|
||||
let k_brk: usize = 54;
|
||||
let rows_brk: usize = 1;
|
||||
let k_lut: usize = 44;
|
||||
let rank: usize = 1;
|
||||
let block_size: usize = 7;
|
||||
|
||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&module, rank);
|
||||
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
|
||||
let sk_glwe_dft: FourierGLWESecret<Vec<u8>, FFT64> = FourierGLWESecret::from(&module, &sk_glwe);
|
||||
|
||||
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
|
||||
sk_lwe.fill_binary_block(block_size, &mut source_xs);
|
||||
|
||||
let mut scratch: ScratchOwned = ScratchOwned::new(BlindRotationKeyCGGI::generate_from_sk_scratch_space(
|
||||
&module, basek, k_brk, rank,
|
||||
) | cggi_blind_rotate_scratch_space(&module, basek, k_lut, k_brk, rows_brk, rank));
|
||||
|
||||
let start: Instant = Instant::now();
|
||||
let mut brk: BlindRotationKeyCGGI<FFT64> = BlindRotationKeyCGGI::allocate(&module, n_lwe, basek, k_brk, rows_brk, rank);
|
||||
brk.generate_from_sk(
|
||||
&module,
|
||||
&sk_glwe_dft,
|
||||
&sk_lwe,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
3.2,
|
||||
scratch.borrow(),
|
||||
);
|
||||
let duration: std::time::Duration = start.elapsed();
|
||||
println!("brk-gen: {} ms", duration.as_millis());
|
||||
|
||||
let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe, basek, k_lwe);
|
||||
|
||||
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe);
|
||||
|
||||
pt_lwe.data.encode_coeff_i64(0, basek, 7, 0, 63, 7);
|
||||
|
||||
println!("{}", pt_lwe.data);
|
||||
|
||||
lwe.encrypt_sk(&pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe, 3.2);
|
||||
|
||||
lwe.decrypt(&mut pt_lwe, &sk_lwe);
|
||||
|
||||
println!("{}", pt_lwe.data);
|
||||
|
||||
let lut: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_lut);
|
||||
|
||||
let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&module, basek, k_lut, rank);
|
||||
|
||||
let start: Instant = Instant::now();
|
||||
(0..32).for_each(|i|{
|
||||
cggi_blind_rotate(&module, &mut res, &lwe, &lut, &brk, scratch.borrow());
|
||||
});
|
||||
|
||||
let duration: std::time::Duration = start.elapsed();
|
||||
println!("blind-rotate: {} ms", duration.as_millis());
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_lut);
|
||||
|
||||
res.decrypt(&module , &mut pt, &sk_glwe_dft, scratch.borrow());
|
||||
|
||||
println!("{}", pt.data);
|
||||
}
|
||||
1
core/src/blind_rotation/test_fft64/mod.rs
Normal file
1
core/src/blind_rotation/test_fft64/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod cggi;
|
||||
Reference in New Issue
Block a user