Skip to content

Commit 76d90c0

Browse files
committed
Make ec_privkey functions aliases for ec_seckey_negate, ec_seckey_tweak_add and ec_seckey_mul
1 parent 45d482e commit 76d90c0

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
@@ -3989,13 +3989,13 @@ void run_eckey_edge_case_test(void) {
39893989
pubkey_negone = pubkey;
39903990
/* Tweak of zero leaves the value unchanged. */
39913991
memset(ctmp2, 0, 32);
3992-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, ctmp2) == 1);
3992+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, ctmp2) == 1);
39933993
CHECK(memcmp(orderc, ctmp, 31) == 0 && ctmp[31] == 0x40);
39943994
memcpy(&pubkey2, &pubkey, sizeof(pubkey));
39953995
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
39963996
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
39973997
/* Multiply tweak of zero zeroizes the output. */
3998-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, ctmp2) == 0);
3998+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, ctmp2) == 0);
39993999
CHECK(memcmp(zeros, ctmp, 32) == 0);
40004000
CHECK(secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, ctmp2) == 0);
40014001
CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
@@ -4006,20 +4006,20 @@ void run_eckey_edge_case_test(void) {
40064006
memset(ctmp2, 0, 32);
40074007
ctmp2[31] = 0x01;
40084008
CHECK(secp256k1_ec_seckey_verify(ctx, ctmp2) == 1);
4009-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, ctmp2) == 0);
4009+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, ctmp2) == 0);
40104010
CHECK(memcmp(zeros, ctmp, 32) == 0);
40114011
memcpy(ctmp, orderc, 32);
4012-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, ctmp2) == 0);
4012+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, ctmp2) == 0);
40134013
CHECK(memcmp(zeros, ctmp, 32) == 0);
40144014
/* If seckey_tweak_add or seckey_tweak_mul are called with an overflowing
40154015
tweak, the seckey is zeroized. */
40164016
memcpy(ctmp, orderc, 32);
40174017
ctmp[31] = 0x40;
4018-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, orderc) == 0);
4018+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, orderc) == 0);
40194019
CHECK(memcmp(zeros, ctmp, 32) == 0);
40204020
memcpy(ctmp, orderc, 32);
40214021
ctmp[31] = 0x40;
4022-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, orderc) == 0);
4022+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, orderc) == 0);
40234023
CHECK(memcmp(zeros, ctmp, 32) == 0);
40244024
memcpy(ctmp, orderc, 32);
40254025
ctmp[31] = 0x40;
@@ -4033,15 +4033,15 @@ void run_eckey_edge_case_test(void) {
40334033
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
40344034
/* Private key tweaks results in a key of zero. */
40354035
ctmp2[31] = 1;
4036-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 0);
4036+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp2, ctmp) == 0);
40374037
CHECK(memcmp(zeros, ctmp2, 32) == 0);
40384038
ctmp2[31] = 1;
40394039
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 0);
40404040
CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
40414041
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
40424042
/* Tweak computation wraps and results in a key of 1. */
40434043
ctmp2[31] = 2;
4044-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 1);
4044+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp2, ctmp) == 1);
40454045
CHECK(memcmp(ctmp2, zeros, 31) == 0 && ctmp2[31] == 1);
40464046
ctmp2[31] = 2;
40474047
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
@@ -4089,16 +4089,16 @@ void run_eckey_edge_case_test(void) {
40894089
CHECK(ecount == 2);
40904090
ecount = 0;
40914091
memset(ctmp2, 0, 32);
4092-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, NULL, ctmp2) == 0);
4092+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, NULL, ctmp2) == 0);
40934093
CHECK(ecount == 1);
4094-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, NULL) == 0);
4094+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, NULL) == 0);
40954095
CHECK(ecount == 2);
40964096
ecount = 0;
40974097
memset(ctmp2, 0, 32);
40984098
ctmp2[31] = 1;
4099-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, NULL, ctmp2) == 0);
4099+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, NULL, ctmp2) == 0);
41004100
CHECK(ecount == 1);
4101-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, NULL) == 0);
4101+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, NULL) == 0);
41024102
CHECK(ecount == 2);
41034103
ecount = 0;
41044104
CHECK(secp256k1_ec_pubkey_create(ctx, NULL, ctmp) == 0);
@@ -4178,27 +4178,32 @@ void run_eckey_negate_test(void) {
41784178
random_scalar_order_b32(seckey);
41794179
memcpy(seckey_tmp, seckey, 32);
41804180

4181-
/* Verify negation changes the key and changes it back */
4182-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 1);
4181+
/* Verify negation changes the key and changes it back */
4182+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
41834183
CHECK(memcmp(seckey, seckey_tmp, 32) != 0);
4184-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 1);
4184+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
4185+
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
4186+
4187+
/* Check that privkey alias gives same result */
4188+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
4189+
CHECK(secp256k1_ec_privkey_negate(ctx, seckey_tmp) == 1);
41854190
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
41864191

4187-
/* Negating all 0s fails */
4192+
/* Negating all 0s fails */
41884193
memset(seckey, 0, 32);
41894194
memset(seckey_tmp, 0, 32);
4190-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 0);
4195+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 0);
41914196
/* Check that seckey is not modified */
41924197
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
41934198

41944199
/* Negating an overflowing seckey fails and the seckey is not modified. In
41954200
* this test, the seckey has 16 random bytes to ensure that
4196-
* ec_privkey_negate doesn't just set seckey to a constant value in case of
4201+
* ec_seckey_negate doesn't just set seckey to a constant value in case of
41974202
* failure.*/
41984203
random_scalar_order_b32(seckey);
41994204
memset(seckey, 0xFF, 16);
42004205
memcpy(seckey_tmp, seckey, 32);
4201-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 0);
4206+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 0);
42024207
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
42034208
}
42044209

@@ -4341,15 +4346,22 @@ void test_ecdsa_end_to_end(void) {
43414346
if (secp256k1_rand_int(3) == 0) {
43424347
int ret1;
43434348
int ret2;
4349+
int ret3;
43444350
unsigned char rnd[32];
4351+
unsigned char privkey_tmp[32];
43454352
secp256k1_pubkey pubkey2;
43464353
secp256k1_rand256_test(rnd);
4347-
ret1 = secp256k1_ec_privkey_tweak_add(ctx, privkey, rnd);
4354+
memcpy(privkey_tmp, privkey, 32);
4355+
ret1 = secp256k1_ec_seckey_tweak_add(ctx, privkey, rnd);
43484356
ret2 = secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, rnd);
4357+
/* Check that privkey alias gives same result */
4358+
ret3 = secp256k1_ec_privkey_tweak_add(ctx, privkey_tmp, rnd);
43494359
CHECK(ret1 == ret2);
4360+
CHECK(ret2 == ret3);
43504361
if (ret1 == 0) {
43514362
return;
43524363
}
4364+
CHECK(memcmp(privkey, privkey_tmp, 32) == 0);
43534365
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
43544366
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
43554367
}
@@ -4358,15 +4370,22 @@ void test_ecdsa_end_to_end(void) {
43584370
if (secp256k1_rand_int(3) == 0) {
43594371
int ret1;
43604372
int ret2;
4373+
int ret3;
43614374
unsigned char rnd[32];
4375+
unsigned char privkey_tmp[32];
43624376
secp256k1_pubkey pubkey2;
43634377
secp256k1_rand256_test(rnd);
4364-
ret1 = secp256k1_ec_privkey_tweak_mul(ctx, privkey, rnd);
4378+
memcpy(privkey_tmp, privkey, 32);
4379+
ret1 = secp256k1_ec_seckey_tweak_mul(ctx, privkey, rnd);
43654380
ret2 = secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, rnd);
4381+
/* Check that privkey alias gives same result */
4382+
ret3 = secp256k1_ec_privkey_tweak_mul(ctx, privkey_tmp, rnd);
43664383
CHECK(ret1 == ret2);
4384+
CHECK(ret2 == ret3);
43674385
if (ret1 == 0) {
43684386
return;
43694387
}
4388+
CHECK(memcmp(privkey, privkey_tmp, 32) == 0);
43704389
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
43714390
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
43724391
}

0 commit comments

Comments
 (0)