@@ -4,8 +4,7 @@ use std::{
44} ;
55
66use cosmian_crypto_core:: {
7- bytes_ser_de:: Serializable ,
8- reexport:: rand_core:: { CryptoRngCore , RngCore } ,
7+ bytes_ser_de:: Serializable , reexport:: rand_core:: CryptoRngCore , shuffle, shuffle_in_place,
98 RandomFixedSizeCBytes , Secret , SymmetricKey ,
109} ;
1110
@@ -45,13 +44,6 @@ fn xor_in_place<const LENGTH: usize>(
4544 lhs
4645}
4746
48- fn shuffle < T > ( xs : & mut [ T ] , rng : & mut impl RngCore ) {
49- for i in 0 ..xs. len ( ) {
50- let j = rng. next_u32 ( ) as usize % xs. len ( ) ;
51- xs. swap ( i, j) ;
52- }
53- }
54-
5547/// Computes the signature of the given USK using the MSK.
5648fn sign (
5749 msk : & MasterSecretKey ,
@@ -325,7 +317,7 @@ pub fn encaps(
325317 // rights are hashed in-order. If shuffling is performed after generating
326318 // the encapsulations, there would be no way to know in which order to
327319 // perform hashing upon decapsulation.
328- shuffle ( & mut coordinate_keys, rng) ;
320+ shuffle_in_place ( & mut coordinate_keys, rng) ;
329321
330322 let S = Secret :: random ( rng) ;
331323 let r = G_hash ( & S ) ?;
@@ -377,14 +369,13 @@ fn h_decaps(
377369
378370 // Shuffle encapsulation to counter timing attacks attempting to determine
379371 // which right was used to open an encapsulation.
380- let mut encs = encs. iter ( ) . collect :: < Vec < _ > > ( ) ;
381- shuffle ( & mut encs, rng) ;
372+ let encs = shuffle ( encs, rng) ;
382373
383374 // Loop order matters: this ordering is faster.
384375 for mut revision in usk. secrets . revisions ( ) {
385376 // Shuffle secrets to counter timing attacks attempting to determine
386377 // whether successive encapsulations target the same user right.
387- shuffle ( & mut revision, rng) ;
378+ shuffle_in_place ( & mut revision, rng) ;
388379 for ( E , F ) in & encs {
389380 for ( _, secret) in & revision {
390381 if let RightSecretKey :: Hybridized { sk, dk } = secret {
@@ -440,14 +431,13 @@ fn c_decaps(
440431
441432 // Shuffle encapsulation to counter timing attacks attempting to determine
442433 // which right was used to open an encapsulation.
443- let mut encs = encs. iter ( ) . collect :: < Vec < _ > > ( ) ;
444- shuffle ( & mut encs, rng) ;
434+ let encs = shuffle ( encs, rng) ;
445435
446436 // Loop order matters: this ordering is faster.
447437 for mut revision in usk. secrets . revisions ( ) {
448438 // Shuffle secrets to counter timing attacks attempting to determine
449439 // whether successive encapsulations target the same user right.
450- shuffle ( & mut revision, rng) ;
440+ shuffle_in_place ( & mut revision, rng) ;
451441 for F in & encs {
452442 for ( _, secret) in & revision {
453443 let sk = match secret {
0 commit comments