14
14
#include <stdio.h>
15
15
#include <assert.h>
16
16
#include <string.h>
17
+ #include <stdlib.h>
17
18
18
19
#include <secp256k1.h>
19
20
#include <secp256k1_extrakeys.h>
@@ -41,18 +42,18 @@ static int create_keypair(const secp256k1_context* ctx, struct signer_secrets *s
41
42
while (1 ) {
42
43
if (!fill_random (seckey , sizeof (seckey ))) {
43
44
printf ("Failed to generate randomness\n" );
44
- return 0 ;
45
+ return EXIT_FAILURE ;
45
46
}
46
47
if (secp256k1_keypair_create (ctx , & signer_secrets -> keypair , seckey )) {
47
48
break ;
48
49
}
49
50
}
50
51
if (!secp256k1_keypair_pub (ctx , & signer -> pubkey , & signer_secrets -> keypair )) {
51
- return 0 ;
52
+ return EXIT_FAILURE ;
52
53
}
53
54
54
55
secure_erase (seckey , sizeof (seckey ));
55
- return 1 ;
56
+ return EXIT_SUCCESS ;
56
57
}
57
58
58
59
/* Tweak the pubkey corresponding to the provided keyagg cache, update the cache
@@ -70,7 +71,7 @@ static int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *agg_pk, s
70
71
/* Plain tweaking which, for example, allows deriving multiple child
71
72
* public keys from a single aggregate key using BIP32 */
72
73
if (!secp256k1_musig_pubkey_ec_tweak_add (ctx , NULL , cache , plain_tweak )) {
73
- return 0 ;
74
+ return EXIT_FAILURE ;
74
75
}
75
76
/* Note that we did not provide an output_pk argument, because the
76
77
* resulting pk is also saved in the cache and so if one is just interested
@@ -81,7 +82,7 @@ static int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *agg_pk, s
81
82
82
83
/* Xonly tweaking which, for example, allows creating Taproot commitments */
83
84
if (!secp256k1_musig_pubkey_xonly_tweak_add (ctx , & output_pk , cache , xonly_tweak )) {
84
- return 0 ;
85
+ return EXIT_FAILURE ;
85
86
}
86
87
/* Note that if we wouldn't care about signing, we can arrive at the same
87
88
* output_pk by providing the untweaked public key to
@@ -93,9 +94,9 @@ static int tweak(const secp256k1_context* ctx, secp256k1_xonly_pubkey *agg_pk, s
93
94
* `pk_parity` output argument; we would need it if we would have to open
94
95
* the Taproot commitment. */
95
96
if (!secp256k1_xonly_pubkey_from_pubkey (ctx , agg_pk , NULL , & output_pk )) {
96
- return 0 ;
97
+ return EXIT_FAILURE ;
97
98
}
98
- return 1 ;
99
+ return EXIT_FAILURE ;
99
100
}
100
101
101
102
/* Sign a message hash with the given key pairs and store the result in sig */
@@ -114,15 +115,15 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr
114
115
* is unique for every call of secp256k1_musig_nonce_gen. Otherwise
115
116
* it's trivial for an attacker to extract the secret key! */
116
117
if (!fill_random (session_secrand , sizeof (session_secrand ))) {
117
- return 0 ;
118
+ return EXIT_FAILURE ;
118
119
}
119
120
if (!secp256k1_keypair_sec (ctx , seckey , & signer_secrets [i ].keypair )) {
120
- return 0 ;
121
+ return EXIT_FAILURE ;
121
122
}
122
123
/* Initialize session and create secret nonce for signing and public
123
124
* nonce to send to the other signers. */
124
125
if (!secp256k1_musig_nonce_gen (ctx , & signer_secrets [i ].secnonce , & signer [i ].pubnonce , session_secrand , seckey , & signer [i ].pubkey , msg32 , NULL , NULL )) {
125
- return 0 ;
126
+ return EXIT_FAILURE ;
126
127
}
127
128
pubnonces [i ] = & signer [i ].pubnonce ;
128
129
@@ -133,21 +134,21 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr
133
134
* coordinator. The coordinator runs secp256k1_musig_nonce_agg and sends
134
135
* agg_pubnonce to each signer */
135
136
if (!secp256k1_musig_nonce_agg (ctx , & agg_pubnonce , pubnonces , N_SIGNERS )) {
136
- return 0 ;
137
+ return EXIT_FAILURE ;
137
138
}
138
139
139
140
/* Every signer creates a partial signature */
140
141
for (i = 0 ; i < N_SIGNERS ; i ++ ) {
141
142
/* Initialize the signing session by processing the aggregate nonce */
142
143
if (!secp256k1_musig_nonce_process (ctx , & session , & agg_pubnonce , msg32 , cache )) {
143
- return 0 ;
144
+ return EXIT_FAILURE ;
144
145
}
145
146
/* partial_sign will clear the secnonce by setting it to 0. That's because
146
147
* you must _never_ reuse the secnonce (or use the same session_secrand to
147
148
* create a secnonce). If you do, you effectively reuse the nonce and
148
149
* leak the secret key. */
149
150
if (!secp256k1_musig_partial_sign (ctx , & signer [i ].partial_sig , & signer_secrets [i ].secnonce , & signer_secrets [i ].keypair , cache , & session )) {
150
- return 0 ;
151
+ return EXIT_FAILURE ;
151
152
}
152
153
partial_sigs [i ] = & signer [i ].partial_sig ;
153
154
}
@@ -166,7 +167,7 @@ static int sign(const secp256k1_context* ctx, struct signer_secrets *signer_secr
166
167
* sigs if it does not work.
167
168
*/
168
169
if (!secp256k1_musig_partial_sig_verify (ctx , & signer [i ].partial_sig , & signer [i ].pubnonce , & signer [i ].pubkey , cache , & session )) {
169
- return 0 ;
170
+ return EXIT_FAILURE ;
170
171
}
171
172
}
172
173
return secp256k1_musig_partial_sig_agg (ctx , sig64 , & session , partial_sigs , N_SIGNERS );
@@ -188,9 +189,9 @@ int main(void) {
188
189
printf ("Creating key pairs......" );
189
190
fflush (stdout );
190
191
for (i = 0 ; i < N_SIGNERS ; i ++ ) {
191
- if (! create_keypair (ctx , & signer_secrets [i ], & signers [i ])) {
192
+ if (EXIT_FAILURE == create_keypair (ctx , & signer_secrets [i ], & signers [i ])) {
192
193
printf ("FAILED\n" );
193
- return 1 ;
194
+ return EXIT_FAILURE ;
194
195
}
195
196
pubkeys_ptr [i ] = & signers [i ].pubkey ;
196
197
}
@@ -205,7 +206,7 @@ int main(void) {
205
206
fflush (stdout );
206
207
if (!secp256k1_ec_pubkey_sort (ctx , pubkeys_ptr , N_SIGNERS )) {
207
208
printf ("FAILED\n" );
208
- return 1 ;
209
+ return EXIT_FAILURE ;
209
210
}
210
211
printf ("ok\n" );
211
212
@@ -216,29 +217,29 @@ int main(void) {
216
217
* while providing a non-NULL agg_pk argument. */
217
218
if (!secp256k1_musig_pubkey_agg (ctx , NULL , & cache , pubkeys_ptr , N_SIGNERS )) {
218
219
printf ("FAILED\n" );
219
- return 1 ;
220
+ return EXIT_FAILURE ;
220
221
}
221
222
printf ("ok\n" );
222
223
printf ("Tweaking................" );
223
224
fflush (stdout );
224
225
/* Optionally tweak the aggregate key */
225
226
if (!tweak (ctx , & agg_pk , & cache )) {
226
227
printf ("FAILED\n" );
227
- return 1 ;
228
+ return EXIT_FAILURE ;
228
229
}
229
230
printf ("ok\n" );
230
231
printf ("Signing message........." );
231
232
fflush (stdout );
232
233
if (!sign (ctx , signer_secrets , signers , & cache , msg , sig )) {
233
234
printf ("FAILED\n" );
234
- return 1 ;
235
+ return EXIT_FAILURE ;
235
236
}
236
237
printf ("ok\n" );
237
238
printf ("Verifying signature....." );
238
239
fflush (stdout );
239
240
if (!secp256k1_schnorrsig_verify (ctx , sig , msg , 32 , & agg_pk )) {
240
241
printf ("FAILED\n" );
241
- return 1 ;
242
+ return EXIT_FAILURE ;
242
243
}
243
244
printf ("ok\n" );
244
245
@@ -253,5 +254,5 @@ int main(void) {
253
254
secure_erase (& signer_secrets [i ], sizeof (signer_secrets [i ]));
254
255
}
255
256
secp256k1_context_destroy (ctx );
256
- return 0 ;
257
+ return EXIT_SUCCESS ;
257
258
}
0 commit comments