Skip to content

Commit ef15156

Browse files
committed
frost: signature generation and aggregation
This commit adds signature generation and aggregation, as well as partial signature serialization and parsing.
1 parent 67c21be commit ef15156

File tree

2 files changed

+315
-1
lines changed

2 files changed

+315
-1
lines changed

include/secp256k1_frost.h

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ extern "C" {
1414
*
1515
* This module implements a variant of Flexible Round-Optimized Schnorr
1616
* Threshold Signatures (FROST) by Chelsea Komlo and Ian Goldberg
17-
* (https://crysp.uwaterloo.ca/software/frost/).
17+
* (https://crysp.uwaterloo.ca/software/frost/). Signatures are compatible with
18+
* BIP-340 ("Schnorr").
1819
*
1920
* The module also supports BIP-341 ("Taproot") and BIP-32 ("ordinary") public
2021
* key tweaking, and adaptor signatures.
@@ -88,6 +89,15 @@ typedef struct {
8889
unsigned char data[133];
8990
} secp256k1_frost_session;
9091

92+
/** Opaque data structure that holds a partial FROST signature.
93+
*
94+
* Guaranteed to be 36 bytes in size. Serialized and parsed with
95+
* `frost_partial_sig_serialize` and `frost_partial_sig_parse`.
96+
*/
97+
typedef struct {
98+
unsigned char data[36];
99+
} secp256k1_frost_partial_sig;
100+
91101
/** Parse a signer's public nonce.
92102
*
93103
* Returns: 1 when the nonce could be parsed, 0 otherwise.
@@ -114,6 +124,36 @@ SECP256K1_API int secp256k1_frost_pubnonce_serialize(
114124
const secp256k1_frost_pubnonce *nonce
115125
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
116126

127+
/** Serialize a FROST partial signature
128+
*
129+
* Returns: 1 when the signature could be serialized, 0 otherwise
130+
* Args: ctx: pointer to a context object
131+
* Out: out32: pointer to a 32-byte array to store the serialized signature
132+
* In: sig: pointer to the signature
133+
*/
134+
SECP256K1_API int secp256k1_frost_partial_sig_serialize(
135+
const secp256k1_context *ctx,
136+
unsigned char *out32,
137+
const secp256k1_frost_partial_sig *sig
138+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
139+
140+
/** Parse a FROST partial signature.
141+
*
142+
* Returns: 1 when the signature could be parsed, 0 otherwise.
143+
* Args: ctx: pointer to a context object
144+
* Out: sig: pointer to a signature object
145+
* In: in32: pointer to the 32-byte signature to be parsed
146+
*
147+
* After the call, sig will always be initialized. If parsing failed or the
148+
* encoded numbers are out of range, signature verification with it is
149+
* guaranteed to fail for every message and public key.
150+
*/
151+
SECP256K1_API int secp256k1_frost_partial_sig_parse(
152+
const secp256k1_context *ctx,
153+
secp256k1_frost_partial_sig *sig,
154+
const unsigned char *in32
155+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
156+
117157
/** Serialize a FROST share
118158
*
119159
* Returns: 1 when the share could be serialized, 0 otherwise
@@ -467,6 +507,90 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_frost_nonce_process(
467507
const secp256k1_pubkey *adaptor
468508
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(8);
469509

510+
/** Produces a partial signature
511+
*
512+
* This function overwrites the given secnonce with zeros and will abort if given a
513+
* secnonce that is all zeros. This is a best effort attempt to protect against nonce
514+
* reuse. However, this is of course easily defeated if the secnonce has been
515+
* copied (or serialized). Remember that nonce reuse will leak the secret key!
516+
*
517+
* Returns: 0 if the arguments are invalid or the provided secnonce has already
518+
* been used for signing, 1 otherwise
519+
* Args: ctx: pointer to a context object
520+
* Out: partial_sig: pointer to struct to store the partial signature
521+
* In/Out: secnonce: pointer to the secnonce struct created in
522+
* frost_nonce_gen that has been never used in a
523+
* partial_sign call before
524+
* In: agg_share: the aggregated share
525+
* session: pointer to the session that was created with
526+
* frost_nonce_process
527+
* tweak_cache: pointer to frost_tweak_cache struct (can be NULL)
528+
*/
529+
SECP256K1_API int secp256k1_frost_partial_sign(
530+
const secp256k1_context *ctx,
531+
secp256k1_frost_partial_sig *partial_sig,
532+
secp256k1_frost_secnonce *secnonce,
533+
const secp256k1_frost_share *agg_share,
534+
const secp256k1_frost_session *session,
535+
const secp256k1_frost_tweak_cache *tweak_cache
536+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
537+
538+
/** Verifies an individual signer's partial signature
539+
*
540+
* The signature is verified for a specific signing session. In order to avoid
541+
* accidentally verifying a signature from a different or non-existing signing
542+
* session, you must ensure the following:
543+
* 1. The `tweak_cache` argument is identical to the one used to create the
544+
* `session` with `frost_nonce_process`.
545+
* 2. The `pubshare` argument must be the output of
546+
* `secp256k1_frost_compute_pubshare` for the signer's ID.
547+
* 3. The `pubnonce` argument must be identical to the one sent by the
548+
* signer and used to create the `session` with `frost_nonce_process`.
549+
*
550+
* This function can be used to assign blame for a failed signature.
551+
*
552+
* Returns: 0 if the arguments are invalid or the partial signature does not
553+
* verify, 1 otherwise
554+
* Args ctx: pointer to a context object
555+
* In: partial_sig: pointer to partial signature to verify, sent by
556+
* the signer associated with `pubnonce` and `pubkey`
557+
* pubnonce: public nonce of the signer in the signing session
558+
* pubshare: public verification share of the signer in the signing
559+
* session that is the output of
560+
* `secp256k1_frost_compute_pubshare`
561+
* session: pointer to the session that was created with
562+
* `frost_nonce_process`
563+
* tweak_cache: pointer to frost_tweak_cache struct (can be NULL)
564+
*/
565+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_frost_partial_sig_verify(
566+
const secp256k1_context *ctx,
567+
const secp256k1_frost_partial_sig *partial_sig,
568+
const secp256k1_frost_pubnonce *pubnonce,
569+
const secp256k1_pubkey *pubshare,
570+
const secp256k1_frost_session *session,
571+
const secp256k1_frost_tweak_cache *tweak_cache
572+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
573+
574+
/** Aggregates partial signatures
575+
*
576+
* Returns: 0 if the arguments are invalid, 1 otherwise (which does NOT mean
577+
* the resulting signature verifies).
578+
* Args: ctx: pointer to a context object
579+
* Out: sig64: complete (but possibly invalid) Schnorr signature
580+
* In: session: pointer to the session that was created with
581+
* frost_nonce_process
582+
* partial_sigs: array of pointers to partial signatures to aggregate
583+
* n_sigs: number of elements in the partial_sigs array. Must be
584+
* greater than 0.
585+
*/
586+
SECP256K1_API int secp256k1_frost_partial_sig_agg(
587+
const secp256k1_context *ctx,
588+
unsigned char *sig64,
589+
const secp256k1_frost_session *session,
590+
const secp256k1_frost_partial_sig * const *partial_sigs,
591+
size_t n_sigs
592+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
593+
470594
/** Extracts the nonce_parity bit from a session
471595
*
472596
* This is used for adaptor signatures.

src/modules/frost/session_impl.h

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ static int secp256k1_frost_session_load(const secp256k1_context* ctx, secp256k1_
117117
return 1;
118118
}
119119

120+
static const unsigned char secp256k1_frost_partial_sig_magic[4] = { 0x8d, 0xd8, 0x31, 0x6e };
121+
122+
static void secp256k1_frost_partial_sig_save(secp256k1_frost_partial_sig* sig, secp256k1_scalar *s) {
123+
memcpy(&sig->data[0], secp256k1_frost_partial_sig_magic, 4);
124+
secp256k1_scalar_get_b32(&sig->data[4], s);
125+
}
126+
127+
static int secp256k1_frost_partial_sig_load(const secp256k1_context* ctx, secp256k1_scalar *s, const secp256k1_frost_partial_sig* sig) {
128+
int overflow;
129+
130+
ARG_CHECK(secp256k1_memcmp_var(&sig->data[0], secp256k1_frost_partial_sig_magic, 4) == 0);
131+
secp256k1_scalar_set_b32(s, &sig->data[4], &overflow);
132+
/* Parsed signatures can not overflow */
133+
VERIFY_CHECK(!overflow);
134+
return 1;
135+
}
136+
120137
int secp256k1_frost_pubnonce_serialize(const secp256k1_context* ctx, unsigned char *out66, const secp256k1_frost_pubnonce* nonce) {
121138
secp256k1_ge ge[2];
122139
int i;
@@ -163,6 +180,29 @@ int secp256k1_frost_pubnonce_parse(const secp256k1_context* ctx, secp256k1_frost
163180
return 1;
164181
}
165182

183+
int secp256k1_frost_partial_sig_serialize(const secp256k1_context* ctx, unsigned char *out32, const secp256k1_frost_partial_sig* sig) {
184+
VERIFY_CHECK(ctx != NULL);
185+
ARG_CHECK(out32 != NULL);
186+
ARG_CHECK(sig != NULL);
187+
memcpy(out32, &sig->data[4], 32);
188+
return 1;
189+
}
190+
191+
int secp256k1_frost_partial_sig_parse(const secp256k1_context* ctx, secp256k1_frost_partial_sig* sig, const unsigned char *in32) {
192+
secp256k1_scalar tmp;
193+
int overflow;
194+
VERIFY_CHECK(ctx != NULL);
195+
ARG_CHECK(sig != NULL);
196+
ARG_CHECK(in32 != NULL);
197+
198+
secp256k1_scalar_set_b32(&tmp, in32, &overflow);
199+
if (overflow) {
200+
return 0;
201+
}
202+
secp256k1_frost_partial_sig_save(sig, &tmp);
203+
return 1;
204+
}
205+
166206
static void secp256k1_nonce_function_frost(secp256k1_scalar *k, const unsigned char *session_id, const unsigned char *msg32, const unsigned char *key32, const unsigned char *pk32, const unsigned char *extra_input32) {
167207
secp256k1_sha256 sha;
168208
unsigned char seed[32];
@@ -469,4 +509,154 @@ int secp256k1_frost_nonce_process(const secp256k1_context* ctx, secp256k1_frost_
469509
return 1;
470510
}
471511

512+
void secp256k1_frost_partial_sign_clear(secp256k1_scalar *sk, secp256k1_scalar *k) {
513+
secp256k1_scalar_clear(sk);
514+
secp256k1_scalar_clear(&k[0]);
515+
secp256k1_scalar_clear(&k[1]);
516+
}
517+
518+
int secp256k1_frost_partial_sign(const secp256k1_context* ctx, secp256k1_frost_partial_sig *partial_sig, secp256k1_frost_secnonce *secnonce, const secp256k1_frost_share *share, const secp256k1_frost_session *session, const secp256k1_frost_tweak_cache *tweak_cache) {
519+
secp256k1_scalar sk;
520+
secp256k1_scalar k[2];
521+
secp256k1_scalar s;
522+
secp256k1_frost_session_internal session_i;
523+
int ret;
524+
525+
VERIFY_CHECK(ctx != NULL);
526+
527+
ARG_CHECK(secnonce != NULL);
528+
/* Fails if the magic doesn't match */
529+
ret = secp256k1_frost_secnonce_load(ctx, k, secnonce);
530+
/* Set nonce to zero to avoid nonce reuse. This will cause subsequent calls
531+
* of this function to fail */
532+
memset(secnonce, 0, sizeof(*secnonce));
533+
if (!ret) {
534+
secp256k1_frost_partial_sign_clear(&sk, k);
535+
return 0;
536+
}
537+
538+
ARG_CHECK(partial_sig != NULL);
539+
ARG_CHECK(share != NULL);
540+
ARG_CHECK(session != NULL);
541+
542+
if (!secp256k1_frost_share_load(ctx, &sk, share)) {
543+
secp256k1_frost_partial_sign_clear(&sk, k);
544+
return 0;
545+
}
546+
if (!secp256k1_frost_session_load(ctx, &session_i, session)) {
547+
secp256k1_frost_partial_sign_clear(&sk, k);
548+
return 0;
549+
}
550+
551+
if (tweak_cache != NULL) {
552+
secp256k1_tweak_cache_internal cache_i;
553+
if (!secp256k1_tweak_cache_load(ctx, &cache_i, tweak_cache)) {
554+
secp256k1_frost_partial_sign_clear(&sk, k);
555+
return 0;
556+
}
557+
if (secp256k1_fe_is_odd(&cache_i.pk.y) != cache_i.parity_acc) {
558+
secp256k1_scalar_negate(&sk, &sk);
559+
}
560+
}
561+
562+
if (session_i.fin_nonce_parity) {
563+
secp256k1_scalar_negate(&k[0], &k[0]);
564+
secp256k1_scalar_negate(&k[1], &k[1]);
565+
}
566+
567+
/* Sign */
568+
secp256k1_scalar_mul(&s, &session_i.challenge, &sk);
569+
secp256k1_scalar_mul(&k[1], &session_i.noncecoef, &k[1]);
570+
secp256k1_scalar_add(&k[0], &k[0], &k[1]);
571+
secp256k1_scalar_add(&s, &s, &k[0]);
572+
secp256k1_frost_partial_sig_save(partial_sig, &s);
573+
secp256k1_frost_partial_sign_clear(&sk, k);
574+
return 1;
575+
}
576+
577+
int secp256k1_frost_partial_sig_verify(const secp256k1_context* ctx, const secp256k1_frost_partial_sig *partial_sig, const secp256k1_frost_pubnonce *pubnonce, const secp256k1_pubkey *pubshare, const secp256k1_frost_session *session, const secp256k1_frost_tweak_cache *tweak_cache) {
578+
secp256k1_frost_session_internal session_i;
579+
secp256k1_scalar e, s;
580+
secp256k1_gej pkj;
581+
secp256k1_ge nonce_pt[2];
582+
secp256k1_gej rj;
583+
secp256k1_gej tmp;
584+
secp256k1_ge pkp;
585+
586+
VERIFY_CHECK(ctx != NULL);
587+
ARG_CHECK(partial_sig != NULL);
588+
ARG_CHECK(pubnonce != NULL);
589+
ARG_CHECK(pubshare != NULL);
590+
ARG_CHECK(session != NULL);
591+
592+
if (!secp256k1_frost_session_load(ctx, &session_i, session)) {
593+
return 0;
594+
}
595+
596+
/* Compute "effective" nonce rj = aggnonce[0] + b*aggnonce[1] */
597+
/* TODO: use multiexp to compute -s*G + e*pubshare + aggnonce[0] + b*aggnonce[1] */
598+
if (!secp256k1_frost_pubnonce_load(ctx, nonce_pt, pubnonce)) {
599+
return 0;
600+
}
601+
secp256k1_gej_set_ge(&rj, &nonce_pt[1]);
602+
secp256k1_ecmult(&rj, &rj, &session_i.noncecoef, NULL);
603+
secp256k1_gej_add_ge_var(&rj, &rj, &nonce_pt[0], NULL);
604+
605+
if (!secp256k1_pubkey_load(ctx, &pkp, pubshare)) {
606+
return 0;
607+
}
608+
609+
secp256k1_scalar_set_int(&e, 1);
610+
if (tweak_cache != NULL) {
611+
secp256k1_tweak_cache_internal cache_i;
612+
if (!secp256k1_tweak_cache_load(ctx, &cache_i, tweak_cache)) {
613+
return 0;
614+
}
615+
if (secp256k1_fe_is_odd(&cache_i.pk.y)
616+
!= cache_i.parity_acc) {
617+
secp256k1_scalar_negate(&e, &e);
618+
}
619+
}
620+
secp256k1_scalar_mul(&e, &e, &session_i.challenge);
621+
622+
if (!secp256k1_frost_partial_sig_load(ctx, &s, partial_sig)) {
623+
return 0;
624+
}
625+
/* Compute -s*G + e*pkj + rj (e already includes the lagrange coefficient l) */
626+
secp256k1_scalar_negate(&s, &s);
627+
secp256k1_gej_set_ge(&pkj, &pkp);
628+
secp256k1_ecmult(&tmp, &pkj, &e, &s);
629+
if (session_i.fin_nonce_parity) {
630+
secp256k1_gej_neg(&rj, &rj);
631+
}
632+
secp256k1_gej_add_var(&tmp, &tmp, &rj, NULL);
633+
634+
return secp256k1_gej_is_infinity(&tmp);
635+
}
636+
637+
int secp256k1_frost_partial_sig_agg(const secp256k1_context* ctx, unsigned char *sig64, const secp256k1_frost_session *session, const secp256k1_frost_partial_sig * const* partial_sigs, size_t n_sigs) {
638+
size_t i;
639+
secp256k1_frost_session_internal session_i;
640+
641+
VERIFY_CHECK(ctx != NULL);
642+
ARG_CHECK(sig64 != NULL);
643+
ARG_CHECK(session != NULL);
644+
ARG_CHECK(partial_sigs != NULL);
645+
ARG_CHECK(n_sigs > 0);
646+
647+
if (!secp256k1_frost_session_load(ctx, &session_i, session)) {
648+
return 0;
649+
}
650+
for (i = 0; i < n_sigs; i++) {
651+
secp256k1_scalar term;
652+
if (!secp256k1_frost_partial_sig_load(ctx, &term, partial_sigs[i])) {
653+
return 0;
654+
}
655+
secp256k1_scalar_add(&session_i.s_part, &session_i.s_part, &term);
656+
}
657+
secp256k1_scalar_get_b32(&sig64[32], &session_i.s_part);
658+
memcpy(&sig64[0], session_i.fin_nonce, 32);
659+
return 1;
660+
}
661+
472662
#endif

0 commit comments

Comments
 (0)