@@ -23,24 +23,29 @@ extern "C" {
23
23
*
24
24
* Returns: 1 if a nonce was successfully generated. 0 will cause signing to
25
25
* return an error.
26
- * Out: nonce32: pointer to a 32-byte array to be filled by the function.
27
- * In: msg32: the 32-byte message hash being verified (will not be NULL)
28
- * key32: pointer to a 32-byte secret key (will not be NULL)
29
- * xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key32
30
- * (will not be NULL)
31
- * algo16: pointer to a 16-byte array describing the signature
32
- * algorithm (will not be NULL).
33
- * data: Arbitrary data pointer that is passed through.
26
+ * Out: nonce32: pointer to a 32-byte array to be filled by the function
27
+ * In: msg: the message being verified. Is NULL if and only if msglen
28
+ * is 0.
29
+ * msglen: the length of the message
30
+ * key32: pointer to a 32-byte secret key (will not be NULL)
31
+ * xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key32
32
+ * (will not be NULL)
33
+ * algo: pointer to an array describing the signature
34
+ * algorithm (will not be NULL)
35
+ * algolen: the length of the algo array
36
+ * data: arbitrary data pointer that is passed through
34
37
*
35
38
* Except for test cases, this function should compute some cryptographic hash of
36
39
* the message, the key, the pubkey, the algorithm description, and data.
37
40
*/
38
41
typedef int (* secp256k1_nonce_function_hardened )(
39
42
unsigned char * nonce32 ,
40
- const unsigned char * msg32 ,
43
+ const unsigned char * msg ,
44
+ size_t msglen ,
41
45
const unsigned char * key32 ,
42
46
const unsigned char * xonly_pk32 ,
43
- const unsigned char * algo16 ,
47
+ const unsigned char * algo ,
48
+ size_t algolen ,
44
49
void * data
45
50
);
46
51
@@ -50,59 +55,113 @@ typedef int (*secp256k1_nonce_function_hardened)(
50
55
*
51
56
* If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
52
57
* auxiliary random data as defined in BIP-340. If the data pointer is NULL,
53
- * schnorrsig_sign does not produce BIP-340 compliant signatures. The algo16
54
- * argument must be non-NULL, otherwise the function will fail and return 0.
55
- * The hash will be tagged with algo16 after removing all terminating null
56
- * bytes. Therefore, to create BIP-340 compliant signatures, algo16 must be set
57
- * to "BIP0340/nonce\0\0\0"
58
+ * the nonce derivation procedure follows BIP-340 by setting the auxiliary
59
+ * random data to zero. The algo argument must be non-NULL, otherwise the
60
+ * function will fail and return 0. The hash will be tagged with algo.
61
+ * Therefore, to create BIP-340 compliant signatures, algo must be set to
62
+ * "BIP0340/nonce" and algolen to 13.
58
63
*/
59
64
SECP256K1_API extern const secp256k1_nonce_function_hardened secp256k1_nonce_function_bip340 ;
60
65
66
+ /** Data structure that contains additional arguments for schnorrsig_sign_custom.
67
+ *
68
+ * A schnorrsig_extraparams structure object can be initialized correctly by
69
+ * setting it to SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT.
70
+ *
71
+ * Members:
72
+ * magic: set to SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC at initialization
73
+ * and has no other function than making sure the object is
74
+ * initialized.
75
+ * noncefp: pointer to a nonce generation function. If NULL,
76
+ * secp256k1_nonce_function_bip340 is used
77
+ * ndata: pointer to arbitrary data used by the nonce generation function
78
+ * (can be NULL). If it is non-NULL and
79
+ * secp256k1_nonce_function_bip340 is used, then ndata must be a
80
+ * pointer to 32-byte auxiliary randomness as per BIP-340.
81
+ */
82
+ typedef struct {
83
+ unsigned char magic [4 ];
84
+ secp256k1_nonce_function_hardened noncefp ;
85
+ void * ndata ;
86
+ } secp256k1_schnorrsig_extraparams ;
87
+
88
+ #define SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC "\xda\x6f\xb3\x8c"
89
+ #define SECP256K1_SCHNORRSIG_EXTRAPARAMS_INIT {\
90
+ SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC,\
91
+ NULL,\
92
+ NULL\
93
+ }
94
+
61
95
/** Create a Schnorr signature.
62
96
*
63
97
* Does _not_ strictly follow BIP-340 because it does not verify the resulting
64
98
* signature. Instead, you can manually use secp256k1_schnorrsig_verify and
65
99
* abort if it fails.
66
100
*
67
- * Otherwise BIP-340 compliant if the noncefp argument is NULL or
68
- * secp256k1_nonce_function_bip340 and the ndata argument is 32-byte auxiliary
69
- * randomness.
101
+ * This function only signs 32-byte messages. If you have messages of a
102
+ * different size (or the same size but without a context-specific tag
103
+ * prefix), it is recommended to create a 32-byte message hash with
104
+ * secp256k1_tagged_sha256 and then sign the hash. Tagged hashing allows
105
+ * providing an context-specific tag for domain separation. This prevents
106
+ * signatures from being valid in multiple contexts by accident.
70
107
*
71
108
* Returns 1 on success, 0 on failure.
72
109
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
73
110
* Out: sig64: pointer to a 64-byte array to store the serialized signature (cannot be NULL)
74
111
* In: msg32: the 32-byte message being signed (cannot be NULL)
75
112
* keypair: pointer to an initialized keypair (cannot be NULL)
76
- * noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_bip340 is used
77
- * ndata: pointer to arbitrary data used by the nonce generation
78
- * function (can be NULL). If it is non-NULL and
79
- * secp256k1_nonce_function_bip340 is used, then ndata must be a
80
- * pointer to 32-byte auxiliary randomness as per BIP-340.
113
+ * aux_rand32: 32 bytes of fresh randomness. While recommended to provide
114
+ * this, it is only supplemental to security and can be NULL. See
115
+ * BIP-340 "Default Signing" for a full explanation of this
116
+ * argument and for guidance if randomness is expensive.
81
117
*/
82
118
SECP256K1_API int secp256k1_schnorrsig_sign (
83
119
const secp256k1_context * ctx ,
84
120
unsigned char * sig64 ,
85
121
const unsigned char * msg32 ,
86
122
const secp256k1_keypair * keypair ,
87
- secp256k1_nonce_function_hardened noncefp ,
88
- void * ndata
123
+ unsigned char * aux_rand32
89
124
) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL (4 );
90
125
126
+ /** Create a Schnorr signature with a more flexible API.
127
+ *
128
+ * Same arguments as secp256k1_schnorrsig_sign except that it allows signing
129
+ * variable length messages and accepts a pointer to an extraparams object that
130
+ * allows customizing signing by passing additional arguments.
131
+ *
132
+ * Creates the same signatures as schnorrsig_sign if msglen is 32 and the
133
+ * extraparams.ndata is the same as aux_rand32.
134
+ *
135
+ * In: msg: the message being signed. Can only be NULL if msglen is 0.
136
+ * msglen: length of the message
137
+ * extraparams: pointer to a extraparams object (can be NULL)
138
+ */
139
+ SECP256K1_API int secp256k1_schnorrsig_sign_custom (
140
+ const secp256k1_context * ctx ,
141
+ unsigned char * sig64 ,
142
+ const unsigned char * msg ,
143
+ size_t msglen ,
144
+ const secp256k1_keypair * keypair ,
145
+ secp256k1_schnorrsig_extraparams * extraparams
146
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (5 );
147
+
91
148
/** Verify a Schnorr signature.
92
149
*
93
150
* Returns: 1: correct signature
94
151
* 0: incorrect signature
95
152
* Args: ctx: a secp256k1 context object, initialized for verification.
96
153
* In: sig64: pointer to the 64-byte signature to verify (cannot be NULL)
97
- * msg32: the 32-byte message being verified (cannot be NULL)
154
+ * msg: the message being verified. Can only be NULL if msglen is 0.
155
+ * msglen: length of the message
98
156
* pubkey: pointer to an x-only public key to verify with (cannot be NULL)
99
157
*/
100
158
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify (
101
159
const secp256k1_context * ctx ,
102
160
const unsigned char * sig64 ,
103
- const unsigned char * msg32 ,
161
+ const unsigned char * msg ,
162
+ size_t msglen ,
104
163
const secp256k1_xonly_pubkey * pubkey
105
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL ( 4 );
164
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (5 );
106
165
107
166
#ifdef __cplusplus
108
167
}
0 commit comments