Skip to content

Commit f789c5b

Browse files
committed
Merge pull request #215
8956111 use 128-bit hex seed (Pieter Wuille) 02efd06 Use RFC6979 for test PRNGs (Pieter Wuille)
2 parents 4bc273b + 8956111 commit f789c5b

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
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: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,44 @@
1111
#include <string.h>
1212

1313
#include "testrand.h"
14+
#include "hash.h"
1415

15-
static uint32_t secp256k1_Rz = 11, secp256k1_Rw = 11;
16+
static secp256k1_rfc6979_hmac_sha256_t secp256k1_test_rng;
17+
static uint32_t secp256k1_test_rng_precomputed[8];
18+
static int secp256k1_test_rng_precomputed_used = 8;
1619

17-
SECP256K1_INLINE static void secp256k1_rand_seed(uint64_t v) {
18-
secp256k1_Rz = v >> 32;
19-
secp256k1_Rw = v;
20-
21-
/* There are two seeds with short (length 1) cycles for the Rz PRNG. */
22-
if (secp256k1_Rz == 0 || secp256k1_Rz == 0x9068ffffU) {
23-
secp256k1_Rz = 111;
24-
}
25-
/* There are four seeds with short (length 1) cycles for the Rw PRNG. */
26-
if (secp256k1_Rw == 0 || secp256k1_Rw == 0x464fffffU ||
27-
secp256k1_Rw == 0x8c9ffffeU || secp256k1_Rw == 0xd2effffdU) {
28-
secp256k1_Rw = 111;
29-
}
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);
3022
}
3123

3224
SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {
33-
/* MWC PRNG for tests. */
34-
secp256k1_Rz = 36969 * (secp256k1_Rz & 0xFFFF) + (secp256k1_Rz >> 16);
35-
secp256k1_Rw = 18000 * (secp256k1_Rw & 0xFFFF) + (secp256k1_Rw >> 16);
36-
return (secp256k1_Rw << 16) + (secp256k1_Rw >> 16) + secp256k1_Rz;
25+
if (secp256k1_test_rng_precomputed_used == 8) {
26+
secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, (unsigned char*)(&secp256k1_test_rng_precomputed[0]), sizeof(secp256k1_test_rng_precomputed));
27+
secp256k1_test_rng_precomputed_used = 0;
28+
}
29+
return secp256k1_test_rng_precomputed[secp256k1_test_rng_precomputed_used++];
3730
}
3831

3932
static void secp256k1_rand256(unsigned char *b32) {
40-
int i;
41-
for (i = 0; i < 8; i++) {
42-
uint32_t r = secp256k1_rand32();
43-
b32[i*4 + 0] = (r >> 0) & 0xFF;
44-
b32[i*4 + 1] = (r >> 8) & 0xFF;
45-
b32[i*4 + 2] = (r >> 16) & 0xFF;
46-
b32[i*4 + 3] = (r >> 24) & 0xFF;
47-
}
33+
secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, b32, 32);
4834
}
4935

5036
static void secp256k1_rand256_test(unsigned char *b32) {
5137
int bits=0;
38+
uint64_t ent = 0;
39+
int entleft = 0;
5240
memset(b32, 0, 32);
5341
while (bits < 256) {
54-
uint32_t ent = secp256k1_rand32();
55-
int now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
56-
uint32_t val = 1 & (ent >> 11);
42+
int now;
43+
uint32_t val;
44+
if (entleft < 12) {
45+
ent |= ((uint64_t)secp256k1_rand32()) << entleft;
46+
entleft += 32;
47+
}
48+
now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
49+
val = 1 & (ent >> 11);
50+
ent >>= 12;
51+
entleft -= 12;
5752
while (now > 0 && bits < 256) {
5853
b32[bits / 8] |= val << (bits % 8);
5954
now--;

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)