|
10 | 10 | #include "../../../include/secp256k1.h"
|
11 | 11 | #include "../../../include/secp256k1_schnorr_adaptor.h"
|
12 | 12 |
|
| 13 | +#include "../../hash.h" |
| 14 | +#include "../../scalar.h" |
| 15 | + |
| 16 | +/* Initializes SHA256 with fixed midstate. This midstate was computed by applying |
| 17 | + * SHA256 to SHA256("SchnorrAdaptor/nonce")||SHA256("SchnorrAdaptor/nonce"). */ |
| 18 | +static void secp256k1_nonce_function_schnorr_adaptor_sha256_tagged(secp256k1_sha256 *sha) { |
| 19 | + secp256k1_sha256_initialize(sha); |
| 20 | + sha->s[0] = 0xe268ac2aul; |
| 21 | + sha->s[1] = 0x3a221b84ul; |
| 22 | + sha->s[2] = 0x69612afdul; |
| 23 | + sha->s[3] = 0x92ce3040ul; |
| 24 | + sha->s[4] = 0xc83ca35ful; |
| 25 | + sha->s[5] = 0xec2ee152ul; |
| 26 | + sha->s[6] = 0xba136ab7ul; |
| 27 | + sha->s[7] = 0x3bf6ec7ful; |
| 28 | + |
| 29 | + sha->bytes = 64; |
| 30 | +} |
| 31 | + |
| 32 | +/* Initializes SHA256 with fixed midstate. This midstate was computed by applying |
| 33 | + * SHA256 to SHA256("SchnorrAdaptor/aux")||SHA256("SchnorrAdaptor/aux"). */ |
| 34 | +static void secp256k1_nonce_function_schnorr_adaptor_sha256_tagged_aux(secp256k1_sha256 *sha) { |
| 35 | + secp256k1_sha256_initialize(sha); |
| 36 | + sha->s[0] = 0x50685e98ul; |
| 37 | + sha->s[1] = 0x6313905eul; |
| 38 | + sha->s[2] = 0x6db24fa0ul; |
| 39 | + sha->s[3] = 0xc8b15c48ul; |
| 40 | + sha->s[4] = 0x6b318921ul; |
| 41 | + sha->s[5] = 0x441d8ff3ul; |
| 42 | + sha->s[6] = 0xa7033a66ul; |
| 43 | + sha->s[7] = 0xc3545cddul; |
| 44 | + |
| 45 | + sha->bytes = 64; |
| 46 | +} |
| 47 | + |
| 48 | +/* algo argument for `nonce_function_schnorr_adaptor` to derive the nonce using a tagged hash function. */ |
| 49 | +static const unsigned char schnorr_adaptor_algo[20] = "SchnorrAdaptor/nonce"; |
| 50 | + |
| 51 | +/* Modified BIP-340 nonce function */ |
| 52 | +static int nonce_function_schnorr_adaptor(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *adaptor33, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) { |
| 53 | + secp256k1_sha256 sha; |
| 54 | + unsigned char masked_key[32]; |
| 55 | + int i; |
| 56 | + |
| 57 | + if (algo == NULL) { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
| 61 | + if (data != NULL) { |
| 62 | + secp256k1_nonce_function_schnorr_adaptor_sha256_tagged_aux(&sha); |
| 63 | + secp256k1_sha256_write(&sha, data, 32); |
| 64 | + secp256k1_sha256_finalize(&sha, masked_key); |
| 65 | + for (i = 0; i < 32; i++) { |
| 66 | + masked_key[i] ^= key32[i]; |
| 67 | + } |
| 68 | + } else { |
| 69 | + /* Precomputed TaggedHash("SchnorrAdaptor/aux", 0x0000...00); */ |
| 70 | + static const unsigned char ZERO_MASK[32] = { |
| 71 | + 65, 206, 231, 5, 44, 99, 30, 162, |
| 72 | + 119, 101, 143, 108, 176, 134, 217, 23, |
| 73 | + 54, 150, 157, 221, 198, 161, 164, 85, |
| 74 | + 235, 82, 28, 56, 164, 220, 113, 53 |
| 75 | + }; |
| 76 | + for (i = 0; i < 32; i++) { |
| 77 | + masked_key[i] = key32[i] ^ ZERO_MASK[i]; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /* Tag the hash with algo which is important to avoid nonce reuse across |
| 82 | + * algorithms. An optimized tagging implementation is used if the default |
| 83 | + * tag is provided. */ |
| 84 | + if (algolen == sizeof(schnorr_adaptor_algo) |
| 85 | + && secp256k1_memcmp_var(algo, schnorr_adaptor_algo, algolen) == 0) { |
| 86 | + secp256k1_nonce_function_schnorr_adaptor_sha256_tagged(&sha); |
| 87 | + } else { |
| 88 | + secp256k1_sha256_initialize_tagged(&sha, algo, algolen); |
| 89 | + } |
| 90 | + |
| 91 | + /* Hash masked-key||adaptor33||pk||msg using the tagged hash */ |
| 92 | + secp256k1_sha256_write(&sha, masked_key, 32); |
| 93 | + secp256k1_sha256_write(&sha, adaptor33, 33); |
| 94 | + secp256k1_sha256_write(&sha, xonly_pk32, 32); |
| 95 | + secp256k1_sha256_write(&sha, msg32, 32); |
| 96 | + secp256k1_sha256_finalize(&sha, nonce32); |
| 97 | + return 1; |
| 98 | +} |
| 99 | + |
| 100 | +const secp256k1_nonce_function_hardened_schnorr_adaptor secp256k1_nonce_function_schnorr_adaptor = nonce_function_schnorr_adaptor; |
| 101 | + |
13 | 102 | #endif
|
0 commit comments