Skip to content

Commit 8956111

Browse files
committed
use 128-bit hex seed
1 parent 02efd06 commit 8956111

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 *seed16);
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
@@ -1763,26 +1763,46 @@ void run_ecdsa_openssl(void) {
17631763
#endif
17641764

17651765
int main(int argc, char **argv) {
1766-
uint64_t seed;
1766+
unsigned char seed16[16] = {0};
1767+
unsigned char run32[32] = {0};
17671768
/* find iteration count */
17681769
if (argc > 1) {
17691770
count = strtol(argv[1], NULL, 0);
17701771
}
17711772

17721773
/* find random seed */
17731774
if (argc > 2) {
1774-
sscanf(argv[2], "%" I64uFORMAT, (unsigned long long*)&seed);
1775+
int pos = 0;
1776+
const char* ch = argv[2];
1777+
while (pos < 16 && ch[0] != 0 && ch[1] != 0) {
1778+
unsigned short sh;
1779+
if (sscanf(ch, "%2hx", &sh)) {
1780+
seed16[pos] = sh;
1781+
} else {
1782+
break;
1783+
}
1784+
ch += 2;
1785+
pos++;
1786+
}
17751787
} else {
17761788
FILE *frand = fopen("/dev/urandom", "r");
1777-
if (!frand || !fread(&seed, sizeof(seed), 1, frand)) {
1778-
seed = time(NULL) * 1337;
1789+
if (!frand || !fread(&seed16, sizeof(seed16), 1, frand)) {
1790+
uint64_t t = time(NULL) * (uint64_t)1337;
1791+
seed16[0] ^= t;
1792+
seed16[1] ^= t >> 8;
1793+
seed16[2] ^= t >> 16;
1794+
seed16[3] ^= t >> 24;
1795+
seed16[4] ^= t >> 32;
1796+
seed16[5] ^= t >> 40;
1797+
seed16[6] ^= t >> 48;
1798+
seed16[7] ^= t >> 56;
17791799
}
17801800
fclose(frand);
17811801
}
1782-
secp256k1_rand_seed(seed);
1802+
secp256k1_rand_seed(seed16);
17831803

17841804
printf("test count = %i\n", count);
1785-
printf("random seed = %" I64uFORMAT "\n", (unsigned long long)seed);
1805+
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]);
17861806

17871807
/* initialize */
17881808
secp256k1_start(SECP256K1_START_SIGN | SECP256K1_START_VERIFY);
@@ -1828,7 +1848,8 @@ int main(int argc, char **argv) {
18281848
run_ecdsa_openssl();
18291849
#endif
18301850

1831-
printf("random run = %llu\n", (unsigned long long)secp256k1_rand32() + ((unsigned long long)secp256k1_rand32() << 32));
1851+
secp256k1_rand256(run32);
1852+
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]);
18321853

18331854
/* shutdown */
18341855
secp256k1_stop();

0 commit comments

Comments
 (0)