Bitcoin Core  29.1.0
P2P Digital Currency
main_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2018-2020 Andrew Poelstra, Jonas Nick *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5  ***********************************************************************/
6 
7 #ifndef SECP256K1_MODULE_SCHNORRSIG_MAIN_H
8 #define SECP256K1_MODULE_SCHNORRSIG_MAIN_H
9 
10 #include "../../../include/secp256k1.h"
11 #include "../../../include/secp256k1_schnorrsig.h"
12 #include "../../hash.h"
13 
14 /* Initializes SHA256 with fixed midstate. This midstate was computed by applying
15  * SHA256 to SHA256("BIP0340/nonce")||SHA256("BIP0340/nonce"). */
18  sha->s[0] = 0x46615b35ul;
19  sha->s[1] = 0xf4bfbff7ul;
20  sha->s[2] = 0x9f8dc671ul;
21  sha->s[3] = 0x83627ab3ul;
22  sha->s[4] = 0x60217180ul;
23  sha->s[5] = 0x57358661ul;
24  sha->s[6] = 0x21a29e54ul;
25  sha->s[7] = 0x68b07b4cul;
26 
27  sha->bytes = 64;
28 }
29 
30 /* Initializes SHA256 with fixed midstate. This midstate was computed by applying
31  * SHA256 to SHA256("BIP0340/aux")||SHA256("BIP0340/aux"). */
34  sha->s[0] = 0x24dd3219ul;
35  sha->s[1] = 0x4eba7e70ul;
36  sha->s[2] = 0xca0fabb9ul;
37  sha->s[3] = 0x0fa3166dul;
38  sha->s[4] = 0x3afbe4b1ul;
39  sha->s[5] = 0x4c44df97ul;
40  sha->s[6] = 0x4aac2739ul;
41  sha->s[7] = 0x249e850aul;
42 
43  sha->bytes = 64;
44 }
45 
46 /* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340
47  * by using the correct tagged hash function. */
48 static const unsigned char bip340_algo[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
49 
51 
52 static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *msg, size_t msglen, const unsigned char *key32, 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) {
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("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  }
79  }
80 
81  /* Tag the hash with algo which is important to avoid nonce reuse across
82  * algorithms. If this nonce function is used in BIP-340 signing as defined
83  * in the spec, an optimized tagging implementation is used. */
84  if (algolen == sizeof(bip340_algo)
85  && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
87  } else {
88  secp256k1_sha256_initialize_tagged(&sha, algo, algolen);
89  }
90 
91  /* Hash masked-key||pk||msg using the tagged hash as per the spec */
92  secp256k1_sha256_write(&sha, masked_key, 32);
93  secp256k1_sha256_write(&sha, xonly_pk32, 32);
94  secp256k1_sha256_write(&sha, msg, msglen);
95  secp256k1_sha256_finalize(&sha, nonce32);
97  return 1;
98 }
99 
101 
102 /* Initializes SHA256 with fixed midstate. This midstate was computed by applying
103  * SHA256 to SHA256("BIP0340/challenge")||SHA256("BIP0340/challenge"). */
106  sha->s[0] = 0x9cecba11ul;
107  sha->s[1] = 0x23925381ul;
108  sha->s[2] = 0x11679112ul;
109  sha->s[3] = 0xd1627e0ful;
110  sha->s[4] = 0x97c87550ul;
111  sha->s[5] = 0x003cc765ul;
112  sha->s[6] = 0x90f61164ul;
113  sha->s[7] = 0x33e9b66aul;
114  sha->bytes = 64;
115 }
116 
117 static void secp256k1_schnorrsig_challenge(secp256k1_scalar* e, const unsigned char *r32, const unsigned char *msg, size_t msglen, const unsigned char *pubkey32)
118 {
119  unsigned char buf[32];
120  secp256k1_sha256 sha;
121 
122  /* tagged hash(r.x, pk.x, msg) */
124  secp256k1_sha256_write(&sha, r32, 32);
125  secp256k1_sha256_write(&sha, pubkey32, 32);
126  secp256k1_sha256_write(&sha, msg, msglen);
127  secp256k1_sha256_finalize(&sha, buf);
128  /* Set scalar e to the challenge hash modulo the curve order as per
129  * BIP340. */
130  secp256k1_scalar_set_b32(e, buf, NULL);
131 }
132 
133 static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_keypair *keypair, secp256k1_nonce_function_hardened noncefp, void *ndata) {
134  secp256k1_scalar sk;
137  secp256k1_gej rj;
139  secp256k1_ge r;
140  unsigned char buf[32] = { 0 };
141  unsigned char pk_buf[32];
142  unsigned char seckey[32];
143  int ret = 1;
144 
145  VERIFY_CHECK(ctx != NULL);
147  ARG_CHECK(sig64 != NULL);
148  ARG_CHECK(msg != NULL || msglen == 0);
149  ARG_CHECK(keypair != NULL);
150 
151  if (noncefp == NULL) {
153  }
154 
155  ret &= secp256k1_keypair_load(ctx, &sk, &pk, keypair);
156  /* Because we are signing for a x-only pubkey, the secret key is negated
157  * before signing if the point corresponding to the secret key does not
158  * have an even Y. */
159  if (secp256k1_fe_is_odd(&pk.y)) {
160  secp256k1_scalar_negate(&sk, &sk);
161  }
162 
163  secp256k1_scalar_get_b32(seckey, &sk);
164  secp256k1_fe_get_b32(pk_buf, &pk.x);
165  ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata);
166  secp256k1_scalar_set_b32(&k, buf, NULL);
169 
170  secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &rj, &k);
171  secp256k1_ge_set_gej(&r, &rj);
172 
173  /* We declassify r to allow using it as a branch point. This is fine
174  * because r is not a secret. */
175  secp256k1_declassify(ctx, &r, sizeof(r));
177  if (secp256k1_fe_is_odd(&r.y)) {
179  }
181  secp256k1_fe_get_b32(&sig64[0], &r.x);
182 
183  secp256k1_schnorrsig_challenge(&e, &sig64[0], msg, msglen, pk_buf);
184  secp256k1_scalar_mul(&e, &e, &sk);
185  secp256k1_scalar_add(&e, &e, &k);
186  secp256k1_scalar_get_b32(&sig64[32], &e);
187 
188  secp256k1_memczero(sig64, 64, !ret);
191  secp256k1_memclear(seckey, sizeof(seckey));
192  secp256k1_gej_clear(&rj);
193 
194  return ret;
195 }
196 
197 int secp256k1_schnorrsig_sign32(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32) {
198  /* We cast away const from the passed aux_rand32 argument since we know the default nonce function does not modify it. */
199  return secp256k1_schnorrsig_sign_internal(ctx, sig64, msg32, 32, keypair, secp256k1_nonce_function_bip340, (unsigned char*)aux_rand32);
200 }
201 
202 int secp256k1_schnorrsig_sign(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32) {
203  return secp256k1_schnorrsig_sign32(ctx, sig64, msg32, keypair, aux_rand32);
204 }
205 
206 int secp256k1_schnorrsig_sign_custom(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_keypair *keypair, secp256k1_schnorrsig_extraparams *extraparams) {
207  secp256k1_nonce_function_hardened noncefp = NULL;
208  void *ndata = NULL;
209  VERIFY_CHECK(ctx != NULL);
210 
211  if (extraparams != NULL) {
212  ARG_CHECK(secp256k1_memcmp_var(extraparams->magic,
214  sizeof(extraparams->magic)) == 0);
215  noncefp = extraparams->noncefp;
216  ndata = extraparams->ndata;
217  }
218  return secp256k1_schnorrsig_sign_internal(ctx, sig64, msg, msglen, keypair, noncefp, ndata);
219 }
220 
221 int secp256k1_schnorrsig_verify(const secp256k1_context* ctx, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey) {
224  secp256k1_gej rj;
226  secp256k1_gej pkj;
227  secp256k1_fe rx;
228  secp256k1_ge r;
229  unsigned char buf[32];
230  int overflow;
231 
232  VERIFY_CHECK(ctx != NULL);
233  ARG_CHECK(sig64 != NULL);
234  ARG_CHECK(msg != NULL || msglen == 0);
235  ARG_CHECK(pubkey != NULL);
236 
237  if (!secp256k1_fe_set_b32_limit(&rx, &sig64[0])) {
238  return 0;
239  }
240 
241  secp256k1_scalar_set_b32(&s, &sig64[32], &overflow);
242  if (overflow) {
243  return 0;
244  }
245 
246  if (!secp256k1_xonly_pubkey_load(ctx, &pk, pubkey)) {
247  return 0;
248  }
249 
250  /* Compute e. */
251  secp256k1_fe_get_b32(buf, &pk.x);
252  secp256k1_schnorrsig_challenge(&e, &sig64[0], msg, msglen, buf);
253 
254  /* Compute rj = s*G + (-e)*pkj */
255  secp256k1_scalar_negate(&e, &e);
256  secp256k1_gej_set_ge(&pkj, &pk);
257  secp256k1_ecmult(&rj, &pkj, &e, &s);
258 
259  secp256k1_ge_set_gej_var(&r, &rj);
260  if (secp256k1_ge_is_infinity(&r)) {
261  return 0;
262  }
263 
265  return !secp256k1_fe_is_odd(&r.y) &&
266  secp256k1_fe_equal(&rx, &r.x);
267 }
268 
269 #endif
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Multiply two scalars (modulo the group order).
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
#define VERIFY_CHECK(cond)
Definition: util.h:159
static void secp256k1_nonce_function_bip340_sha256_tagged(secp256k1_sha256 *sha)
Definition: main_impl.h:16
This field implementation represents the value as 10 uint32_t limbs in base 2^26. ...
Definition: field_10x26.h:14
int ret
#define secp256k1_fe_set_b32_limit
Definition: field.h:88
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *a)
Multiply with the generator: R = a*G.
static void secp256k1_schnorrsig_challenge(secp256k1_scalar *e, const unsigned char *r32, const unsigned char *msg, size_t msglen, const unsigned char *pubkey32)
Definition: main_impl.h:117
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal.
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
const secp256k1_nonce_function_hardened secp256k1_nonce_function_bip340
Definition: main_impl.h:100
static const unsigned char schnorrsig_extraparams_magic[4]
Definition: main_impl.h:50
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
int secp256k1_schnorrsig_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32)
Same as secp256k1_schnorrsig_sign32, but DEPRECATED.
Definition: main_impl.h:202
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
static int secp256k1_keypair_load(const secp256k1_context *ctx, secp256k1_scalar *sk, secp256k1_ge *pk, const secp256k1_keypair *keypair)
Definition: main_impl.h:176
static void secp256k1_ecmult(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Double multiply: R = na*A + ng*G.
#define SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC
Data structure that contains additional arguments for schnorrsig_sign_custom.
static int secp256k1_schnorrsig_sign_internal(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_keypair *keypair, secp256k1_nonce_function_hardened noncefp, void *ndata)
Definition: main_impl.h:133
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
Definition: secp256k1.c:236
int secp256k1_schnorrsig_sign_custom(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_keypair *keypair, secp256k1_schnorrsig_extraparams *extraparams)
Create a Schnorr signature with a more flexible API.
Definition: main_impl.h:206
Opaque data structure that holds a parsed and valid "x-only" public key.
uint32_t s[8]
Definition: hash.h:14
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:62
#define ARG_CHECK(cond)
Definition: secp256k1.c:45
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
static void secp256k1_gej_clear(secp256k1_gej *r)
Clear a secp256k1_gej to prevent leaking sensitive information.
#define secp256k1_fe_is_odd
Definition: field.h:85
static void secp256k1_sha256_clear(secp256k1_sha256 *hash)
int secp256k1_schnorrsig_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey)
Verify a Schnorr signature.
Definition: main_impl.h:221
static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *msg, size_t msglen, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data)
Definition: main_impl.h:52
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
secp256k1_fe x
Definition: group.h:17
static const unsigned char bip340_algo[]
Definition: main_impl.h:48
int(* secp256k1_nonce_function_hardened)(unsigned char *nonce32, const unsigned char *msg, size_t msglen, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data)
This module implements a variant of Schnorr signatures compliant with Bitcoin Improvement Proposal 34...
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
uint64_t bytes
Definition: hash.h:16
static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 *sha)
Definition: main_impl.h:32
Opaque data structure that holds a keypair consisting of a secret and a public key.
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ctx)
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size)
static void secp256k1_sha256_initialize_tagged(secp256k1_sha256 *hash, const unsigned char *tag, size_t taglen)
Definition: hash_impl.h:163
int secp256k1_schnorrsig_sign32(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32)
Create a Schnorr signature.
Definition: main_impl.h:197
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
Definition: util.h:208
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition: util.h:255
#define secp256k1_fe_normalize_var
Definition: field.h:80
#define secp256k1_fe_get_b32
Definition: field.h:89
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
secp256k1_nonce_function_hardened noncefp
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
static SECP256K1_INLINE void secp256k1_memclear(void *ptr, size_t len)
Definition: util.h:223
static SECP256K1_INLINE int secp256k1_xonly_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_xonly_pubkey *pubkey)
Definition: main_impl.h:14
secp256k1_fe y
Definition: group.h:18
static void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
static const secp256k1_scalar secp256k1_scalar_one
Definition: scalar_impl.h:27
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_schnorrsig_sha256_tagged(secp256k1_sha256 *sha)
Definition: main_impl.h:104