]> Git Repo - secp256k1.git/blob - src/secp256k1.c
Convert the rest of the codebase to C89.
[secp256k1.git] / src / secp256k1.c
1 /**********************************************************************
2  * Copyright (c) 2013, 2014 Pieter Wuille                             *
3  * Distributed under the MIT software license, see the accompanying   *
4  * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6
7 #define SECP256K1_BUILD (1)
8
9 #include "include/secp256k1.h"
10
11 #include "util.h"
12 #include "num_impl.h"
13 #include "field_impl.h"
14 #include "scalar_impl.h"
15 #include "group_impl.h"
16 #include "ecmult_impl.h"
17 #include "ecmult_gen_impl.h"
18 #include "ecdsa_impl.h"
19 #include "eckey_impl.h"
20 #include "hash_impl.h"
21
22 void secp256k1_start(unsigned int flags) {
23     if (flags & SECP256K1_START_SIGN) {
24         secp256k1_ecmult_gen_start();
25     }
26     if (flags & SECP256K1_START_VERIFY) {
27         secp256k1_ecmult_start();
28     }
29 }
30
31 void secp256k1_stop(void) {
32     secp256k1_ecmult_stop();
33     secp256k1_ecmult_gen_stop();
34 }
35
36 int secp256k1_ecdsa_verify(const unsigned char *msg32, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
37     secp256k1_ge_t q;
38     secp256k1_ecdsa_sig_t s;
39     secp256k1_scalar_t m;
40     int ret = -3;
41     DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
42     DEBUG_CHECK(msg32 != NULL);
43     DEBUG_CHECK(sig != NULL);
44     DEBUG_CHECK(pubkey != NULL);
45
46     secp256k1_scalar_set_b32(&m, msg32, NULL);
47
48     if (!secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen)) {
49         ret = -1;
50         goto end;
51     }
52     if (!secp256k1_ecdsa_sig_parse(&s, sig, siglen)) {
53         ret = -2;
54         goto end;
55     }
56     if (!secp256k1_ecdsa_sig_verify(&s, &q, &m)) {
57         ret = 0;
58         goto end;
59     }
60     ret = 1;
61 end:
62     return ret;
63 }
64
65 static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, unsigned int counter, const void *data) {
66    secp256k1_rfc6979_hmac_sha256_t rng;
67    unsigned int i;
68    (void)data;
69    secp256k1_rfc6979_hmac_sha256_initialize(&rng, key32, 32, msg32, 32);
70    for (i = 0; i <= counter; i++) {
71        secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
72    }
73    secp256k1_rfc6979_hmac_sha256_finalize(&rng);
74    return 1;
75 }
76
77 const secp256k1_nonce_function_t secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
78 const secp256k1_nonce_function_t secp256k1_nonce_function_default = nonce_function_rfc6979;
79
80 int secp256k1_ecdsa_sign(const unsigned char *msg32, unsigned char *signature, int *signaturelen, const unsigned char *seckey, secp256k1_nonce_function_t noncefp, const void* noncedata) {
81     secp256k1_ecdsa_sig_t sig;
82     secp256k1_scalar_t sec, non, msg;
83     int ret = 0;
84     int overflow = 0;
85     unsigned int count = 0;
86     DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
87     DEBUG_CHECK(msg32 != NULL);
88     DEBUG_CHECK(signature != NULL);
89     DEBUG_CHECK(signaturelen != NULL);
90     DEBUG_CHECK(seckey != NULL);
91     if (noncefp == NULL) {
92         noncefp = secp256k1_nonce_function_default;
93     }
94
95     secp256k1_scalar_set_b32(&sec, seckey, NULL);
96     secp256k1_scalar_set_b32(&msg, msg32, NULL);
97     while (1) {
98         unsigned char nonce32[32];
99         ret = noncefp(nonce32, msg32, seckey, count, noncedata);
100         if (!ret) {
101             break;
102         }
103         secp256k1_scalar_set_b32(&non, nonce32, &overflow);
104         memset(nonce32, 0, 32);
105         if (!secp256k1_scalar_is_zero(&non) && !overflow) {
106             if (secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, NULL)) {
107                 break;
108             }
109         }
110         count++;
111     }
112     if (ret) {
113         ret = secp256k1_ecdsa_sig_serialize(signature, signaturelen, &sig);
114     }
115     secp256k1_scalar_clear(&msg);
116     secp256k1_scalar_clear(&non);
117     secp256k1_scalar_clear(&sec);
118     return ret;
119 }
120
121 int secp256k1_ecdsa_sign_compact(const unsigned char *msg32, unsigned char *sig64, const unsigned char *seckey, secp256k1_nonce_function_t noncefp, const void* noncedata, int *recid) {
122     secp256k1_ecdsa_sig_t sig;
123     secp256k1_scalar_t sec, non, msg;
124     int ret = 0;
125     int overflow = 0;
126     unsigned int count = 0;
127     DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
128     DEBUG_CHECK(msg32 != NULL);
129     DEBUG_CHECK(sig64 != NULL);
130     DEBUG_CHECK(seckey != NULL);
131     if (noncefp == NULL) {
132         noncefp = secp256k1_nonce_function_default;
133     }
134
135     secp256k1_scalar_set_b32(&sec, seckey, NULL);
136     secp256k1_scalar_set_b32(&msg, msg32, NULL);
137     while (1) {
138         unsigned char nonce32[32];
139         ret = noncefp(nonce32, msg32, seckey, count, noncedata);
140         if (!ret) {
141             break;
142         }
143         secp256k1_scalar_set_b32(&non, nonce32, &overflow);
144         memset(nonce32, 0, 32);
145         if (!secp256k1_scalar_is_zero(&non) && !overflow) {
146             if (secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, recid)) {
147                 break;
148             }
149         }
150         count++;
151     }
152     if (ret) {
153         secp256k1_scalar_get_b32(sig64, &sig.r);
154         secp256k1_scalar_get_b32(sig64 + 32, &sig.s);
155     }
156     secp256k1_scalar_clear(&msg);
157     secp256k1_scalar_clear(&non);
158     secp256k1_scalar_clear(&sec);
159     return ret;
160 }
161
162 int secp256k1_ecdsa_recover_compact(const unsigned char *msg32, const unsigned char *sig64, unsigned char *pubkey, int *pubkeylen, int compressed, int recid) {
163     secp256k1_ge_t q;
164     secp256k1_ecdsa_sig_t sig;
165     secp256k1_scalar_t m;
166     int ret = 0;
167     int overflow = 0;
168     DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
169     DEBUG_CHECK(msg32 != NULL);
170     DEBUG_CHECK(sig64 != NULL);
171     DEBUG_CHECK(pubkey != NULL);
172     DEBUG_CHECK(pubkeylen != NULL);
173     DEBUG_CHECK(recid >= 0 && recid <= 3);
174
175     secp256k1_scalar_set_b32(&sig.r, sig64, &overflow);
176     if (overflow) {
177         return 0;
178     }
179     secp256k1_scalar_set_b32(&sig.s, sig64 + 32, &overflow);
180     if (overflow) {
181         return 0;
182     }
183     secp256k1_scalar_set_b32(&m, msg32, NULL);
184
185     if (secp256k1_ecdsa_sig_recover(&sig, &q, &m, recid)) {
186         ret = secp256k1_eckey_pubkey_serialize(&q, pubkey, pubkeylen, compressed);
187     }
188     return ret;
189 }
190
191 int secp256k1_ec_seckey_verify(const unsigned char *seckey) {
192     secp256k1_scalar_t sec;
193     int ret;
194     int overflow;
195     DEBUG_CHECK(seckey != NULL);
196
197     secp256k1_scalar_set_b32(&sec, seckey, &overflow);
198     ret = !secp256k1_scalar_is_zero(&sec) && !overflow;
199     secp256k1_scalar_clear(&sec);
200     return ret;
201 }
202
203 int secp256k1_ec_pubkey_verify(const unsigned char *pubkey, int pubkeylen) {
204     secp256k1_ge_t q;
205     DEBUG_CHECK(pubkey != NULL);
206
207     return secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen);
208 }
209
210 int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed) {
211     secp256k1_gej_t pj;
212     secp256k1_ge_t p;
213     secp256k1_scalar_t sec;
214     DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
215     DEBUG_CHECK(pubkey != NULL);
216     DEBUG_CHECK(pubkeylen != NULL);
217     DEBUG_CHECK(seckey != NULL);
218
219     secp256k1_scalar_set_b32(&sec, seckey, NULL);
220     secp256k1_ecmult_gen(&pj, &sec);
221     secp256k1_scalar_clear(&sec);
222     secp256k1_ge_set_gej(&p, &pj);
223     return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, compressed);
224 }
225
226 int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) {
227     secp256k1_ge_t p;
228     DEBUG_CHECK(pubkey != NULL);
229     DEBUG_CHECK(pubkeylen != NULL);
230
231     if (!secp256k1_eckey_pubkey_parse(&p, pubkey, *pubkeylen))
232         return 0;
233     return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, 0);
234 }
235
236 int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak) {
237     secp256k1_scalar_t term;
238     secp256k1_scalar_t sec;
239     int ret;
240     int overflow = 0;
241     DEBUG_CHECK(seckey != NULL);
242     DEBUG_CHECK(tweak != NULL);
243
244     secp256k1_scalar_set_b32(&term, tweak, &overflow);
245     secp256k1_scalar_set_b32(&sec, seckey, NULL);
246
247     ret = secp256k1_eckey_privkey_tweak_add(&sec, &term) && !overflow;
248     if (ret) {
249         secp256k1_scalar_get_b32(seckey, &sec);
250     }
251
252     secp256k1_scalar_clear(&sec);
253     secp256k1_scalar_clear(&term);
254     return ret;
255 }
256
257 int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
258     secp256k1_ge_t p;
259     secp256k1_scalar_t term;
260     int ret;
261     int overflow = 0;
262     DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
263     DEBUG_CHECK(pubkey != NULL);
264     DEBUG_CHECK(tweak != NULL);
265
266     secp256k1_scalar_set_b32(&term, tweak, &overflow);
267     if (overflow) {
268         return 0;
269     }
270     ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
271     if (ret) {
272         ret = secp256k1_eckey_pubkey_tweak_add(&p, &term);
273     }
274     if (ret) {
275         int oldlen = pubkeylen;
276         ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
277         VERIFY_CHECK(pubkeylen == oldlen);
278     }
279
280     return ret;
281 }
282
283 int secp256k1_ec_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak) {
284     secp256k1_scalar_t factor;
285     secp256k1_scalar_t sec;
286     int ret;
287     int overflow = 0;
288     DEBUG_CHECK(seckey != NULL);
289     DEBUG_CHECK(tweak != NULL);
290
291     secp256k1_scalar_set_b32(&factor, tweak, &overflow);
292     secp256k1_scalar_set_b32(&sec, seckey, NULL);
293     ret = secp256k1_eckey_privkey_tweak_mul(&sec, &factor) && !overflow;
294     if (ret) {
295         secp256k1_scalar_get_b32(seckey, &sec);
296     }
297
298     secp256k1_scalar_clear(&sec);
299     secp256k1_scalar_clear(&factor);
300     return ret;
301 }
302
303 int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
304     secp256k1_ge_t p;
305     secp256k1_scalar_t factor;
306     int ret;
307     int overflow = 0;
308     DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
309     DEBUG_CHECK(pubkey != NULL);
310     DEBUG_CHECK(tweak != NULL);
311
312     secp256k1_scalar_set_b32(&factor, tweak, &overflow);
313     if (overflow) {
314         return 0;
315     }
316     ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
317     if (ret) {
318         ret = secp256k1_eckey_pubkey_tweak_mul(&p, &factor);
319     }
320     if (ret) {
321         int oldlen = pubkeylen;
322         ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
323         VERIFY_CHECK(pubkeylen == oldlen);
324     }
325
326     return ret;
327 }
328
329 int secp256k1_ec_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) {
330     secp256k1_scalar_t key;
331     int ret;
332     DEBUG_CHECK(seckey != NULL);
333     DEBUG_CHECK(privkey != NULL);
334     DEBUG_CHECK(privkeylen != NULL);
335
336     secp256k1_scalar_set_b32(&key, seckey, NULL);
337     ret = secp256k1_eckey_privkey_serialize(privkey, privkeylen, &key, compressed);
338     secp256k1_scalar_clear(&key);
339     return ret;
340 }
341
342 int secp256k1_ec_privkey_import(unsigned char *seckey, const unsigned char *privkey, int privkeylen) {
343     secp256k1_scalar_t key;
344     int ret;
345     DEBUG_CHECK(seckey != NULL);
346     DEBUG_CHECK(privkey != NULL);
347
348     ret = secp256k1_eckey_privkey_parse(&key, privkey, privkeylen);
349     if (ret)
350         secp256k1_scalar_get_b32(seckey, &key);
351     secp256k1_scalar_clear(&key);
352     return ret;
353 }
This page took 0.065904 seconds and 4 git commands to generate.