Skip to content

Commit bd159ad

Browse files
committed
Use Squares RNG instead of RFC6979 for tests
1 parent 09971a3 commit bd159ad

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

src/modules/schnorrsig/tests_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void run_nonce_function_bip340_tests(void) {
8787
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, NULL, 0, NULL) == 0);
8888
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, algo, algolen, NULL) == 1);
8989
/* Other algo is fine */
90-
secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, algo, algolen);
90+
secp256k1_testrand_bytes_test(algo, algolen);
9191
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, algo, algolen, NULL) == 1);
9292

9393
for (i = 0; i < count; i++) {

src/testrand_impl.h

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,44 @@
1414
#include "testrand.h"
1515
#include "hash.h"
1616

17-
static secp256k1_rfc6979_hmac_sha256 secp256k1_test_rng;
18-
static uint32_t secp256k1_test_rng_precomputed[8];
19-
static int secp256k1_test_rng_precomputed_used = 8;
17+
static uint64_t secp256k1_test_rng_key1, secp256k1_test_rng_key2;
18+
static uint64_t secp256k1_test_rng_cnt = 1;
2019
static uint64_t secp256k1_test_rng_integer;
2120
static int secp256k1_test_rng_integer_bits_left = 0;
2221

23-
SECP256K1_INLINE static void secp256k1_testrand_seed(const unsigned char *seed16) {
24-
secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, seed16, 16);
22+
SECP256K1_INLINE static uint32_t secp256k1_testrand32(void) {
23+
/* RNG based on https://arxiv.org/abs/2004.06278, using two separate keys, and
24+
* only using odd counters to avoid entropy loss of key1. */
25+
uint64_t x, y, z;
26+
y = x = secp256k1_test_rng_cnt * secp256k1_test_rng_key1;
27+
z = y + secp256k1_test_rng_key2;
28+
secp256k1_test_rng_cnt += 2;
29+
30+
x = x*x + y; x = (x>>32) | (x<<32); /* round 1 */
31+
x = x*x + z; x = (x>>32) | (x<<32); /* round 2 */
32+
x = x*x + y; x = (x>>32) | (x<<32); /* round 3 */
33+
return (x*x + z) >> 32; /* round 4 */
2534
}
2635

27-
SECP256K1_INLINE static uint32_t secp256k1_testrand32(void) {
28-
if (secp256k1_test_rng_precomputed_used == 8) {
29-
secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, (unsigned char*)(&secp256k1_test_rng_precomputed[0]), sizeof(secp256k1_test_rng_precomputed));
30-
secp256k1_test_rng_precomputed_used = 0;
36+
SECP256K1_INLINE static void secp256k1_testrand_seed(const unsigned char *seed16) {
37+
static const unsigned char PREFIX[19] = "secp256k1 RNG init";
38+
unsigned char out32[32];
39+
int i;
40+
41+
secp256k1_sha256 hash;
42+
secp256k1_sha256_initialize(&hash);
43+
secp256k1_sha256_write(&hash, PREFIX, sizeof(PREFIX));
44+
secp256k1_sha256_write(&hash, seed16, 16);
45+
secp256k1_sha256_finalize(&hash, out32);
46+
47+
secp256k1_test_rng_key1 = 0;
48+
secp256k1_test_rng_key2 = 0;
49+
for (i = 0; i < 8; ++i) {
50+
secp256k1_test_rng_key1 = (secp256k1_test_rng_key1 << 8) | out32[i];
51+
secp256k1_test_rng_key2 = (secp256k1_test_rng_key2 << 8) | out32[i + 8];
3152
}
32-
return secp256k1_test_rng_precomputed[secp256k1_test_rng_precomputed_used++];
53+
secp256k1_test_rng_cnt = 0;
54+
secp256k1_test_rng_integer_bits_left = 0;
3355
}
3456

3557
static uint32_t secp256k1_testrand_bits(int bits) {
@@ -85,7 +107,15 @@ static uint32_t secp256k1_testrand_int(uint32_t range) {
85107
}
86108

87109
static void secp256k1_testrand256(unsigned char *b32) {
88-
secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, b32, 32);
110+
int i;
111+
for (i = 0; i < 8; ++i) {
112+
uint32_t val = secp256k1_testrand32();
113+
b32[0] = val;
114+
b32[1] = val >> 8;
115+
b32[2] = val >> 16;
116+
b32[3] = val >> 24;
117+
b32 += 4;
118+
}
89119
}
90120

91121
static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len) {
@@ -109,7 +139,7 @@ static void secp256k1_testrand256_test(unsigned char *b32) {
109139
}
110140

111141
static void secp256k1_testrand_flip(unsigned char *b, size_t len) {
112-
b[secp256k1_testrand_int(len)] ^= (1 << secp256k1_testrand_int(8));
142+
b[secp256k1_testrand_int(len)] ^= (1 << secp256k1_testrand_bits(3));
113143
}
114144

115145
static void secp256k1_testrand_init(const char* hexseed) {

0 commit comments

Comments
 (0)