fixed scratch API

This commit is contained in:
Pro7ech
2025-10-21 10:47:46 +02:00
parent 681ec7e349
commit fef2a2fc27
28 changed files with 112 additions and 153 deletions

View File

@@ -142,7 +142,7 @@ where
let mut source_xa = Source::new(seed);
let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(self, res);
let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(res);
for col_i in 0..rank_in {
for d_i in 0..dnum {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt

View File

@@ -8,7 +8,7 @@ use crate::{
ScratchTakeCore,
encryption::{GGSWEncryptSk, GLWEEncryptSkInternal, SIGMA},
layouts::{
GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos,
GGSWCompressedSeedMut, GGSWInfos, LWEInfos,
compressed::{GGSWCompressed, GGSWCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
@@ -107,7 +107,7 @@ where
println!("res.seed: {:?}", res.seed);
let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(self, &res.glwe_layout());
let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(res);
let mut source = Source::new(seed_xa);

View File

@@ -98,8 +98,8 @@ where
let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank());
{
let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank());
for i in 0..res.rank_out().into() {
let (mut sk_out, _) = scratch_1.take_glwe_secret(self.n().into(), sk.rank());
for i in 0..sk.rank().into() {
self.vec_znx_automorphism(
self.galois_element_inv(p),
&mut sk_out.data.as_vec_znx_mut(),

View File

@@ -102,7 +102,7 @@ where
self.gglwe_compressed_encrypt_sk_tmp_bytes(res)
);
let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into());
let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self.n(), sk_in.rank().into());
for i in 0..sk_in.rank().into() {
self.vec_znx_switch_ring(
&mut sk_in_tmp.as_vec_znx_mut(),
@@ -114,7 +114,7 @@ where
let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank());
{
let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1);
let (mut tmp, _) = scratch_2.take_scalar_znx(self.n(), 1);
for i in 0..sk_out.rank().into() {
self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);

View File

@@ -99,7 +99,7 @@ where
R: GGLWEInfos + TensorKeyCompressedAtMut<D>,
S: GLWESecretToRef + GetDistribution,
{
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank_out());
let (mut sk_dft_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, res.rank());
sk_dft_prep.prepare(self, sk);
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
@@ -120,7 +120,7 @@ where
}
let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1);
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1));
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self.n().into(), Rank(1));
let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1);
let mut source_xa: Source = Source::new(seed_xa);

View File

@@ -6,7 +6,6 @@ use poulpy_hal::{
use crate::{
GLWEEncryptSk, ScratchTakeCore,
layouts::GLWEInfos,
layouts::{
GGLWE, GGLWEInfos, GGLWEToMut, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
@@ -143,7 +142,7 @@ where
let base2k: usize = res.base2k().into();
let rank_in: usize = res.rank_in().into();
let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(self, &res.glwe_layout());
let (mut tmp_pt, scrach_1) = scratch.take_glwe_plaintext(res);
// For each input column (i.e. rank) produces a GGLWE of rank_out+1 columns
//
// Example for ksk rank 2 to rank 3:

View File

@@ -109,7 +109,7 @@ where
let dsize: usize = res.dsize().into();
let cols: usize = (rank + 1).into();
let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(self, res);
let (mut tmp_pt, scratch_1) = scratch.take_glwe_plaintext(res);
for row_i in 0..res.dnum().into() {
tmp_pt.data.zero();

View File

@@ -372,7 +372,7 @@ where
let (mut u_dft, scratch_1) = scratch.take_svp_ppol(self, 1);
{
let (mut u, _) = scratch_1.take_scalar_znx(self, 1);
let (mut u, _) = scratch_1.take_scalar_znx(self.n(), 1);
match pk.dist() {
Distribution::NONE => panic!(
"invalid public key: SecretDistribution::NONE, ensure it has been correctly intialized through \
@@ -499,11 +499,11 @@ where
let size: usize = ct.size();
let (mut c0, scratch_1) = scratch.take_vec_znx(self, 1, size);
let (mut c0, scratch_1) = scratch.take_vec_znx(self.n(), 1, size);
c0.zero();
{
let (mut ci, scratch_2) = scratch_1.take_vec_znx(self, 1, size);
let (mut ci, scratch_2) = scratch_1.take_vec_znx(self.n(), 1, size);
// ct[i] = uniform
// ct[0] -= c[i] * s[i],

View File

@@ -7,8 +7,8 @@ use poulpy_hal::{
use crate::{
GGLWEEncryptSk, ScratchTakeCore,
layouts::{
AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared, GLWESecretPreparedFactory,
GLWESecretToRef, LWEInfos, SetGaloisElement,
AutomorphismKey, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESecret, GLWESecretPrepared,
GLWESecretPreparedFactory, GLWESecretToRef, LWEInfos, SetGaloisElement,
},
};
@@ -115,8 +115,8 @@ where
let (mut sk_out_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank());
{
let (mut sk_out, _) = scratch_1.take_glwe_secret(self, sk.rank());
for i in 0..res.rank().into() {
let (mut sk_out, _) = scratch_1.take_glwe_secret(sk.n(), sk.rank());
for i in 0..sk.rank().into() {
self.vec_znx_automorphism(
self.galois_element_inv(p),
&mut sk_out.data.as_vec_znx_mut(),

View File

@@ -109,7 +109,7 @@ where
self.glwe_switching_key_encrypt_sk_tmp_bytes(res)
);
let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into());
let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self.n(), sk_in.rank().into());
for i in 0..sk_in.rank().into() {
self.vec_znx_switch_ring(
&mut sk_in_tmp.as_vec_znx_mut(),
@@ -121,7 +121,7 @@ where
let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank());
{
let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1);
let (mut tmp, _) = scratch_2.take_scalar_znx(self.n(), 1);
for i in 0..sk_out.rank().into() {
self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);

View File

@@ -35,7 +35,7 @@ impl<DataSelf: DataMut> TensorKey<DataSelf> {
scratch: &mut Scratch<BE>,
) where
M: TensorKeyEncryptSk<BE>,
S: GLWESecretToRef + GetDistribution,
S: GLWESecretToRef + GetDistribution + GLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch);
@@ -56,7 +56,7 @@ pub trait TensorKeyEncryptSk<BE: Backend> {
scratch: &mut Scratch<BE>,
) where
R: TensorKeyToMut,
S: GLWESecretToRef + GetDistribution;
S: GLWESecretToRef + GetDistribution + GLWEInfos;
}
impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE>
@@ -93,14 +93,14 @@ where
scratch: &mut Scratch<BE>,
) where
R: TensorKeyToMut,
S: GLWESecretToRef + GetDistribution,
S: GLWESecretToRef + GetDistribution + GLWEInfos,
{
let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut();
// let n: RingDegree = sk.n();
let rank: Rank = res.rank_out();
let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, rank);
let (mut sk_prepared, scratch_1) = scratch.take_glwe_secret_prepared(self, sk.rank());
sk_prepared.prepare(self, sk);
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
@@ -115,7 +115,7 @@ where
});
let (mut sk_ij_big, scratch_3) = scratch_2.take_vec_znx_big(self, 1, 1);
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self, Rank(1));
let (mut sk_ij, scratch_4) = scratch_3.take_glwe_secret(self.n().into(), Rank(1));
let (mut sk_ij_dft, scratch_5) = scratch_4.take_vec_znx_dft(self, 1, 1);
(0..rank.into()).for_each(|i| {

View File

@@ -100,7 +100,7 @@ where
let (mut sk_lwe_as_glwe_prep, scratch_1) = scratch.take_glwe_secret_prepared(self, Rank(1));
{
let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1));
let (mut sk_lwe_as_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), sk_lwe_as_glwe_prep.rank());
sk_lwe_as_glwe.data.zero();
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_2);

View File

@@ -111,8 +111,8 @@ where
assert!(sk_lwe_out.n().0 <= res.n().0);
assert!(res.n() <= self.n() as u32);
let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1));
let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self, Rank(1));
let (mut sk_in_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1));
let (mut sk_out_glwe, scratch_2) = scratch_1.take_glwe_secret(self.n().into(), Rank(1));
sk_out_glwe.data.at_mut(0, 0)[..sk_lwe_out.n().into()].copy_from_slice(sk_lwe_out.data.at(0, 0));
sk_out_glwe.data.at_mut(0, 0)[sk_lwe_out.n().into()..].fill(0);

View File

@@ -99,7 +99,7 @@ where
assert!(sk_lwe.n().0 <= self.n() as u32);
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self, Rank(1));
let (mut sk_lwe_as_glwe, scratch_1) = scratch.take_glwe_secret(self.n().into(), Rank(1));
sk_lwe_as_glwe.data.at_mut(0, 0)[..sk_lwe.n().into()].copy_from_slice(sk_lwe.data.at(0, 0));
sk_lwe_as_glwe.data.at_mut(0, 0)[sk_lwe.n().into()..].fill(0);
self.vec_znx_automorphism_inplace(-1, &mut sk_lwe_as_glwe.data.as_vec_znx_mut(), 0, scratch_1);