@@ -40,42 +40,60 @@ extern "C" {
40
40
# define SECP256K1_ARG_NONNULL (_x )
41
41
# endif
42
42
43
+ /** Opaque data structure that holds context information (precomputed tables etc.).
44
+ * Only functions that take a pointer to a non-const context require exclusive
45
+ * access to it. Multiple functions that take a pointer to a const context may
46
+ * run simultaneously.
47
+ */
48
+ typedef struct secp256k1_context_struct secp256k1_context_t ;
49
+
50
+ /** Flags to pass to secp256k1_context_create. */
51
+ # define SECP256K1_CONTEXT_VERIFY (1 << 0)
52
+ # define SECP256K1_CONTEXT_SIGN (1 << 1)
43
53
44
- /** Flags to pass to secp256k1_start. */
45
- # define SECP256K1_START_VERIFY (1 << 0)
46
- # define SECP256K1_START_SIGN (1 << 1)
54
+ /** Create a secp256k1 context object.
55
+ * Returns: a newly created context object.
56
+ * In: flags: which parts of the context to initialize.
57
+ */
58
+ secp256k1_context_t * secp256k1_context_create (
59
+ int flags
60
+ ) SECP256K1_WARN_UNUSED_RESULT ;
47
61
48
- /** Initialize the library. This may take some time (10-100 ms).
49
- * You need to call this before calling any other function.
50
- * It cannot run in parallel with any other functions, but once
51
- * secp256k1_start() returns, all other functions are thread-safe.
62
+ /** Copies a secp256k1 context object.
63
+ * Returns: a newly created context object.
64
+ * In: ctx: an existing context to copy
52
65
*/
53
- void secp256k1_start (unsigned int flags );
66
+ secp256k1_context_t * secp256k1_context_clone (
67
+ const secp256k1_context_t * ctx
68
+ ) SECP256K1_WARN_UNUSED_RESULT ;
54
69
55
- /** Free all memory associated with this library. After this, no
56
- * functions can be called anymore, except secp256k1_start()
70
+ /** Destroy a secp256k1 context object.
71
+ * The context pointer may not be used afterwards.
57
72
*/
58
- void secp256k1_stop (void );
73
+ void secp256k1_context_destroy (
74
+ secp256k1_context_t * ctx
75
+ ) SECP256K1_ARG_NONNULL (1 );
59
76
60
77
/** Verify an ECDSA signature.
61
78
* Returns: 1: correct signature
62
79
* 0: incorrect signature
63
80
* -1: invalid public key
64
81
* -2: invalid signature
65
- * In: msg32: the 32-byte message hash being verified (cannot be NULL)
82
+ * In: ctx: a secp256k1 context object, initialized for verification.
83
+ * msg32: the 32-byte message hash being verified (cannot be NULL)
66
84
* sig: the signature being verified (cannot be NULL)
67
85
* siglen: the length of the signature
68
86
* pubkey: the public key to verify with (cannot be NULL)
69
87
* pubkeylen: the length of pubkey
70
- * Requires starting using SECP256K1_START_VERIFY.
71
88
*/
72
89
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify (
90
+ const secp256k1_context_t * ctx ,
73
91
const unsigned char * msg32 ,
74
92
const unsigned char * sig ,
75
93
int siglen ,
76
94
const unsigned char * pubkey ,
77
95
int pubkeylen
78
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (4 );
96
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL ( 5 );
79
97
80
98
/** A pointer to a function to deterministically generate a nonce.
81
99
* Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
@@ -111,15 +129,14 @@ extern const secp256k1_nonce_function_t secp256k1_nonce_function_default;
111
129
* Returns: 1: signature created
112
130
* 0: the nonce generation function failed, the private key was invalid, or there is not
113
131
* enough space in the signature (as indicated by siglen).
114
- * In: msg32: the 32-byte message hash being signed (cannot be NULL)
132
+ * In: ctx: pointer to a context object, initialized for signing (cannot be NULL)
133
+ * msg32: the 32-byte message hash being signed (cannot be NULL)
115
134
* seckey: pointer to a 32-byte secret key (cannot be NULL)
116
135
* noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
117
136
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
118
137
* Out: sig: pointer to an array where the signature will be placed (cannot be NULL)
119
138
* In/Out: siglen: pointer to an int with the length of sig, which will be updated
120
- * to contain the actual signature length (<=72). If 0 is returned, this will be
121
- * set to zero.
122
- * Requires starting using SECP256K1_START_SIGN.
139
+ * to contain the actual signature length (<=72).
123
140
*
124
141
* The sig always has an s value in the lower half of the range (From 0x1
125
142
* to 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0,
@@ -148,145 +165,180 @@ extern const secp256k1_nonce_function_t secp256k1_nonce_function_default;
148
165
* be taken when this property is required for an application.
149
166
*/
150
167
int secp256k1_ecdsa_sign (
168
+ const secp256k1_context_t * ctx ,
151
169
const unsigned char * msg32 ,
152
170
unsigned char * sig ,
153
171
int * siglen ,
154
172
const unsigned char * seckey ,
155
173
secp256k1_nonce_function_t noncefp ,
156
174
const void * ndata
157
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL (4 );
175
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL (4 ) SECP256K1_ARG_NONNULL ( 5 ) ;
158
176
159
177
/** Create a compact ECDSA signature (64 byte + recovery id).
160
178
* Returns: 1: signature created
161
179
* 0: the nonce generation function failed, or the secret key was invalid.
162
- * In: msg32: the 32-byte message hash being signed (cannot be NULL)
180
+ * In: ctx: pointer to a context object, initialized for signing (cannot be NULL)
181
+ * msg32: the 32-byte message hash being signed (cannot be NULL)
163
182
* seckey: pointer to a 32-byte secret key (cannot be NULL)
164
183
* noncefp:pointer to a nonce generation function. If NULL, secp256k1_nonce_function_default is used
165
184
* ndata: pointer to arbitrary data used by the nonce generation function (can be NULL)
166
185
* Out: sig: pointer to a 64-byte array where the signature will be placed (cannot be NULL)
167
186
* In case 0 is returned, the returned signature length will be zero.
168
187
* recid: pointer to an int, which will be updated to contain the recovery id (can be NULL)
169
- * Requires starting using SECP256K1_START_SIGN.
170
188
*/
171
189
int secp256k1_ecdsa_sign_compact (
190
+ const secp256k1_context_t * ctx ,
172
191
const unsigned char * msg32 ,
173
192
unsigned char * sig64 ,
174
193
const unsigned char * seckey ,
175
194
secp256k1_nonce_function_t noncefp ,
176
195
const void * ndata ,
177
196
int * recid
178
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 );
197
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL ( 4 ) ;
179
198
180
199
/** Recover an ECDSA public key from a compact signature.
181
200
* Returns: 1: public key successfully recovered (which guarantees a correct signature).
182
201
* 0: otherwise.
183
- * In: msg32: the 32-byte message hash assumed to be signed (cannot be NULL)
202
+ * In: ctx: pointer to a context object, initialized for verification (cannot be NULL)
203
+ * msg32: the 32-byte message hash assumed to be signed (cannot be NULL)
184
204
* sig64: signature as 64 byte array (cannot be NULL)
185
205
* compressed: whether to recover a compressed or uncompressed pubkey
186
206
* recid: the recovery id (0-3, as returned by ecdsa_sign_compact)
187
207
* Out: pubkey: pointer to a 33 or 65 byte array to put the pubkey (cannot be NULL)
188
208
* pubkeylen: pointer to an int that will contain the pubkey length (cannot be NULL)
189
- * Requires starting using SECP256K1_START_VERIFY.
190
209
*/
191
210
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_recover_compact (
211
+ const secp256k1_context_t * ctx ,
192
212
const unsigned char * msg32 ,
193
213
const unsigned char * sig64 ,
194
214
unsigned char * pubkey ,
195
215
int * pubkeylen ,
196
216
int compressed ,
197
217
int recid
198
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL (4 );
218
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL (4 ) SECP256K1_ARG_NONNULL ( 5 ) ;
199
219
200
220
/** Verify an ECDSA secret key.
201
221
* Returns: 1: secret key is valid
202
222
* 0: secret key is invalid
203
- * In: seckey: pointer to a 32-byte secret key (cannot be NULL)
223
+ * In: ctx: pointer to a context object (cannot be NULL)
224
+ * seckey: pointer to a 32-byte secret key (cannot be NULL)
204
225
*/
205
- SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify (const unsigned char * seckey ) SECP256K1_ARG_NONNULL (1 );
226
+ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify (
227
+ const secp256k1_context_t * ctx ,
228
+ const unsigned char * seckey
229
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 );
206
230
207
231
/** Just validate a public key.
208
- * Returns: 1: valid public key
209
- * 0: invalid public key
210
- * In: pubkey: pointer to a 33-byte or 65-byte public key (cannot be NULL).
232
+ * Returns: 1: public key is valid
233
+ * 0: public key is invalid
234
+ * In: ctx: pointer to a context object (cannot be NULL)
235
+ * pubkey: pointer to a 33-byte or 65-byte public key (cannot be NULL).
211
236
* pubkeylen: length of pubkey
212
237
*/
213
- SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_verify (const unsigned char * pubkey , int pubkeylen ) SECP256K1_ARG_NONNULL (1 );
238
+ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_verify (
239
+ const secp256k1_context_t * ctx ,
240
+ const unsigned char * pubkey ,
241
+ int pubkeylen
242
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 );
214
243
215
244
/** Compute the public key for a secret key.
216
- * In: compressed: whether the computed public key should be compressed
245
+ * In: ctx: pointer to a context object, initialized for signing (cannot be NULL)
246
+ * compressed: whether the computed public key should be compressed
217
247
* seckey: pointer to a 32-byte private key (cannot be NULL)
218
248
* Out: pubkey: pointer to a 33-byte (if compressed) or 65-byte (if uncompressed)
219
249
* area to store the public key (cannot be NULL)
220
250
* pubkeylen: pointer to int that will be updated to contains the pubkey's
221
251
* length (cannot be NULL)
222
252
* Returns: 1: secret was valid, public key stores
223
- * 0: secret was invalid, try again.
224
- * Requires starting using SECP256K1_START_SIGN.
253
+ * 0: secret was invalid, try again
225
254
*/
226
255
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create (
256
+ const secp256k1_context_t * ctx ,
227
257
unsigned char * pubkey ,
228
258
int * pubkeylen ,
229
259
const unsigned char * seckey ,
230
260
int compressed
231
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 );
261
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL ( 4 ) ;
232
262
233
263
/** Decompress a public key.
264
+ * In: ctx: pointer to a context object (cannot be NULL)
234
265
* In/Out: pubkey: pointer to a 65-byte array to put the decompressed public key.
235
- It must contain a 33-byte or 65-byte public key already (cannot be NULL)
266
+ * It must contain a 33-byte or 65-byte public key already (cannot be NULL)
236
267
* pubkeylen: pointer to the size of the public key pointed to by pubkey (cannot be NULL)
237
- It will be updated to reflect the new size.
238
- * Returns: 0 if the passed public key was invalid, 1 otherwise. If 1 is returned, the
239
- pubkey is replaced with its decompressed version.
268
+ * It will be updated to reflect the new size.
269
+ * Returns: 0: pubkey was invalid
270
+ * 1: pubkey was valid, and was replaced with its decompressed version
240
271
*/
241
272
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_decompress (
273
+ const secp256k1_context_t * ctx ,
242
274
unsigned char * pubkey ,
243
275
int * pubkeylen
244
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 );
276
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL ( 3 ) ;
245
277
246
- /** Export a private key in DER format. */
278
+ /** Export a private key in DER format.
279
+ * In: ctx: pointer to a context object, initialized for signing (cannot be NULL)
280
+ */
247
281
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_export (
282
+ const secp256k1_context_t * ctx ,
248
283
const unsigned char * seckey ,
249
284
unsigned char * privkey ,
250
285
int * privkeylen ,
251
286
int compressed
252
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 );
287
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (3 ) SECP256K1_ARG_NONNULL ( 4 ) ;
253
288
254
289
/** Import a private key in DER format. */
255
290
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_import (
291
+ const secp256k1_context_t * ctx ,
256
292
unsigned char * seckey ,
257
293
const unsigned char * privkey ,
258
294
int privkeylen
259
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 );
295
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL ( 3 ) ;
260
296
261
297
/** Tweak a private key by adding tweak to it. */
262
298
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add (
299
+ const secp256k1_context_t * ctx ,
263
300
unsigned char * seckey ,
264
301
const unsigned char * tweak
265
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 );
302
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL ( 3 ) ;
266
303
267
304
/** Tweak a public key by adding tweak times the generator to it.
268
- * Requires starting with SECP256K1_START_VERIFY.
305
+ * In: ctx: pointer to a context object, initialized for verification (cannot be NULL)
269
306
*/
270
307
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add (
308
+ const secp256k1_context_t * ctx ,
271
309
unsigned char * pubkey ,
272
310
int pubkeylen ,
273
311
const unsigned char * tweak
274
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (3 );
312
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL ( 4 );
275
313
276
314
/** Tweak a private key by multiplying it with tweak. */
277
315
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul (
316
+ const secp256k1_context_t * ctx ,
278
317
unsigned char * seckey ,
279
318
const unsigned char * tweak
280
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 );
319
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL ( 3 ) ;
281
320
282
321
/** Tweak a public key by multiplying it with tweak.
283
- * Requires starting with SECP256K1_START_VERIFY.
322
+ * In: ctx: pointer to a context object, initialized for verification (cannot be NULL)
284
323
*/
285
324
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul (
325
+ const secp256k1_context_t * ctx ,
286
326
unsigned char * pubkey ,
287
327
int pubkeylen ,
288
328
const unsigned char * tweak
289
- ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (3 );
329
+ ) SECP256K1_ARG_NONNULL (1 ) SECP256K1_ARG_NONNULL (2 ) SECP256K1_ARG_NONNULL (4 );
330
+
331
+ /** Updates the context randomization.
332
+ * Returns: 1: randomization successfully updated
333
+ * 0: error
334
+ * In: ctx: pointer to a context object (cannot be NULL)
335
+ * seed32: pointer to a 32-byte random seed (NULL resets to initial state)
336
+ */
337
+ SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize (
338
+ secp256k1_context_t * ctx ,
339
+ const unsigned char * seed32
340
+ ) SECP256K1_ARG_NONNULL (1 );
341
+
290
342
291
343
# ifdef __cplusplus
292
344
}
0 commit comments