12
12
#include "include/secp256k1_frost.h"
13
13
#include "hash.h"
14
14
15
- int secp256k1_frost_keygen_init (const secp256k1_context * ctx , secp256k1_scalar * privcoeff , secp256k1_pubkey * pubcoeff , const size_t threshold , const size_t n_signers , const unsigned char * seckey32 ) {
15
+ int secp256k1_frost_keygen_init (const secp256k1_context * ctx , secp256k1_frost_keygen_session * session , secp256k1_scalar * privcoeff , secp256k1_pubkey * pubcoeff , const size_t threshold , const size_t n_signers , const size_t my_index , const unsigned char * seckey32 ) {
16
16
secp256k1_sha256 sha ;
17
17
size_t i ;
18
18
unsigned char rngseed [32 ];
@@ -25,15 +25,20 @@ int secp256k1_frost_keygen_init(const secp256k1_context *ctx, secp256k1_scalar *
25
25
return 0 ;
26
26
}
27
27
28
+ session -> threshold = threshold ;
29
+ session -> my_index = my_index ;
30
+ session -> n_signers = n_signers ;
31
+
28
32
/* Compute a random seed which commits to all inputs */
29
33
/* TODO: allow user suplied function that takes seckey, threshold, and n_signers as inputs and supplies the rngseed */
30
34
secp256k1_sha256_initialize (& sha );
31
35
secp256k1_sha256_write (& sha , seckey32 , 32 );
32
36
for (i = 0 ; i < 8 ; i ++ ) {
33
37
rngseed [i + 0 ] = threshold / (1ull << (i * 8 ));
34
38
rngseed [i + 8 ] = n_signers / (1ull << (i * 8 ));
39
+ rngseed [i + 16 ] = my_index / (1ull << (i * 8 ));
35
40
}
36
- secp256k1_sha256_write (& sha , rngseed , 16 );
41
+ secp256k1_sha256_write (& sha , rngseed , 24 );
37
42
secp256k1_sha256_finalize (& sha , rngseed );
38
43
39
44
/* Derive coefficients from the seed. */
@@ -50,36 +55,40 @@ int secp256k1_frost_keygen_init(const secp256k1_context *ctx, secp256k1_scalar *
50
55
secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & rj , & rand [i % 2 ]);
51
56
secp256k1_ge_set_gej (& rp , & rj );
52
57
secp256k1_pubkey_save (& pubcoeff [i ], & rp );
58
+
59
+ if (i == 0 ) {
60
+ secp256k1_pubkey_save (& session -> coeff_pk , & rp );
61
+ }
53
62
}
54
63
55
64
return 1 ;
56
65
}
57
66
58
- void secp256k1_frost_generate_shares (secp256k1_frost_share * shares , const secp256k1_scalar * coefficients , const size_t threshold , const size_t n_signers ) {
67
+ void secp256k1_frost_generate_shares (secp256k1_frost_share * shares , secp256k1_scalar * coeff , const secp256k1_frost_keygen_session * session ) {
59
68
size_t i ;
60
69
61
- for (i = 0 ; i < n_signers ; i ++ ) {
70
+ for (i = 0 ; i < session -> n_signers ; i ++ ) {
62
71
size_t j ;
63
72
secp256k1_scalar share_i ;
64
73
secp256k1_scalar scalar_i ;
65
74
66
75
/* Horner's method */
67
76
secp256k1_scalar_clear (& share_i );
68
77
secp256k1_scalar_set_int (& scalar_i , i + 1 );
69
- for (j = threshold ; j > 0 ; j -- ) {
78
+ for (j = session -> threshold ; j > 0 ; j -- ) {
70
79
secp256k1_scalar_mul (& share_i , & share_i , & scalar_i );
71
- secp256k1_scalar_add (& share_i , & share_i , & coefficients [j - 1 ]);
80
+ secp256k1_scalar_add (& share_i , & share_i , & coeff [j - 1 ]);
72
81
}
73
82
secp256k1_scalar_get_b32 (shares [i ].data , & share_i );
74
83
}
75
84
}
76
85
77
- void secp256k1_frost_aggregate_shares (secp256k1_frost_share * aggregate_share , secp256k1_frost_share * shares , const size_t n_signers ) {
86
+ void secp256k1_frost_aggregate_shares (secp256k1_frost_share * aggregate_share , const secp256k1_frost_share * shares , const secp256k1_frost_keygen_session * session ) {
78
87
size_t i ;
79
88
secp256k1_scalar acc ;
80
89
81
90
secp256k1_scalar_clear (& acc );
82
- for (i = 0 ; i < n_signers ; i ++ ) {
91
+ for (i = 0 ; i < session -> n_signers ; i ++ ) {
83
92
secp256k1_scalar share_i ;
84
93
secp256k1_scalar_set_b32 (& share_i , shares [i ].data , NULL );
85
94
secp256k1_scalar_add (& acc , & acc , & share_i );
@@ -98,28 +107,27 @@ static int secp256k1_frost_pubkey_combine_callback(secp256k1_scalar *sc, secp256
98
107
return secp256k1_pubkey_load (ctx -> ctx , pt , & ctx -> pks [idx ]);
99
108
}
100
109
101
- int secp256k1_frost_pubkey_combine (const secp256k1_context * ctx , secp256k1_scratch_space * scratch , secp256k1_xonly_pubkey * combined_pk , const secp256k1_pubkey * pubkeys , size_t n_pubkeys ) {
110
+ int secp256k1_frost_pubkey_combine (const secp256k1_context * ctx , secp256k1_scratch_space * scratch , secp256k1_frost_keygen_session * session , const secp256k1_pubkey * pubkeys ) {
102
111
secp256k1_frost_pubkey_combine_ecmult_data ecmult_data ;
103
112
secp256k1_gej pkj ;
104
113
secp256k1_ge pkp ;
105
114
106
115
VERIFY_CHECK (ctx != NULL );
107
- ARG_CHECK (combined_pk != NULL );
108
116
ARG_CHECK (secp256k1_ecmult_context_is_built (& ctx -> ecmult_ctx ));
109
117
ARG_CHECK (pubkeys != NULL );
110
- ARG_CHECK (n_pubkeys > 0 );
118
+ ARG_CHECK (session -> n_signers > 0 );
111
119
112
120
ecmult_data .ctx = ctx ;
113
121
ecmult_data .pks = pubkeys ;
114
122
115
- if (!secp256k1_ecmult_multi_var (& ctx -> error_callback , & ctx -> ecmult_ctx , scratch , & pkj , NULL , secp256k1_frost_pubkey_combine_callback , (void * ) & ecmult_data , n_pubkeys )) {
123
+ if (!secp256k1_ecmult_multi_var (& ctx -> error_callback , & ctx -> ecmult_ctx , scratch , & pkj , NULL , secp256k1_frost_pubkey_combine_callback , (void * ) & ecmult_data , session -> n_signers )) {
116
124
return 0 ;
117
125
}
118
126
119
127
secp256k1_ge_set_gej (& pkp , & pkj );
120
- secp256k1_fe_normalize (& pkp .y );
121
- secp256k1_extrakeys_ge_even_y (& pkp );
122
- secp256k1_xonly_pubkey_save (combined_pk , & pkp );
128
+ secp256k1_fe_normalize_var (& pkp .y );
129
+ session -> pk_parity = secp256k1_extrakeys_ge_even_y (& pkp );
130
+ secp256k1_xonly_pubkey_save (& session -> combined_pk , & pkp );
123
131
124
132
return 1 ;
125
133
}
0 commit comments