mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
add test for GLWEBlindRotation
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
use poulpy_core::{
|
||||
GLWECopy, GLWERotate, ScratchTakeCore,
|
||||
layouts::{GGSW, GGSWInfos, GGSWToMut, GLWE, GLWEInfos, GLWEToMut},
|
||||
layouts::{GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWE, GLWEInfos, GLWEToMut, GLWEToRef},
|
||||
};
|
||||
use poulpy_hal::layouts::{Backend, Scratch};
|
||||
use poulpy_hal::layouts::{Backend, Module, Scratch};
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{Cmux, GetGGSWBit, UnsignedInteger};
|
||||
|
||||
impl<T: UnsignedInteger, BE: Backend> GGSWBlindRotation<T, BE> for Module<BE>
|
||||
where
|
||||
Self: GLWEBlindRotation<T, BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
}
|
||||
|
||||
pub trait GGSWBlindRotation<T: UnsignedInteger, BE: Backend>
|
||||
where
|
||||
Self: GLWEBlindRotation<T, BE>,
|
||||
@@ -19,9 +26,10 @@ where
|
||||
self.glwe_blind_rotation_tmp_bytes(res_infos, k_infos)
|
||||
}
|
||||
|
||||
fn ggsw_blind_rotation<R, K>(
|
||||
fn ggsw_blind_rotation<R, G, K>(
|
||||
&self,
|
||||
res: &mut R,
|
||||
test_ggsw: &G,
|
||||
k: &K,
|
||||
bit_start: usize,
|
||||
bit_size: usize,
|
||||
@@ -29,15 +37,18 @@ where
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GGSWToMut,
|
||||
G: GGSWToRef,
|
||||
K: GetGGSWBit<T, BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
|
||||
let test_ggsw: &GGSW<&[u8]> = &test_ggsw.to_ref();
|
||||
|
||||
for row in 0..res.dnum().into() {
|
||||
for col in 0..(res.rank() + 1).into() {
|
||||
self.glwe_blind_rotation(
|
||||
&mut res.at_mut(row, col),
|
||||
&test_ggsw.at(row, col),
|
||||
k,
|
||||
bit_start,
|
||||
bit_size,
|
||||
@@ -49,6 +60,13 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: UnsignedInteger, BE: Backend> GLWEBlindRotation<T, BE> for Module<BE>
|
||||
where
|
||||
Self: GLWECopy + GLWERotate<BE> + Cmux<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
}
|
||||
|
||||
pub trait GLWEBlindRotation<T: UnsignedInteger, BE: Backend>
|
||||
where
|
||||
Self: GLWECopy + GLWERotate<BE> + Cmux<BE>,
|
||||
@@ -63,9 +81,10 @@ where
|
||||
}
|
||||
|
||||
/// Homomorphic multiplication of res by X^{k[bit_start..bit_start + bit_size] * bit_step}.
|
||||
fn glwe_blind_rotation<R, K>(
|
||||
fn glwe_blind_rotation<R, G, K>(
|
||||
&self,
|
||||
res: &mut R,
|
||||
test_glwe: &G,
|
||||
k: &K,
|
||||
bit_start: usize,
|
||||
bit_size: usize,
|
||||
@@ -73,21 +92,43 @@ where
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GLWEToMut,
|
||||
G: GLWEToRef,
|
||||
K: GetGGSWBit<T, BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
|
||||
assert!(bit_start + bit_size <= T::WORD_SIZE);
|
||||
|
||||
let (mut tmp_res, scratch_1) = scratch.take_glwe(res);
|
||||
let mut res: GLWE<&mut [u8]> = res.to_mut();
|
||||
|
||||
self.glwe_copy(&mut tmp_res, res);
|
||||
let (mut tmp_res, scratch_1) = scratch.take_glwe(&res);
|
||||
|
||||
for i in 1..bit_size {
|
||||
// res' = res * X^2^(i * bit_step)
|
||||
self.glwe_rotate(1 << (i + bit_step), &mut tmp_res, res);
|
||||
// res <- test_glwe
|
||||
self.glwe_copy(&mut res, test_glwe);
|
||||
|
||||
// res = (res - res') * GGSW(b[i]) + res'
|
||||
self.cmux_inplace(res, &tmp_res, &k.get_bit(i + bit_start), scratch_1);
|
||||
// a_is_res = true => (a, b) = (&mut res, &mut tmp_res)
|
||||
// a_is_res = false => (a, b) = (&mut tmp_res, &mut res)
|
||||
let mut a_is_res: bool = true;
|
||||
|
||||
for i in 0..bit_size {
|
||||
let (a, b) = if a_is_res {
|
||||
(&mut res, &mut tmp_res)
|
||||
} else {
|
||||
(&mut tmp_res, &mut res)
|
||||
};
|
||||
|
||||
// a <- a ; b <- a * X^{-2^{i + bit_step}}
|
||||
self.glwe_rotate(-1 << (i + bit_step), b, a);
|
||||
|
||||
// b <- (b - a) * GGSW(b[i]) + a
|
||||
self.cmux_inplace(b, a, &k.get_bit(i + bit_start), scratch_1);
|
||||
|
||||
// ping-pong roles for next iter
|
||||
a_is_res = !a_is_res;
|
||||
}
|
||||
|
||||
// Ensure the final value ends up in `res`
|
||||
if !a_is_res {
|
||||
self.glwe_copy(&mut res, &tmp_res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,16 @@ use poulpy_backend::FFT64Ref;
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::tests::test_suite::{
|
||||
test_bdd_add, test_bdd_and, test_bdd_or, test_bdd_prepare, test_bdd_sll, test_bdd_slt, test_bdd_sltu, test_bdd_sra,
|
||||
test_bdd_srl, test_bdd_sub, test_bdd_xor,
|
||||
test_bdd_srl, test_bdd_sub, test_bdd_xor, test_glwe_blind_rotation,
|
||||
},
|
||||
blind_rotation::CGGI,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_glwe_blind_rotation_fft64_ref() {
|
||||
test_glwe_blind_rotation::<FFT64Ref>()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bdd_prepare_fft64_ref() {
|
||||
test_bdd_prepare::<CGGI, FFT64Ref>()
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
use poulpy_core::{
|
||||
GGSWEncryptSk, GLWEDecrypt, GLWEEncryptSk, ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGSWLayout, GGSWPreparedFactory, GLWE, GLWELayout, GLWEPlaintext, GLWESecret,
|
||||
GLWESecretPrepared, GLWESecretPreparedFactory, LWEInfos, Rank, TorusPrecision,
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{ModuleNew, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned},
|
||||
source::Source,
|
||||
};
|
||||
use rand::RngCore;
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{FheUintBlocksPrepared, GLWEBlindRotation};
|
||||
|
||||
pub fn test_glwe_blind_rotation<BE: Backend>()
|
||||
where
|
||||
Module<BE>: ModuleNew<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GGSWPreparedFactory<BE>
|
||||
+ GGSWEncryptSk<BE>
|
||||
+ GLWEBlindRotation<u32, BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWEEncryptSk<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
let n: Degree = Degree(1 << 11);
|
||||
let base2k: Base2K = Base2K(13);
|
||||
let rank: Rank = Rank(1);
|
||||
let k_glwe: TorusPrecision = TorusPrecision(26);
|
||||
let k_ggsw: TorusPrecision = TorusPrecision(39);
|
||||
let dnum: Dnum = Dnum(3);
|
||||
|
||||
let glwe_infos: GLWELayout = GLWELayout {
|
||||
n,
|
||||
base2k,
|
||||
k: k_glwe,
|
||||
rank,
|
||||
};
|
||||
let ggsw_infos: GGSWLayout = GGSWLayout {
|
||||
n,
|
||||
base2k,
|
||||
k: k_ggsw,
|
||||
rank,
|
||||
dnum,
|
||||
dsize: Dsize(1),
|
||||
};
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
|
||||
let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
|
||||
let mut source: Source = Source::new([6u8; 32]);
|
||||
let mut source_xs: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([2u8; 32]);
|
||||
let mut source_xe: Source = Source::new([3u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
|
||||
|
||||
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
|
||||
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
|
||||
let mut sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(&module, &glwe_infos);
|
||||
sk_glwe_prep.prepare(&module, &sk_glwe);
|
||||
|
||||
let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
|
||||
|
||||
let mut test_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
let mut data: Vec<i64> = vec![0i64; module.n()];
|
||||
data.iter_mut().enumerate().for_each(|(i, x)| *x = i as i64);
|
||||
test_glwe.encode_vec_i64(&data, base2k.as_usize().into());
|
||||
|
||||
println!("pt: {}", test_glwe);
|
||||
|
||||
let k: u32 = source.next_u32();
|
||||
|
||||
println!("k: {k}");
|
||||
|
||||
let mut k_enc_prep: FheUintBlocksPrepared<Vec<u8>, u32, BE> =
|
||||
FheUintBlocksPrepared::<Vec<u8>, u32, BE>::alloc(&module, &ggsw_infos);
|
||||
k_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
k,
|
||||
&sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let base: [usize; 2] = [6, 5];
|
||||
|
||||
assert_eq!(base.iter().sum::<usize>(), module.log_n());
|
||||
|
||||
// Starting bit
|
||||
let mut bit_start: usize = 0;
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
|
||||
for _ in 0..32_usize.div_ceil(module.log_n()) {
|
||||
// By how many bits to left shift
|
||||
let mut bit_step: usize = 0;
|
||||
|
||||
for digit in base {
|
||||
let mask: u32 = (1 << digit) - 1;
|
||||
|
||||
// How many bits to take
|
||||
let bit_size: usize = (32 - bit_start).min(digit);
|
||||
|
||||
module.glwe_blind_rotation(
|
||||
&mut res,
|
||||
&test_glwe,
|
||||
&k_enc_prep,
|
||||
bit_start,
|
||||
bit_size,
|
||||
bit_step,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
res.decrypt(&module, &mut pt, &sk_glwe_prep, scratch.borrow());
|
||||
|
||||
assert_eq!(
|
||||
(((k >> bit_start) & mask) << bit_step) as i64,
|
||||
pt.decode_coeff_i64(base2k.as_usize().into(), 0)
|
||||
);
|
||||
|
||||
bit_step += digit;
|
||||
bit_start += digit;
|
||||
|
||||
if bit_start >= 32 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
mod add;
|
||||
mod and;
|
||||
mod glwe_blind_rotation;
|
||||
mod or;
|
||||
mod prepare;
|
||||
mod sll;
|
||||
@@ -12,6 +13,7 @@ mod xor;
|
||||
|
||||
pub use add::*;
|
||||
pub use and::*;
|
||||
pub use glwe_blind_rotation::*;
|
||||
pub use or::*;
|
||||
pub use prepare::*;
|
||||
pub use sll::*;
|
||||
|
||||
Reference in New Issue
Block a user