Skip to content

Commit 7de7a2a

Browse files
committed
fix pre_sign. Now example passess
1 parent 061ce25 commit 7de7a2a

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

examples/schnorr_adaptor.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int main(void) {
4040
* for instance, the upstream PTLC secret he discovers. */
4141
unsigned char sec_adaptor[32];
4242
/* Secret adaptor t' (identical to t) that Alice extracts from
43-
* her pre-signature and BIP340 signature */
43+
* the pre-signature and BIP340 signature */
4444
unsigned char extracted_sec_adaptor[32];
4545

4646
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
@@ -53,7 +53,7 @@ int main(void) {
5353
}
5454
/* Adaptor point T (= t*G) is given to Alice by original PTLC sender */
5555
return_val = secp256k1_ec_pubkey_create(ctx, &adaptor_pk, sec_adaptor);
56-
assert(return_val == 1);
56+
assert(return_val);
5757

5858
/* Alice generates her keypair, and the message hash to sign */
5959
if (!fill_random(alice_seckey, sizeof(alice_seckey))) {
@@ -72,19 +72,19 @@ int main(void) {
7272
/* Alice creates a pre-signature using the adaptor point T(=Z+A+B),
7373
* and sends it to Bob */
7474
return_val = secp256k1_schnorr_adaptor_presign(ctx, pre_signature, msg_hash, &alice_keypair, &adaptor_pk, NULL);
75-
assert(return_val == 1);
75+
assert(return_val);
7676

7777
/* Bob extracts T from the pre-signature */
7878
return_val = secp256k1_schnorr_adaptor_extract(ctx, &extracted_adaptor_pk, pre_signature, msg_hash, &alice_pubkey);
79-
assert(return_val == 1);
79+
assert(return_val);
8080
assert(secp256k1_ec_pubkey_cmp(ctx, &adaptor_pk, &extracted_adaptor_pk) == 0);
8181

8282
/* Bob forwards the PTLC. Bob's outgoing PTLC (Z+A+B+C) is claimed
8383
* by Carol. Bob decides to go to chain with the full signature to
8484
* claim PTLC, subtracting local secret `c`.
8585
*/
8686
return_val = secp256k1_schnorr_adaptor_adapt(ctx, signature, pre_signature, sec_adaptor);
87-
assert(return_val == 1);
87+
assert(return_val);
8888
/* Signature should be valid! */
8989
is_signature_valid = secp256k1_schnorrsig_verify(ctx, signature, msg_hash, 32, &alice_pubkey);
9090
assert(is_signature_valid);
@@ -96,9 +96,10 @@ int main(void) {
9696
assert(memcmp(sec_adaptor, extracted_sec_adaptor, sizeof(sec_adaptor)) == 0);
9797

9898
/* Alice subtracts out local blinding factor `b`, can now claim incoming
99-
* PTLC from Alice with sec_adaptor(=z+a) */
100-
101-
printf("Success!\n");
99+
* PTLC from Alice with sec_adaptor(=z+a)
100+
*/
102101

102+
printf("Success!\n\n");
103+
secp256k1_context_destroy(ctx);
103104
return 0;
104105
}

schnorr_adaptor_example

-320 Bytes
Binary file not shown.

src/modules/schnorr_adaptor/main_impl.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static int secp256k1_schnorr_adaptor_presign_internal(const secp256k1_context *c
108108
secp256k1_ge r, rp;
109109
secp256k1_ge pk;
110110
secp256k1_ge adaptor_ge;
111-
unsigned char nonce32[32] = {0};
111+
unsigned char nonce32[32] = { 0 };
112112
unsigned char pk_buf[32];
113113
unsigned char seckey[32];
114114
unsigned char adaptor_buff[33];
@@ -177,11 +177,12 @@ static int secp256k1_schnorr_adaptor_presign_internal(const secp256k1_context *c
177177
if (secp256k1_fe_is_odd(&rp.y)) {
178178
secp256k1_scalar_negate(&k, &k);
179179
}
180+
ret &= secp256k1_eckey_pubkey_serialize(&rp, pre_sig65, &cmprssd_len, 1);
181+
180182
secp256k1_schnorrsig_challenge(&e, &pre_sig65[1], msg32, 32, pk_buf);
181183
secp256k1_scalar_mul(&e, &e, &sk);
182184
secp256k1_scalar_add(&e, &e, &k);
183185
secp256k1_scalar_get_b32(&pre_sig65[33], &e);
184-
ret &= secp256k1_eckey_pubkey_serialize(&rp, pre_sig65, &cmprssd_len, 1);
185186

186187
secp256k1_memczero(pre_sig65, 65, !ret);
187188
secp256k1_scalar_clear(&k);
@@ -233,7 +234,7 @@ int secp256k1_schnorr_adaptor_extract(const secp256k1_context *ctx, secp256k1_pu
233234
secp256k1_fe_get_b32(buf, &pk.x);
234235
secp256k1_schnorrsig_challenge(&e, &pre_sig65[1], msg32, 32, buf);
235236

236-
/* Compute R = s*G - e*P */
237+
/* Compute R = s*G + (-e)*P */
237238
secp256k1_scalar_negate(&e, &e);
238239
secp256k1_gej_set_ge(&pkj, &pk);
239240
secp256k1_ecmult(&rj, &pkj, &e, &s);
@@ -245,12 +246,13 @@ int secp256k1_schnorr_adaptor_extract(const secp256k1_context *ctx, secp256k1_pu
245246
*
246247
* `adaptor_presign` negates the secret nonce k when R’.y is odd, during
247248
* the computation of the s value (i.e., presig[33:65]). Therefore, we need
248-
* to negate R = k*G (if R'.y is odd) before subtracting it from R'.
249+
* to negate R = k*G (if R'.y is odd) before subtracting it from R' = R + T.
249250
*
250251
* T = R' - R if R'.y is even
251-
* = R' + R if R'.y is odd
252+
* = R' + R if R'.y is odd
252253
*/
253-
if (secp256k1_fe_is_odd(&rp.y)) {
254+
secp256k1_fe_normalize_var(&rp.y);
255+
if (!secp256k1_fe_is_odd(&rp.y)) {
254256
secp256k1_gej_neg(&rj, &rj);
255257
}
256258
secp256k1_gej_add_ge_var(&adaptor_gej, &rj, &rp, NULL);

0 commit comments

Comments
 (0)