Skip to content

Commit d3a8952

Browse files
committed
fixup! move extrakeys to main
1 parent 468c5c4 commit d3a8952

File tree

10 files changed

+250
-247
lines changed

10 files changed

+250
-247
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ noinst_HEADERS += src/field.h
6464
noinst_HEADERS += src/field_impl.h
6565
noinst_HEADERS += src/bench.h
6666
noinst_HEADERS += src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h
67+
noinst_HEADERS += src/hsort.h
68+
noinst_HEADERS += src/hsort_impl.h
6769
noinst_HEADERS += contrib/lax_der_parsing.h
6870
noinst_HEADERS += contrib/lax_der_parsing.c
6971
noinst_HEADERS += contrib/lax_der_privatekey_parsing.h

include/secp256k1.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,20 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(
474474
const secp256k1_pubkey *pubkey2
475475
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
476476

477+
/** Sort public keys keys using lexicographic (of compressed serialization) order
478+
*
479+
* Returns: 0 if the arguments are invalid. 1 otherwise.
480+
*
481+
* Args: ctx: pointer to a context object
482+
* In: pubkeys: array of pointers to pubkeys to sort
483+
* n_pubkeys: number of elements in the pubkeys array
484+
*/
485+
SECP256K1_API int secp256k1_pubkey_sort(
486+
const secp256k1_context *ctx,
487+
const secp256k1_pubkey **pubkeys,
488+
size_t n_pubkeys
489+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
490+
477491
/** Parse an ECDSA signature in compact (64 bytes) format.
478492
*
479493
* Returns: 1 when the signature could be parsed, 0 otherwise.

include/secp256k1_extrakeys.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,6 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add
240240
const unsigned char *tweak32
241241
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
242242

243-
/** Sort public keys using lexicographic order of their compressed
244-
* serialization.
245-
*
246-
* Returns: 0 if the arguments are invalid. 1 otherwise.
247-
*
248-
* Args: ctx: pointer to a context object
249-
* In: pubkeys: array of pointers to pubkeys to sort
250-
* n_pubkeys: number of elements in the pubkeys array
251-
*/
252-
SECP256K1_API int secp256k1_pubkey_sort(
253-
const secp256k1_context *ctx,
254-
const secp256k1_pubkey **pubkeys,
255-
size_t n_pubkeys
256-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
257-
258243
#ifdef __cplusplus
259244
}
260245
#endif
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
include_HEADERS += include/secp256k1_extrakeys.h
22
noinst_HEADERS += src/modules/extrakeys/tests_impl.h
33
noinst_HEADERS += src/modules/extrakeys/tests_exhaustive_impl.h
4-
noinst_HEADERS += src/modules/extrakeys/main_impl.h
5-
noinst_HEADERS += src/modules/extrakeys/hsort.h
6-
noinst_HEADERS += src/modules/extrakeys/hsort_impl.h
4+
noinst_HEADERS += src/modules/extrakeys/main_impl.h

src/modules/extrakeys/main_impl.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "../../../include/secp256k1.h"
1111
#include "../../../include/secp256k1_extrakeys.h"
12-
#include "hsort_impl.h"
1312
#include "../../util.h"
1413

1514
static SECP256K1_INLINE int secp256k1_xonly_pubkey_load(const secp256k1_context* ctx, secp256k1_ge *ge, const secp256k1_xonly_pubkey *pubkey) {
@@ -283,38 +282,4 @@ int secp256k1_keypair_xonly_tweak_add(const secp256k1_context* ctx, secp256k1_ke
283282
return ret;
284283
}
285284

286-
/* This struct wraps a const context pointer to satisfy the secp256k1_hsort api
287-
* which expects a non-const cmp_data pointer. */
288-
typedef struct {
289-
const secp256k1_context *ctx;
290-
} secp256k1_pubkey_sort_cmp_data;
291-
292-
static int secp256k1_pubkey_sort_cmp(const void* pk1, const void* pk2, void *cmp_data) {
293-
return secp256k1_ec_pubkey_cmp(((secp256k1_pubkey_sort_cmp_data*)cmp_data)->ctx,
294-
*(secp256k1_pubkey **)pk1,
295-
*(secp256k1_pubkey **)pk2);
296-
}
297-
298-
int secp256k1_pubkey_sort(const secp256k1_context* ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys) {
299-
secp256k1_pubkey_sort_cmp_data cmp_data;
300-
VERIFY_CHECK(ctx != NULL);
301-
ARG_CHECK(pubkeys != NULL);
302-
303-
cmp_data.ctx = ctx;
304-
305-
/* Suppress wrong warning (fixed in MSVC 19.33) */
306-
#if defined(_MSC_VER) && (_MSC_VER < 1933)
307-
#pragma warning(push)
308-
#pragma warning(disable: 4090)
309-
#endif
310-
311-
secp256k1_hsort(pubkeys, n_pubkeys, sizeof(*pubkeys), secp256k1_pubkey_sort_cmp, &cmp_data);
312-
313-
#if defined(_MSC_VER) && (_MSC_VER < 1933)
314-
#pragma warning(pop)
315-
#endif
316-
317-
return 1;
318-
}
319-
320285
#endif

src/modules/extrakeys/tests_impl.h

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -467,194 +467,6 @@ static void test_keypair_add(void) {
467467
}
468468
}
469469

470-
static void test_hsort_is_sorted(int *ints, size_t n) {
471-
size_t i;
472-
for (i = 1; i < n; i++) {
473-
CHECK(ints[i-1] <= ints[i]);
474-
}
475-
}
476-
477-
static int test_hsort_cmp(const void *i1, const void *i2, void *counter) {
478-
*(size_t*)counter += 1;
479-
return *(int*)i1 - *(int*)i2;
480-
}
481-
482-
#define NUM 64
483-
static void test_hsort(void) {
484-
int ints[NUM] = { 0 };
485-
size_t counter = 0;
486-
int i, j;
487-
488-
secp256k1_hsort(ints, 0, sizeof(ints[0]), test_hsort_cmp, &counter);
489-
CHECK(counter == 0);
490-
secp256k1_hsort(ints, 1, sizeof(ints[0]), test_hsort_cmp, &counter);
491-
CHECK(counter == 0);
492-
secp256k1_hsort(ints, NUM, sizeof(ints[0]), test_hsort_cmp, &counter);
493-
CHECK(counter > 0);
494-
test_hsort_is_sorted(ints, NUM);
495-
496-
/* Test hsort with length n array and random elements in
497-
* [-interval/2, interval/2] */
498-
for (i = 0; i < COUNT; i++) {
499-
int n = secp256k1_testrand_int(NUM);
500-
int interval = secp256k1_testrand_int(63) + 1;
501-
for (j = 0; j < n; j++) {
502-
ints[j] = secp256k1_testrand_int(interval) - interval/2;
503-
}
504-
secp256k1_hsort(ints, n, sizeof(ints[0]), test_hsort_cmp, &counter);
505-
test_hsort_is_sorted(ints, n);
506-
}
507-
}
508-
#undef NUM
509-
510-
static void test_sort_helper(secp256k1_pubkey *pk, size_t *pk_order, size_t n_pk) {
511-
size_t i;
512-
const secp256k1_pubkey *pk_test[5];
513-
514-
for (i = 0; i < n_pk; i++) {
515-
pk_test[i] = &pk[pk_order[i]];
516-
}
517-
secp256k1_pubkey_sort(CTX, pk_test, n_pk);
518-
for (i = 0; i < n_pk; i++) {
519-
CHECK(secp256k1_memcmp_var(pk_test[i], &pk[i], sizeof(*pk_test[i])) == 0);
520-
}
521-
}
522-
523-
static void permute(size_t *arr, size_t n) {
524-
size_t i;
525-
for (i = n - 1; i >= 1; i--) {
526-
size_t tmp, j;
527-
j = secp256k1_testrand_int(i + 1);
528-
tmp = arr[i];
529-
arr[i] = arr[j];
530-
arr[j] = tmp;
531-
}
532-
}
533-
534-
static void rand_pk(secp256k1_pubkey *pk) {
535-
unsigned char seckey[32];
536-
secp256k1_keypair keypair;
537-
secp256k1_testrand256(seckey);
538-
CHECK(secp256k1_keypair_create(CTX, &keypair, seckey) == 1);
539-
CHECK(secp256k1_keypair_pub(CTX, pk, &keypair) == 1);
540-
}
541-
542-
static void test_sort_api(void) {
543-
secp256k1_pubkey pks[2];
544-
const secp256k1_pubkey *pks_ptr[2];
545-
546-
pks_ptr[0] = &pks[0];
547-
pks_ptr[1] = &pks[1];
548-
549-
rand_pk(&pks[0]);
550-
rand_pk(&pks[1]);
551-
552-
CHECK(secp256k1_pubkey_sort(CTX, pks_ptr, 2) == 1);
553-
CHECK_ILLEGAL(CTX, secp256k1_pubkey_sort(CTX, NULL, 2));
554-
CHECK(secp256k1_pubkey_sort(CTX, pks_ptr, 0) == 1);
555-
/* Test illegal public keys */
556-
memset(&pks[0], 0, sizeof(pks[0]));
557-
CHECK_ILLEGAL_VOID(CTX, CHECK(secp256k1_pubkey_sort(CTX, pks_ptr, 2) == 1));
558-
memset(&pks[1], 0, sizeof(pks[1]));
559-
{
560-
int32_t ecount = 0;
561-
secp256k1_context_set_illegal_callback(CTX, counting_callback_fn, &ecount);
562-
CHECK(secp256k1_pubkey_sort(CTX, pks_ptr, 2) == 1);
563-
CHECK(ecount == 2);
564-
secp256k1_context_set_illegal_callback(CTX, NULL, NULL);
565-
}
566-
}
567-
568-
static void test_sort(void) {
569-
secp256k1_pubkey pk[5];
570-
unsigned char pk_ser[5][33] = {
571-
{ 0x02, 0x08 },
572-
{ 0x02, 0x0b },
573-
{ 0x02, 0x0c },
574-
{ 0x03, 0x05 },
575-
{ 0x03, 0x0a },
576-
};
577-
int i;
578-
size_t pk_order[5] = { 0, 1, 2, 3, 4 };
579-
580-
for (i = 0; i < 5; i++) {
581-
CHECK(secp256k1_ec_pubkey_parse(CTX, &pk[i], pk_ser[i], sizeof(pk_ser[i])));
582-
}
583-
584-
permute(pk_order, 1);
585-
test_sort_helper(pk, pk_order, 1);
586-
permute(pk_order, 2);
587-
test_sort_helper(pk, pk_order, 2);
588-
permute(pk_order, 3);
589-
test_sort_helper(pk, pk_order, 3);
590-
for (i = 0; i < COUNT; i++) {
591-
permute(pk_order, 4);
592-
test_sort_helper(pk, pk_order, 4);
593-
}
594-
for (i = 0; i < COUNT; i++) {
595-
permute(pk_order, 5);
596-
test_sort_helper(pk, pk_order, 5);
597-
}
598-
/* Check that sorting also works for random pubkeys */
599-
for (i = 0; i < COUNT; i++) {
600-
int j;
601-
const secp256k1_pubkey *pk_ptr[5];
602-
for (j = 0; j < 5; j++) {
603-
rand_pk(&pk[j]);
604-
pk_ptr[j] = &pk[j];
605-
}
606-
secp256k1_pubkey_sort(CTX, pk_ptr, 5);
607-
for (j = 1; j < 5; j++) {
608-
CHECK(secp256k1_pubkey_sort_cmp(&pk_ptr[j - 1], &pk_ptr[j], CTX) <= 0);
609-
}
610-
}
611-
}
612-
613-
/* Test vectors from BIP-MuSig2 */
614-
static void test_sort_vectors(void) {
615-
enum { N_PUBKEYS = 6 };
616-
unsigned char pk_ser[N_PUBKEYS][33] = {
617-
{ 0x02, 0xDD, 0x30, 0x8A, 0xFE, 0xC5, 0x77, 0x7E, 0x13, 0x12, 0x1F,
618-
0xA7, 0x2B, 0x9C, 0xC1, 0xB7, 0xCC, 0x01, 0x39, 0x71, 0x53, 0x09,
619-
0xB0, 0x86, 0xC9, 0x60, 0xE1, 0x8F, 0xD9, 0x69, 0x77, 0x4E, 0xB8 },
620-
{ 0x02, 0xF9, 0x30, 0x8A, 0x01, 0x92, 0x58, 0xC3, 0x10, 0x49, 0x34,
621-
0x4F, 0x85, 0xF8, 0x9D, 0x52, 0x29, 0xB5, 0x31, 0xC8, 0x45, 0x83,
622-
0x6F, 0x99, 0xB0, 0x86, 0x01, 0xF1, 0x13, 0xBC, 0xE0, 0x36, 0xF9 },
623-
{ 0x03, 0xDF, 0xF1, 0xD7, 0x7F, 0x2A, 0x67, 0x1C, 0x5F, 0x36, 0x18,
624-
0x37, 0x26, 0xDB, 0x23, 0x41, 0xBE, 0x58, 0xFE, 0xAE, 0x1D, 0xA2,
625-
0xDE, 0xCE, 0xD8, 0x43, 0x24, 0x0F, 0x7B, 0x50, 0x2B, 0xA6, 0x59 },
626-
{ 0x02, 0x35, 0x90, 0xA9, 0x4E, 0x76, 0x8F, 0x8E, 0x18, 0x15, 0xC2,
627-
0xF2, 0x4B, 0x4D, 0x80, 0xA8, 0xE3, 0x14, 0x93, 0x16, 0xC3, 0x51,
628-
0x8C, 0xE7, 0xB7, 0xAD, 0x33, 0x83, 0x68, 0xD0, 0x38, 0xCA, 0x66 },
629-
{ 0x02, 0xDD, 0x30, 0x8A, 0xFE, 0xC5, 0x77, 0x7E, 0x13, 0x12, 0x1F,
630-
0xA7, 0x2B, 0x9C, 0xC1, 0xB7, 0xCC, 0x01, 0x39, 0x71, 0x53, 0x09,
631-
0xB0, 0x86, 0xC9, 0x60, 0xE1, 0x8F, 0xD9, 0x69, 0x77, 0x4E, 0xFF },
632-
{ 0x02, 0xDD, 0x30, 0x8A, 0xFE, 0xC5, 0x77, 0x7E, 0x13, 0x12, 0x1F,
633-
0xA7, 0x2B, 0x9C, 0xC1, 0xB7, 0xCC, 0x01, 0x39, 0x71, 0x53, 0x09,
634-
0xB0, 0x86, 0xC9, 0x60, 0xE1, 0x8F, 0xD9, 0x69, 0x77, 0x4E, 0xB8 }
635-
};
636-
secp256k1_pubkey pubkeys[N_PUBKEYS];
637-
secp256k1_pubkey *sorted[N_PUBKEYS];
638-
const secp256k1_pubkey *pks_ptr[N_PUBKEYS];
639-
int i;
640-
641-
sorted[0] = &pubkeys[3];
642-
sorted[1] = &pubkeys[0];
643-
sorted[2] = &pubkeys[0];
644-
sorted[3] = &pubkeys[4];
645-
sorted[4] = &pubkeys[1];
646-
sorted[5] = &pubkeys[2];
647-
648-
for (i = 0; i < N_PUBKEYS; i++) {
649-
CHECK(secp256k1_ec_pubkey_parse(CTX, &pubkeys[i], pk_ser[i], sizeof(pk_ser[i])));
650-
pks_ptr[i] = &pubkeys[i];
651-
}
652-
CHECK(secp256k1_pubkey_sort(CTX, pks_ptr, N_PUBKEYS) == 1);
653-
for (i = 0; i < N_PUBKEYS; i++) {
654-
CHECK(secp256k1_memcmp_var(pks_ptr[i], sorted[i], sizeof(secp256k1_pubkey)) == 0);
655-
}
656-
}
657-
658470
static void run_extrakeys_tests(void) {
659471
/* xonly key test cases */
660472
test_xonly_pubkey();
@@ -666,12 +478,6 @@ static void run_extrakeys_tests(void) {
666478
/* keypair tests */
667479
test_keypair();
668480
test_keypair_add();
669-
670-
/* sorting tests */
671-
test_hsort();
672-
test_sort_api();
673-
test_sort();
674-
test_sort_vectors();
675481
}
676482

677483
#endif

src/secp256k1.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "int128_impl.h"
3737
#include "scratch_impl.h"
3838
#include "selftest.h"
39+
#include "hsort_impl.h"
3940

4041
#ifdef SECP256K1_NO_BUILD
4142
# error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c"
@@ -313,6 +314,40 @@ int secp256k1_ec_pubkey_cmp(const secp256k1_context* ctx, const secp256k1_pubkey
313314
return secp256k1_memcmp_var(out[0], out[1], sizeof(out[0]));
314315
}
315316

317+
/* This struct wraps a const context pointer to satisfy the secp256k1_hsort api
318+
* which expects a non-const cmp_data pointer. */
319+
typedef struct {
320+
const secp256k1_context *ctx;
321+
} secp256k1_pubkey_sort_cmp_data;
322+
323+
static int secp256k1_pubkey_sort_cmp(const void* pk1, const void* pk2, void *cmp_data) {
324+
return secp256k1_ec_pubkey_cmp(((secp256k1_pubkey_sort_cmp_data*)cmp_data)->ctx,
325+
*(secp256k1_pubkey **)pk1,
326+
*(secp256k1_pubkey **)pk2);
327+
}
328+
329+
int secp256k1_pubkey_sort(const secp256k1_context* ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys) {
330+
secp256k1_pubkey_sort_cmp_data cmp_data;
331+
VERIFY_CHECK(ctx != NULL);
332+
ARG_CHECK(pubkeys != NULL);
333+
334+
cmp_data.ctx = ctx;
335+
336+
/* Suppress wrong warning (fixed in MSVC 19.33) */
337+
#if defined(_MSC_VER) && (_MSC_VER < 1933)
338+
#pragma warning(push)
339+
#pragma warning(disable: 4090)
340+
#endif
341+
342+
secp256k1_hsort(pubkeys, n_pubkeys, sizeof(*pubkeys), secp256k1_pubkey_sort_cmp, &cmp_data);
343+
344+
#if defined(_MSC_VER) && (_MSC_VER < 1933)
345+
#pragma warning(pop)
346+
#endif
347+
348+
return 1;
349+
}
350+
316351
static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
317352
(void)ctx;
318353
if (sizeof(secp256k1_scalar) == 32) {

0 commit comments

Comments
 (0)