use backend::hal::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy, VecZnxDftAlloc, VecZnxFillUniform, VecZnxStd, VecZnxSubABInplace}, layouts::{Backend, Module, ScratchOwned}, oep::{ ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, }, }; use sampling::source::Source; use crate::{ Decompress, GLWECiphertext, GLWECiphertextCompressed, GLWEDecryptFamily, GLWEEncryptPkFamily, GLWEEncryptSkFamily, GLWEOps, GLWEPlaintext, GLWEPublicKey, GLWEPublicKeyExec, GLWESecret, GLWESecretExec, GLWESecretFamily, Infos, }; pub(crate) trait EncryptionTestModuleFamily = GLWEDecryptFamily + GLWESecretFamily + VecZnxStd; pub(crate) trait EncryptionTestScratchFamily = TakeVecZnxDftImpl + TakeVecZnxBigImpl + TakeSvpPPolImpl + ScratchOwnedAllocImpl + ScratchOwnedBorrowImpl + ScratchAvailableImpl + TakeScalarZnxImpl + TakeVecZnxImpl; pub(crate) fn test_encrypt_sk(module: &Module, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize) where Module: EncryptionTestModuleFamily + GLWEEncryptSkFamily, B: EncryptionTestScratchFamily, { let n = module.n(); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_ct, rank); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_pt); let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_pt); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k()) | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k()), ); let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa); ct.encrypt_sk( module, &pt_want, &sk_exec, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); pt_want.sub_inplace_ab(module, &pt_have); let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0) * (ct.k() as f64).exp2(); let noise_want: f64 = sigma; assert!(noise_have <= noise_want + 0.2); } pub(crate) fn test_encrypt_sk_compressed( module: &Module, basek: usize, k_ct: usize, k_pt: usize, sigma: f64, rank: usize, ) where Module: EncryptionTestModuleFamily + GLWEEncryptSkFamily + VecZnxCopy, B: EncryptionTestScratchFamily, { let n = module.n(); let mut ct_compressed: GLWECiphertextCompressed> = GLWECiphertextCompressed::alloc(n, basek, k_ct, rank); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_pt); let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_pt); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertextCompressed::encrypt_sk_scratch_space(module, n, basek, k_ct) | GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct), ); let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_pt, &mut source_xa); let seed_xa: [u8; 32] = [1u8; 32]; ct_compressed.encrypt_sk( module, &pt_want, &sk_exec, seed_xa, &mut source_xe, sigma, scratch.borrow(), ); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_ct, rank); ct.decompress(module, &ct_compressed); ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); pt_want.sub_inplace_ab(module, &pt_have); let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0) * (ct.k() as f64).exp2(); let noise_want: f64 = sigma; assert!( noise_have <= noise_want + 0.2, "{} <= {}", noise_have, noise_want + 0.2 ); } pub(crate) fn test_encrypt_zero_sk(module: &Module, basek: usize, k_ct: usize, sigma: f64, rank: usize) where Module: EncryptionTestModuleFamily + GLWEEncryptSkFamily, B: EncryptionTestScratchFamily, { let n = module.n(); let mut pt: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_ct); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_ct, rank); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::decrypt_scratch_space(module, n, basek, k_ct) | GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k_ct), ); ct.encrypt_zero_sk( module, &sk_exec, &mut source_xa, &mut source_xe, sigma, scratch.borrow(), ); ct.decrypt(module, &mut pt, &sk_exec, scratch.borrow()); assert!((sigma - module.vec_znx_std(basek, &pt.data, 0) * (k_ct as f64).exp2()) <= 0.2); } pub(crate) fn test_encrypt_pk(module: &Module, basek: usize, k_ct: usize, k_pk: usize, sigma: f64, rank: usize) where Module: EncryptionTestModuleFamily + GLWEEncryptPkFamily + GLWEEncryptSkFamily + VecZnxDftAlloc + VecZnxFillUniform + VecZnxSubABInplace, B: EncryptionTestScratchFamily, { let n: usize = module.n(); let mut ct: GLWECiphertext> = GLWECiphertext::alloc(n, basek, k_ct, rank); let mut pt_have: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_ct); let mut pt_want: GLWEPlaintext> = GLWEPlaintext::alloc(n, basek, k_ct); let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xu: Source = Source::new([0u8; 32]); let mut sk: GLWESecret> = GLWESecret::alloc(n, rank); sk.fill_ternary_prob(0.5, &mut source_xs); let sk_exec: GLWESecretExec, B> = GLWESecretExec::from(module, &sk); let mut pk: GLWEPublicKey> = GLWEPublicKey::alloc(n, basek, k_pk, rank); pk.generate_from_sk(module, &sk_exec, &mut source_xa, &mut source_xe, sigma); let mut scratch: ScratchOwned = ScratchOwned::alloc( GLWECiphertext::encrypt_sk_scratch_space(module, n, basek, ct.k()) | GLWECiphertext::decrypt_scratch_space(module, n, basek, ct.k()) | GLWECiphertext::encrypt_pk_scratch_space(module, n, basek, pk.k()), ); module.vec_znx_fill_uniform(basek, &mut pt_want.data, 0, k_ct, &mut source_xa); let pk_exec: GLWEPublicKeyExec, B> = GLWEPublicKeyExec::from(module, &pk, scratch.borrow()); ct.encrypt_pk( module, &pt_want, &pk_exec, &mut source_xu, &mut source_xe, sigma, scratch.borrow(), ); ct.decrypt(module, &mut pt_have, &sk_exec, scratch.borrow()); pt_want.sub_inplace_ab(module, &pt_have); let noise_have: f64 = module.vec_znx_std(basek, &pt_want.data, 0).log2(); let noise_want: f64 = ((((rank as f64) + 1.0) * n as f64 * 0.5 * sigma * sigma).sqrt()).log2() - (k_ct as f64); assert!( noise_have <= noise_want + 0.2, "{} {}", noise_have, noise_want ); }