|
| 1 | +#ifndef SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_IMPL_H |
| 2 | +#define SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_IMPL_H |
| 3 | + |
| 4 | +#include "../../../include/secp256k1_extrakeys.h" |
| 5 | +#include "../../../include/secp256k1_tweak_check_batch.h" |
| 6 | +#include "../batch/main_impl.h" |
| 7 | + |
| 8 | +/* The number of scalar-point pairs allocated on the scratch space |
| 9 | + * by `secp256k1_batch_add_xonlypub_tweak_check` */ |
| 10 | +#define BATCH_TWEAK_CHECK_SCRATCH_OBJS 1 |
| 11 | + |
| 12 | +/** Computes a 16-byte deterministic randomizer by |
| 13 | + * SHA256(batch_add_tag || tweaked pubkey || parity || tweak || internal pubkey) */ |
| 14 | +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) { |
| 15 | + secp256k1_sha256 sha256_cpy; |
| 16 | + unsigned char batch_add_type = (unsigned char) tweak_check; |
| 17 | + |
| 18 | + secp256k1_sha256_write(sha256, &batch_add_type, sizeof(batch_add_type)); |
| 19 | + /* add tweaked pubkey check data to sha object */ |
| 20 | + secp256k1_sha256_write(sha256, tweaked_pubkey32, 32); |
| 21 | + secp256k1_sha256_write(sha256, tweaked_pk_parity, 1); |
| 22 | + secp256k1_sha256_write(sha256, tweak32, 32); |
| 23 | + secp256k1_sha256_write(sha256, internal_pk33, 33); |
| 24 | + |
| 25 | + /* generate randomizer */ |
| 26 | + sha256_cpy = *sha256; |
| 27 | + secp256k1_sha256_finalize(&sha256_cpy, randomizer32); |
| 28 | + /* 16 byte randomizer is sufficient */ |
| 29 | + memset(randomizer32, 0, 16); |
| 30 | +} |
| 31 | + |
| 32 | +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) { |
| 33 | + unsigned char randomizer[32]; |
| 34 | + unsigned char internal_buf[33]; |
| 35 | + size_t internal_buflen = sizeof(internal_buf); |
| 36 | + unsigned char parity = (unsigned char) tweaked_pk_parity; |
| 37 | + int overflow; |
| 38 | + /* t = 2^127 */ |
| 39 | + secp256k1_scalar t = SECP256K1_SCALAR_CONST(0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x80000000, 0x00000000, 0x00000000, 0x00000000); |
| 40 | + |
| 41 | + /* We use compressed serialization here. If we would use |
| 42 | + * xonly_pubkey serialization and a user would wrongly memcpy |
| 43 | + * normal secp256k1_pubkeys into xonly_pubkeys then the randomizer |
| 44 | + * would be the same for two different pubkeys. */ |
| 45 | + if (!secp256k1_ec_pubkey_serialize(ctx, internal_buf, &internal_buflen, (const secp256k1_pubkey *) internal_pubkey, SECP256K1_EC_COMPRESSED)) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + secp256k1_batch_xonlypub_tweak_randomizer_gen(randomizer, &batch->sha256, tweaked_pubkey32, &parity, internal_buf, tweak32); |
| 50 | + secp256k1_scalar_set_b32(r, randomizer, &overflow); |
| 51 | + /* Shift scalar to range [-2^127, 2^127-1] */ |
| 52 | + secp256k1_scalar_negate(&t, &t); |
| 53 | + secp256k1_scalar_add(r, r, &t); |
| 54 | + VERIFY_CHECK(overflow == 0); |
| 55 | + |
| 56 | + return 1; |
| 57 | +} |
| 58 | + |
| 59 | +/** Adds the given x-only tweaked public key check to the batch. |
| 60 | + * |
| 61 | + * Updates the batch object by: |
| 62 | + * 1. adding the point P-Q to the scratch space |
| 63 | + * -> the point is of type `secp256k1_gej` |
| 64 | + * 2. adding the scalar ai to the scratch space |
| 65 | + * -> ai is the scalar coefficient of P-Q (in multi multiplication) |
| 66 | + * 3. incrementing sc_g (scalar of G) by ai.tweak |
| 67 | + * |
| 68 | + * Conventions used above: |
| 69 | + * -> Q (tweaked pubkey) = EC point where parity(y) = tweaked_pk_parity |
| 70 | + * and x = tweaked_pubkey32 |
| 71 | + * -> P (internal pubkey) = internal pubkey |
| 72 | + * -> ai (randomizer) = sha256_tagged(batch_add_tag || tweaked_pubkey32 || |
| 73 | + * tweaked_pk_parity || tweak32 || pubkey) |
| 74 | + * -> tweak (challenge) = tweak32 |
| 75 | + * |
| 76 | + * This function is based on `secp256k1_xonly_pubkey_tweak_add_check`. |
| 77 | + */ |
| 78 | +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) { |
| 79 | + secp256k1_scalar tweak; |
| 80 | + secp256k1_scalar ai; |
| 81 | + secp256k1_ge pk; |
| 82 | + secp256k1_ge q; |
| 83 | + secp256k1_gej tmpj; |
| 84 | + secp256k1_fe qx; |
| 85 | + int overflow; |
| 86 | + size_t i; |
| 87 | + |
| 88 | + VERIFY_CHECK(ctx != NULL); |
| 89 | + ARG_CHECK(batch != NULL); |
| 90 | + ARG_CHECK(internal_pubkey != NULL); |
| 91 | + ARG_CHECK(tweaked_pubkey32 != NULL); |
| 92 | + ARG_CHECK(tweak32 != NULL); |
| 93 | + |
| 94 | + if(batch->result == 0) { |
| 95 | + return 0; |
| 96 | + } |
| 97 | + |
| 98 | + if (!secp256k1_fe_set_b32_limit(&qx, tweaked_pubkey32)) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + secp256k1_scalar_set_b32(&tweak, tweak32, &overflow); |
| 103 | + if (overflow) { |
| 104 | + return 0; |
| 105 | + } |
| 106 | + |
| 107 | + if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)) { |
| 108 | + return 0; |
| 109 | + } |
| 110 | + |
| 111 | + /* if insufficient space in batch, verify the inputs (stored in curr batch) and |
| 112 | + * save the result. This extends the batch capacity since `secp256k1_batch_verify` |
| 113 | + * clears the batch after verification. */ |
| 114 | + if (batch->capacity - batch->len < BATCH_TWEAK_CHECK_SCRATCH_OBJS) { |
| 115 | + secp256k1_batch_verify(ctx, batch); |
| 116 | + } |
| 117 | + |
| 118 | + i = batch->len; |
| 119 | + /* append point P-Q to the scratch space */ |
| 120 | + if (!secp256k1_ge_set_xo_var(&q, &qx, tweaked_pk_parity)) { |
| 121 | + return 0; |
| 122 | + } |
| 123 | + if (!secp256k1_ge_is_in_correct_subgroup(&q)) { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + secp256k1_ge_neg(&q, &q); |
| 127 | + secp256k1_gej_set_ge(&tmpj, &q); |
| 128 | + secp256k1_gej_add_ge_var(&tmpj, &tmpj, &pk, NULL); |
| 129 | + batch->points[i] = tmpj; |
| 130 | + |
| 131 | + /* Compute ai (randomizer) */ |
| 132 | + if (batch->len == 0) { |
| 133 | + /* set randomizer as 1 for the first term in batch */ |
| 134 | + ai = secp256k1_scalar_one; |
| 135 | + } else if(!secp256k1_batch_xonlypub_tweak_randomizer_set(ctx, batch, &ai, tweaked_pubkey32, tweaked_pk_parity, internal_pubkey, tweak32)) { |
| 136 | + return 0; |
| 137 | + } |
| 138 | + |
| 139 | + /* append scalar ai to scratch space */ |
| 140 | + batch->scalars[i] = ai; |
| 141 | + |
| 142 | + /* increment scalar of G by ai.tweak */ |
| 143 | + secp256k1_scalar_mul(&tweak, &tweak, &ai); |
| 144 | + secp256k1_scalar_add(&batch->sc_g, &batch->sc_g, &tweak); |
| 145 | + |
| 146 | + batch->len += 1; |
| 147 | + |
| 148 | + return 1; |
| 149 | +} |
| 150 | + |
| 151 | +#endif /* SECP256K1_MODULE_EXTRAKEYS_BATCH_ADD_IMPL_H */ |
0 commit comments