Skip to content

Commit 7763132

Browse files
- examples/frost.c: zero-init secp256k1_frost_keygen_cache to avoid UB.
- tests_impl.h: - compute pubshares for 0..4, call frost_pubkey_gen(ids), zero pk before pubkey_get(). - generate for 3 signers, **populate pubnonce_ptr[]**, process with ids[], and use per-signer session[]. - **populate partial_sig_ptr[]** before aggregation; call partial_sig_agg with &session[0]. - call nonce_parity with &session[0] (not &session). - generate shares/pubshares, pass ids[] into pubkey_gen; fix loop bound (i < 2) for failure branch. - C89: move loop variable declarations out of for() headers.
1 parent 1123736 commit 7763132

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

examples/frost.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ int main(void) {
196196
struct signer signers[N_SIGNERS];
197197
const secp256k1_pubkey *pubshares_ptr[N_SIGNERS];
198198
secp256k1_xonly_pubkey pk;
199-
secp256k1_frost_keygen_cache keygen_cache;
199+
secp256k1_frost_keygen_cache keygen_cache = {0};
200200
const unsigned char msg[32] = "this_could_be_the_hash_of_a_msg!";
201201
unsigned char sig[64];
202202
size_t ids[5];

src/modules/frost/tests_impl.h

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void frost_api_tests(void) {
209209
}
210210
CHECK_ILLEGAL(CTX, secp256k1_frost_shares_gen(CTX, shares, vss_commitment, seed, 3, 0));
211211
CHECK_ILLEGAL(CTX, secp256k1_frost_shares_gen(CTX, shares, vss_commitment, seed, 3, 2));
212-
for (i = 0; i < 5; i++) {
212+
for (i = 0; i < 2; i++) {
213213
CHECK(frost_memcmp_and_randomize(shares[i].data, zeros68, sizeof(shares[i].data)) == 0);
214214
}
215215

@@ -256,7 +256,17 @@ void frost_api_tests(void) {
256256
/* CHECK_ILLEGAL(CTX, secp256k1_frost_pubkey_gen(CTX, &keygen_cache, pubshare_ptr, 0, id_ptr)); */
257257
/* CHECK_ILLEGAL(CTX, secp256k1_frost_pubkey_gen(CTX, &keygen_cache, pubshare_ptr, 5, NULL)); */
258258

259+
for (i = 0; i < 5; i++) {
260+
CHECK(secp256k1_frost_compute_pubshare(
261+
CTX, &pubshare[i], 3, i, vss_commitment) == 1);
262+
pubshare_ptr[i] = &pubshare[i];
263+
}
264+
CHECK(secp256k1_frost_pubkey_gen(
265+
CTX, &keygen_cache, pubshare_ptr, 5, ids) == 1);
266+
267+
259268
/* pubkey_get */
269+
memset(&pk, 0, sizeof(pk));
260270
CHECK_ILLEGAL(CTX, secp256k1_frost_pubkey_get(CTX, NULL, &keygen_cache));
261271
CHECK_ILLEGAL(CTX, secp256k1_frost_pubkey_get(CTX, &pk, NULL));
262272
CHECK(secp256k1_memcmp_var(&pk, zeros68, sizeof(pk)) == 0);
@@ -321,6 +331,34 @@ void frost_api_tests(void) {
321331
/* CHECK(secp256k1_frost_nonce_gen(CTX, &secnonce[1], &pubnonce[1], session_id[1], &shares[1], NULL, NULL, NULL) == 1); */
322332
/* CHECK(secp256k1_frost_nonce_gen(CTX, &secnonce[2], &pubnonce[2], session_id[2], &shares[2], NULL, NULL, NULL) == 1); */
323333

334+
for (i = 0; i < 3; i++) {
335+
CHECK(secp256k1_frost_nonce_gen(
336+
CTX,
337+
&secnonce[i],
338+
&pubnonce[i],
339+
session_id[i],
340+
&shares[i],
341+
NULL,
342+
NULL,
343+
NULL
344+
) == 1);
345+
pubnonce_ptr[i] = &pubnonce[i];
346+
}
347+
348+
for (i = 0; i < 3; i++) {
349+
CHECK(secp256k1_frost_nonce_process(
350+
CTX,
351+
&session[i],
352+
pubnonce_ptr,
353+
3,
354+
msg,
355+
ids[i],
356+
ids,
357+
&keygen_cache,
358+
&adaptor
359+
) == 1);
360+
}
361+
324362
/** Serialize and parse public nonces **/
325363
CHECK_ILLEGAL(CTX, secp256k1_frost_pubnonce_serialize(CTX, NULL, &pubnonce[0]));
326364
CHECK_ILLEGAL(CTX, secp256k1_frost_pubnonce_serialize(CTX, pubnonce_ser, NULL));
@@ -568,6 +606,7 @@ void frost_tweak_test_helper(const secp256k1_xonly_pubkey* agg_pk, const secp256
568606
secp256k1_frost_partial_sig partial_sig[3];
569607
const secp256k1_frost_partial_sig *partial_sig_ptr[3];
570608
unsigned char final_sig[64];
609+
size_t ids[3] = {0, 1, 2};
571610
int i;
572611

573612
for (i = 0; i < 3; i++) {
@@ -587,6 +626,34 @@ void frost_tweak_test_helper(const secp256k1_xonly_pubkey* agg_pk, const secp256
587626
/* CHECK(secp256k1_frost_nonce_process(CTX, &session[1], pubnonce_ptr, 3, msg, ids33[1], ids33, keygen_cache, NULL) == 1); */
588627
/* CHECK(secp256k1_frost_nonce_process(CTX, &session[2], pubnonce_ptr, 3, msg, ids33[2], ids33, keygen_cache, NULL) == 1); */
589628

629+
for (i = 0; i < 3; i++) {
630+
const secp256k1_frost_secshare *share = (i==0 ? sr0 : i==1 ? sr1 : sr2);
631+
CHECK(secp256k1_frost_nonce_gen(
632+
CTX,
633+
&secnonce[i],
634+
&pubnonce[i],
635+
session_id[i],
636+
share,
637+
NULL,
638+
NULL,
639+
NULL
640+
) == 1);
641+
}
642+
643+
for (i = 0; i < 3; i++) {
644+
CHECK(secp256k1_frost_nonce_process(
645+
CTX,
646+
&session[i],
647+
pubnonce_ptr,
648+
3,
649+
msg,
650+
ids[i],
651+
ids,
652+
keygen_cache,
653+
NULL
654+
) == 1);
655+
}
656+
590657

591658
CHECK(secp256k1_frost_partial_sign(CTX, &partial_sig[0], &secnonce[0], sr0, &session[0], keygen_cache) == 1);
592659
CHECK(secp256k1_frost_partial_sign(CTX, &partial_sig[1], &secnonce[1], sr1, &session[1], keygen_cache) == 1);
@@ -624,11 +691,17 @@ void frost_tweak_test(void) {
624691
pubshare_ptr[i] = &pubshare[i];
625692
}
626693
secp256k1_testrand256(seed);
694+
size_t ids[5];
695+
CHECK(secp256k1_frost_shares_gen(CTX, shares, vss_commitment, seed, 3, 5) == 1);
627696
/* CHECK(secp256k1_frost_shares_gen(CTX, shares, vss_commitment, seed, 3, 5, id_ptr) == 1); */
628697
for (i = 0; i < 5; i++) {
698+
CHECK(secp256k1_frost_compute_pubshare(CTX, &pubshare[i], 3, i, vss_commitment) == 1);
699+
pubshare_ptr[i] = &pubshare[i];
700+
ids[i] = i;
629701
/* CHECK(secp256k1_frost_share_verify(CTX, 3, id_ptr[i], &shares[i], vss_commitment) == 1); */
630702
/* CHECK(secp256k1_frost_compute_pubshare(CTX, &pubshare[i], 3, id_ptr[i], vss_commitment) == 1); */
631703
}
704+
CHECK(secp256k1_frost_pubkey_gen(CTX, &keygen_cache, pubshare_ptr, 5, ids) == 1);
632705
/* Compute P0 and test signing for it */
633706
/* CHECK(secp256k1_frost_pubkey_gen(CTX, &keygen_cache, pubshare_ptr, 5, id_ptr) == 1); */
634707
CHECK(secp256k1_frost_pubkey_get(CTX, &P[0], &keygen_cache) == 1);
@@ -679,11 +752,14 @@ void frost_dkg_test_helper(secp256k1_frost_keygen_cache *keygen_cache, secp256k1
679752
for (i = 0; i < 5; i++) {
680753
pubshare_ptr[i] = &pubshare[i];
681754
}
682-
/* CHECK(secp256k1_frost_shares_gen(CTX, shares, vss_commitment, seed, 3, 5, ids33) == 1); */
755+
CHECK(secp256k1_frost_shares_gen(CTX, shares, vss_commitment, seed, 3, 5) == 1);
683756
for (i = 0; i < 5; i++) {
684-
/* CHECK(secp256k1_frost_compute_pubshare(CTX, &pubshare[i], 3, ids33[i], vss_commitment) == 1); */
757+
CHECK(secp256k1_frost_compute_pubshare(CTX, &pubshare[i], 3, (size_t)i, vss_commitment) == 1);
758+
}
759+
{
760+
size_t ids[5] = {0,1,2,3,4};
761+
CHECK(secp256k1_frost_pubkey_gen(CTX, keygen_cache, pubshare_ptr, 5, ids) == 1);
685762
}
686-
/* CHECK(secp256k1_frost_pubkey_gen(CTX, keygen_cache, pubshare_ptr, 5, ids33) == 1); */
687763
}
688764

689765
/* Signs a message with a FROST keypair */
@@ -694,7 +770,7 @@ int frost_sign_test_helper(unsigned char *final_sig, const secp256k1_frost_secsh
694770
const secp256k1_frost_pubnonce *pubnonce_ptr[3];
695771
secp256k1_frost_partial_sig partial_sig[5];
696772
const secp256k1_frost_partial_sig *partial_sig_ptr[5];
697-
secp256k1_frost_session session;
773+
secp256k1_frost_session session[3];
698774
int i;
699775
int nonce_parity;
700776
secp256k1_frost_session_internal session_i;
@@ -706,14 +782,18 @@ int frost_sign_test_helper(unsigned char *final_sig, const secp256k1_frost_secsh
706782

707783
for (i = 0; i < 3; i++) {
708784
secp256k1_testrand256(session_id[i]);
709-
710-
/* CHECK(secp256k1_frost_nonce_gen(CTX, &secnonce[i], &pubnonce[i], session_id[i], &shares[i], NULL, NULL, NULL) == 1); */
785+
CHECK(secp256k1_frost_nonce_gen(CTX, &secnonce[i], &pubnonce[i], session_id[i], &shares[i], NULL, NULL, NULL) == 1);
786+
}
787+
{
788+
size_t ids[3] = {0, 1, 2};
789+
for (i = 0; i < 3; i++) {
790+
CHECK(secp256k1_frost_nonce_process(CTX, &session[i], pubnonce_ptr, 3, msg, ids[i], ids, keygen_cache, adaptor) == 1);
791+
}
711792
}
712793
for (i = 0; i < 3; i++) {
713-
/* CHECK(secp256k1_frost_nonce_process(CTX, &session, pubnonce_ptr, 3, msg, ids33[i], ids33, keygen_cache, adaptor) == 1); */
714-
CHECK(secp256k1_frost_partial_sign(CTX, &partial_sig[i], &secnonce[i], &shares[i], &session, keygen_cache) == 1);
794+
CHECK(secp256k1_frost_partial_sign(CTX, &partial_sig[i], &secnonce[i], &shares[i], &session[i], keygen_cache) == 1);
715795
}
716-
CHECK(secp256k1_frost_partial_sig_agg(CTX, final_sig, &session, partial_sig_ptr, 3) == 1);
796+
CHECK(secp256k1_frost_partial_sig_agg(CTX, final_sig, &session[0], partial_sig_ptr, 3) == 1);
717797

718798
CHECK(secp256k1_frost_nonce_parity(CTX, &nonce_parity, &session));
719799

0 commit comments

Comments
 (0)