mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
update BDD ciphertext types + API for GLWEToLWE
This commit is contained in:
@@ -4,7 +4,7 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEKeyswitch, ScratchTakeCore,
|
||||
GLWEKeyswitch, GLWERotate, ScratchTakeCore,
|
||||
layouts::{GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWELayout, GLWEToRef, LWE, LWEInfos, LWEToMut, Rank},
|
||||
};
|
||||
|
||||
@@ -37,11 +37,11 @@ where
|
||||
}
|
||||
|
||||
impl<BE: Backend> LWESampleExtract for Module<BE> where Self: ModuleN {}
|
||||
impl<BE: Backend> LWEFromGLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + LWESampleExtract {}
|
||||
impl<BE: Backend> LWEFromGLWE<BE> for Module<BE> where Self: GLWEKeyswitch<BE> + LWESampleExtract + GLWERotate<BE> {}
|
||||
|
||||
pub trait LWEFromGLWE<BE: Backend>
|
||||
where
|
||||
Self: GLWEKeyswitch<BE> + LWESampleExtract,
|
||||
Self: GLWEKeyswitch<BE> + LWESampleExtract + GLWERotate<BE>,
|
||||
{
|
||||
fn lwe_from_glwe_tmp_bytes<R, A, K>(&self, lwe_infos: &R, glwe_infos: &A, key_infos: &K) -> usize
|
||||
where
|
||||
@@ -61,10 +61,11 @@ where
|
||||
lwe_infos.base2k(),
|
||||
lwe_infos.k(),
|
||||
1u32.into(),
|
||||
) + self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos)
|
||||
) + GLWE::bytes_of_from_infos(glwe_infos)
|
||||
+ self.glwe_keyswitch_tmp_bytes(&res_infos, glwe_infos, key_infos)
|
||||
}
|
||||
|
||||
fn lwe_from_glwe<R, A, K>(&self, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>)
|
||||
fn lwe_from_glwe<R, A, K>(&self, res: &mut R, a: &A, a_idx: usize, key: &K, scratch: &mut Scratch<BE>)
|
||||
where
|
||||
R: LWEToMut,
|
||||
A: GLWEToRef,
|
||||
@@ -85,9 +86,20 @@ where
|
||||
rank: Rank(1),
|
||||
};
|
||||
|
||||
let (mut tmp_glwe, scratch_1) = scratch.take_glwe(&glwe_layout);
|
||||
self.glwe_keyswitch(&mut tmp_glwe, a, key, scratch_1);
|
||||
self.lwe_sample_extract(res, &tmp_glwe);
|
||||
let (mut tmp_glwe_rank_1, scratch_1) = scratch.take_glwe(&glwe_layout);
|
||||
|
||||
match a_idx {
|
||||
0 => {
|
||||
self.glwe_keyswitch(&mut tmp_glwe_rank_1, a, key, scratch_1);
|
||||
}
|
||||
_ => {
|
||||
let (mut tmp_glwe_in, scratch_2) = scratch_1.take_glwe(a);
|
||||
self.glwe_rotate(-(a_idx as i64), &mut tmp_glwe_in, a);
|
||||
self.glwe_keyswitch(&mut tmp_glwe_rank_1, &tmp_glwe_in, key, scratch_2);
|
||||
}
|
||||
}
|
||||
|
||||
self.lwe_sample_extract(res, &tmp_glwe_rank_1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,13 +124,13 @@ impl<D: DataMut> LWE<D> {
|
||||
module.lwe_sample_extract(self, a);
|
||||
}
|
||||
|
||||
pub fn from_glwe<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>)
|
||||
pub fn from_glwe<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, a_idx: usize, key: &K, scratch: &mut Scratch<BE>)
|
||||
where
|
||||
A: GLWEToRef,
|
||||
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
|
||||
M: LWEFromGLWE<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
module.lwe_from_glwe(self, a, key, scratch);
|
||||
module.lwe_from_glwe(self, a, a_idx, key, scratch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk,
|
||||
GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk, LWEFromGLWE,
|
||||
LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey,
|
||||
@@ -110,6 +110,7 @@ where
|
||||
+ GLWEToLWESwitchingKeyEncryptSk<BE>
|
||||
+ GLWEEncryptSk<BE>
|
||||
+ LWEDecrypt<BE>
|
||||
+ LWEFromGLWE<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEToLWESwitchingKeyEncryptSk<BE>
|
||||
@@ -163,9 +164,14 @@ where
|
||||
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
|
||||
sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
|
||||
|
||||
let data: i64 = 17;
|
||||
let a_idx: usize = 1;
|
||||
|
||||
let mut data: Vec<i64> = vec![0i64; module.n()];
|
||||
data[a_idx] = 17;
|
||||
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
|
||||
glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0);
|
||||
glwe_pt.encode_vec_i64(&data, k_lwe_pt);
|
||||
|
||||
println!("glwe_pt: {glwe_pt}");
|
||||
|
||||
let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
|
||||
glwe_ct.encrypt_sk(
|
||||
@@ -193,10 +199,10 @@ where
|
||||
let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
ksk_prepared.prepare(module, &ksk, scratch.borrow());
|
||||
|
||||
lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
|
||||
lwe_ct.from_glwe(module, &glwe_ct, a_idx, &ksk_prepared, scratch.borrow());
|
||||
|
||||
let mut lwe_pt: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
|
||||
lwe_ct.decrypt(module, &mut lwe_pt, &sk_lwe);
|
||||
|
||||
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
|
||||
assert_eq!(glwe_pt.data.at(0, 0)[a_idx], lwe_pt.data.at(0, 0)[0]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user