Skip to content

Commit 8a1da31

Browse files
committed
tests: impl bitflip test for xonly pub tweak randomizer gen
1 parent b98021a commit 8a1da31

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

src/modules/extrakeys/batch_add_impl.h

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

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) {
8+
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) {
99
secp256k1_sha256 sha256_cpy;
10-
unsigned char parity = (unsigned char) tweaked_pk_parity;
1110

1211
/* add tweaked pubkey check data to sha object */
1312
secp256k1_sha256_write(sha256, tweaked_pubkey32, 32);
14-
secp256k1_sha256_write(sha256, &parity, sizeof(parity));
13+
secp256k1_sha256_write(sha256, tweaked_pk_parity, 1);
1514
secp256k1_sha256_write(sha256, tweak32, 32);
1615
secp256k1_sha256_write(sha256, internal_pk33, 33);
1716

@@ -20,10 +19,11 @@ static void secp256k1_batch_add_xonlypub_tweak_check_randomizer_gen(unsigned cha
2019
secp256k1_sha256_finalize(&sha256_cpy, randomizer32);
2120
}
2221

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) {
22+
static int secp256k1_batch_xonlypub_tweak_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) {
2423
unsigned char randomizer[32];
2524
unsigned char internal_buf[33];
2625
size_t internal_buflen = sizeof(internal_buf);
26+
unsigned char parity = (unsigned char) tweaked_pk_parity;
2727
int overflow;
2828

2929
/* We use compressed serialization here. If we would use
@@ -34,7 +34,7 @@ static int secp256k1_batch_add_xonlypub_tweak_check_randomizer_set(const secp256
3434
return 0;
3535
}
3636

37-
secp256k1_batch_add_xonlypub_tweak_check_randomizer_gen(randomizer, &batch->sha256, tweaked_pubkey32, tweaked_pk_parity, internal_buf, tweak32);
37+
secp256k1_batch_xonlypub_tweak_randomizer_gen(randomizer, &batch->sha256, tweaked_pubkey32, &parity, internal_buf, tweak32);
3838
secp256k1_scalar_set_b32(r, randomizer, &overflow);
3939
VERIFY_CHECK(overflow == 0);
4040

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

119119
/* Compute ai */
120-
secp256k1_batch_add_xonlypub_tweak_check_randomizer_set(ctx, batch, &ai, tweaked_pubkey32, tweaked_pk_parity, internal_pubkey, tweak32);
120+
if(!secp256k1_batch_xonlypub_tweak_randomizer_set(ctx, batch, &ai, tweaked_pubkey32, tweaked_pk_parity, internal_pubkey, tweak32)) {
121+
return 0;
122+
}
121123

122124
/* append scalars -ai, ai respectively to scratch space */
123125
secp256k1_scalar_negate(&tmp, &ai);

src/modules/extrakeys/batch_add_tests_impl.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,63 @@
33

44
#include "include/secp256k1_extrakeys.h"
55

6+
/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
7+
* bytes) changes the hash function */
8+
void batch_xonlypub_tweak_randomizer_gen_bitflip(secp256k1_sha256 *sha, unsigned char **args, size_t n_flip, size_t n_bytes) {
9+
unsigned char randomizers[2][32];
10+
secp256k1_sha256 sha_cpy;
11+
sha_cpy = *sha;
12+
secp256k1_batch_xonlypub_tweak_randomizer_gen(randomizers[0], &sha_cpy, args[0], args[1], args[2], args[3]);
13+
secp256k1_testrand_flip(args[n_flip], n_bytes);
14+
sha_cpy = *sha;
15+
secp256k1_batch_xonlypub_tweak_randomizer_gen(randomizers[1], &sha_cpy, args[0], args[1], args[2], args[3]);
16+
CHECK(secp256k1_memcmp_var(randomizers[0], randomizers[1], 32) != 0);
17+
}
18+
619
void run_batch_xonlypub_tweak_randomizer_gen_tests(void) {
20+
secp256k1_sha256 sha;
21+
size_t n_checks = 20;
22+
unsigned char tweaked_pk[32];
23+
unsigned char tweaked_pk_parity;
24+
unsigned char tweak[32];
25+
unsigned char internal_pk[33];
26+
unsigned char *args[4];
27+
size_t i; /* loops through n_checks */
28+
int j; /* loops through count */
29+
30+
secp256k1_batch_sha256_tagged(&sha);
31+
32+
for (i = 0; i < n_checks; i++) {
33+
uint8_t temp_rand;
34+
35+
/* generate i-th tweak check data */
36+
secp256k1_testrand256(tweaked_pk);
37+
tweaked_pk_parity = (unsigned char) secp256k1_testrand_int(2);
38+
secp256k1_testrand256(tweak);
39+
secp256k1_testrand256(&internal_pk[1]);
40+
temp_rand = secp256k1_testrand_int(2) + 2; /* randomly choose 2 or 3 */
41+
internal_pk[0] = (unsigned char)temp_rand;
42+
43+
/* check bitflip in any argument results in generates randomizers */
44+
args[0] = tweaked_pk;
45+
args[1] = &tweaked_pk_parity;
46+
args[2] = internal_pk;
47+
args[3] = tweak;
48+
49+
for (j = 0; j < count; j++) {
50+
batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 0, 32);
51+
batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 1, 1);
52+
batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 2, 32);
53+
batch_xonlypub_tweak_randomizer_gen_bitflip(&sha, args, 3, 33);
54+
}
55+
56+
/* write i-th tweak check data to the sha object
57+
* this is required for generating the next randomizer */
58+
secp256k1_sha256_write(&sha, tweaked_pk, 32);
59+
secp256k1_sha256_write(&sha, &tweaked_pk_parity, 1);
60+
secp256k1_sha256_write(&sha, tweak, 32);
61+
secp256k1_sha256_write(&sha, internal_pk, 33);
62+
}
763

864
}
965

src/modules/schnorrsig/batch_add_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ static void secp256k1_batch_schnorrsig_randomizer_gen(unsigned char *randomizer3
1919
}
2020

2121
static int secp256k1_batch_schnorrsig_randomizer_set(const secp256k1_context *ctx, secp256k1_batch *batch, secp256k1_scalar *r, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey) {
22-
int overflow;
2322
unsigned char randomizer[32];
2423
unsigned char buf[33];
2524
size_t buflen = sizeof(buf);
25+
int overflow;
2626

2727
/* We use compressed serialization here. If we would use
2828
* xonly_pubkey serialization and a user would wrongly memcpy

0 commit comments

Comments
 (0)