arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/tests/generics/keyswitch/lwe_ct.rs
core/src/tests/generics/keyswitch/lwe_ct.rsRaw
  1use backend::hal::{
  2    api::{
  3        ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace,
  4        VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
  5        VecZnxBigAllocBytes, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAllocBytes, VecZnxDftFromVecZnx,
  6        VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes,
  7        VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree, VmpApply, VmpApplyAdd, VmpApplyTmpBytes, VmpPMatAlloc, VmpPrepare,
  8        ZnxView,
  9    },
 10    layouts::{Backend, Module, ScratchOwned},
 11    oep::{
 12        ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
 13        TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
 14    },
 15};
 16use sampling::source::Source;
 17
 18use crate::layouts::{
 19    Infos, LWECiphertext, LWEPlaintext, LWESecret, LWESwitchingKey,
 20    prepared::{LWESwitchingKeyPrepared, PrepareAlloc},
 21};
 22
 23pub fn test_lwe_keyswitch<B: Backend>(module: &Module<B>)
 24where
 25    Module<B>: VecZnxDftAllocBytes
 26        + VecZnxBigNormalize<B>
 27        + VecZnxDftFromVecZnx<B>
 28        + SvpApplyInplace<B>
 29        + VecZnxDftToVecZnxBigConsume<B>
 30        + VecZnxFillUniform
 31        + VecZnxSubABInplace
 32        + VecZnxAddInplace
 33        + VecZnxNormalizeInplace<B>
 34        + VecZnxAddNormal
 35        + VecZnxNormalize<B>
 36        + VecZnxSub
 37        + SvpPrepare<B>
 38        + SvpPPolAllocBytes
 39        + SvpPPolAlloc<B>
 40        + VecZnxBigAllocBytes
 41        + VecZnxBigAddInplace<B>
 42        + VecZnxBigAddSmallInplace<B>
 43        + VecZnxNormalizeTmpBytes
 44        + VecZnxAddScalarInplace
 45        + VmpPMatAlloc<B>
 46        + VmpPrepare<B>
 47        + VmpApplyTmpBytes
 48        + VmpApply<B>
 49        + VmpApplyAdd<B>
 50        + VecZnxBigNormalizeTmpBytes
 51        + VecZnxSwithcDegree
 52        + VecZnxAutomorphismInplace,
 53    B: TakeVecZnxDftImpl<B>
 54        + TakeVecZnxBigImpl<B>
 55        + TakeSvpPPolImpl<B>
 56        + ScratchOwnedAllocImpl<B>
 57        + ScratchOwnedBorrowImpl<B>
 58        + ScratchAvailableImpl<B>
 59        + TakeScalarZnxImpl<B>
 60        + TakeVecZnxImpl<B>,
 61{
 62    let n: usize = module.n();
 63    let basek: usize = 17;
 64    let sigma: f64 = 3.2;
 65
 66    let n_lwe_in: usize = 22;
 67    let n_lwe_out: usize = 30;
 68    let k_lwe_ct: usize = 2 * basek;
 69    let k_lwe_pt: usize = 8;
 70
 71    let k_ksk: usize = k_lwe_ct + basek;
 72
 73    let mut source_xs: Source = Source::new([0u8; 32]);
 74    let mut source_xa: Source = Source::new([0u8; 32]);
 75    let mut source_xe: Source = Source::new([0u8; 32]);
 76
 77    let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
 78        LWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k_ksk)
 79            | LWECiphertext::keyswitch_scratch_space(module, n, basek, k_lwe_ct, k_lwe_ct, k_ksk),
 80    );
 81
 82    let mut sk_lwe_in: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe_in);
 83    sk_lwe_in.fill_ternary_prob(0.5, &mut source_xs);
 84
 85    let mut sk_lwe_out: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe_out);
 86    sk_lwe_out.fill_ternary_prob(0.5, &mut source_xs);
 87
 88    let data: i64 = 17;
 89
 90    let mut lwe_pt_in: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_pt);
 91
 92    lwe_pt_in.encode_i64(data, k_lwe_pt);
 93
 94    let mut lwe_ct_in: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_in, basek, k_lwe_ct);
 95    lwe_ct_in.encrypt_sk(
 96        module,
 97        &lwe_pt_in,
 98        &sk_lwe_in,
 99        &mut source_xa,
100        &mut source_xe,
101        sigma,
102    );
103
104    let mut ksk: LWESwitchingKey<Vec<u8>> = LWESwitchingKey::alloc(n, basek, k_ksk, lwe_ct_in.size());
105
106    ksk.encrypt_sk(
107        module,
108        &sk_lwe_in,
109        &sk_lwe_out,
110        &mut source_xa,
111        &mut source_xe,
112        sigma,
113        scratch.borrow(),
114    );
115
116    let mut lwe_ct_out: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(n_lwe_out, basek, k_lwe_ct);
117
118    let ksk_prepared: LWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
119
120    lwe_ct_out.keyswitch(module, &lwe_ct_in, &ksk_prepared, scratch.borrow());
121
122    let mut lwe_pt_out: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(basek, k_lwe_ct);
123    lwe_ct_out.decrypt(module, &mut lwe_pt_out, &sk_lwe_out);
124
125    assert_eq!(lwe_pt_in.data.at(0, 0)[0], lwe_pt_out.data.at(0, 0)[0]);
126}