@@ -348,4 +348,53 @@ mod test {
348
348
349
349
assert_eq ! ( rng. 0 . get_word_pos( ) , 32 ) ;
350
350
}
351
+
352
+ #[ test]
353
+ fn test_chacha_counter ( ) {
354
+ // Source: none
355
+ // Test: all zero key and IV, block set to u32::MAX, rounds 12, 256-bit key
356
+
357
+ let seed = [ 0u8 ; 32 ] ;
358
+ let mut rng = StdRng :: from_seed ( seed) ;
359
+ let block = u32:: MAX ;
360
+ let words_per_block = 16 ;
361
+ rng. 0 . set_word_pos ( ( block as u128 ) * words_per_block) ;
362
+
363
+ let mut expected = [ 0u32 ; 12 ] ;
364
+ // Note: four blocks get generated by set_word_pos
365
+ expected[ 8 ] = block. wrapping_add ( 4 ) ;
366
+ expected[ 9 ] = 1 ;
367
+ assert_eq ! ( rng_state( & rng) , & expected) ;
368
+
369
+ let mut results = [ 0u8 ; 64 ] ;
370
+ rng. fill_bytes ( & mut results) ;
371
+ #[ rustfmt:: skip]
372
+ let expected = [
373
+ 0xd7 , 0xa6 , 0xaf , 0x50 , 0xf1 , 0xc9 , 0x2a , 0x29 ,
374
+ 0x48 , 0x42 , 0x52 , 0xbb , 0xfc , 0xe2 , 0x06 , 0xf1 ,
375
+ 0x7d , 0x01 , 0xdd , 0x13 , 0x95 , 0x30 , 0xa3 , 0x83 ,
376
+ 0x0a , 0xb5 , 0x83 , 0xc1 , 0xf6 , 0x2e , 0x03 , 0x12 ,
377
+ 0x82 , 0x93 , 0x61 , 0xa1 , 0x9a , 0x8a , 0x95 , 0x6c ,
378
+ 0xed , 0xea , 0x38 , 0x04 , 0x30 , 0xff , 0x93 , 0x2c ,
379
+ 0xd0 , 0x52 , 0xdb , 0x5e , 0x94 , 0x77 , 0x83 , 0x50 ,
380
+ 0x58 , 0xb8 , 0x0a , 0x27 , 0x24 , 0x06 , 0xfc , 0x74 ,
381
+ ] ;
382
+ assert_eq ! ( results, expected) ;
383
+
384
+ rng. fill_bytes ( & mut results) ;
385
+ #[ rustfmt:: skip]
386
+ let expected = [
387
+ 0xcc , 0x7b , 0x53 , 0xdc , 0x11 , 0x89 , 0x4d , 0x26 ,
388
+ 0x24 , 0x05 , 0x81 , 0xb8 , 0xa8 , 0xf4 , 0xf4 , 0xe5 ,
389
+ 0xaf , 0x40 , 0x67 , 0x05 , 0x80 , 0x12 , 0x23 , 0xb1 ,
390
+ 0x3f , 0x82 , 0x1f , 0xdc , 0xcb , 0xa6 , 0xa6 , 0x18 ,
391
+ 0x8a , 0x63 , 0xf8 , 0xd3 , 0xdc , 0x83 , 0xcc , 0xbc ,
392
+ 0xed , 0x45 , 0x1f , 0x4b , 0xa4 , 0xe0 , 0xda , 0xab ,
393
+ 0x22 , 0x8a , 0xbb , 0x0d , 0x74 , 0x39 , 0xcc , 0x67 ,
394
+ 0xe5 , 0x0d , 0xf7 , 0x12 , 0x9f , 0x64 , 0x6b , 0xad ,
395
+ ] ;
396
+ assert_eq ! ( results, expected) ;
397
+
398
+ assert_eq ! ( rng. 0 . get_word_pos( ) , ( block as u128 ) * words_per_block + 32 ) ;
399
+ }
351
400
}
0 commit comments