@@ -29,11 +29,12 @@ static int secp256k1_musig_compute_ell(const secp256k1_context *ctx, unsigned ch
29
29
return 1 ;
30
30
}
31
31
32
- /* Compute r = SHA256(ell, idx). The serialization of idx is least significant byte
33
- * first and variable-length such that the last byte is non-zero. */
34
- static void secp256k1_musig_coefficient (secp256k1_scalar * r , const unsigned char * ell , size_t idx ) {
32
+ /* Compute r = SHA256(ell, idx). The four bytes of idx are serialized least significant byte first. */
33
+ static void secp256k1_musig_coefficient (secp256k1_scalar * r , const unsigned char * ell , uint32_t idx ) {
35
34
secp256k1_sha256 sha ;
36
35
unsigned char buf [32 ];
36
+ size_t i ;
37
+
37
38
secp256k1_sha256_initialize (& sha );
38
39
secp256k1_sha256_write (& sha , ell , 32 );
39
40
/* We're hashing the index of the signer instead of its public key as specified
@@ -48,13 +49,12 @@ static void secp256k1_musig_coefficient(secp256k1_scalar *r, const unsigned char
48
49
* equivalent to hashing the public key. Because the public key can be
49
50
* identified by the index given the ordered list of public keys (included in
50
51
* ell), the index is just a different encoding of the public key.*/
51
- while ( idx > 0 ) {
52
+ for ( i = 0 ; i < sizeof ( uint32_t ); i ++ ) {
52
53
unsigned char c = idx ;
53
54
secp256k1_sha256_write (& sha , & c , 1 );
54
- idx /= 0x100 ;
55
+ idx >>= 8 ;
55
56
}
56
57
secp256k1_sha256_finalize (& sha , buf );
57
-
58
58
secp256k1_scalar_set_b32 (r , buf , NULL );
59
59
}
60
60
@@ -72,8 +72,8 @@ static int secp256k1_musig_pubkey_combine_callback(secp256k1_scalar *sc, secp256
72
72
}
73
73
74
74
75
- static void secp256k1_musig_signers_init (secp256k1_musig_session_signer_data * signers , size_t n_signers ) {
76
- size_t i ;
75
+ static void secp256k1_musig_signers_init (secp256k1_musig_session_signer_data * signers , uint32_t n_signers ) {
76
+ uint32_t i ;
77
77
for (i = 0 ; i < n_signers ; i ++ ) {
78
78
memset (& signers [i ], 0 , sizeof (signers [i ]));
79
79
signers [i ].index = i ;
@@ -144,8 +144,11 @@ int secp256k1_musig_session_initialize(const secp256k1_context* ctx, secp256k1_m
144
144
if (n_signers == 0 || my_index >= n_signers ) {
145
145
return 0 ;
146
146
}
147
- session -> n_signers = n_signers ;
148
- secp256k1_musig_signers_init (signers , n_signers );
147
+ if (n_signers > UINT32_MAX ) {
148
+ return 0 ;
149
+ }
150
+ session -> n_signers = (uint32_t ) n_signers ;
151
+ secp256k1_musig_signers_init (signers , session -> n_signers );
149
152
session -> nonce_commitments_hash_is_set = 0 ;
150
153
151
154
/* Compute secret key */
@@ -154,7 +157,7 @@ int secp256k1_musig_session_initialize(const secp256k1_context* ctx, secp256k1_m
154
157
secp256k1_scalar_clear (& secret );
155
158
return 0 ;
156
159
}
157
- secp256k1_musig_coefficient (& mu , pk_hash32 , my_index );
160
+ secp256k1_musig_coefficient (& mu , pk_hash32 , ( uint32_t ) my_index );
158
161
secp256k1_scalar_mul (& secret , & secret , & mu );
159
162
secp256k1_scalar_get_b32 (session -> seckey , & secret );
160
163
@@ -238,6 +241,11 @@ int secp256k1_musig_session_initialize_verifier(const secp256k1_context* ctx, se
238
241
ARG_CHECK (combined_pk != NULL );
239
242
ARG_CHECK (pk_hash32 != NULL );
240
243
ARG_CHECK (commitments != NULL );
244
+ /* Check n_signers before checking commitments to allow testing the case where
245
+ * n_signers is big without allocating the space. */
246
+ if (n_signers > UINT32_MAX ) {
247
+ return 0 ;
248
+ }
241
249
for (i = 0 ; i < n_signers ; i ++ ) {
242
250
ARG_CHECK (commitments [i ] != NULL );
243
251
}
@@ -249,8 +257,8 @@ int secp256k1_musig_session_initialize_verifier(const secp256k1_context* ctx, se
249
257
if (n_signers == 0 ) {
250
258
return 0 ;
251
259
}
252
- session -> n_signers = n_signers ;
253
- secp256k1_musig_signers_init (signers , n_signers );
260
+ session -> n_signers = ( uint32_t ) n_signers ;
261
+ secp256k1_musig_signers_init (signers , session -> n_signers );
254
262
255
263
memcpy (session -> pk_hash , pk_hash32 , 32 );
256
264
session -> nonce_is_set = 0 ;
0 commit comments