aboutsummaryrefslogtreecommitdiff
path: root/src/secp256k1.c
blob: 1ab5b3722ce76f3ab6fda83fa19a36d1a2a45be9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**********************************************************************
 * Copyright (c) 2013, 2014 Pieter Wuille                             *
 * Distributed under the MIT software license, see the accompanying   *
 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
 **********************************************************************/

#define SECP256K1_BUILD (1)

#include "include/secp256k1.h"

#include "util.h"
#include "num_impl.h"
#include "field_impl.h"
#include "scalar_impl.h"
#include "group_impl.h"
#include "ecmult_impl.h"
#include "ecmult_gen_impl.h"
#include "ecdsa_impl.h"
#include "eckey_impl.h"

void secp256k1_start(unsigned int flags) {
    secp256k1_fe_start();
    secp256k1_ge_start();
    if (flags & SECP256K1_START_SIGN) {
        secp256k1_ecmult_gen_start();
    }
    if (flags & SECP256K1_START_VERIFY) {
        secp256k1_ecmult_start();
    }
}

void secp256k1_stop(void) {
    secp256k1_ecmult_stop();
    secp256k1_ecmult_gen_stop();
    secp256k1_ge_stop();
    secp256k1_fe_stop();
}

int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
    DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
    DEBUG_CHECK(msg != NULL);
    DEBUG_CHECK(msglen <= 32);
    DEBUG_CHECK(sig != NULL);
    DEBUG_CHECK(pubkey != NULL);

    int ret = -3;
    secp256k1_num_t m;
    secp256k1_ecdsa_sig_t s;
    secp256k1_ge_t q;
    secp256k1_num_set_bin(&m, msg, msglen);

    if (!secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen)) {
        ret = -1;
        goto end;
    }
    if (!secp256k1_ecdsa_sig_parse(&s, sig, siglen)) {
        ret = -2;
        goto end;
    }
    if (!secp256k1_ecdsa_sig_verify(&s, &q, &m)) {
        ret = 0;
        goto end;
    }
    ret = 1;
end:
    return ret;
}

int secp256k1_ecdsa_sign(const unsigned char *message, int messagelen, unsigned char *signature, int *signaturelen, const unsigned char *seckey, const unsigned char *nonce) {
    DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
    DEBUG_CHECK(message != NULL);
    DEBUG_CHECK(messagelen <= 32);
    DEBUG_CHECK(signature != NULL);
    DEBUG_CHECK(signaturelen != NULL);
    DEBUG_CHECK(seckey != NULL);
    DEBUG_CHECK(nonce != NULL);

    secp256k1_scalar_t sec, non, msg;
    secp256k1_scalar_set_b32(&sec, seckey, NULL);
    int overflow = 0;
    secp256k1_scalar_set_b32(&non, nonce, &overflow);
    {
        unsigned char c[32] = {0};
        memcpy(c + 32 - messagelen, message, messagelen);
        secp256k1_scalar_set_b32(&msg, c, NULL);
        memset(c, 0, 32);
    }
    int ret = !secp256k1_scalar_is_zero(&non) && !overflow;
    secp256k1_ecdsa_sig_t sig;
    if (ret) {
        ret = secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, NULL);
    }
    if (ret) {
        secp256k1_ecdsa_sig_serialize(signature, signaturelen, &sig);
    }
    secp256k1_scalar_clear(&msg);
    secp256k1_scalar_clear(&non);
    secp256k1_scalar_clear(&sec);
    return ret;
}

int secp256k1_ecdsa_sign_compact(const unsigned char *message, int messagelen, unsigned char *sig64, const unsigned char *seckey, const unsigned char *nonce, int *recid) {
    DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
    DEBUG_CHECK(message != NULL);
    DEBUG_CHECK(messagelen <= 32);
    DEBUG_CHECK(sig64 != NULL);
    DEBUG_CHECK(seckey != NULL);
    DEBUG_CHECK(nonce != NULL);

    secp256k1_scalar_t sec, non, msg;
    secp256k1_scalar_set_b32(&sec, seckey, NULL);
    int overflow = 0;
    secp256k1_scalar_set_b32(&non, nonce, &overflow);
    {
        unsigned char c[32] = {0};
        memcpy(c + 32 - messagelen, message, messagelen);
        secp256k1_scalar_set_b32(&msg, c, NULL);
        memset(c, 0, 32);
    }
    int ret = !secp256k1_scalar_is_zero(&non) && !overflow;
    secp256k1_ecdsa_sig_t sig;
    if (ret) {
        ret = secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, recid);
    }
    if (ret) {
        secp256k1_num_get_bin(sig64, 32, &sig.r);
        secp256k1_num_get_bin(sig64 + 32, 32, &sig.s);
    }
    secp256k1_scalar_clear(&msg);
    secp256k1_scalar_clear(&non);
    secp256k1_scalar_clear(&sec);
    return ret;
}

int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen, const unsigned char *sig64, unsigned char *pubkey, int *pubkeylen, int compressed, int recid) {
    DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
    DEBUG_CHECK(msg != NULL);
    DEBUG_CHECK(msglen <= 32);
    DEBUG_CHECK(sig64 != NULL);
    DEBUG_CHECK(pubkey != NULL);
    DEBUG_CHECK(pubkeylen != NULL);
    DEBUG_CHECK(recid >= 0 && recid <= 3);

    int ret = 0;
    secp256k1_num_t m;
    secp256k1_ecdsa_sig_t sig;
    secp256k1_num_set_bin(&sig.r, sig64, 32);
    secp256k1_num_set_bin(&sig.s, sig64 + 32, 32);
    secp256k1_num_set_bin(&m, msg, msglen);

    secp256k1_ge_t q;
    if (secp256k1_ecdsa_sig_recover(&sig, &q, &m, recid)) {
        ret = secp256k1_eckey_pubkey_serialize(&q, pubkey, pubkeylen, compressed);
    }
    return ret;
}

int secp256k1_ec_seckey_verify(const unsigned char *seckey) {
    DEBUG_CHECK(seckey != NULL);

    secp256k1_scalar_t sec;
    int overflow;
    secp256k1_scalar_set_b32(&sec, seckey, &overflow);
    int ret = !secp256k1_scalar_is_zero(&sec) && !overflow;
    secp256k1_scalar_clear(&sec);
    return ret;
}

int secp256k1_ec_pubkey_verify(const unsigned char *pubkey, int pubkeylen) {
    DEBUG_CHECK(pubkey != NULL);

    secp256k1_ge_t q;
    return secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen);
}

int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed) {
    DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
    DEBUG_CHECK(pubkey != NULL);
    DEBUG_CHECK(pubkeylen != NULL);
    DEBUG_CHECK(seckey != NULL);

    secp256k1_scalar_t sec;
    secp256k1_scalar_set_b32(&sec, seckey, NULL);
    secp256k1_gej_t pj;
    secp256k1_ecmult_gen(&pj, &sec);
    secp256k1_scalar_clear(&sec);
    secp256k1_ge_t p;
    secp256k1_ge_set_gej(&p, &pj);
    return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, compressed);
}

int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) {
    DEBUG_CHECK(pubkey != NULL);
    DEBUG_CHECK(pubkeylen != NULL);

    secp256k1_ge_t p;
    if (!secp256k1_eckey_pubkey_parse(&p, pubkey, *pubkeylen))
        return 0;
    return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, 0);
}

int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak) {
    DEBUG_CHECK(seckey != NULL);
    DEBUG_CHECK(tweak != NULL);

    secp256k1_scalar_t term;
    int overflow = 0;
    secp256k1_scalar_set_b32(&term, tweak, &overflow);
    secp256k1_scalar_t sec;
    secp256k1_scalar_set_b32(&sec, seckey, NULL);

    int ret = secp256k1_eckey_privkey_tweak_add(&sec, &term) && !overflow;
    if (ret) {
        secp256k1_scalar_get_b32(seckey, &sec);
    }

    secp256k1_scalar_clear(&sec);
    secp256k1_scalar_clear(&term);
    return ret;
}

int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
    DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
    DEBUG_CHECK(pubkey != NULL);
    DEBUG_CHECK(tweak != NULL);

    secp256k1_num_t term;
    secp256k1_num_set_bin(&term, tweak, 32);
    secp256k1_ge_t p;
    int ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
    if (ret) {
        ret = secp256k1_eckey_pubkey_tweak_add(&p, &term);
    }
    if (ret) {
        int oldlen = pubkeylen;
        ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
        VERIFY_CHECK(pubkeylen == oldlen);
    }

    return ret;
}

int secp256k1_ec_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak) {
    DEBUG_CHECK(seckey != NULL);
    DEBUG_CHECK(tweak != NULL);

    secp256k1_scalar_t factor;
    int overflow = 0;
    secp256k1_scalar_set_b32(&factor, tweak, &overflow);
    secp256k1_scalar_t sec;
    secp256k1_scalar_set_b32(&sec, seckey, NULL);
    int ret = secp256k1_eckey_privkey_tweak_mul(&sec, &factor) && !overflow;
    if (ret) {
        secp256k1_scalar_get_b32(seckey, &sec);
    }

    secp256k1_scalar_clear(&sec);
    secp256k1_scalar_clear(&factor);
    return ret;
}

int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
    DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
    DEBUG_CHECK(pubkey != NULL);
    DEBUG_CHECK(tweak != NULL);

    secp256k1_num_t factor;
    secp256k1_num_set_bin(&factor, tweak, 32);
    secp256k1_ge_t p;
    int ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
    if (ret) {
        ret = secp256k1_eckey_pubkey_tweak_mul(&p, &factor);
    }
    if (ret) {
        int oldlen = pubkeylen;
        ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
        VERIFY_CHECK(pubkeylen == oldlen);
    }

    return ret;
}

int secp256k1_ec_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) {
    DEBUG_CHECK(seckey != NULL);
    DEBUG_CHECK(privkey != NULL);
    DEBUG_CHECK(privkeylen != NULL);

    secp256k1_scalar_t key;
    secp256k1_scalar_set_b32(&key, seckey, NULL);
    int ret = secp256k1_eckey_privkey_serialize(privkey, privkeylen, &key, compressed);
    secp256k1_scalar_clear(&key);
    return ret;
}

int secp256k1_ec_privkey_import(unsigned char *seckey, const unsigned char *privkey, int privkeylen) {
    DEBUG_CHECK(seckey != NULL);
    DEBUG_CHECK(privkey != NULL);

    secp256k1_scalar_t key;
    int ret = secp256k1_eckey_privkey_parse(&key, privkey, privkeylen);
    if (ret)
        secp256k1_scalar_get_b32(seckey, &key);
    secp256k1_scalar_clear(&key);
    return ret;
}