Skip to content

Commit e1ba262

Browse files
committed
extrakeys: add secp256k1_pubkey_sort
1 parent a9db9f2 commit e1ba262

File tree

6 files changed

+384
-0
lines changed

6 files changed

+384
-0
lines changed

include/secp256k1_extrakeys.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,21 @@ 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+
243258
#ifdef __cplusplus
244259
}
245260
#endif

src/modules/extrakeys/Makefile.am.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ 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
44
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

src/modules/extrakeys/hsort.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
*
17+
* See the qsort_r manpage for a description of the interface.
18+
*/
19+
static void secp256k1_hsort(void *ptr, size_t count, size_t size,
20+
int (*cmp)(const void *, const void *, void *),
21+
void *cmp_data);
22+
#endif

src/modules/extrakeys/hsort_impl.h

Lines changed: 116 additions & 0 deletions
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 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 child2(size_t i) {
22+
VERIFY_CHECK(i <= SIZE_MAX/2 - 1);
23+
return child1(i)+1;
24+
}
25+
26+
static SECP256K1_INLINE void 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 heap_swap(unsigned char *a, size_t i, size_t j, size_t stride) {
35+
while (64 < stride) {
36+
heap_swap64(a + (stride - 64), i, j, 64);
37+
stride -= 64;
38+
}
39+
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(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 (child2(i) < heap_size
72+
&& 0 <= cmp(a + child2(i)*stride, a + child1(i)*stride, cmp_data)) {
73+
if (0 < cmp(a + child2(i)*stride, a + i*stride, cmp_data)) {
74+
heap_swap(a, i, child2(i), stride);
75+
i = 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 + child1(i)*stride, a + i*stride, cmp_data)) {
83+
heap_swap(a, i, child1(i), stride);
84+
i = 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+
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/modules/extrakeys/main_impl.h

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

1010
#include "../../../include/secp256k1.h"
1111
#include "../../../include/secp256k1_extrakeys.h"
12+
#include "hsort_impl.h"
1213
#include "../../util.h"
1314

1415
static SECP256K1_INLINE int secp256k1_xonly_pubkey_load(const secp256k1_context* ctx, secp256k1_ge *ge, const secp256k1_xonly_pubkey *pubkey) {
@@ -282,4 +283,38 @@ int secp256k1_keypair_xonly_tweak_add(const secp256k1_context* ctx, secp256k1_ke
282283
return ret;
283284
}
284285

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+
285320
#endif

0 commit comments

Comments
 (0)