Skip to content

Commit 0b84d51

Browse files
committed
use 128-bit hex seed
1 parent cf21cdc commit 0b84d51

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/testrand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/* A non-cryptographic RNG used only for test infrastructure. */
1515

1616
/** Seed the pseudorandom number generator for testing. */
17-
SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v);
17+
SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed32);
1818

1919
/** Generate a pseudorandom 32-bit number. */
2020
static uint32_t secp256k1_rand32(void);

src/testrand_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ static secp256k1_rfc6979_hmac_sha256_t secp256k1_test_rng;
1717
static uint32_t secp256k1_test_rng_precomputed[8];
1818
static int secp256k1_test_rng_precomputed_used = 8;
1919

20-
SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v) {
21-
secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, (const unsigned char*)"PRNG", 4, (const unsigned char*)&v, sizeof(v));
20+
SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16) {
21+
secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, (const unsigned char*)"TestRNG", 7, seed16, 16);
2222
}
2323

2424
SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {

src/tests.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,26 +1733,46 @@ void run_ecdsa_openssl(void) {
17331733
#endif
17341734

17351735
int main(int argc, char **argv) {
1736-
uint64_t seed;
1736+
unsigned char seed16[16] = {0};
1737+
unsigned char run32[32] = {0};
17371738
/* find iteration count */
17381739
if (argc > 1) {
17391740
count = strtol(argv[1], NULL, 0);
17401741
}
17411742

17421743
/* find random seed */
17431744
if (argc > 2) {
1744-
sscanf(argv[2], "%" I64uFORMAT, (unsigned long long*)&seed);
1745+
int pos = 0;
1746+
const char* ch = argv[2];
1747+
while (pos < 16 && *ch != 0) {
1748+
unsigned short sh;
1749+
if (sscanf(ch, "%2hx", &sh)) {
1750+
seed16[pos] = sh;
1751+
} else {
1752+
break;
1753+
}
1754+
ch += 2;
1755+
pos++;
1756+
}
17451757
} else {
17461758
FILE *frand = fopen("/dev/urandom", "r");
1747-
if (!frand || !fread(&seed, sizeof(seed), 1, frand)) {
1748-
seed = time(NULL) * 1337;
1759+
if (!frand || !fread(&seed16, sizeof(seed16), 1, frand)) {
1760+
uint64_t t = time(NULL) * (uint64_t)1337;
1761+
seed16[0] ^= t;
1762+
seed16[1] ^= t >> 8;
1763+
seed16[2] ^= t >> 16;
1764+
seed16[3] ^= t >> 24;
1765+
seed16[4] ^= t >> 32;
1766+
seed16[5] ^= t >> 40;
1767+
seed16[6] ^= t >> 48;
1768+
seed16[7] ^= t >> 56;
17491769
}
17501770
fclose(frand);
17511771
}
1752-
secp256k1_rand_seed(seed);
1772+
secp256k1_rand_seed(seed16);
17531773

17541774
printf("test count = %i\n", count);
1755-
printf("random seed = %" I64uFORMAT "\n", (unsigned long long)seed);
1775+
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]);
17561776

17571777
/* initialize */
17581778
secp256k1_start(SECP256K1_START_SIGN | SECP256K1_START_VERIFY);
@@ -1798,7 +1818,8 @@ int main(int argc, char **argv) {
17981818
run_ecdsa_openssl();
17991819
#endif
18001820

1801-
printf("random run = %llu\n", (unsigned long long)secp256k1_rand32() + ((unsigned long long)secp256k1_rand32() << 32));
1821+
secp256k1_rand256(run32);
1822+
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]);
18021823

18031824
/* shutdown */
18041825
secp256k1_stop();

0 commit comments

Comments
 (0)