7#ifndef SECP256K1_MODULE_SCHNORRSIG_MAIN_H
8#define SECP256K1_MODULE_SCHNORRSIG_MAIN_H
16static void secp256k1_nonce_function_bip340_sha256_tagged(
secp256k1_sha256 *sha) {
17 secp256k1_sha256_initialize(sha);
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;
32static void secp256k1_nonce_function_bip340_sha256_tagged_aux(
secp256k1_sha256 *sha) {
33 secp256k1_sha256_initialize(sha);
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;
48static const unsigned char bip340_algo[13] =
"BIP0340/nonce";
52static 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) {
54 unsigned char masked_key[32];
62 secp256k1_nonce_function_bip340_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];
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
76 for (i = 0; i < 32; i++) {
77 masked_key[i] = key32[i] ^ ZERO_MASK[i];
84 if (algolen ==
sizeof(bip340_algo)
85 && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
86 secp256k1_nonce_function_bip340_sha256_tagged(&sha);
88 secp256k1_sha256_initialize_tagged(&sha, algo, algolen);
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);
104 secp256k1_sha256_initialize(sha);
105 sha->
s[0] = 0x9cecba11ul;
106 sha->
s[1] = 0x23925381ul;
107 sha->
s[2] = 0x11679112ul;
108 sha->
s[3] = 0xd1627e0ful;
109 sha->
s[4] = 0x97c87550ul;
110 sha->
s[5] = 0x003cc765ul;
111 sha->
s[6] = 0x90f61164ul;
112 sha->
s[7] = 0x33e9b66aul;
116static void secp256k1_schnorrsig_challenge(
secp256k1_scalar* e,
const unsigned char *r32,
const unsigned char *msg,
size_t msglen,
const unsigned char *pubkey32)
118 unsigned char buf[32];
122 secp256k1_schnorrsig_sha256_tagged(&sha);
123 secp256k1_sha256_write(&sha, r32, 32);
124 secp256k1_sha256_write(&sha, pubkey32, 32);
125 secp256k1_sha256_write(&sha, msg, msglen);
126 secp256k1_sha256_finalize(&sha,
buf);
129 secp256k1_scalar_set_b32(e,
buf, NULL);
139 unsigned char buf[32] = { 0 };
140 unsigned char pk_buf[32];
141 unsigned char seckey[32];
150 if (noncefp == NULL) {
154 ret &= secp256k1_keypair_load(ctx, &sk, &pk, keypair);
158 if (secp256k1_fe_is_odd(&pk.
y)) {
159 secp256k1_scalar_negate(&sk, &sk);
162 secp256k1_scalar_get_b32(seckey, &sk);
163 secp256k1_fe_get_b32(pk_buf, &pk.
x);
164 ret &= !!noncefp(
buf, msg, msglen, seckey, pk_buf, bip340_algo,
sizeof(bip340_algo), ndata);
165 secp256k1_scalar_set_b32(&k,
buf, NULL);
166 ret &= !secp256k1_scalar_is_zero(&k);
167 secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !
ret);
170 secp256k1_ge_set_gej(&
r, &rj);
174 secp256k1_declassify(ctx, &
r,
sizeof(
r));
175 secp256k1_fe_normalize_var(&
r.y);
176 if (secp256k1_fe_is_odd(&
r.y)) {
177 secp256k1_scalar_negate(&k, &k);
179 secp256k1_fe_normalize_var(&
r.x);
180 secp256k1_fe_get_b32(&sig64[0], &
r.x);
182 secp256k1_schnorrsig_challenge(&e, &sig64[0], msg, msglen, pk_buf);
183 secp256k1_scalar_mul(&e, &e, &sk);
184 secp256k1_scalar_add(&e, &e, &k);
185 secp256k1_scalar_get_b32(&sig64[32], &e);
187 secp256k1_memczero(sig64, 64, !
ret);
188 secp256k1_scalar_clear(&k);
189 secp256k1_scalar_clear(&sk);
190 memset(seckey, 0,
sizeof(seckey));
209 if (extraparams != NULL) {
211 schnorrsig_extraparams_magic,
212 sizeof(extraparams->
magic)) == 0);
213 noncefp = extraparams->
noncefp;
214 ndata = extraparams->
ndata;
216 return secp256k1_schnorrsig_sign_internal(ctx, sig64, msg, msglen, keypair, noncefp, ndata);
227 unsigned char buf[32];
235 if (!secp256k1_fe_set_b32(&rx, &sig64[0])) {
239 secp256k1_scalar_set_b32(&
s, &sig64[32], &
overflow);
244 if (!secp256k1_xonly_pubkey_load(ctx, &pk,
pubkey)) {
249 secp256k1_fe_get_b32(
buf, &pk.
x);
250 secp256k1_schnorrsig_challenge(&e, &sig64[0], msg, msglen,
buf);
253 secp256k1_scalar_negate(&e, &e);
254 secp256k1_gej_set_ge(&pkj, &pk);
255 secp256k1_ecmult(&rj, &pkj, &e, &
s);
257 secp256k1_ge_set_gej_var(&
r, &rj);
258 if (secp256k1_ge_is_infinity(&
r)) {
262 secp256k1_fe_normalize_var(&
r.y);
263 return !secp256k1_fe_is_odd(&
r.y) &&
264 secp256k1_fe_equal_var(&rx, &
r.x);
#define VERIFY_CHECK(cond)
int secp256k1_schnorrsig_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32)
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)
int secp256k1_schnorrsig_sign32(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32)
const secp256k1_nonce_function_hardened secp256k1_nonce_function_bip340
int secp256k1_schnorrsig_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey)
#define SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC
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)
secp256k1_ecmult_gen_context ecmult_gen_ctx
memset(pInfo->slotDescription, ' ', 64)