-
Notifications
You must be signed in to change notification settings - Fork 216
Add MuSig Key Aggregation spec #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f31affd
extrakeys: add hsort, in-place, iterative heapsort
jonasnick 9b3d7bf
extrakeys: add xonly_sort function
jonasnick 9683c8a
musig: add static test vectors for key aggregation
jonasnick 2310849
musig: compute musig coefficient by hashing key instead of index
jonasnick 4bc46d8
musig: optimize key aggregation using const 1 for 2nd key
jonasnick 4a9b059
musig: rename Musig coefficient to KeyAgg coefficient
jonasnick 08fa02d
musig: add key aggregation spec draft
jonasnick 56014e8
musig: change pubkey_combine arg to array of pointers to pks
jonasnick f27fd1d
musig: improve test coverage of pubkey_combine
jonasnick 5860b5e
musig: do not also require schnorrsig module config flag
jonasnick 48f63ef
musig: remove unnecessary branch in pubkey_tweak_add
jonasnick fc26ca8
musig: remove unnecessary constant time normalize in combine
jonasnick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/*********************************************************************** | ||
* Copyright (c) 2021 Russell O'Connor, Jonas Nick * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or https://www.opensource.org/licenses/mit-license.php.* | ||
***********************************************************************/ | ||
|
||
#ifndef SECP256K1_HSORT_H_ | ||
#define SECP256K1_HSORT_H_ | ||
|
||
#include <stddef.h> | ||
#include <string.h> | ||
|
||
/* In-place, iterative heapsort with an interface matching glibc's qsort_r. This | ||
* is preferred over standard library implementations because they generally | ||
* make no guarantee about being fast for malicious inputs. | ||
* | ||
* See the qsort_r manpage for a description of the interface. | ||
*/ | ||
static void secp256k1_hsort(void *ptr, size_t count, size_t size, | ||
int (*cmp)(const void *, const void *, void *), | ||
void *cmp_data); | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/*********************************************************************** | ||
* Copyright (c) 2021 Russell O'Connor, Jonas Nick * | ||
* Distributed under the MIT software license, see the accompanying * | ||
* file COPYING or https://www.opensource.org/licenses/mit-license.php.* | ||
***********************************************************************/ | ||
|
||
#ifndef SECP256K1_HSORT_IMPL_H_ | ||
#define SECP256K1_HSORT_IMPL_H_ | ||
|
||
#include "hsort.h" | ||
|
||
/* An array is a heap when, for all non-zero indexes i, the element at index i | ||
* compares as less than or equal to the element at index parent(i) = (i-1)/2. | ||
*/ | ||
|
||
static SECP256K1_INLINE size_t child1(size_t i) { | ||
VERIFY_CHECK(i <= (SIZE_MAX - 1)/2); | ||
return 2*i + 1; | ||
} | ||
|
||
static SECP256K1_INLINE size_t child2(size_t i) { | ||
VERIFY_CHECK(i <= SIZE_MAX/2 - 1); | ||
return child1(i)+1; | ||
} | ||
|
||
static SECP256K1_INLINE void swap64(unsigned char *a, size_t i, size_t j, size_t stride) { | ||
unsigned char tmp[64]; | ||
VERIFY_CHECK(stride <= 64); | ||
memcpy(tmp, a + i*stride, stride); | ||
memmove(a + i*stride, a + j*stride, stride); | ||
memcpy(a + j*stride, tmp, stride); | ||
} | ||
|
||
static SECP256K1_INLINE void swap(unsigned char *a, size_t i, size_t j, size_t stride) { | ||
while (64 < stride) { | ||
swap64(a + (stride - 64), i, j, 64); | ||
stride -= 64; | ||
} | ||
swap64(a, i, j, stride); | ||
} | ||
|
||
static SECP256K1_INLINE void heap_down(unsigned char *a, size_t i, size_t heap_size, size_t stride, | ||
int (*cmp)(const void *, const void *, void *), void *cmp_data) { | ||
while (i < heap_size/2) { | ||
VERIFY_CHECK(i <= SIZE_MAX/2 - 1); | ||
/* Proof: | ||
* i < heap_size/2 | ||
* i + 1 <= heap_size/2 | ||
* 2*i + 2 <= heap_size <= SIZE_MAX | ||
* 2*i <= SIZE_MAX - 2 | ||
*/ | ||
|
||
VERIFY_CHECK(child1(i) < heap_size); | ||
/* Proof: | ||
* i < heap_size/2 | ||
* i + 1 <= heap_size/2 | ||
* 2*i + 2 <= heap_size | ||
* 2*i + 1 < heap_size | ||
* child1(i) < heap_size | ||
*/ | ||
|
||
/* Let [x] be notation for the contents at a[x*stride]. | ||
* | ||
* If [child1(i)] > [i] and [child2(i)] > [i], | ||
* swap [i] with the larger child to ensure the new parent is larger | ||
* than both children. When [child1(i)] == [child2(i)], swap [i] with | ||
* [child2(i)]. | ||
* Else if [child1(i)] > [i], swap [i] with [child1(i)]. | ||
* Else if [child2(i)] > [i], swap [i] with [child2(i)]. | ||
*/ | ||
if (child2(i) < heap_size | ||
&& 0 <= cmp(a + child2(i)*stride, a + child1(i)*stride, cmp_data)) { | ||
if (0 < cmp(a + child2(i)*stride, a + i*stride, cmp_data)) { | ||
swap(a, i, child2(i), stride); | ||
i = child2(i); | ||
} else { | ||
/* At this point we have [child2(i)] >= [child1(i)] and we have | ||
* [child2(i)] <= [i], and thus [child1(i)] <= [i] which means | ||
* that the next comparison can be skipped. */ | ||
return; | ||
} | ||
} else if (0 < cmp(a + child1(i)*stride, a + i*stride, cmp_data)) { | ||
swap(a, i, child1(i), stride); | ||
i = child1(i); | ||
} else { | ||
return; | ||
} | ||
} | ||
/* heap_size/2 <= i | ||
* heap_size/2 < i + 1 | ||
* heap_size < 2*i + 2 | ||
* heap_size <= 2*i + 1 | ||
* heap_size <= child1(i) | ||
* Thus child1(i) and child2(i) are now out of bounds and we are at a leaf. | ||
*/ | ||
} | ||
|
||
/* In-place heap sort. */ | ||
static void secp256k1_hsort(void *ptr, size_t count, size_t size, | ||
int (*cmp)(const void *, const void *, void *), | ||
void *cmp_data ) { | ||
size_t i; | ||
|
||
for(i = count/2; 0 < i; --i) { | ||
heap_down(ptr, i-1, count, size, cmp, cmp_data); | ||
} | ||
for(i = count; 1 < i; --i) { | ||
/* Extract the largest value from the heap */ | ||
swap(ptr, 0, i-1, size); | ||
|
||
/* Repair the heap condition */ | ||
heap_down(ptr, 0, i-1, size, cmp, cmp_data); | ||
} | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.