Skip to content

Commit 137ed38

Browse files
committed
Add secp256k1_pubkey_sort
1 parent d831168 commit 137ed38

File tree

6 files changed

+388
-0
lines changed

6 files changed

+388
-0
lines changed

Makefile.am

+2
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

+14
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_ec_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.

src/hsort.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/***********************************************************************
2+
* Copyright (c) 2021 Russell O'Connor, Jonas Nick *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef SECP256K1_HSORT_H
8+
#define SECP256K1_HSORT_H
9+
10+
#include <stddef.h>
11+
#include <string.h>
12+
13+
/* In-place, iterative heapsort with an interface matching glibc's qsort_r. This
14+
* is preferred over standard library implementations because they generally
15+
* make no guarantee about being fast for malicious inputs.
16+
* Remeber that heapsort is unstable.
17+
*
18+
* See the qsort_r manpage for a description of the interface.
19+
*/
20+
static void secp256k1_hsort(void *ptr, size_t count, size_t size,
21+
int (*cmp)(const void *, const void *, void *),
22+
void *cmp_data);
23+
#endif

src/hsort_impl.h

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/***********************************************************************
2+
* Copyright (c) 2021 Russell O'Connor, Jonas Nick *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef SECP256K1_HSORT_IMPL_H
8+
#define SECP256K1_HSORT_IMPL_H
9+
10+
#include "hsort.h"
11+
12+
/* An array is a heap when, for all non-zero indexes i, the element at index i
13+
* compares as less than or equal to the element at index parent(i) = (i-1)/2.
14+
*/
15+
16+
static SECP256K1_INLINE size_t secp256k1_heap_child1(size_t i) {
17+
VERIFY_CHECK(i <= (SIZE_MAX - 1)/2);
18+
return 2*i + 1;
19+
}
20+
21+
static SECP256K1_INLINE size_t secp256k1_heap_child2(size_t i) {
22+
VERIFY_CHECK(i <= SIZE_MAX/2 - 1);
23+
return secp256k1_heap_child1(i)+1;
24+
}
25+
26+
static SECP256K1_INLINE void secp256k1_heap_swap64(unsigned char *a, size_t i, size_t j, size_t stride) {
27+
unsigned char tmp[64];
28+
VERIFY_CHECK(stride <= 64);
29+
memcpy(tmp, a + i*stride, stride);
30+
memmove(a + i*stride, a + j*stride, stride);
31+
memcpy(a + j*stride, tmp, stride);
32+
}
33+
34+
static SECP256K1_INLINE void secp256k1_heap_swap(unsigned char *a, size_t i, size_t j, size_t stride) {
35+
while (64 < stride) {
36+
secp256k1_heap_swap64(a + (stride - 64), i, j, 64);
37+
stride -= 64;
38+
}
39+
secp256k1_heap_swap64(a, i, j, stride);
40+
}
41+
42+
static SECP256K1_INLINE void heap_down(unsigned char *a, size_t i, size_t heap_size, size_t stride,
43+
int (*cmp)(const void *, const void *, void *), void *cmp_data) {
44+
while (i < heap_size/2) {
45+
VERIFY_CHECK(i <= SIZE_MAX/2 - 1);
46+
/* Proof:
47+
* i < heap_size/2
48+
* i + 1 <= heap_size/2
49+
* 2*i + 2 <= heap_size <= SIZE_MAX
50+
* 2*i <= SIZE_MAX - 2
51+
*/
52+
53+
VERIFY_CHECK(secp256k1_heap_child1(i) < heap_size);
54+
/* Proof:
55+
* i < heap_size/2
56+
* i + 1 <= heap_size/2
57+
* 2*i + 2 <= heap_size
58+
* 2*i + 1 < heap_size
59+
* child1(i) < heap_size
60+
*/
61+
62+
/* Let [x] be notation for the contents at a[x*stride].
63+
*
64+
* If [child1(i)] > [i] and [child2(i)] > [i],
65+
* swap [i] with the larger child to ensure the new parent is larger
66+
* than both children. When [child1(i)] == [child2(i)], swap [i] with
67+
* [child2(i)].
68+
* Else if [child1(i)] > [i], swap [i] with [child1(i)].
69+
* Else if [child2(i)] > [i], swap [i] with [child2(i)].
70+
*/
71+
if (secp256k1_heap_child2(i) < heap_size
72+
&& 0 <= cmp(a + secp256k1_heap_child2(i)*stride, a + secp256k1_heap_child1(i)*stride, cmp_data)) {
73+
if (0 < cmp(a + secp256k1_heap_child2(i)*stride, a + i*stride, cmp_data)) {
74+
secp256k1_heap_swap(a, i, secp256k1_heap_child2(i), stride);
75+
i = secp256k1_heap_child2(i);
76+
} else {
77+
/* At this point we have [child2(i)] >= [child1(i)] and we have
78+
* [child2(i)] <= [i], and thus [child1(i)] <= [i] which means
79+
* that the next comparison can be skipped. */
80+
return;
81+
}
82+
} else if (0 < cmp(a + secp256k1_heap_child1(i)*stride, a + i*stride, cmp_data)) {
83+
secp256k1_heap_swap(a, i, secp256k1_heap_child1(i), stride);
84+
i = secp256k1_heap_child1(i);
85+
} else {
86+
return;
87+
}
88+
}
89+
/* heap_size/2 <= i
90+
* heap_size/2 < i + 1
91+
* heap_size < 2*i + 2
92+
* heap_size <= 2*i + 1
93+
* heap_size <= child1(i)
94+
* Thus child1(i) and child2(i) are now out of bounds and we are at a leaf.
95+
*/
96+
}
97+
98+
/* In-place heap sort. */
99+
static void secp256k1_hsort(void *ptr, size_t count, size_t size,
100+
int (*cmp)(const void *, const void *, void *),
101+
void *cmp_data ) {
102+
size_t i;
103+
104+
for(i = count/2; 0 < i; --i) {
105+
heap_down(ptr, i-1, count, size, cmp, cmp_data);
106+
}
107+
for(i = count; 1 < i; --i) {
108+
/* Extract the largest value from the heap */
109+
secp256k1_heap_swap(ptr, 0, i-1, size);
110+
111+
/* Repair the heap condition */
112+
heap_down(ptr, 0, i-1, size, cmp, cmp_data);
113+
}
114+
}
115+
116+
#endif

src/secp256k1.c

+35
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"
@@ -325,6 +326,40 @@ int secp256k1_ec_pubkey_cmp(const secp256k1_context* ctx, const secp256k1_pubkey
325326
return secp256k1_memcmp_var(out[0], out[1], sizeof(out[0]));
326327
}
327328

329+
/* This struct wraps a const context pointer to satisfy the secp256k1_hsort api
330+
* which expects a non-const cmp_data pointer. */
331+
typedef struct {
332+
const secp256k1_context *ctx;
333+
} secp256k1_ec_pubkey_sort_cmp_data;
334+
335+
static int secp256k1_ec_pubkey_sort_cmp(const void* pk1, const void* pk2, void *cmp_data) {
336+
return secp256k1_ec_pubkey_cmp(((secp256k1_ec_pubkey_sort_cmp_data*)cmp_data)->ctx,
337+
*(secp256k1_pubkey **)pk1,
338+
*(secp256k1_pubkey **)pk2);
339+
}
340+
341+
int secp256k1_ec_pubkey_sort(const secp256k1_context* ctx, const secp256k1_pubkey **pubkeys, size_t n_pubkeys) {
342+
secp256k1_ec_pubkey_sort_cmp_data cmp_data;
343+
VERIFY_CHECK(ctx != NULL);
344+
ARG_CHECK(pubkeys != NULL);
345+
346+
cmp_data.ctx = ctx;
347+
348+
/* Suppress wrong warning (fixed in MSVC 19.33) */
349+
#if defined(_MSC_VER) && (_MSC_VER < 1933)
350+
#pragma warning(push)
351+
#pragma warning(disable: 4090)
352+
#endif
353+
354+
secp256k1_hsort(pubkeys, n_pubkeys, sizeof(*pubkeys), secp256k1_ec_pubkey_sort_cmp, &cmp_data);
355+
356+
#if defined(_MSC_VER) && (_MSC_VER < 1933)
357+
#pragma warning(pop)
358+
#endif
359+
360+
return 1;
361+
}
362+
328363
static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) {
329364
(void)ctx;
330365
if (sizeof(secp256k1_scalar) == 32) {

0 commit comments

Comments
 (0)