Skip to content

Commit 8fa4120

Browse files
Merge bitcoin-core/secp256k1#1002: Make aux_rnd32==NULL behave identical to 0x0000..00.
5324f89 Make aux_rnd32==NULL behave identical to 0x0000..00. (Pieter Wuille) Pull request description: BIP340's default signing algorithm always requires an aux_rnd argument, but permits using an all-zero one when no randomness is available. Make secp256k1_schnorrsig_sign follow this even when aux_rnd32==NULL, by treating the same as if an all-zero byte array was provided as input. ACKs for top commit: junderw: ACK 5324f89 elichai: ACK 5324f89 jonasnick: ACK 5324f89 real-or-random: utACK 5324f89 Tree-SHA512: caa1d5a0eacea3239d8aaace5284eedcd850058bbe759768e626233a010199db6c637618aedccfb51fe94ec8d28f45bc0c441be77e2e12fa2a393b9cc3a5d3ae
2 parents 21c188b + 5324f89 commit 8fa4120

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

include/secp256k1_schnorrsig.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ typedef struct {
111111
* In: msg32: the 32-byte message being signed.
112112
* keypair: pointer to an initialized keypair.
113113
* aux_rand32: 32 bytes of fresh randomness. While recommended to provide
114-
* this, it is only supplemental to security and can be NULL. See
114+
* this, it is only supplemental to security and can be NULL. A
115+
* NULL argument is treated the same as an all-zero one. See
115116
* BIP-340 "Default Signing" for a full explanation of this
116117
* argument and for guidance if randomness is expensive.
117118
*/

src/modules/schnorrsig/main_impl.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms
6565
for (i = 0; i < 32; i++) {
6666
masked_key[i] ^= key32[i];
6767
}
68+
} else {
69+
/* Precomputed TaggedHash("BIP0340/aux", 0x0000...00); */
70+
static const unsigned char ZERO_MASK[32] = {
71+
84, 241, 105, 207, 201, 226, 229, 114,
72+
116, 128, 68, 31, 144, 186, 37, 196,
73+
136, 244, 97, 199, 11, 94, 165, 220,
74+
170, 247, 175, 105, 39, 10, 165, 20
75+
};
76+
for (i = 0; i < 32; i++) {
77+
masked_key[i] = key32[i] ^ ZERO_MASK[i];
78+
}
6879
}
6980

7081
/* Tag the hash with algo which is important to avoid nonce reuse across
@@ -77,12 +88,8 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms
7788
secp256k1_sha256_initialize_tagged(&sha, algo, algolen);
7889
}
7990

80-
/* Hash (masked-)key||pk||msg using the tagged hash as per the spec */
81-
if (data != NULL) {
82-
secp256k1_sha256_write(&sha, masked_key, 32);
83-
} else {
84-
secp256k1_sha256_write(&sha, key32, 32);
85-
}
91+
/* Hash masked-key||pk||msg using the tagged hash as per the spec */
92+
secp256k1_sha256_write(&sha, masked_key, 32);
8693
secp256k1_sha256_write(&sha, xonly_pk32, 32);
8794
secp256k1_sha256_write(&sha, msg, msglen);
8895
secp256k1_sha256_finalize(&sha, nonce32);

src/modules/schnorrsig/tests_impl.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void run_nonce_function_bip340_tests(void) {
3838
size_t algolen = sizeof(algo);
3939
secp256k1_sha256 sha;
4040
secp256k1_sha256 sha_optimized;
41-
unsigned char nonce[32];
41+
unsigned char nonce[32], nonce_z[32];
4242
unsigned char msg[32];
4343
size_t msglen = sizeof(msg);
4444
unsigned char key[32];
@@ -107,8 +107,11 @@ void run_nonce_function_bip340_tests(void) {
107107
CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0);
108108
}
109109

110-
/* NULL aux_rand argument is allowed. */
110+
/* NULL aux_rand argument is allowed, and identical to passing all zero aux_rand. */
111+
memset(aux_rand, 0, 32);
112+
CHECK(nonce_function_bip340(nonce_z, msg, msglen, key, pk, algo, algolen, &aux_rand) == 1);
111113
CHECK(nonce_function_bip340(nonce, msg, msglen, key, pk, algo, algolen, NULL) == 1);
114+
CHECK(secp256k1_memcmp_var(nonce_z, nonce, 32) == 0);
112115
}
113116

114117
void test_schnorrsig_api(void) {

0 commit comments

Comments
 (0)