Skip to content

Commit c244564

Browse files
committed
Make ec_privkey functions aliases for ec_seckey_negate, ec_seckey_tweak_add and ec_seckey_mul
1 parent 9c30a48 commit c244564

File tree

3 files changed

+78
-24
lines changed

3 files changed

+78
-24
lines changed

include/secp256k1.h

+23
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,13 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
588588
* If this function returns 0, seckey will be some
589589
* unspecified value. (cannot be NULL)
590590
*/
591+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(
592+
const secp256k1_context* ctx,
593+
unsigned char *seckey
594+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
595+
596+
/** Same as secp256k1_ec_seckey_negate, but DEPRECATED. Will be removed in
597+
* future versions. */
591598
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
592599
const secp256k1_context* ctx,
593600
unsigned char *seckey
@@ -617,6 +624,14 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
617624
* 32-byte arrays the chance of being out of range is
618625
* negligible (around 1 in 2^128). (cannot be NULL)
619626
*/
627+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(
628+
const secp256k1_context* ctx,
629+
unsigned char *seckey,
630+
const unsigned char *tweak
631+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
632+
633+
/** Same as secp256k1_ec_seckey_tweak_add, but DEPRECATED. Will be removed in
634+
* future versions. */
620635
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
621636
const secp256k1_context* ctx,
622637
unsigned char *seckey,
@@ -651,6 +666,14 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
651666
* 32-byte arrays the chance of being out of range is
652667
* negligible (around 1 in 2^128). (cannot be NULL)
653668
*/
669+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(
670+
const secp256k1_context* ctx,
671+
unsigned char *seckey,
672+
const unsigned char *tweak
673+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
674+
675+
/** Same as secp256k1_ec_seckey_tweak_mul, but DEPRECATED. Will be removed in
676+
* future versions. */
654677
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
655678
const secp256k1_context* ctx,
656679
unsigned char *seckey,

src/secp256k1.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *p
521521
return ret;
522522
}
523523

524-
int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
524+
int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
525525
secp256k1_scalar sec;
526526
VERIFY_CHECK(ctx != NULL);
527527
ARG_CHECK(seckey != NULL);
@@ -536,6 +536,10 @@ int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *sec
536536
return 1;
537537
}
538538

539+
int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
540+
return secp256k1_ec_seckey_negate(ctx, seckey);
541+
}
542+
539543
int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
540544
int ret = 0;
541545
secp256k1_ge p;
@@ -551,7 +555,7 @@ int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *p
551555
return ret;
552556
}
553557

554-
int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
558+
int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
555559
secp256k1_scalar term;
556560
secp256k1_scalar sec;
557561
int ret = 0;
@@ -574,6 +578,10 @@ int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *
574578
return ret;
575579
}
576580

581+
int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
582+
return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak);
583+
}
584+
577585
int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
578586
secp256k1_ge p;
579587
secp256k1_scalar term;
@@ -598,7 +606,7 @@ int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey
598606
return ret;
599607
}
600608

601-
int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
609+
int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
602610
secp256k1_scalar factor;
603611
secp256k1_scalar sec;
604612
int ret = 0;
@@ -621,6 +629,10 @@ int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *
621629
return ret;
622630
}
623631

632+
int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
633+
return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak);
634+
}
635+
624636
int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
625637
secp256k1_ge p;
626638
secp256k1_scalar factor;

src/tests.c

+40-21
Original file line numberDiff line numberDiff line change
@@ -3983,13 +3983,13 @@ void run_eckey_edge_case_test(void) {
39833983
pubkey_negone = pubkey;
39843984
/* Tweak of zero leaves the value unchanged. */
39853985
memset(ctmp2, 0, 32);
3986-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, ctmp2) == 1);
3986+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, ctmp2) == 1);
39873987
CHECK(memcmp(orderc, ctmp, 31) == 0 && ctmp[31] == 0x40);
39883988
memcpy(&pubkey2, &pubkey, sizeof(pubkey));
39893989
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
39903990
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
39913991
/* Multiply tweak of zero zeroizes the output. */
3992-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, ctmp2) == 0);
3992+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, ctmp2) == 0);
39933993
CHECK(memcmp(zeros, ctmp, 32) == 0);
39943994
CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp2) == 0);
39953995
CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
@@ -3999,19 +3999,19 @@ void run_eckey_edge_case_test(void) {
39993999
memset(ctmp2, 0, 32);
40004000
ctmp2[31] = 0x01;
40014001
CHECK(secp256k1_ec_seckey_verify(ctx, ctmp2) == 1);
4002-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, ctmp2) == 0);
4002+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, ctmp2) == 0);
40034003
CHECK(memcmp(zeros, ctmp, 32) == 0);
40044004
memcpy(ctmp, orderc, 32);
4005-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, ctmp2) == 0);
4005+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, ctmp2) == 0);
40064006
CHECK(memcmp(zeros, ctmp, 32) == 0);
40074007
/* Overflowing key tweak zeroizes. */
40084008
memcpy(ctmp, orderc, 32);
40094009
ctmp[31] = 0x40;
4010-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, orderc) == 0);
4010+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, orderc) == 0);
40114011
CHECK(memcmp(zeros, ctmp, 32) == 0);
40124012
memcpy(ctmp, orderc, 32);
40134013
ctmp[31] = 0x40;
4014-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, orderc) == 0);
4014+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, orderc) == 0);
40154015
CHECK(memcmp(zeros, ctmp, 32) == 0);
40164016
memcpy(ctmp, orderc, 32);
40174017
ctmp[31] = 0x40;
@@ -4023,15 +4023,15 @@ void run_eckey_edge_case_test(void) {
40234023
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
40244024
/* Private key tweaks results in a key of zero. */
40254025
ctmp2[31] = 1;
4026-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 0);
4026+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp2, ctmp) == 0);
40274027
CHECK(memcmp(zeros, ctmp2, 32) == 0);
40284028
ctmp2[31] = 1;
40294029
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 0);
40304030
CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
40314031
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
40324032
/* Tweak computation wraps and results in a key of 1. */
40334033
ctmp2[31] = 2;
4034-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 1);
4034+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp2, ctmp) == 1);
40354035
CHECK(memcmp(ctmp2, zeros, 31) == 0 && ctmp2[31] == 1);
40364036
ctmp2[31] = 2;
40374037
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
@@ -4079,16 +4079,16 @@ void run_eckey_edge_case_test(void) {
40794079
CHECK(ecount == 2);
40804080
ecount = 0;
40814081
memset(ctmp2, 0, 32);
4082-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, NULL, ctmp2) == 0);
4082+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, NULL, ctmp2) == 0);
40834083
CHECK(ecount == 1);
4084-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, NULL) == 0);
4084+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, NULL) == 0);
40854085
CHECK(ecount == 2);
40864086
ecount = 0;
40874087
memset(ctmp2, 0, 32);
40884088
ctmp2[31] = 1;
4089-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, NULL, ctmp2) == 0);
4089+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, NULL, ctmp2) == 0);
40904090
CHECK(ecount == 1);
4091-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, NULL) == 0);
4091+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, NULL) == 0);
40924092
CHECK(ecount == 2);
40934093
ecount = 0;
40944094
CHECK(secp256k1_ec_pubkey_create(ctx, NULL, ctmp) == 0);
@@ -4168,27 +4168,32 @@ void run_eckey_negate_test(void) {
41684168
secp256k1_rand256(seckey);
41694169
memcpy(seckey_tmp, seckey, 32);
41704170

4171-
/* Verify negation changes the key and changes it back */
4172-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 1);
4171+
/* Verify negation changes the key and changes it back */
4172+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
41734173
CHECK(memcmp(seckey, seckey_tmp, 32) != 0);
4174-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 1);
4174+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
4175+
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
4176+
4177+
/* Check that privkey alias gives same result */
4178+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
4179+
CHECK(secp256k1_ec_privkey_negate(ctx, seckey_tmp) == 1);
41754180
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
41764181

4177-
/* Negating all 0s fails */
4182+
/* Negating all 0s fails */
41784183
memset(seckey, 0, 32);
41794184
memset(seckey_tmp, 0, 32);
4180-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 0);
4185+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 0);
41814186
/* Check that seckey is not modified */
41824187
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
41834188

41844189
/* Negating an overflowing seckey fails and the seckey is not modified. In
41854190
* this test, the seckey has 16 random bytes to ensure that
4186-
* ec_privkey_negate doesn't just set seckey to a constant value in case of
4191+
* ec_seckey_negate doesn't just set seckey to a constant value in case of
41874192
* failure.*/
41884193
secp256k1_rand256_test(seckey);
41894194
memset(seckey, 0xFF, 16);
41904195
memcpy(seckey_tmp, seckey, 32);
4191-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 0);
4196+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 0);
41924197
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
41934198
}
41944199

@@ -4331,15 +4336,22 @@ void test_ecdsa_end_to_end(void) {
43314336
if (secp256k1_rand_int(3) == 0) {
43324337
int ret1;
43334338
int ret2;
4339+
int ret3;
43344340
unsigned char rnd[32];
4341+
unsigned char privkey_tmp[32];
43354342
secp256k1_pubkey pubkey2;
43364343
secp256k1_rand256_test(rnd);
4337-
ret1 = secp256k1_ec_privkey_tweak_add(ctx, privkey, rnd);
4344+
memcpy(privkey_tmp, privkey, 32);
4345+
ret1 = secp256k1_ec_seckey_tweak_add(ctx, privkey, rnd);
43384346
ret2 = secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, rnd);
4347+
/* Check that privkey alias gives same result */
4348+
ret3 = secp256k1_ec_privkey_tweak_add(ctx, privkey_tmp, rnd);
43394349
CHECK(ret1 == ret2);
4350+
CHECK(ret2 == ret3);
43404351
if (ret1 == 0) {
43414352
return;
43424353
}
4354+
CHECK(memcmp(privkey, privkey_tmp, 32) == 0);
43434355
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
43444356
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
43454357
}
@@ -4348,15 +4360,22 @@ void test_ecdsa_end_to_end(void) {
43484360
if (secp256k1_rand_int(3) == 0) {
43494361
int ret1;
43504362
int ret2;
4363+
int ret3;
43514364
unsigned char rnd[32];
4365+
unsigned char privkey_tmp[32];
43524366
secp256k1_pubkey pubkey2;
43534367
secp256k1_rand256_test(rnd);
4354-
ret1 = secp256k1_ec_privkey_tweak_mul(ctx, privkey, rnd);
4368+
memcpy(privkey_tmp, privkey, 32);
4369+
ret1 = secp256k1_ec_seckey_tweak_mul(ctx, privkey, rnd);
43554370
ret2 = secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, rnd);
4371+
/* Check that privkey alias gives same result */
4372+
ret3 = secp256k1_ec_privkey_tweak_mul(ctx, privkey_tmp, rnd);
43564373
CHECK(ret1 == ret2);
4374+
CHECK(ret2 == ret3);
43574375
if (ret1 == 0) {
43584376
return;
43594377
}
4378+
CHECK(memcmp(privkey, privkey_tmp, 32) == 0);
43604379
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
43614380
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
43624381
}

0 commit comments

Comments
 (0)