11
11
#include "../../../include/secp256k1_schnorr_adaptor.h"
12
12
#include "../../hash.h"
13
13
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
+
14
50
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 ) {
15
51
secp256k1_sha256 sha ;
16
52
unsigned char masked_key [32 ];
@@ -21,19 +57,19 @@ static int adaptor_nonce_function_bip340(unsigned char *nonce32, const unsigned
21
57
}
22
58
23
59
if (data != NULL ) {
24
- secp256k1_nonce_function_bip340_sha256_tagged_aux (& sha );
60
+ secp256k1_adaptor_nonce_function_bip340_sha256_tagged_aux (& sha );
25
61
secp256k1_sha256_write (& sha , data , 32 );
26
62
secp256k1_sha256_finalize (& sha , masked_key );
27
63
for (i = 0 ; i < 32 ; i ++ ) {
28
64
masked_key [i ] ^= key32 [i ];
29
65
}
30
66
} else {
31
- /* Precomputed TaggedHash("BIP0340 /aux", 0x0000...00); */
67
+ /* Precomputed TaggedHash("SchnorrAdaptor /aux", 0x0000...00); */
32
68
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
37
73
};
38
74
for (i = 0 ; i < 32 ; i ++ ) {
39
75
masked_key [i ] = key32 [i ] ^ ZERO_MASK [i ];
@@ -43,9 +79,9 @@ static int adaptor_nonce_function_bip340(unsigned char *nonce32, const unsigned
43
79
/* Tag the hash with algo which is important to avoid nonce reuse across
44
80
* algorithms. If this nonce function is used in BIP-340 signing as defined
45
81
* 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 );
49
85
} else {
50
86
secp256k1_sha256_initialize_tagged (& sha , algo , algolen );
51
87
}
@@ -221,6 +257,7 @@ int secp256k1_schnorr_adaptor_adapt(const secp256k1_context *ctx, unsigned char
221
257
VERIFY_CHECK (ctx != NULL );
222
258
ARG_CHECK (sig64 != NULL );
223
259
ARG_CHECK (sig65 != NULL );
260
+ ARG_CHECK (sig65 [0 ] == SECP256K1_TAG_PUBKEY_EVEN || sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD );
224
261
ARG_CHECK (t32 != NULL );
225
262
226
263
/* s0 */
@@ -236,8 +273,6 @@ int secp256k1_schnorr_adaptor_adapt(const secp256k1_context *ctx, unsigned char
236
273
} else if (sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD ) {
237
274
secp256k1_scalar_negate (& t , & t );
238
275
secp256k1_scalar_add (& s , & s0 , & t );
239
- } else {
240
- ret = 0 ;
241
276
}
242
277
243
278
memcpy (sig64 , & sig65 [1 ], 32 );
@@ -259,6 +294,7 @@ int secp256k1_schnorr_adaptor_extract_adaptor(const secp256k1_context *ctx, unsi
259
294
VERIFY_CHECK (ctx != NULL );
260
295
ARG_CHECK (t32 != NULL );
261
296
ARG_CHECK (sig65 != NULL );
297
+ ARG_CHECK (sig65 [0 ] == SECP256K1_TAG_PUBKEY_EVEN || sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD );
262
298
ARG_CHECK (sig64 != NULL );
263
299
264
300
/* s0 */
@@ -275,8 +311,6 @@ int secp256k1_schnorr_adaptor_extract_adaptor(const secp256k1_context *ctx, unsi
275
311
} else if (sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD ) {
276
312
secp256k1_scalar_negate (& s , & s );
277
313
secp256k1_scalar_add (& t , & s0 , & s );
278
- } else {
279
- ret = 0 ;
280
314
}
281
315
282
316
secp256k1_scalar_get_b32 (t32 , & t );
0 commit comments