diff --git a/core/src/blind_rotation/ccgi.rs b/core/src/blind_rotation/ccgi.rs index 341045e..769ebb5 100644 --- a/core/src/blind_rotation/ccgi.rs +++ b/core/src/blind_rotation/ccgi.rs @@ -4,8 +4,7 @@ use backend::{MatZnxDftOps, MatZnxDftScratch, Module, ScalarZnxDftOps, Scratch, use itertools::izip; use crate::{ - GGSWCiphertext, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEPlaintext, Infos, LWECiphertext, - ScratchCore, blind_rotation::key::BlindRotationKeyCGGI, lwe::ciphertext::LWECiphertextToRef, + blind_rotation::key::BlindRotationKeyCGGI, lwe::ciphertext::LWECiphertextToRef, FourierGLWESecret, GGSWCiphertext, GLWECiphertext, GLWECiphertextToMut, GLWECiphertextToRef, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWESecret, ScratchCore }; pub fn cggi_blind_rotate_scratch_space( @@ -34,8 +33,7 @@ pub fn cggi_blind_rotate( DataIn: AsRef<[u8]>, DataLUT: AsRef<[u8]>, { - - println!("{}", lwe.n()); + let basek = res.basek(); let mut lwe_2n: Vec = vec![0i64; lwe.n() + 1]; // TODO: from scratch space let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut(); @@ -60,7 +58,7 @@ pub fn cggi_blind_rotate( 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 vmp_res, scratch3) = scratch2.tmp_glwe_fourier(module, basek, out_mut.k(), out_mut.rank()); 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); @@ -73,13 +71,13 @@ pub fn cggi_blind_rotate( 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); + module.vmp_apply(&mut vmp_res.data, &acc_dft.data, &skii.data, scratch5); // DFT(X^ai -1) xai_minus_one.zero(); @@ -90,19 +88,23 @@ pub fn cggi_blind_rotate( // 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); + module.svp_apply_inplace(&mut vmp_res.data, i, &xai_minus_one_dft, 0); + module.vec_znx_dft_add_inplace(&mut acc_add_dft.data, i, &vmp_res.data, i); }); - }); - acc_add_dft.idft(module, &mut out_mut, scratch5); + (0..cols).for_each(|i|{ + module.vec_znx_dft_add_inplace(&mut acc_dft.data, i, &acc_add_dft.data, i); + }); + + acc_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, res: &mut [i64], lwe: &LWECiphertext<&[u8]>) { +pub(crate) fn mod_switch_2n(module: &Module, res: &mut [i64], lwe: &LWECiphertext<&[u8]>) { let basek: usize = lwe.basek(); let log2n: usize = module.log_n() + 1; diff --git a/core/src/blind_rotation/test_fft64/cggi.rs b/core/src/blind_rotation/test_fft64/cggi.rs index 8a9246f..746ec1e 100644 --- a/core/src/blind_rotation/test_fft64/cggi.rs +++ b/core/src/blind_rotation/test_fft64/cggi.rs @@ -1,24 +1,23 @@ -use core::time; use std::time::Instant; -use backend::{Encoding, Module, ScratchOwned, FFT64}; +use backend::{Encoding, Module, ScratchOwned, Stats, ZnxView, ZnxViewMut, 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 + blind_rotation::{ccgi::{cggi_blind_rotate, cggi_blind_rotate_scratch_space, mod_switch_2n}, key::BlindRotationKeyCGGI}, lwe::{ciphertext::{LWECiphertextToMut, LWECiphertextToRef}, LWEPlaintext}, FourierGLWESecret, GLWECiphertext, GLWEOps, GLWEPlaintext, GLWESecret, Infos, LWECiphertext, LWESecret }; #[test] fn blind_rotation() { let module: Module = Module::::new(2048); - let basek: usize = 17; + let basek: usize = 20; 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 k_brk: usize = 60; + let rows_brk: usize = 2; + let k_lut: usize = 60; let rank: usize = 1; let block_size: usize = 7; @@ -55,7 +54,10 @@ fn blind_rotation() { let mut pt_lwe: LWEPlaintext> = LWEPlaintext::alloc(basek, k_lwe); - pt_lwe.data.encode_coeff_i64(0, basek, 7, 0, 63, 7); + let x: i64 = 0; + let bits: usize = 6; + + pt_lwe.data.encode_coeff_i64(0, basek, bits, 0, x, bits); println!("{}", pt_lwe.data); @@ -65,21 +67,44 @@ fn blind_rotation() { println!("{}", pt_lwe.data); - let lut: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, k_lut); + let mut lut: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, k_lut); + + + lut.data.at_mut(0, 0)[0] = 0; + (1..module.n()).for_each(|i|{ + lut.data.at_mut(0, 0)[i] = - ((module.n() as i64 - i as i64 - 1)<<(basek - module.log_n() - 1)); + }); + + + let mut res: GLWECiphertext> = GLWECiphertext::alloc(&module, basek, k_lut, rank); let start: Instant = Instant::now(); - (0..32).for_each(|i|{ + (0..1).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> = GLWEPlaintext::alloc(&module, basek, k_lut); + let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(&module, basek, k_lut); - res.decrypt(&module , &mut pt, &sk_glwe_dft, scratch.borrow()); + res.decrypt(&module , &mut pt_have, &sk_glwe_dft, scratch.borrow()); - println!("{}", pt.data); + let mut lwe_2n: Vec = vec![0i64; lwe.n() + 1]; // TODO: from scratch space + + mod_switch_2n(&module, &mut lwe_2n, &lwe.to_ref()); + + let pt_want: i64 = (lwe_2n[0] + lwe_2n[1..].iter().zip(sk_lwe.data.at(0, 0)).map(|(x, y)| x * y).sum::()) % (module.n() as i64 * 2); + + lut.rotate_inplace(&module, pt_want); + + lut.sub_inplace_ab(&module, &pt_have); + + let noise: f64 = lut.data.std(0, basek); + + println!("noise: {}", noise); + + } diff --git a/core/src/lwe/ciphertext.rs b/core/src/lwe/ciphertext.rs index 432a866..4b8c85d 100644 --- a/core/src/lwe/ciphertext.rs +++ b/core/src/lwe/ciphertext.rs @@ -11,7 +11,7 @@ pub struct LWECiphertext { impl LWECiphertext> { pub fn alloc(n: usize, basek: usize, k: usize) -> Self { Self { - data: VecZnx::new::(n, 1, k.div_ceil(basek)), + data: VecZnx::new::(n+1, 1, k.div_ceil(basek)), k: k, basek: basek, }