use 128-bit hex seed

This commit is contained in:
Pieter Wuille 2015-02-14 15:30:58 -08:00
parent 02efd06554
commit 8956111802
3 changed files with 31 additions and 10 deletions

View File

@ -14,7 +14,7 @@
/* A non-cryptographic RNG used only for test infrastructure. */ /* A non-cryptographic RNG used only for test infrastructure. */
/** Seed the pseudorandom number generator for testing. */ /** Seed the pseudorandom number generator for testing. */
SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v); SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16);
/** Generate a pseudorandom 32-bit number. */ /** Generate a pseudorandom 32-bit number. */
static uint32_t secp256k1_rand32(void); static uint32_t secp256k1_rand32(void);

View File

@ -17,8 +17,8 @@ static secp256k1_rfc6979_hmac_sha256_t secp256k1_test_rng;
static uint32_t secp256k1_test_rng_precomputed[8]; static uint32_t secp256k1_test_rng_precomputed[8];
static int secp256k1_test_rng_precomputed_used = 8; static int secp256k1_test_rng_precomputed_used = 8;
SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v) { SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16) {
secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, (const unsigned char*)"PRNG", 4, (const unsigned char*)&v, sizeof(v)); secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, (const unsigned char*)"TestRNG", 7, seed16, 16);
} }
SECP256K1_INLINE static uint32_t secp256k1_rand32(void) { SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {

View File

@ -1763,7 +1763,8 @@ void run_ecdsa_openssl(void) {
#endif #endif
int main(int argc, char **argv) { int main(int argc, char **argv) {
uint64_t seed; unsigned char seed16[16] = {0};
unsigned char run32[32] = {0};
/* find iteration count */ /* find iteration count */
if (argc > 1) { if (argc > 1) {
count = strtol(argv[1], NULL, 0); count = strtol(argv[1], NULL, 0);
@ -1771,18 +1772,37 @@ int main(int argc, char **argv) {
/* find random seed */ /* find random seed */
if (argc > 2) { if (argc > 2) {
sscanf(argv[2], "%" I64uFORMAT, (unsigned long long*)&seed); int pos = 0;
const char* ch = argv[2];
while (pos < 16 && ch[0] != 0 && ch[1] != 0) {
unsigned short sh;
if (sscanf(ch, "%2hx", &sh)) {
seed16[pos] = sh;
} else {
break;
}
ch += 2;
pos++;
}
} else { } else {
FILE *frand = fopen("/dev/urandom", "r"); FILE *frand = fopen("/dev/urandom", "r");
if (!frand || !fread(&seed, sizeof(seed), 1, frand)) { if (!frand || !fread(&seed16, sizeof(seed16), 1, frand)) {
seed = time(NULL) * 1337; uint64_t t = time(NULL) * (uint64_t)1337;
seed16[0] ^= t;
seed16[1] ^= t >> 8;
seed16[2] ^= t >> 16;
seed16[3] ^= t >> 24;
seed16[4] ^= t >> 32;
seed16[5] ^= t >> 40;
seed16[6] ^= t >> 48;
seed16[7] ^= t >> 56;
} }
fclose(frand); fclose(frand);
} }
secp256k1_rand_seed(seed); secp256k1_rand_seed(seed16);
printf("test count = %i\n", count); printf("test count = %i\n", count);
printf("random seed = %" I64uFORMAT "\n", (unsigned long long)seed); printf("random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", seed16[0], seed16[1], seed16[2], seed16[3], seed16[4], seed16[5], seed16[6], seed16[7], seed16[8], seed16[9], seed16[10], seed16[11], seed16[12], seed16[13], seed16[14], seed16[15]);
/* initialize */ /* initialize */
secp256k1_start(SECP256K1_START_SIGN | SECP256K1_START_VERIFY); secp256k1_start(SECP256K1_START_SIGN | SECP256K1_START_VERIFY);
@ -1828,7 +1848,8 @@ int main(int argc, char **argv) {
run_ecdsa_openssl(); run_ecdsa_openssl();
#endif #endif
printf("random run = %llu\n", (unsigned long long)secp256k1_rand32() + ((unsigned long long)secp256k1_rand32() << 32)); secp256k1_rand256(run32);
printf("random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]);
/* shutdown */ /* shutdown */
secp256k1_stop(); secp256k1_stop();