Skip to content

Commit 8add7ce

Browse files
committed
batch_interface: move batch context functions to empty batch modules
- move create, destroy and verify function into batch module - rename `batch_context` to `batch` (in function names) - rename `batch_ctx` variable to `batch` - documentation uses "batch object" instead of "batch context"
1 parent cac2701 commit 8add7ce

File tree

9 files changed

+290
-302
lines changed

9 files changed

+290
-302
lines changed

Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ noinst_HEADERS += src/assumptions.h
5050
noinst_HEADERS += src/util.h
5151
noinst_HEADERS += src/scratch.h
5252
noinst_HEADERS += src/scratch_impl.h
53-
noinst_HEADERS += src/batch_impl.h
5453
noinst_HEADERS += src/selftest.h
5554
noinst_HEADERS += src/testrand.h
5655
noinst_HEADERS += src/testrand_impl.h
@@ -172,6 +171,7 @@ if BUILD_WINDOWS
172171
schnorr_example_LDFLAGS += -lbcrypt
173172
endif
174173
TESTS += schnorr_example
174+
if ENABLE_MODULE_BATCH
175175
noinst_PROGRAMS += batch_example
176176
batch_example_SOURCES = examples/batch.c
177177
batch_example_CPPFLAGS = -I$(top_srcdir)/include
@@ -183,6 +183,7 @@ endif
183183
TESTS += batch_example
184184
endif
185185
endif
186+
endif
186187

187188
### Precomputed tables
188189
EXTRA_PROGRAMS = precompute_ecmult precompute_ecmult_gen

examples/batch.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string.h>
44

55
#include <secp256k1.h>
6+
#include <secp256k1_batch.h>
67
#include <secp256k1_schnorrsig.h>
78

89
#include "random.h"
@@ -82,12 +83,14 @@ int generate_xonlypub_tweak_checks(secp256k1_context *ctx) {
8283
int main(void) {
8384
int ret;
8485
size_t i;
85-
/* batch_context uses secp256k1_context only for the error callback function*/
86+
/* batch object uses secp256k1_context only for the error callback function
87+
* here, we create secp256k1_context that can sign and verify, only to generate
88+
* input data (schnorrsigs, tweak checks) required for the batch */
8689
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
87-
secp256k1_batch_context *batch_ctx = secp256k1_batch_context_create(ctx, N_TERMS);
90+
secp256k1_batch *batch = secp256k1_batch_create(ctx, N_TERMS);
8891

8992
assert(ctx != NULL);
90-
assert(batch_ctx != NULL);
93+
assert(batch != NULL);
9194

9295
/* key pair generation */
9396
printf("Creating a key pair.........................");
@@ -105,9 +108,9 @@ int main(void) {
105108
}
106109
printf("ok\n");
107110

108-
printf("Adding signatures to the batch context......");
111+
printf("Adding signatures to the batch object.......");
109112
for (i = 0; i < N_SIGS; i++) {
110-
ret = secp256k1_batch_context_add_schnorrsig(ctx, batch_ctx, sig[i], msg[i], sizeof(msg[i]), &pk);
113+
ret = secp256k1_batch_add_schnorrsig(ctx, batch, sig[i], msg[i], sizeof(msg[i]), &pk);
111114
if(!ret) {
112115
printf("FAILED\n");
113116
return 1;
@@ -122,24 +125,24 @@ int main(void) {
122125
}
123126
printf("ok\n");
124127

125-
printf("Adding tweak checks to the batch context....");
128+
printf("Adding tweak checks to the batch object.....");
126129
for (i = 0; i < N_CHECKS; i++) {
127-
ret = secp256k1_batch_context_add_xonlypub_tweak(ctx, batch_ctx, tweaked_pubkey[i], tweaked_pk_parity[i], &pk, tweak[i]);
130+
ret = secp256k1_batch_add_xonlypub_tweak(ctx, batch, tweaked_pubkey[i], tweaked_pk_parity[i], &pk, tweak[i]);
128131
if(!ret) {
129132
printf("FAILED\n");
130133
return 1;
131134
}
132135
}
133136
printf("ok\n");
134137

135-
printf("Verifying the batch context.................");
136-
if(!secp256k1_batch_context_verify(ctx, batch_ctx)) {
138+
printf("Verifying the batch object..................");
139+
if(!secp256k1_batch_verify(ctx, batch)) {
137140
printf("FAILED\n");
138141
return 1;
139142
}
140143
printf("ok\n");
141144

142-
secp256k1_batch_context_destroy(ctx, batch_ctx);
145+
secp256k1_batch_destroy(ctx, batch);
143146
secp256k1_context_destroy(ctx);
144147

145148
return 0;

include/secp256k1.h

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,6 @@ typedef struct secp256k1_context_struct secp256k1_context;
5858
*/
5959
typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;
6060

61-
/** Opaque data structure that holds context information for schnorr batch verification.
62-
*
63-
* The purpose of this structure is to store elliptic curve points, their scalars,
64-
* and scalar of generator point participating in Multi-Scalar Point Multiplication
65-
* computation. This computation is done by secp256k1_ecmult_strauss_batch or
66-
* secp256k1_ecmult_pippenger_batch.
67-
*/
68-
typedef struct secp256k1_batch_context_struct secp256k1_batch_context;
69-
7061
/** Opaque data structure that holds a parsed and valid public key.
7162
*
7263
* The exact representation of data inside is implementation defined and not
@@ -360,51 +351,6 @@ SECP256K1_API void secp256k1_scratch_space_destroy(
360351
secp256k1_scratch_space* scratch
361352
) SECP256K1_ARG_NONNULL(1);
362353

363-
/** Create a secp256k1 batch context object (in dynamically allocated memory).
364-
*
365-
* This function uses malloc to allocate memory. It is guaranteed that malloc is
366-
* called at most twice for every call of this function.
367-
*
368-
* Returns: a newly created batch context object.
369-
* Args: ctx: an existing secp256k1_context object. Not to be confused
370-
* with the batch context object that this function creates.
371-
* In: max_terms: max number of (scalar, curve point) pairs that the batch
372-
* object can store.
373-
*/
374-
SECP256K1_API secp256k1_batch_context* secp256k1_batch_context_create(
375-
const secp256k1_context* ctx,
376-
size_t max_terms
377-
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
378-
379-
/** Destroy a secp256k1 batch context object (created in dynamically allocated memory).
380-
*
381-
* The context pointer may not be used afterwards.
382-
*
383-
* Args: ctx: a secp256k1 context object.
384-
* batch_ctx: an existing batch context to destroy, constructed
385-
* using secp256k1_batch_context_create
386-
*/
387-
SECP256K1_API void secp256k1_batch_context_destroy(
388-
const secp256k1_context* ctx,
389-
secp256k1_batch_context* batch_ctx
390-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
391-
392-
/** Verify the set of schnorr signatures or tweaked pubkeys present in the secp256k1_batch_context.
393-
*
394-
* Returns: 1: correct schnorrsigs/tweaks
395-
* 0: incorrect schnorrsigs/tweaks
396-
*
397-
* In particular, returns 1 if the batch context is empty (i.e, batch_ctx->len = 0).
398-
*
399-
* Args: ctx: a secp256k1 context object (can be initialized for none).
400-
* batch_ctx: a secp256k1 batch context object that contains a
401-
* set of schnorrsigs/tweaks.
402-
*/
403-
SECP256K1_API int secp256k1_batch_context_verify(
404-
const secp256k1_context *ctx,
405-
secp256k1_batch_context *batch_ctx
406-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
407-
408354
/** Parse a variable-length public key into the pubkey object.
409355
*
410356
* Returns: 1 if the public key was fully valid.

include/secp256k1_batch.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef SECP256K1_BATCH_H
22
#define SECP256K1_BATCH_H
33

4+
#include "include/secp256k1.h"
5+
46
#ifdef __cplusplus
57
extern "C" {
68
#endif
79

8-
/** This module implements a Batch Verification context that supports:
10+
/** This module implements a Batch Verification object that supports:
911
*
1012
* 1. Schnorr signatures compliant with Bitcoin Improvement Proposal 340
1113
* "Schnorr Signatures for secp256k1"
@@ -16,6 +18,59 @@ extern "C" {
1618
* (https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki).
1719
*/
1820

21+
/** Opaque data structure that holds information required for the batch verification.
22+
*
23+
* The purpose of this structure is to store elliptic curve points, their scalars,
24+
* and scalar of generator point participating in Multi-Scalar Point Multiplication
25+
* computation. This computation is done by secp256k1_ecmult_strauss_batch or
26+
* secp256k1_ecmult_pippenger_batch.
27+
*/
28+
typedef struct secp256k1_batch_struct secp256k1_batch;
29+
30+
/** Create a secp256k1 batch object object (in dynamically allocated memory).
31+
*
32+
* This function uses malloc to allocate memory. It is guaranteed that malloc is
33+
* called at most twice for every call of this function.
34+
*
35+
* Returns: a newly created batch object.
36+
* Args: ctx: an existing secp256k1_context object. Not to be confused
37+
* with the batch object object that this function creates.
38+
* In: max_terms: max number of (scalar, curve point) pairs that the batch
39+
* object can store.
40+
*/
41+
SECP256K1_API secp256k1_batch* secp256k1_batch_create(
42+
const secp256k1_context* ctx,
43+
size_t max_terms
44+
) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT;
45+
46+
/** Destroy a secp256k1 batch object (created in dynamically allocated memory).
47+
*
48+
* The batch object's pointer may not be used afterwards.
49+
*
50+
* Args: ctx: a secp256k1 context object.
51+
* batch: an existing batch object to destroy, constructed
52+
* using secp256k1_batch_create
53+
*/
54+
SECP256K1_API void secp256k1_batch_destroy(
55+
const secp256k1_context* ctx,
56+
secp256k1_batch* batch
57+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
58+
59+
/** Verify the set of schnorr signatures or tweaked pubkeys present in the secp256k1_batch.
60+
*
61+
* Returns: 1: correct schnorrsigs/tweaks
62+
* 0: incorrect schnorrsigs/tweaks
63+
*
64+
* In particular, returns 1 if the batch object is empty (i.e, batch->len = 0).
65+
*
66+
* Args: ctx: a secp256k1 context object (can be initialized for none).
67+
* batch: a secp256k1 batch object that contains a set of schnorrsigs/tweaks.
68+
*/
69+
SECP256K1_API int secp256k1_batch_verify(
70+
const secp256k1_context *ctx,
71+
secp256k1_batch *batch
72+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
73+
1974
#ifdef __cplusplus
2075
}
2176
#endif

include/secp256k1_schnorrsig.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "secp256k1.h"
55
#include "secp256k1_extrakeys.h"
6+
#include "include/secp256k1_batch.h"
67

78
#ifdef __cplusplus
89
extern "C" {
@@ -164,8 +165,8 @@ SECP256K1_API int secp256k1_schnorrsig_sign_custom(
164165
* Args: ctx: a secp256k1 context object, initialized for verification.
165166
* In: sig64: pointer to the 64-byte signature to verify.
166167
* msg: the message being verified. Can only be NULL if msglen is 0.
167-
* msglen: length of the message
168-
* pubkey: pointer to an x-only public key to verify with (cannot be NULL)
168+
* msglen: length of the message.
169+
* pubkey: pointer to an x-only public key to verify with (cannot be NULL).
169170
*/
170171
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(
171172
const secp256k1_context* ctx,
@@ -175,36 +176,34 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(
175176
const secp256k1_xonly_pubkey *pubkey
176177
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5);
177178

178-
/** Adds the given schnorrsig verification data to secp256k1_batch_context.
179+
/** Adds the given schnorrsig verification data to secp256k1_batch.
179180
*
180181
* Returns 1 on success, 0 on failure.
181182
* Args: ctx: a secp256k1 context object (can be initialized for none).
182-
* batch_ctx: a secp256k1 batch context object created using
183-
* the secp256k1_batch_context_create API
183+
* batch: a secp256k1 batch object created using `secp256k1_batch_create`.
184184
* In: sig64: pointer to the 64-byte signature to verify.
185185
* msg: the message being verified. Can only be NULL if msglen is 0.
186-
* msglen: length of the message
187-
* pubkey: pointer to an x-only public key to verify with (cannot be NULL)
186+
* msglen: length of the message.
187+
* pubkey: pointer to an x-only public key to verify with (cannot be NULL).
188188
*/
189-
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_batch_context_add_schnorrsig(
189+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_batch_add_schnorrsig(
190190
const secp256k1_context* ctx,
191-
secp256k1_batch_context *batch_ctx,
191+
secp256k1_batch *batch,
192192
const unsigned char *sig64,
193193
const unsigned char *msg,
194194
size_t msglen,
195195
const secp256k1_xonly_pubkey *pubkey
196196
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(6);
197197

198-
/** Adds the given tweaked pubkey, internal pubkey, and tweak32 to secp256k1_batch_context.
198+
/** Adds the given tweaked pubkey, internal pubkey, and tweak32 to secp256k1_batch.
199199
*
200200
* The tweaked pubkey is represented by its 32-byte x-only serialization and
201201
* its pk_parity, which can both be obtained by converting the result of
202202
* tweak_add to a secp256k1_xonly_pubkey.
203203
*
204204
* Returns 1 on success, 0 on failure.
205205
* Args: ctx: pointer to a context object initialized for verification.
206-
* batch_ctx: a secp256k1 batch context object created using
207-
* the secp256k1_batch_context_create API.
206+
* batch: a secp256k1 batch object created using `secp256k1_batch_create`.
208207
* In: tweaked_pubkey32: pointer to a serialized xonly_pubkey.
209208
* tweaked_pk_parity: the parity of the tweaked pubkey (whose serialization
210209
* is passed in as tweaked_pubkey32). This must match the
@@ -214,9 +213,9 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_batch_context_add_schno
214213
* internal_pubkey: pointer to an x-only public key object to apply the tweak to.
215214
* tweak32: pointer to a 32-byte tweak.
216215
*/
217-
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_batch_context_add_xonlypub_tweak(
216+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_batch_add_xonlypub_tweak(
218217
const secp256k1_context* ctx,
219-
secp256k1_batch_context *batch_ctx,
218+
secp256k1_batch *batch,
220219
const unsigned char *tweaked_pubkey32,
221220
int tweaked_pk_parity,
222221
const secp256k1_xonly_pubkey *internal_pubkey,

0 commit comments

Comments
 (0)