Skip to content

Commit 5bb9c26

Browse files
committed
batch_add: impl batch_reset argument to log transparent verify
1 parent 7978122 commit 5bb9c26

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

examples/batch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ int main(void) {
8989
* input data (schnorrsigs, tweak checks) required for the batch */
9090
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
9191
secp256k1_batch *batch = secp256k1_batch_create(ctx, N_TERMS);
92+
int batch_reset;
9293

9394
assert(ctx != NULL);
9495
assert(batch != NULL);
@@ -111,7 +112,7 @@ int main(void) {
111112

112113
printf("Adding signatures to the batch object.......");
113114
for (i = 0; i < N_SIGS; i++) {
114-
ret = secp256k1_batch_add_schnorrsig(ctx, batch, sig[i], msg[i], sizeof(msg[i]), &pk);
115+
ret = secp256k1_batch_add_schnorrsig(ctx, batch, sig[i], msg[i], sizeof(msg[i]), &pk, &batch_reset);
115116
if(!ret) {
116117
printf("FAILED\n");
117118
return 1;
@@ -128,7 +129,7 @@ int main(void) {
128129

129130
printf("Adding tweak checks to the batch object.....");
130131
for (i = 0; i < N_CHECKS; i++) {
131-
ret = secp256k1_batch_add_xonlypub_tweak_check(ctx, batch, tweaked_pubkey[i], tweaked_pk_parity[i], &pk, tweak[i]);
132+
ret = secp256k1_batch_add_xonlypub_tweak_check(ctx, batch, tweaked_pubkey[i], tweaked_pk_parity[i], &pk, tweak[i], &batch_reset);
132133
if(!ret) {
133134
printf("FAILED\n");
134135
return 1;

include/secp256k1_extrakeys.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,17 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add
261261
* this function will fail.
262262
* internal_pubkey: pointer to an x-only public key object to apply the tweak to.
263263
* tweak32: pointer to a 32-byte tweak.
264+
* batch_reset: non-zero if the batch was cleared (due to insufficient space),
265+
* zero otherwise (can be NULL).
264266
*/
265267
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_batch_add_xonlypub_tweak_check(
266268
const secp256k1_context* ctx,
267269
secp256k1_batch *batch,
268270
const unsigned char *tweaked_pubkey32,
269271
int tweaked_pk_parity,
270272
const secp256k1_xonly_pubkey *internal_pubkey,
271-
const unsigned char *tweak32
273+
const unsigned char *tweak32,
274+
int *batch_reset
272275
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6);
273276
#endif
274277

include/secp256k1_schnorrsig.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,17 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(
186186
* msg: the message being verified. Can only be NULL if msglen is 0.
187187
* msglen: length of the message.
188188
* pubkey: pointer to an x-only public key to verify with (cannot be NULL).
189+
* batch_reset: non-zero if the batch was cleared (due to insufficient space),
190+
* zero otherwise (can be NULL).
189191
*/
190192
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_batch_add_schnorrsig(
191193
const secp256k1_context* ctx,
192194
secp256k1_batch *batch,
193195
const unsigned char *sig64,
194196
const unsigned char *msg,
195197
size_t msglen,
196-
const secp256k1_xonly_pubkey *pubkey
198+
const secp256k1_xonly_pubkey *pubkey,
199+
int *batch_reset
197200
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(6);
198201
#endif
199202

src/modules/extrakeys/batch_add_impl.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "src/hash.h"
66
#include "src/modules/batch/main_impl.h"
77

8+
/* The number of objects allocated on the scratch space by
9+
* secp256k1_batch_add_schnorrsig*/
10+
#define BATCH_TWEAK_CHECK_SCRATCH_OBJS 2
11+
812
static void secp256k1_batch_xonlypub_tweak_randomizer_gen(unsigned char *randomizer32, secp256k1_sha256 *sha256, const unsigned char *tweaked_pubkey32, const unsigned char *tweaked_pk_parity, const unsigned char *internal_pk33, const unsigned char *tweak32) {
913
secp256k1_sha256 sha256_cpy;
1014

@@ -61,7 +65,7 @@ static int secp256k1_batch_xonlypub_tweak_randomizer_set(const secp256k1_context
6165
*
6266
* This function's algorithm is based on secp256k1_xonly_pubkey_tweak_add_check.
6367
*/
64-
int secp256k1_batch_add_xonlypub_tweak_check(const secp256k1_context* ctx, secp256k1_batch *batch, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey,const unsigned char *tweak32) {
68+
int secp256k1_batch_add_xonlypub_tweak_check(const secp256k1_context* ctx, secp256k1_batch *batch, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32, int *batch_len_reset) {
6569
secp256k1_scalar tweak;
6670
secp256k1_scalar ai;
6771
secp256k1_scalar tmp;
@@ -90,9 +94,12 @@ int secp256k1_batch_add_xonlypub_tweak_check(const secp256k1_context* ctx, secp2
9094
return 0;
9195
}
9296

97+
if(batch_len_reset) {
98+
*batch_len_reset = 0;
99+
}
100+
93101
/* run verify if batch object's scratch is full */
94-
/* todo: create a function to do this?? */
95-
if (batch->capacity - batch->len < 2) {
102+
if (batch->capacity - batch->len < BATCH_TWEAK_CHECK_SCRATCH_OBJS) {
96103
printf("\nbatch_add_xonlypub_tweak: Batch object is full...\n");
97104
printf("batch_add_xonlypub_tweak: Verifying the batch object...\n");
98105
if (!secp256k1_batch_verify(ctx, batch)) {
@@ -101,6 +108,9 @@ int secp256k1_batch_add_xonlypub_tweak_check(const secp256k1_context* ctx, secp2
101108
}
102109
printf("batch_add_xonlypub_tweak: Clearing the batch object for future use...\n");
103110
secp256k1_batch_scratch_clear(batch);
111+
if(batch_len_reset) {
112+
*batch_len_reset = 1;
113+
}
104114
}
105115

106116
i = batch->len;

src/modules/schnorrsig/batch_add_impl.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "src/hash.h"
66
#include "src/modules/batch/main_impl.h"
77

8+
/* The number of objects allocated on the scratch space by
9+
* secp256k1_batch_add_schnorrsig*/
10+
#define BATCH_SCHNORRSIG_SCRATCH_OBJS 2
11+
812
static void secp256k1_batch_schnorrsig_randomizer_gen(unsigned char *randomizer32, secp256k1_sha256 *sha256, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const unsigned char *compressed_pk33) {
913
secp256k1_sha256 sha256_cpy;
1014

@@ -57,7 +61,7 @@ static int secp256k1_batch_schnorrsig_randomizer_set(const secp256k1_context *ct
5761
*
5862
* This function's algorithm is based on secp256k1_schnorrsig_verify.
5963
*/
60-
int secp256k1_batch_add_schnorrsig(const secp256k1_context* ctx, secp256k1_batch *batch, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey) {
64+
int secp256k1_batch_add_schnorrsig(const secp256k1_context* ctx, secp256k1_batch *batch, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey, int *batch_len_reset) {
6165
secp256k1_scalar s;
6266
secp256k1_scalar e;
6367
secp256k1_scalar ai;
@@ -87,8 +91,11 @@ int secp256k1_batch_add_schnorrsig(const secp256k1_context* ctx, secp256k1_batch
8791
return 0;
8892
}
8993

94+
if(batch_len_reset) {
95+
*batch_len_reset = 0;
96+
}
9097
/* run verify if batch object's scratch is full */
91-
if (batch->capacity - batch->len < 2) {
98+
if (batch->capacity - batch->len < BATCH_SCHNORRSIG_SCRATCH_OBJS) {
9299
printf("\nbatch_add: Batch object is full...\n");
93100
printf("batch_add: Verifying the batch object...\n");
94101
if (!secp256k1_batch_verify(ctx, batch)) {
@@ -97,6 +104,9 @@ int secp256k1_batch_add_schnorrsig(const secp256k1_context* ctx, secp256k1_batch
97104
}
98105
printf("batch_add: Clearing the batch object for future use...\n");
99106
secp256k1_batch_scratch_clear(batch);
107+
if(batch_len_reset) {
108+
*batch_len_reset = 1;
109+
}
100110
}
101111

102112
i = batch->len;

0 commit comments

Comments
 (0)