Skip to content

Commit 41fc785

Browse files
committed
Make ec_privkey functions aliases for ec_seckey_negate, ec_seckey_tweak_add and ec_seckey_mul
1 parent 22911ee commit 41fc785

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

include/secp256k1.h

+23
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,13 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
592592
* If this function returns 0, seckey will be some
593593
* unspecified value. (cannot be NULL)
594594
*/
595+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(
596+
const secp256k1_context* ctx,
597+
unsigned char *seckey
598+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
599+
600+
/** Same as secp256k1_ec_seckey_negate, but DEPRECATED. Will be removed in
601+
* future versions. */
595602
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_negate(
596603
const secp256k1_context* ctx,
597604
unsigned char *seckey
@@ -621,6 +628,14 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_negate(
621628
* 32-byte arrays the chance of being out of range is
622629
* negligible (around 1 in 2^128). (cannot be NULL)
623630
*/
631+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(
632+
const secp256k1_context* ctx,
633+
unsigned char *seckey,
634+
const unsigned char *tweak
635+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
636+
637+
/** Same as secp256k1_ec_seckey_tweak_add, but DEPRECATED. Will be removed in
638+
* future versions. */
624639
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(
625640
const secp256k1_context* ctx,
626641
unsigned char *seckey,
@@ -655,6 +670,14 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(
655670
* 32-byte arrays the chance of being out of range is
656671
* negligible (around 1 in 2^128). (cannot be NULL)
657672
*/
673+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(
674+
const secp256k1_context* ctx,
675+
unsigned char *seckey,
676+
const unsigned char *tweak
677+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
678+
679+
/** Same as secp256k1_ec_seckey_tweak_mul, but DEPRECATED. Will be removed in
680+
* future versions. */
658681
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(
659682
const secp256k1_context* ctx,
660683
unsigned char *seckey,

src/secp256k1.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *p
554554
return ret;
555555
}
556556

557-
int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
557+
int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
558558
secp256k1_scalar sec;
559559
int ret = 0;
560560
VERIFY_CHECK(ctx != NULL);
@@ -569,6 +569,10 @@ int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *sec
569569
return ret;
570570
}
571571

572+
int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) {
573+
return secp256k1_ec_seckey_negate(ctx, seckey);
574+
}
575+
572576
int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) {
573577
int ret = 0;
574578
secp256k1_ge p;
@@ -584,7 +588,7 @@ int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *p
584588
return ret;
585589
}
586590

587-
int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
591+
int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
588592
secp256k1_scalar term;
589593
secp256k1_scalar sec;
590594
int ret = 0;
@@ -605,6 +609,10 @@ int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *
605609
return ret;
606610
}
607611

612+
int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
613+
return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak);
614+
}
615+
608616
int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
609617
secp256k1_ge p;
610618
secp256k1_scalar term;
@@ -629,7 +637,7 @@ int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey
629637
return ret;
630638
}
631639

632-
int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
640+
int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
633641
secp256k1_scalar factor;
634642
secp256k1_scalar sec;
635643
int ret = 0;
@@ -649,6 +657,10 @@ int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *
649657
return ret;
650658
}
651659

660+
int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak) {
661+
return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak);
662+
}
663+
652664
int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak) {
653665
secp256k1_ge p;
654666
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);
@@ -4007,20 +4007,20 @@ void run_eckey_edge_case_test(void) {
40074007
ctmp2[31] = 0x01;
40084008
CHECK(secp256k1_ec_seckey_verify(ctx, ctmp2) == 1);
40094009
CHECK(secp256k1_ec_seckey_verify(ctx, ctmp) == 0);
4010-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, ctmp2) == 0);
4010+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, ctmp2) == 0);
40114011
CHECK(memcmp(zeros, ctmp, 32) == 0);
40124012
memcpy(ctmp, orderc, 32);
4013-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, ctmp2) == 0);
4013+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, ctmp2) == 0);
40144014
CHECK(memcmp(zeros, ctmp, 32) == 0);
40154015
/* If seckey_tweak_add or seckey_tweak_mul are called with an overflowing
40164016
tweak, the seckey is zeroized. */
40174017
memcpy(ctmp, orderc, 32);
40184018
ctmp[31] = 0x40;
4019-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, orderc) == 0);
4019+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, orderc) == 0);
40204020
CHECK(memcmp(zeros, ctmp, 32) == 0);
40214021
memcpy(ctmp, orderc, 32);
40224022
ctmp[31] = 0x40;
4023-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, orderc) == 0);
4023+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, orderc) == 0);
40244024
CHECK(memcmp(zeros, ctmp, 32) == 0);
40254025
memcpy(ctmp, orderc, 32);
40264026
ctmp[31] = 0x40;
@@ -4039,15 +4039,15 @@ void run_eckey_edge_case_test(void) {
40394039
ctmp[31] = 0x40;
40404040
memset(ctmp2, 0, 32);
40414041
ctmp2[31] = 1;
4042-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 0);
4042+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp2, ctmp) == 0);
40434043
CHECK(memcmp(zeros, ctmp2, 32) == 0);
40444044
ctmp2[31] = 1;
40454045
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 0);
40464046
CHECK(memcmp(&pubkey, zeros, sizeof(pubkey)) == 0);
40474047
memcpy(&pubkey, &pubkey2, sizeof(pubkey));
40484048
/* Tweak computation wraps and results in a key of 1. */
40494049
ctmp2[31] = 2;
4050-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp2, ctmp) == 1);
4050+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp2, ctmp) == 1);
40514051
CHECK(memcmp(ctmp2, zeros, 31) == 0 && ctmp2[31] == 1);
40524052
ctmp2[31] = 2;
40534053
CHECK(secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, ctmp2) == 1);
@@ -4095,16 +4095,16 @@ void run_eckey_edge_case_test(void) {
40954095
CHECK(ecount == 2);
40964096
ecount = 0;
40974097
memset(ctmp2, 0, 32);
4098-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, NULL, ctmp2) == 0);
4098+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, NULL, ctmp2) == 0);
40994099
CHECK(ecount == 1);
4100-
CHECK(secp256k1_ec_privkey_tweak_add(ctx, ctmp, NULL) == 0);
4100+
CHECK(secp256k1_ec_seckey_tweak_add(ctx, ctmp, NULL) == 0);
41014101
CHECK(ecount == 2);
41024102
ecount = 0;
41034103
memset(ctmp2, 0, 32);
41044104
ctmp2[31] = 1;
4105-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, NULL, ctmp2) == 0);
4105+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, NULL, ctmp2) == 0);
41064106
CHECK(ecount == 1);
4107-
CHECK(secp256k1_ec_privkey_tweak_mul(ctx, ctmp, NULL) == 0);
4107+
CHECK(secp256k1_ec_seckey_tweak_mul(ctx, ctmp, NULL) == 0);
41084108
CHECK(ecount == 2);
41094109
ecount = 0;
41104110
CHECK(secp256k1_ec_pubkey_create(ctx, NULL, ctmp) == 0);
@@ -4184,26 +4184,31 @@ void run_eckey_negate_test(void) {
41844184
random_scalar_order_b32(seckey);
41854185
memcpy(seckey_tmp, seckey, 32);
41864186

4187-
/* Verify negation changes the key and changes it back */
4188-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 1);
4187+
/* Verify negation changes the key and changes it back */
4188+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
41894189
CHECK(memcmp(seckey, seckey_tmp, 32) != 0);
4190-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 1);
4190+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
4191+
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
4192+
4193+
/* Check that privkey alias gives same result */
4194+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 1);
4195+
CHECK(secp256k1_ec_privkey_negate(ctx, seckey_tmp) == 1);
41914196
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
41924197

4193-
/* Negating all 0s fails */
4198+
/* Negating all 0s fails */
41944199
memset(seckey, 0, 32);
41954200
memset(seckey_tmp, 0, 32);
4196-
CHECK(secp256k1_ec_privkey_negate(ctx, seckey) == 0);
4201+
CHECK(secp256k1_ec_seckey_negate(ctx, seckey) == 0);
41974202
/* Check that seckey is not modified */
41984203
CHECK(memcmp(seckey, seckey_tmp, 32) == 0);
41994204

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

@@ -4346,15 +4351,22 @@ void test_ecdsa_end_to_end(void) {
43464351
if (secp256k1_rand_int(3) == 0) {
43474352
int ret1;
43484353
int ret2;
4354+
int ret3;
43494355
unsigned char rnd[32];
4356+
unsigned char privkey_tmp[32];
43504357
secp256k1_pubkey pubkey2;
43514358
secp256k1_rand256_test(rnd);
4352-
ret1 = secp256k1_ec_privkey_tweak_add(ctx, privkey, rnd);
4359+
memcpy(privkey_tmp, privkey, 32);
4360+
ret1 = secp256k1_ec_seckey_tweak_add(ctx, privkey, rnd);
43534361
ret2 = secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, rnd);
4362+
/* Check that privkey alias gives same result */
4363+
ret3 = secp256k1_ec_privkey_tweak_add(ctx, privkey_tmp, rnd);
43544364
CHECK(ret1 == ret2);
4365+
CHECK(ret2 == ret3);
43554366
if (ret1 == 0) {
43564367
return;
43574368
}
4369+
CHECK(memcmp(privkey, privkey_tmp, 32) == 0);
43584370
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
43594371
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
43604372
}
@@ -4363,15 +4375,22 @@ void test_ecdsa_end_to_end(void) {
43634375
if (secp256k1_rand_int(3) == 0) {
43644376
int ret1;
43654377
int ret2;
4378+
int ret3;
43664379
unsigned char rnd[32];
4380+
unsigned char privkey_tmp[32];
43674381
secp256k1_pubkey pubkey2;
43684382
secp256k1_rand256_test(rnd);
4369-
ret1 = secp256k1_ec_privkey_tweak_mul(ctx, privkey, rnd);
4383+
memcpy(privkey_tmp, privkey, 32);
4384+
ret1 = secp256k1_ec_seckey_tweak_mul(ctx, privkey, rnd);
43704385
ret2 = secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, rnd);
4386+
/* Check that privkey alias gives same result */
4387+
ret3 = secp256k1_ec_privkey_tweak_mul(ctx, privkey_tmp, rnd);
43714388
CHECK(ret1 == ret2);
4389+
CHECK(ret2 == ret3);
43724390
if (ret1 == 0) {
43734391
return;
43744392
}
4393+
CHECK(memcmp(privkey, privkey_tmp, 32) == 0);
43754394
CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey2, privkey) == 1);
43764395
CHECK(memcmp(&pubkey, &pubkey2, sizeof(pubkey)) == 0);
43774396
}

src/valgrind_ctime_test.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ int main(void) {
7373
CHECK(ret == 1);
7474

7575
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
76-
ret = secp256k1_ec_privkey_negate(ctx, key);
76+
ret = secp256k1_ec_seckey_negate(ctx, key);
7777
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
7878
CHECK(ret == 1);
7979

8080
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
8181
VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
82-
ret = secp256k1_ec_privkey_tweak_add(ctx, key, msg);
82+
ret = secp256k1_ec_seckey_tweak_add(ctx, key, msg);
8383
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
8484
CHECK(ret == 1);
8585

8686
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
8787
VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
88-
ret = secp256k1_ec_privkey_tweak_mul(ctx, key, msg);
88+
ret = secp256k1_ec_seckey_tweak_mul(ctx, key, msg);
8989
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
9090
CHECK(ret == 1);
9191

0 commit comments

Comments
 (0)