-
Notifications
You must be signed in to change notification settings - Fork 219
FROST Trusted Dealer #278
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
Open
jesseposner
wants to merge
19
commits into
BlockstreamResearch:master
Choose a base branch
from
jesseposner:frost-trusted-dealer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
FROST Trusted Dealer #278
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6216645
frost trusted dealer: initialize project
jesseposner 833d4d8
frost trusted dealer: share generation
jesseposner a64a904
frost trusted dealer: share verification
jesseposner 3787674
frost trusted dealer: key tweaking
jesseposner 9fa15f5
frost trusted dealer: nonce generation
jesseposner af4dfaf
frost trusted dealer: nonce aggregation and adaptor signatures
jesseposner ab1bb83
frost trusted dealer: signature generation and aggregation
jesseposner aea613b
frost trusted dealer: add example file
jesseposner fafc1f1
frost trusted dealer: add tests
jesseposner f41560c
frost trusted dealer: add documentation file
jesseposner c139f91
frost trusted dealer: build: add CMake support
theStack f045caf
Use VERIFY_CHECK for zero x-coordinate
jesseposner 298accf
Remove unused ret variable
jesseposner 640d61c
Rename secp256k1_frost_share to secp256k1_frost_secshare
jesseposner 4b387ef
Revise nonce reuse warning
jesseposner 6aa7df2
Check for equality before computing indexhash
jesseposner eb543cb
Simplify code
jesseposner b8c671c
Improve documentation
jesseposner 4ead1a7
wip
jesseposner 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,39 @@ | |
#include "../../hash.h" | ||
#include "../../scalar.h" | ||
|
||
static const unsigned char secp256k1_frost_keygen_cache_magic[4] = { 0x40, 0x25, 0x2e, 0x41 }; | ||
|
||
/* A tweak cache consists of | ||
* - 4 byte magic set during initialization to allow detecting an uninitialized | ||
* object. | ||
* - 64 byte aggregate (and potentially tweaked) public key | ||
* - 1 byte the parity of the internal key (if tweaked, otherwise 0) | ||
* - 32 byte tweak | ||
*/ | ||
/* Requires that cache_i->pk is not infinity. */ | ||
static void secp256k1_keygen_cache_save(secp256k1_frost_keygen_cache *cache, secp256k1_keygen_cache_internal *cache_i) { | ||
unsigned char *ptr = cache->data; | ||
memcpy(ptr, secp256k1_frost_keygen_cache_magic, 4); | ||
ptr += 4; | ||
secp256k1_ge_to_bytes(ptr, &cache_i->pk); | ||
ptr += 64; | ||
*ptr = cache_i->parity_acc; | ||
ptr += 1; | ||
secp256k1_scalar_get_b32(ptr, &cache_i->tweak); | ||
} | ||
|
||
static int secp256k1_keygen_cache_load(const secp256k1_context* ctx, secp256k1_keygen_cache_internal *cache_i, const secp256k1_frost_keygen_cache *cache) { | ||
const unsigned char *ptr = cache->data; | ||
ARG_CHECK(secp256k1_memcmp_var(ptr, secp256k1_frost_keygen_cache_magic, 4) == 0); | ||
ptr += 4; | ||
secp256k1_ge_from_bytes(&cache_i->pk, ptr); | ||
ptr += 64; | ||
cache_i->parity_acc = *ptr & 1; | ||
ptr += 1; | ||
secp256k1_scalar_set_b32(&cache_i->tweak, ptr, NULL); | ||
return 1; | ||
} | ||
|
||
/* Computes indexhash = tagged_hash(pk) */ | ||
static int secp256k1_frost_compute_indexhash(secp256k1_scalar *indexhash, const unsigned char *id33) { | ||
secp256k1_sha256 sha; | ||
|
@@ -182,6 +215,13 @@ typedef struct { | |
const secp256k1_pubkey *vss_commitment; | ||
} secp256k1_frost_evaluate_vss_ecmult_data; | ||
|
||
typedef struct { | ||
const secp256k1_context *ctx; | ||
const secp256k1_pubkey * const* pubshares; | ||
const unsigned char * const *ids33; | ||
size_t n_pubshares; | ||
} secp256k1_frost_interpolate_pubkey_ecmult_data; | ||
|
||
static int secp256k1_frost_evaluate_vss_ecmult_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *data) { | ||
secp256k1_frost_evaluate_vss_ecmult_data *ctx = (secp256k1_frost_evaluate_vss_ecmult_data *) data; | ||
if (!secp256k1_pubkey_load(ctx->ctx, pt, &ctx->vss_commitment[idx])) { | ||
|
@@ -193,6 +233,23 @@ static int secp256k1_frost_evaluate_vss_ecmult_callback(secp256k1_scalar *sc, se | |
return 1; | ||
} | ||
|
||
static int secp256k1_frost_interpolate_pubkey_ecmult_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *data) { | ||
secp256k1_frost_interpolate_pubkey_ecmult_data *ctx = (secp256k1_frost_interpolate_pubkey_ecmult_data *) data; | ||
secp256k1_scalar l; | ||
|
||
if (!secp256k1_pubkey_load(ctx->ctx, pt, ctx->pubshares[idx])) { | ||
return 0; | ||
} | ||
|
||
if (!secp256k1_frost_lagrange_coefficient(&l, ctx->ids33, ctx->n_pubshares, ctx->ids33[idx])) { | ||
return 0; | ||
} | ||
|
||
*sc = l; | ||
|
||
return 1; | ||
} | ||
|
||
static int secp256k1_frost_evaluate_vss(const secp256k1_context* ctx, secp256k1_gej *share, size_t threshold, const unsigned char *id33, const secp256k1_pubkey *vss_commitment) { | ||
secp256k1_frost_evaluate_vss_ecmult_data evaluate_vss_ecmult_data; | ||
|
||
|
@@ -266,4 +323,122 @@ int secp256k1_frost_compute_pubshare(const secp256k1_context* ctx, secp256k1_pub | |
return 1; | ||
} | ||
|
||
int secp256k1_frost_pubkey_get(const secp256k1_context* ctx, secp256k1_pubkey *agg_pk, const secp256k1_frost_keygen_cache *keyagg_cache) { | ||
secp256k1_keygen_cache_internal cache_i; | ||
VERIFY_CHECK(ctx != NULL); | ||
ARG_CHECK(agg_pk != NULL); | ||
memset(agg_pk, 0, sizeof(*agg_pk)); | ||
ARG_CHECK(keyagg_cache != NULL); | ||
|
||
if(!secp256k1_keygen_cache_load(ctx, &cache_i, keyagg_cache)) { | ||
return 0; | ||
} | ||
secp256k1_pubkey_save(agg_pk, &cache_i.pk); | ||
return 1; | ||
} | ||
|
||
int secp256k1_frost_pubkey_gen(const secp256k1_context* ctx, secp256k1_frost_keygen_cache *cache, const secp256k1_pubkey * const *pubshares, size_t n_pubshares, const unsigned char * const *ids33) { | ||
secp256k1_gej pkj; | ||
secp256k1_frost_interpolate_pubkey_ecmult_data interpolate_pubkey_ecmult_data; | ||
secp256k1_keygen_cache_internal cache_i = { 0 }; | ||
|
||
VERIFY_CHECK(ctx != NULL); | ||
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); | ||
ARG_CHECK(cache != NULL); | ||
ARG_CHECK(pubshares != NULL); | ||
ARG_CHECK(ids33 != NULL); | ||
ARG_CHECK(n_pubshares > 1); | ||
|
||
interpolate_pubkey_ecmult_data.ctx = ctx; | ||
interpolate_pubkey_ecmult_data.pubshares = pubshares; | ||
interpolate_pubkey_ecmult_data.ids33 = ids33; | ||
interpolate_pubkey_ecmult_data.n_pubshares = n_pubshares; | ||
|
||
/* TODO: add scratch */ | ||
if (!secp256k1_ecmult_multi_var(&ctx->error_callback, NULL, &pkj, NULL, secp256k1_frost_interpolate_pubkey_ecmult_callback, (void *) &interpolate_pubkey_ecmult_data, n_pubshares)) { | ||
return 0; | ||
} | ||
secp256k1_ge_set_gej(&cache_i.pk, &pkj); | ||
secp256k1_keygen_cache_save(cache, &cache_i); | ||
|
||
return 1; | ||
} | ||
|
||
static int secp256k1_frost_pubkey_tweak_add_internal(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, secp256k1_frost_keygen_cache *keygen_cache, const unsigned char *tweak32, int xonly) { | ||
secp256k1_keygen_cache_internal cache_i; | ||
int overflow = 0; | ||
secp256k1_scalar tweak; | ||
|
||
VERIFY_CHECK(ctx != NULL); | ||
if (output_pubkey != NULL) { | ||
memset(output_pubkey, 0, sizeof(*output_pubkey)); | ||
} | ||
ARG_CHECK(keygen_cache != NULL); | ||
ARG_CHECK(tweak32 != NULL); | ||
|
||
if (!secp256k1_keygen_cache_load(ctx, &cache_i, keygen_cache)) { | ||
return 0; | ||
} | ||
secp256k1_scalar_set_b32(&tweak, tweak32, &overflow); | ||
if (overflow) { | ||
return 0; | ||
} | ||
if (xonly && secp256k1_extrakeys_ge_even_y(&cache_i.pk)) { | ||
cache_i.parity_acc ^= 1; | ||
secp256k1_scalar_negate(&cache_i.tweak, &cache_i.tweak); | ||
} | ||
secp256k1_scalar_add(&cache_i.tweak, &cache_i.tweak, &tweak); | ||
if (!secp256k1_eckey_pubkey_tweak_add(&cache_i.pk, &tweak)) { | ||
return 0; | ||
} | ||
/* eckey_pubkey_tweak_add fails if cache_i.pk is infinity */ | ||
VERIFY_CHECK(!secp256k1_ge_is_infinity(&cache_i.pk)); | ||
secp256k1_keygen_cache_save(keygen_cache, &cache_i); | ||
if (output_pubkey != NULL) { | ||
secp256k1_pubkey_save(output_pubkey, &cache_i.pk); | ||
} | ||
return 1; | ||
} | ||
|
||
int secp256k1_frost_pubkey_ec_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, secp256k1_frost_keygen_cache *keygen_cache, const unsigned char *tweak32) { | ||
return secp256k1_frost_pubkey_tweak_add_internal(ctx, output_pubkey, keygen_cache, tweak32, 0); | ||
} | ||
|
||
int secp256k1_frost_pubkey_xonly_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, secp256k1_frost_keygen_cache *keygen_cache, const unsigned char *tweak32) { | ||
return secp256k1_frost_pubkey_tweak_add_internal(ctx, output_pubkey, keygen_cache, tweak32, 1); | ||
} | ||
|
||
static int secp256k1_frost_lagrange_coefficient(secp256k1_scalar *r, const unsigned char * const *ids33, size_t n_participants, const unsigned char *my_id33) { | ||
size_t i; | ||
secp256k1_scalar num; | ||
secp256k1_scalar den; | ||
secp256k1_scalar party_idx; | ||
|
||
secp256k1_scalar_set_int(&num, 1); | ||
secp256k1_scalar_set_int(&den, 1); | ||
if (!secp256k1_frost_compute_indexhash(&party_idx, my_id33)) { | ||
return 0; | ||
} | ||
for (i = 0; i < n_participants; i++) { | ||
secp256k1_scalar mul; | ||
|
||
if (!secp256k1_frost_compute_indexhash(&mul, ids33[i])) { | ||
return 0; | ||
} | ||
if (secp256k1_scalar_eq(&mul, &party_idx)) { | ||
continue; | ||
} | ||
Comment on lines
+397
to
+399
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe more a matter of taste, but the "skip for our own id" logic could be implemented before computing the _i_th indexhash by simply comparing the ids, e.g.
(untested) |
||
|
||
secp256k1_scalar_negate(&mul, &mul); | ||
secp256k1_scalar_mul(&num, &num, &mul); | ||
secp256k1_scalar_add(&mul, &mul, &party_idx); | ||
secp256k1_scalar_mul(&den, &den, &mul); | ||
} | ||
|
||
secp256k1_scalar_inverse_var(&den, &den); | ||
secp256k1_scalar_mul(r, &num, &den); | ||
|
||
return 1; | ||
} | ||
|
||
#endif |
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.