Skip to content

Commit b98021a

Browse files
committed
batch_add_xonlypub_tweaks: refactor randomizer generation
- refactored randomizer generation function for easier testing - inital setup for batch_add_xonlypub_tweaks testing
1 parent 38ddc75 commit b98021a

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

src/modules/extrakeys/batch_add_impl.h

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

8-
static int secp256k1_batch_xonlypub_tweak_check_randomizer(const secp256k1_context* ctx, secp256k1_batch *batch, secp256k1_scalar *r, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey,const unsigned char *tweak32) {
8+
static void secp256k1_batch_add_xonlypub_tweak_check_randomizer_gen(unsigned char *randomizer32, secp256k1_sha256 *sha256, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const unsigned char *internal_pk33, const unsigned char *tweak32) {
99
secp256k1_sha256 sha256_cpy;
10-
unsigned char randomizer[32];
1110
unsigned char parity = (unsigned char) tweaked_pk_parity;
11+
12+
/* add tweaked pubkey check data to sha object */
13+
secp256k1_sha256_write(sha256, tweaked_pubkey32, 32);
14+
secp256k1_sha256_write(sha256, &parity, sizeof(parity));
15+
secp256k1_sha256_write(sha256, tweak32, 32);
16+
secp256k1_sha256_write(sha256, internal_pk33, 33);
17+
18+
/* generate randomizer */
19+
sha256_cpy = *sha256;
20+
secp256k1_sha256_finalize(&sha256_cpy, randomizer32);
21+
}
22+
23+
static int secp256k1_batch_add_xonlypub_tweak_check_randomizer_set(const secp256k1_context* ctx, secp256k1_batch *batch, secp256k1_scalar *r, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey,const unsigned char *tweak32) {
24+
unsigned char randomizer[32];
1225
unsigned char internal_buf[33];
1326
size_t internal_buflen = sizeof(internal_buf);
1427
int overflow;
@@ -21,15 +34,7 @@ static int secp256k1_batch_xonlypub_tweak_check_randomizer(const secp256k1_conte
2134
return 0;
2235
}
2336

24-
/* add tweaked pubkey check data to sha object */
25-
secp256k1_sha256_write(&batch->sha256, tweaked_pubkey32, 32);
26-
secp256k1_sha256_write(&batch->sha256, &parity, sizeof(parity));
27-
secp256k1_sha256_write(&batch->sha256, tweak32, 32);
28-
secp256k1_sha256_write(&batch->sha256, internal_buf, internal_buflen);
29-
30-
/* generate randomizer */
31-
sha256_cpy = batch->sha256;
32-
secp256k1_sha256_finalize(&sha256_cpy, randomizer);
37+
secp256k1_batch_add_xonlypub_tweak_check_randomizer_gen(randomizer, &batch->sha256, tweaked_pubkey32, tweaked_pk_parity, internal_buf, tweak32);
3338
secp256k1_scalar_set_b32(r, randomizer, &overflow);
3439
VERIFY_CHECK(overflow == 0);
3540

@@ -112,9 +117,7 @@ int secp256k1_batch_add_xonlypub_tweak_check(const secp256k1_context* ctx, secp2
112117
secp256k1_gej_set_ge(&batch->points[i+1], &pk);
113118

114119
/* Compute ai */
115-
if (!secp256k1_batch_xonlypub_tweak_check_randomizer(ctx, batch, &ai, tweaked_pubkey32, tweaked_pk_parity, internal_pubkey, tweak32)) {
116-
return 0;
117-
}
120+
secp256k1_batch_add_xonlypub_tweak_check_randomizer_set(ctx, batch, &ai, tweaked_pubkey32, tweaked_pk_parity, internal_pubkey, tweak32);
118121

119122
/* append scalars -ai, ai respectively to scratch space */
120123
secp256k1_scalar_negate(&tmp, &ai);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_TESTS_IMPL_H
2+
#define SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_TESTS_IMPL_H
3+
4+
#include "include/secp256k1_extrakeys.h"
5+
6+
void run_batch_xonlypub_tweak_randomizer_gen_tests(void) {
7+
8+
}
9+
10+
void run_batch_add_xonlypub_tweak_tests(void) {
11+
run_batch_xonlypub_tweak_randomizer_gen_tests();
12+
}
13+
14+
15+
#endif /* SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_TESTS_IMPL_H */

src/tests.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6866,6 +6866,9 @@ void run_ecdsa_edge_cases(void) {
68666866

68676867
#ifdef ENABLE_MODULE_EXTRAKEYS
68686868
# include "modules/extrakeys/tests_impl.h"
6869+
# ifdef ENABLE_MODULE_BATCH
6870+
# include "modules/extrakeys/batch_add_tests_impl.h"
6871+
# endif
68696872
#endif
68706873

68716874
#ifdef ENABLE_MODULE_SCHNORRSIG
@@ -7173,6 +7176,9 @@ int main(int argc, char **argv) {
71737176

71747177
#ifdef ENABLE_MODULE_EXTRAKEYS
71757178
run_extrakeys_tests();
7179+
# ifdef ENABLE_MODULE_BATCH
7180+
run_batch_add_xonlypub_tweak_tests();
7181+
# endif
71767182
#endif
71777183

71787184
#ifdef ENABLE_MODULE_SCHNORRSIG

0 commit comments

Comments
 (0)