Browse Source

Better randomization for tests

master
Pieter Wuille 8 years ago
parent
commit
3fd6253e13
  1. 23
      src/tests.c
  2. 3
      src/util.h
  3. 21
      src/util_impl.h

23
src/tests.c

@ -19,7 +19,7 @@ @@ -19,7 +19,7 @@
#include "openssl/obj_mac.h"
#endif
static int count = 100;
static int count = 750;
/***** NUM TESTS *****/
@ -632,6 +632,7 @@ void test_ecdsa_end_to_end() { @@ -632,6 +632,7 @@ void test_ecdsa_end_to_end() {
CHECK(secp256k1_ecdsa_recover_compact(message, 32, csignature, recpubkey, &recpubkeylen, pubkeylen == 33, recid) != 1 ||
memcmp(pubkey, recpubkey, pubkeylen) != 0);
CHECK(recpubkeylen == pubkeylen);
}
void run_ecdsa_end_to_end() {
@ -698,10 +699,26 @@ void run_ecdsa_openssl() { @@ -698,10 +699,26 @@ void run_ecdsa_openssl() {
#endif
int main(int argc, char **argv) {
if (argc > 1)
count = strtol(argv[1], NULL, 0)*47;
// find iteration count
if (argc > 1) {
count = strtol(argv[1], NULL, 0);
}
// find random seed
uint64_t seed;
if (argc > 2) {
seed = strtoull(argv[2], NULL, 0);
} else {
FILE *frand = fopen("/dev/urandom", "r");
if (!frand || !fread(&seed, sizeof(seed), 1, frand)) {
seed = time(NULL) * 1337;
}
fclose(frand);
}
secp256k1_rand_seed(seed);
printf("test count = %i\n", count);
printf("random seed = %llu\n", (unsigned long long)seed);
// initialize
secp256k1_start(SECP256K1_START_SIGN | SECP256K1_START_VERIFY);

3
src/util.h

@ -43,6 +43,9 @@ @@ -43,6 +43,9 @@
#define VERIFY_CHECK(cond) do { (cond); } while(0)
#endif
/** Seed the pseudorandom number generator. */
static inline void secp256k1_rand_seed(uint64_t v);
/** Generate a pseudorandom 32-bit number. */
static uint32_t secp256k1_rand32(void);

21
src/util_impl.h

@ -10,11 +10,24 @@ @@ -10,11 +10,24 @@
#include "util.h"
static uint32_t secp256k1_Rz = 11, secp256k1_Rw = 11;
static inline void secp256k1_rand_seed(uint64_t v) {
secp256k1_Rz = v >> 32;
secp256k1_Rw = v;
if (secp256k1_Rz == 0 || secp256k1_Rz == 0x9068ffffU) {
secp256k1_Rz = 111;
}
if (secp256k1_Rw == 0 || secp256k1_Rw == 0x464fffffU) {
secp256k1_Rw = 111;
}
}
static inline uint32_t secp256k1_rand32(void) {
static uint32_t Rz = 11, Rw = 11;
Rz = 36969 * (Rz & 0xFFFF) + (Rz >> 16);
Rw = 18000 * (Rw & 0xFFFF) + (Rw >> 16);
return (Rw << 16) + (Rw >> 16) + Rz;
secp256k1_Rz = 36969 * (secp256k1_Rz & 0xFFFF) + (secp256k1_Rz >> 16);
secp256k1_Rw = 18000 * (secp256k1_Rw & 0xFFFF) + (secp256k1_Rw >> 16);
return (secp256k1_Rw << 16) + (secp256k1_Rw >> 16) + secp256k1_Rz;
}
static void secp256k1_rand256(unsigned char *b32) {

Loading…
Cancel
Save