@@ -85,11 +85,11 @@ impl SecretKey {
8585 * c. as_mut ( ) -= & m;
8686 let ctx = c. ctx ( ) . clone ( ) ;
8787 let c_inner = std:: mem:: replace ( c. as_mut ( ) , Poly :: < Ntt > :: zero ( & ctx) ) ;
88- let c = c_inner. into_power_basis ( ) ;
88+ let c = Zeroizing :: new ( c_inner. into_power_basis ( ) ) ;
8989
9090 let ciphertext_modulus = ct[ 0 ] . ctx ( ) . modulus ( ) ;
9191 let mut noise = 0usize ;
92- for coeff in Vec :: < BigUint > :: from ( & c ) {
92+ for coeff in Vec :: < BigUint > :: from ( c . as_ref ( ) ) {
9393 noise = std:: cmp:: max (
9494 noise,
9595 std:: cmp:: min ( coeff. bits ( ) , ( ciphertext_modulus - & coeff) . bits ( ) ) as usize ,
@@ -227,8 +227,8 @@ impl FheDecrypter<Plaintext, Ciphertext> for SecretKey {
227227 let ctx_lvl = self . par . context_level_at ( ct. level ) . unwrap ( ) ;
228228 let ctx = c. ctx ( ) . clone ( ) ;
229229 let c_inner = std:: mem:: replace ( c. as_mut ( ) , Poly :: < Ntt > :: zero ( & ctx) ) ;
230- let c_pb = c_inner. into_power_basis ( ) ;
231- let d = Zeroizing :: new ( c_pb. scale ( & ctx_lvl. cipher_plain_context . scaler ) ?) ;
230+ let c_pb = Zeroizing :: new ( c_inner. into_power_basis ( ) ) ;
231+ let d = Zeroizing :: new ( c_pb. as_ref ( ) . scale ( & ctx_lvl. cipher_plain_context . scaler ) ?) ;
232232
233233 let value = match self . par . plaintext {
234234 PlaintextModulus :: Small { .. } => {
@@ -334,6 +334,27 @@ mod tests {
334334 Ok ( ( ) )
335335 }
336336
337+ #[ test]
338+ fn measure_noise_within_modulus_bits ( ) -> Result < ( ) , Box < dyn Error > > {
339+ let mut rng = rng ( ) ;
340+ let params = BfvParameters :: default_arc ( 1 , 16 ) ;
341+ let sk = SecretKey :: random ( & params, & mut rng) ;
342+ let q = fhe_math:: zq:: Modulus :: new ( params. plaintext ( ) ) . unwrap ( ) ;
343+
344+ let pt = Plaintext :: try_encode (
345+ & q. random_vec ( params. degree ( ) , & mut rng) ,
346+ Encoding :: poly_at_level ( 0 ) ,
347+ & params,
348+ ) ?;
349+ let ct = sk. try_encrypt ( & pt, & mut rng) ?;
350+ let noise = unsafe { sk. measure_noise ( & ct) ? } ;
351+
352+ let modulus_bits = ct[ 0 ] . ctx ( ) . modulus ( ) . bits ( ) as usize ;
353+ assert ! ( noise <= modulus_bits) ;
354+
355+ Ok ( ( ) )
356+ }
357+
337358 #[ test]
338359 fn serialize_roundtrip ( ) -> Result < ( ) , Box < dyn Error > > {
339360 let mut rng = rng ( ) ;
0 commit comments