Skip to content

Commit d268b56

Browse files
author
ZhePang
committed
added new hash tags specifically for schnorr adaptor
1 parent 9c60058 commit d268b56

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

src/modules/schnorr_adaptor/main_impl.h

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,42 @@
1111
#include "../../../include/secp256k1_schnorr_adaptor.h"
1212
#include "../../hash.h"
1313

14+
/* Initializes SHA256 with fixed midstate. This midstate was computed by applying
15+
* SHA256 to SHA256("SchnorrAdaptor/nonce")||SHA256("SchnorrAdaptor/nonce"). */
16+
static void secp256k1_adaptor_nonce_function_bip340_sha256_tagged(secp256k1_sha256 *sha) {
17+
secp256k1_sha256_initialize(sha);
18+
sha->s[0] = 0xe268ac2aul;
19+
sha->s[1] = 0x3a221b84ul;
20+
sha->s[2] = 0x69612afdul;
21+
sha->s[3] = 0x92ce3040ul;
22+
sha->s[4] = 0xc83ca35ful;
23+
sha->s[5] = 0xec2ee152ul;
24+
sha->s[6] = 0xba136ab7ul;
25+
sha->s[7] = 0x3bf6ec7ful;
26+
27+
sha->bytes = 64;
28+
}
29+
30+
/* Initializes SHA256 with fixed midstate. This midstate was computed by applying
31+
* SHA256 to SHA256("SchnorrAdaptor/aux")||SHA256("SchnorrAdaptor/aux"). */
32+
static void secp256k1_adaptor_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 *sha) {
33+
secp256k1_sha256_initialize(sha);
34+
sha->s[0] = 0x60c4ec6dul;
35+
sha->s[1] = 0x2fc91363ul;
36+
sha->s[2] = 0xce54f4a5ul;
37+
sha->s[3] = 0x962e1565ul;
38+
sha->s[4] = 0x2b5da649ul;
39+
sha->s[5] = 0x6ba94748ul;
40+
sha->s[6] = 0x456c70adul;
41+
sha->s[7] = 0x842cbaddul;
42+
43+
sha->bytes = 64;
44+
}
45+
46+
/* algo argument for adaptor_nonce_function_bip340 to derive the nonce of Schnorr adaptor signature
47+
* by using the correct tagged hash function. */
48+
static const unsigned char adaptor_bip340_algo[20] = "SchnorrAdaptor/nonce";
49+
1450
static int adaptor_nonce_function_bip340(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *t33, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) {
1551
secp256k1_sha256 sha;
1652
unsigned char masked_key[32];
@@ -21,19 +57,19 @@ static int adaptor_nonce_function_bip340(unsigned char *nonce32, const unsigned
2157
}
2258

2359
if (data != NULL) {
24-
secp256k1_nonce_function_bip340_sha256_tagged_aux(&sha);
60+
secp256k1_adaptor_nonce_function_bip340_sha256_tagged_aux(&sha);
2561
secp256k1_sha256_write(&sha, data, 32);
2662
secp256k1_sha256_finalize(&sha, masked_key);
2763
for (i = 0; i < 32; i++) {
2864
masked_key[i] ^= key32[i];
2965
}
3066
} else {
31-
/* Precomputed TaggedHash("BIP0340/aux", 0x0000...00); */
67+
/* Precomputed TaggedHash("SchnorrAdaptor/aux", 0x0000...00); */
3268
static const unsigned char ZERO_MASK[32] = {
33-
84, 241, 105, 207, 201, 226, 229, 114,
34-
116, 128, 68, 31, 144, 186, 37, 196,
35-
136, 244, 97, 199, 11, 94, 165, 220,
36-
170, 247, 175, 105, 39, 10, 165, 20
69+
65, 206, 231, 5, 44, 99, 30, 162,
70+
119, 101, 143, 108, 176, 134, 217, 23,
71+
54, 150, 157, 221, 198, 161, 164, 85,
72+
235, 82, 28, 56, 164, 220, 113, 53
3773
};
3874
for (i = 0; i < 32; i++) {
3975
masked_key[i] = key32[i] ^ ZERO_MASK[i];
@@ -43,9 +79,9 @@ static int adaptor_nonce_function_bip340(unsigned char *nonce32, const unsigned
4379
/* Tag the hash with algo which is important to avoid nonce reuse across
4480
* algorithms. If this nonce function is used in BIP-340 signing as defined
4581
* in the spec, an optimized tagging implementation is used. */
46-
if (algolen == sizeof(bip340_algo)
47-
&& secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
48-
secp256k1_nonce_function_bip340_sha256_tagged(&sha);
82+
if (algolen == sizeof(adaptor_bip340_algo)
83+
&& secp256k1_memcmp_var(algo, adaptor_bip340_algo, algolen) == 0) {
84+
secp256k1_adaptor_nonce_function_bip340_sha256_tagged(&sha);
4985
} else {
5086
secp256k1_sha256_initialize_tagged(&sha, algo, algolen);
5187
}
@@ -221,6 +257,7 @@ int secp256k1_schnorr_adaptor_adapt(const secp256k1_context *ctx, unsigned char
221257
VERIFY_CHECK(ctx != NULL);
222258
ARG_CHECK(sig64 != NULL);
223259
ARG_CHECK(sig65 != NULL);
260+
ARG_CHECK(sig65[0] == SECP256K1_TAG_PUBKEY_EVEN || sig65[0] == SECP256K1_TAG_PUBKEY_ODD);
224261
ARG_CHECK(t32 != NULL);
225262

226263
/* s0 */
@@ -236,8 +273,6 @@ int secp256k1_schnorr_adaptor_adapt(const secp256k1_context *ctx, unsigned char
236273
} else if (sig65[0] == SECP256K1_TAG_PUBKEY_ODD) {
237274
secp256k1_scalar_negate(&t, &t);
238275
secp256k1_scalar_add(&s, &s0, &t);
239-
} else {
240-
ret = 0;
241276
}
242277

243278
memcpy(sig64, &sig65[1], 32);
@@ -259,6 +294,7 @@ int secp256k1_schnorr_adaptor_extract_adaptor(const secp256k1_context *ctx, unsi
259294
VERIFY_CHECK(ctx != NULL);
260295
ARG_CHECK(t32 != NULL);
261296
ARG_CHECK(sig65 != NULL);
297+
ARG_CHECK(sig65[0] == SECP256K1_TAG_PUBKEY_EVEN || sig65[0] == SECP256K1_TAG_PUBKEY_ODD);
262298
ARG_CHECK(sig64 != NULL);
263299

264300
/* s0 */
@@ -275,8 +311,6 @@ int secp256k1_schnorr_adaptor_extract_adaptor(const secp256k1_context *ctx, unsi
275311
} else if (sig65[0] == SECP256K1_TAG_PUBKEY_ODD) {
276312
secp256k1_scalar_negate(&s, &s);
277313
secp256k1_scalar_add(&t, &s0, &s);
278-
} else {
279-
ret = 0;
280314
}
281315

282316
secp256k1_scalar_get_b32(t32, &t);

src/modules/schnorr_adaptor/tests_impl.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ void adaptor_nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip,
2121
}
2222

2323
void run_adaptor_nonce_function_bip340_tests(void) {
24-
unsigned char tag[13] = "BIP0340/nonce";
25-
unsigned char aux_tag[11] = "BIP0340/aux";
26-
unsigned char algo[13] = "BIP0340/nonce";
24+
unsigned char tag[20] = "SchnorrAdaptor/nonce";
25+
unsigned char aux_tag[18] = "SchnorrAdaptor/aux";
26+
unsigned char algo[20] = "SchnorrAdaptor/nonce";
2727
size_t algolen = sizeof(algo);
2828
secp256k1_sha256 sha;
2929
secp256k1_sha256 sha_optimized;
@@ -45,14 +45,14 @@ void run_adaptor_nonce_function_bip340_tests(void) {
4545
* secp256k1_nonce_function_bip340_sha256_tagged has the expected
4646
* state. */
4747
secp256k1_sha256_initialize_tagged(&sha, tag, sizeof(tag));
48-
secp256k1_nonce_function_bip340_sha256_tagged(&sha_optimized);
48+
secp256k1_adaptor_nonce_function_bip340_sha256_tagged(&sha_optimized);
4949
test_sha256_eq(&sha, &sha_optimized);
5050

5151
/* Check that hash initialized by
5252
* secp256k1_nonce_function_bip340_sha256_tagged_aux has the expected
5353
* state. */
5454
secp256k1_sha256_initialize_tagged(&sha, aux_tag, sizeof(aux_tag));
55-
secp256k1_nonce_function_bip340_sha256_tagged_aux(&sha_optimized);
55+
secp256k1_adaptor_nonce_function_bip340_sha256_tagged_aux(&sha_optimized);
5656
test_sha256_eq(&sha, &sha_optimized);
5757

5858
secp256k1_testrand256(msg);
@@ -78,7 +78,7 @@ void run_adaptor_nonce_function_bip340_tests(void) {
7878
adaptor_nonce_function_bip340_bitflip(args, 1, 32, algolen);
7979
adaptor_nonce_function_bip340_bitflip(args, 2, 32, algolen);
8080
adaptor_nonce_function_bip340_bitflip(args, 3, 32, algolen);
81-
/* Flip algo special case "BIP0340/nonce" */
81+
/* Flip algo special case "SchnorrAdaptor/nonce" */
8282
adaptor_nonce_function_bip340_bitflip(args, 4, algolen, algolen);
8383
/* Flip algo again */
8484
adaptor_nonce_function_bip340_bitflip(args, 4, algolen, algolen);

0 commit comments

Comments
 (0)