@@ -181,6 +181,35 @@ static void random_scalar_order_b32(unsigned char *b32) {
181181 secp256k1_scalar_get_b32 (b32 , & num );
182182}
183183
184+ static void run_xoshiro256pp_tests (void ) {
185+ {
186+ size_t i ;
187+ /* Sanity check that we run before the actual seeding. */
188+ for (i = 0 ; i < sizeof (secp256k1_test_state )/sizeof (secp256k1_test_state [0 ]); i ++ ) {
189+ CHECK (secp256k1_test_state [i ] == 0 );
190+ }
191+ }
192+ {
193+ int i ;
194+ unsigned char buf32 [32 ];
195+ unsigned char seed16 [16 ] = {
196+ 'C' , 'H' , 'I' , 'C' , 'K' , 'E' , 'N' , '!' ,
197+ 'C' , 'H' , 'I' , 'C' , 'K' , 'E' , 'N' , '!' ,
198+ };
199+ unsigned char buf32_expected [32 ] = {
200+ 0xAF , 0xCC , 0xA9 , 0x16 , 0xB5 , 0x6C , 0xE3 , 0xF0 ,
201+ 0x44 , 0x3F , 0x45 , 0xE0 , 0x47 , 0xA5 , 0x08 , 0x36 ,
202+ 0x4C , 0xCC , 0xC1 , 0x18 , 0xB2 , 0xD8 , 0x8F , 0xEF ,
203+ 0x43 , 0x26 , 0x15 , 0x57 , 0x37 , 0x00 , 0xEF , 0x30 ,
204+ };
205+ secp256k1_testrand_seed (seed16 );
206+ for (i = 0 ; i < 17 ; i ++ ) {
207+ secp256k1_testrand256 (buf32 );
208+ }
209+ CHECK (secp256k1_memcmp_var (buf32 , buf32_expected , sizeof (buf32 )) == 0 );
210+ }
211+ }
212+
184213static void run_selftest_tests (void ) {
185214 /* Test public API */
186215 secp256k1_selftest ();
@@ -824,78 +853,6 @@ static void run_tagged_sha256_tests(void) {
824853 CHECK (secp256k1_memcmp_var (hash32 , hash_expected , sizeof (hash32 )) == 0 );
825854}
826855
827- /***** RANDOM TESTS *****/
828-
829- static void test_rand_bits (int rand32 , int bits ) {
830- /* (1-1/2^B)^rounds[B] < 1/10^9, so rounds is the number of iterations to
831- * get a false negative chance below once in a billion */
832- static const unsigned int rounds [7 ] = {1 , 30 , 73 , 156 , 322 , 653 , 1316 };
833- /* We try multiplying the results with various odd numbers, which shouldn't
834- * influence the uniform distribution modulo a power of 2. */
835- static const uint32_t mults [6 ] = {1 , 3 , 21 , 289 , 0x9999 , 0x80402011 };
836- /* We only select up to 6 bits from the output to analyse */
837- unsigned int usebits = bits > 6 ? 6 : bits ;
838- unsigned int maxshift = bits - usebits ;
839- /* For each of the maxshift+1 usebits-bit sequences inside a bits-bit
840- number, track all observed outcomes, one per bit in a uint64_t. */
841- uint64_t x [6 ][27 ] = {{0 }};
842- unsigned int i , shift , m ;
843- /* Multiply the output of all rand calls with the odd number m, which
844- should not change the uniformity of its distribution. */
845- for (i = 0 ; i < rounds [usebits ]; i ++ ) {
846- uint32_t r = (rand32 ? secp256k1_testrand32 () : secp256k1_testrand_bits (bits ));
847- CHECK ((((uint64_t )r ) >> bits ) == 0 );
848- for (m = 0 ; m < sizeof (mults ) / sizeof (mults [0 ]); m ++ ) {
849- uint32_t rm = r * mults [m ];
850- for (shift = 0 ; shift <= maxshift ; shift ++ ) {
851- x [m ][shift ] |= (((uint64_t )1 ) << ((rm >> shift ) & ((1 << usebits ) - 1 )));
852- }
853- }
854- }
855- for (m = 0 ; m < sizeof (mults ) / sizeof (mults [0 ]); m ++ ) {
856- for (shift = 0 ; shift <= maxshift ; shift ++ ) {
857- /* Test that the lower usebits bits of x[shift] are 1 */
858- CHECK (((~x [m ][shift ]) << (64 - (1 << usebits ))) == 0 );
859- }
860- }
861- }
862-
863- /* Subrange must be a whole divisor of range, and at most 64 */
864- static void test_rand_int (uint32_t range , uint32_t subrange ) {
865- /* (1-1/subrange)^rounds < 1/10^9 */
866- int rounds = (subrange * 2073 ) / 100 ;
867- int i ;
868- uint64_t x = 0 ;
869- CHECK ((range % subrange ) == 0 );
870- for (i = 0 ; i < rounds ; i ++ ) {
871- uint32_t r = secp256k1_testrand_int (range );
872- CHECK (r < range );
873- r = r % subrange ;
874- x |= (((uint64_t )1 ) << r );
875- }
876- /* Test that the lower subrange bits of x are 1. */
877- CHECK (((~x ) << (64 - subrange )) == 0 );
878- }
879-
880- static void run_rand_bits (void ) {
881- size_t b ;
882- test_rand_bits (1 , 32 );
883- for (b = 1 ; b <= 32 ; b ++ ) {
884- test_rand_bits (0 , b );
885- }
886- }
887-
888- static void run_rand_int (void ) {
889- static const uint32_t ms [] = {1 , 3 , 17 , 1000 , 13771 , 999999 , 33554432 };
890- static const uint32_t ss [] = {1 , 3 , 6 , 9 , 13 , 31 , 64 };
891- unsigned int m , s ;
892- for (m = 0 ; m < sizeof (ms ) / sizeof (ms [0 ]); m ++ ) {
893- for (s = 0 ; s < sizeof (ss ) / sizeof (ss [0 ]); s ++ ) {
894- test_rand_int (ms [m ] * ss [s ], ss [s ]);
895- }
896- }
897- }
898-
899856/***** MODINV TESTS *****/
900857
901858/* Compute the modular inverse of (odd) x mod 2^64. */
@@ -7735,6 +7692,9 @@ int main(int argc, char **argv) {
77357692 }
77367693 printf ("test count = %i\n" , COUNT );
77377694
7695+ /* run test RNG tests (must run before we really initialize the test RNG) */
7696+ run_xoshiro256pp_tests ();
7697+
77387698 /* find random seed */
77397699 secp256k1_testrand_init (argc > 2 ? argv [2 ] : NULL );
77407700
@@ -7772,10 +7732,6 @@ int main(int argc, char **argv) {
77727732 /* scratch tests */
77737733 run_scratch_tests ();
77747734
7775- /* randomness tests */
7776- run_rand_bits ();
7777- run_rand_int ();
7778-
77797735 /* integer arithmetic tests */
77807736#ifdef SECP256K1_WIDEMUL_INT128
77817737 run_int128_tests ();
0 commit comments