]> Git Repo - secp256k1.git/blame - src/secp256k1.c
Merge pull request #103
[secp256k1.git] / src / secp256k1.c
CommitLineData
0a433ea2
PW
1// Copyright (c) 2013 Pieter Wuille
2// Distributed under the MIT/X11 software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
8563713a
GM
5#define SECP256K1_BUILD (1)
6
04e34d18
PW
7#include "include/secp256k1.h"
8
1c7fa133 9#include "util.h"
11ab5622
PW
10#include "num_impl.h"
11#include "field_impl.h"
a9f5c8b8 12#include "scalar_impl.h"
11ab5622
PW
13#include "group_impl.h"
14#include "ecmult_impl.h"
949c1ebb 15#include "ecmult_gen_impl.h"
11ab5622 16#include "ecdsa_impl.h"
e2f71f1e 17#include "eckey_impl.h"
254327e4 18
04e34d18 19void secp256k1_start(unsigned int flags) {
254327e4 20 secp256k1_fe_start();
f11ff5be 21 secp256k1_ge_start();
04e34d18
PW
22 if (flags & SECP256K1_START_SIGN) {
23 secp256k1_ecmult_gen_start();
24 }
25 if (flags & SECP256K1_START_VERIFY) {
26 secp256k1_ecmult_start();
27 }
254327e4
PW
28}
29
d41e93a5 30void secp256k1_stop(void) {
b1483f87 31 secp256k1_ecmult_stop();
04e34d18 32 secp256k1_ecmult_gen_stop();
f11ff5be 33 secp256k1_ge_stop();
254327e4 34 secp256k1_fe_stop();
254327e4
PW
35}
36
d41e93a5 37int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
04e34d18 38 DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
1c7fa133
PW
39 DEBUG_CHECK(msg != NULL);
40 DEBUG_CHECK(msglen <= 32);
41 DEBUG_CHECK(sig != NULL);
42 DEBUG_CHECK(pubkey != NULL);
43
4adf6b2a
PW
44 int ret = -3;
45 secp256k1_num_t m;
d41e93a5 46 secp256k1_ecdsa_sig_t s;
764332d0 47 secp256k1_ge_t q;
4adf6b2a 48 secp256k1_num_set_bin(&m, msg, msglen);
d41e93a5 49
ffffc878 50 if (!secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen)) {
4adf6b2a
PW
51 ret = -1;
52 goto end;
53 }
d41e93a5 54 if (!secp256k1_ecdsa_sig_parse(&s, sig, siglen)) {
4adf6b2a
PW
55 ret = -2;
56 goto end;
57 }
d41e93a5 58 if (!secp256k1_ecdsa_sig_verify(&s, &q, &m)) {
4adf6b2a
PW
59 ret = 0;
60 goto end;
607884fc 61 }
4adf6b2a
PW
62 ret = 1;
63end:
4adf6b2a 64 return ret;
607884fc
PW
65}
66
78239167 67int secp256k1_ecdsa_sign(const unsigned char *message, int messagelen, unsigned char *signature, int *signaturelen, const unsigned char *seckey, const unsigned char *nonce) {
04e34d18 68 DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
1c7fa133
PW
69 DEBUG_CHECK(message != NULL);
70 DEBUG_CHECK(messagelen <= 32);
71 DEBUG_CHECK(signature != NULL);
72 DEBUG_CHECK(signaturelen != NULL);
73 DEBUG_CHECK(seckey != NULL);
74 DEBUG_CHECK(nonce != NULL);
75
a9f5c8b8 76 secp256k1_scalar_t sec, non, msg;
eca6cdb1 77 secp256k1_scalar_set_b32(&sec, seckey, NULL);
a9f5c8b8 78 int overflow = 0;
eca6cdb1
PW
79 secp256k1_scalar_set_b32(&non, nonce, &overflow);
80 {
81 unsigned char c[32] = {0};
82 memcpy(c + 32 - messagelen, message, messagelen);
83 secp256k1_scalar_set_b32(&msg, c, NULL);
84 memset(c, 0, 32);
85 }
a9f5c8b8 86 int ret = !secp256k1_scalar_is_zero(&non) && !overflow;
78239167 87 secp256k1_ecdsa_sig_t sig;
ba8fc0e2
WS
88 if (ret) {
89 ret = secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, NULL);
90 }
78239167
PW
91 if (ret) {
92 secp256k1_ecdsa_sig_serialize(signature, signaturelen, &sig);
93 }
a9f5c8b8
PW
94 secp256k1_scalar_clear(&msg);
95 secp256k1_scalar_clear(&non);
96 secp256k1_scalar_clear(&sec);
78239167
PW
97 return ret;
98}
42cccdaf 99
50eb498e 100int secp256k1_ecdsa_sign_compact(const unsigned char *message, int messagelen, unsigned char *sig64, const unsigned char *seckey, const unsigned char *nonce, int *recid) {
04e34d18 101 DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
1c7fa133
PW
102 DEBUG_CHECK(message != NULL);
103 DEBUG_CHECK(messagelen <= 32);
104 DEBUG_CHECK(sig64 != NULL);
105 DEBUG_CHECK(seckey != NULL);
106 DEBUG_CHECK(nonce != NULL);
107
a9f5c8b8 108 secp256k1_scalar_t sec, non, msg;
eca6cdb1 109 secp256k1_scalar_set_b32(&sec, seckey, NULL);
a9f5c8b8 110 int overflow = 0;
eca6cdb1
PW
111 secp256k1_scalar_set_b32(&non, nonce, &overflow);
112 {
113 unsigned char c[32] = {0};
114 memcpy(c + 32 - messagelen, message, messagelen);
115 secp256k1_scalar_set_b32(&msg, c, NULL);
116 memset(c, 0, 32);
117 }
a9f5c8b8 118 int ret = !secp256k1_scalar_is_zero(&non) && !overflow;
50eb498e 119 secp256k1_ecdsa_sig_t sig;
ba8fc0e2
WS
120 if (ret) {
121 ret = secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, recid);
122 }
50eb498e
PW
123 if (ret) {
124 secp256k1_num_get_bin(sig64, 32, &sig.r);
125 secp256k1_num_get_bin(sig64 + 32, 32, &sig.s);
126 }
a9f5c8b8
PW
127 secp256k1_scalar_clear(&msg);
128 secp256k1_scalar_clear(&non);
129 secp256k1_scalar_clear(&sec);
50eb498e
PW
130 return ret;
131}
132
133int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen, const unsigned char *sig64, unsigned char *pubkey, int *pubkeylen, int compressed, int recid) {
04e34d18 134 DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
1c7fa133
PW
135 DEBUG_CHECK(msg != NULL);
136 DEBUG_CHECK(msglen <= 32);
137 DEBUG_CHECK(sig64 != NULL);
138 DEBUG_CHECK(pubkey != NULL);
139 DEBUG_CHECK(pubkeylen != NULL);
140 DEBUG_CHECK(recid >= 0 && recid <= 3);
141
50eb498e
PW
142 int ret = 0;
143 secp256k1_num_t m;
50eb498e 144 secp256k1_ecdsa_sig_t sig;
50eb498e
PW
145 secp256k1_num_set_bin(&sig.r, sig64, 32);
146 secp256k1_num_set_bin(&sig.s, sig64 + 32, 32);
147 secp256k1_num_set_bin(&m, msg, msglen);
148
149 secp256k1_ge_t q;
150 if (secp256k1_ecdsa_sig_recover(&sig, &q, &m, recid)) {
ffffc878 151 secp256k1_eckey_pubkey_serialize(&q, pubkey, pubkeylen, compressed);
50eb498e
PW
152 ret = 1;
153 }
50eb498e
PW
154 return ret;
155}
156
ae6bc76e 157int secp256k1_ec_seckey_verify(const unsigned char *seckey) {
1c7fa133
PW
158 DEBUG_CHECK(seckey != NULL);
159
a9f5c8b8 160 secp256k1_scalar_t sec;
a9f5c8b8 161 int overflow;
eca6cdb1 162 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
a9f5c8b8
PW
163 int ret = !secp256k1_scalar_is_zero(&sec) && !overflow;
164 secp256k1_scalar_clear(&sec);
42cccdaf
PW
165 return ret;
166}
167
ae6bc76e 168int secp256k1_ec_pubkey_verify(const unsigned char *pubkey, int pubkeylen) {
1c7fa133
PW
169 DEBUG_CHECK(pubkey != NULL);
170
764332d0 171 secp256k1_ge_t q;
ffffc878 172 return secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen);
42cccdaf
PW
173}
174
ae6bc76e 175int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed) {
04e34d18 176 DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
1c7fa133
PW
177 DEBUG_CHECK(pubkey != NULL);
178 DEBUG_CHECK(pubkeylen != NULL);
179 DEBUG_CHECK(seckey != NULL);
180
a9f5c8b8 181 secp256k1_scalar_t sec;
eca6cdb1 182 secp256k1_scalar_set_b32(&sec, seckey, NULL);
42cccdaf
PW
183 secp256k1_gej_t pj;
184 secp256k1_ecmult_gen(&pj, &sec);
a9f5c8b8 185 secp256k1_scalar_clear(&sec);
42cccdaf
PW
186 secp256k1_ge_t p;
187 secp256k1_ge_set_gej(&p, &pj);
ffffc878 188 secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, compressed);
42cccdaf
PW
189 return 1;
190}
191
ae6bc76e 192int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) {
1c7fa133
PW
193 DEBUG_CHECK(pubkey != NULL);
194 DEBUG_CHECK(pubkeylen != NULL);
195
42cccdaf 196 secp256k1_ge_t p;
ffffc878 197 if (!secp256k1_eckey_pubkey_parse(&p, pubkey, *pubkeylen))
764332d0 198 return 0;
ffffc878 199 secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, 0);
42cccdaf
PW
200 return 1;
201}
da3038c7 202
ae6bc76e 203int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak) {
1c7fa133
PW
204 DEBUG_CHECK(seckey != NULL);
205 DEBUG_CHECK(tweak != NULL);
206
a9f5c8b8 207 secp256k1_scalar_t term;
a9f5c8b8 208 int overflow = 0;
eca6cdb1 209 secp256k1_scalar_set_b32(&term, tweak, &overflow);
a9f5c8b8 210 secp256k1_scalar_t sec;
eca6cdb1 211 secp256k1_scalar_set_b32(&sec, seckey, NULL);
eb74c36b 212
a9f5c8b8 213 int ret = secp256k1_eckey_privkey_tweak_add(&sec, &term) && !overflow;
561b0e10 214 if (ret) {
eca6cdb1 215 secp256k1_scalar_get_b32(seckey, &sec);
eb74c36b
PW
216 }
217
a9f5c8b8
PW
218 secp256k1_scalar_clear(&sec);
219 secp256k1_scalar_clear(&term);
561b0e10
PW
220 return ret;
221}
222
ae6bc76e 223int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
04e34d18 224 DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
1c7fa133
PW
225 DEBUG_CHECK(pubkey != NULL);
226 DEBUG_CHECK(tweak != NULL);
227
561b0e10 228 secp256k1_num_t term;
561b0e10 229 secp256k1_num_set_bin(&term, tweak, 32);
561b0e10 230 secp256k1_ge_t p;
eb74c36b 231 int ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
561b0e10 232 if (ret) {
eb74c36b 233 ret = secp256k1_eckey_pubkey_tweak_add(&p, &term);
561b0e10
PW
234 }
235 if (ret) {
86d3cce2 236 int oldlen = pubkeylen;
ffffc878 237 secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
1c7fa133 238 VERIFY_CHECK(pubkeylen == oldlen);
86d3cce2 239 }
eb74c36b 240
86d3cce2
PW
241 return ret;
242}
243
ae6bc76e 244int secp256k1_ec_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak) {
1c7fa133
PW
245 DEBUG_CHECK(seckey != NULL);
246 DEBUG_CHECK(tweak != NULL);
247
a9f5c8b8 248 secp256k1_scalar_t factor;
a9f5c8b8 249 int overflow = 0;
eca6cdb1 250 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
a9f5c8b8 251 secp256k1_scalar_t sec;
eca6cdb1 252 secp256k1_scalar_set_b32(&sec, seckey, NULL);
a9f5c8b8 253 int ret = secp256k1_eckey_privkey_tweak_mul(&sec, &factor) && !overflow;
86d3cce2 254 if (ret) {
eca6cdb1 255 secp256k1_scalar_get_b32(seckey, &sec);
eb74c36b
PW
256 }
257
a9f5c8b8
PW
258 secp256k1_scalar_clear(&sec);
259 secp256k1_scalar_clear(&factor);
86d3cce2
PW
260 return ret;
261}
262
ae6bc76e 263int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
04e34d18 264 DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
1c7fa133
PW
265 DEBUG_CHECK(pubkey != NULL);
266 DEBUG_CHECK(tweak != NULL);
267
350ade2a 268 secp256k1_num_t factor;
350ade2a 269 secp256k1_num_set_bin(&factor, tweak, 32);
86d3cce2 270 secp256k1_ge_t p;
eb74c36b 271 int ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
86d3cce2 272 if (ret) {
eb74c36b 273 ret = secp256k1_eckey_pubkey_tweak_mul(&p, &factor);
86d3cce2
PW
274 }
275 if (ret) {
561b0e10 276 int oldlen = pubkeylen;
ffffc878 277 secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
1c7fa133 278 VERIFY_CHECK(pubkeylen == oldlen);
561b0e10 279 }
eb74c36b 280
561b0e10
PW
281 return ret;
282}
283
ae6bc76e 284int secp256k1_ec_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) {
1c7fa133
PW
285 DEBUG_CHECK(seckey != NULL);
286 DEBUG_CHECK(privkey != NULL);
287 DEBUG_CHECK(privkeylen != NULL);
288
a9f5c8b8 289 secp256k1_scalar_t key;
eca6cdb1 290 secp256k1_scalar_set_b32(&key, seckey, NULL);
ffffc878 291 int ret = secp256k1_eckey_privkey_serialize(privkey, privkeylen, &key, compressed);
a9f5c8b8 292 secp256k1_scalar_clear(&key);
da3038c7
PW
293 return ret;
294}
295
ae6bc76e 296int secp256k1_ec_privkey_import(unsigned char *seckey, const unsigned char *privkey, int privkeylen) {
1c7fa133
PW
297 DEBUG_CHECK(seckey != NULL);
298 DEBUG_CHECK(privkey != NULL);
299
a9f5c8b8 300 secp256k1_scalar_t key;
ffffc878 301 int ret = secp256k1_eckey_privkey_parse(&key, privkey, privkeylen);
da3038c7 302 if (ret)
eca6cdb1 303 secp256k1_scalar_get_b32(seckey, &key);
a9f5c8b8 304 secp256k1_scalar_clear(&key);
da3038c7
PW
305 return ret;
306}
This page took 0.067826 seconds and 4 git commands to generate.