PRCYCoin  2.0.0.7rc1
P2P Digital Currency
secp256k1.c
Go to the documentation of this file.
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 
21 void secp256k1_start(unsigned int flags) {
22  secp256k1_fe_start();
23  secp256k1_ge_start();
24  secp256k1_scalar_start();
25  secp256k1_ecdsa_start();
27  secp256k1_ecmult_gen_start();
28  }
30  secp256k1_ecmult_start();
31  }
32 }
33 
34 void secp256k1_stop(void) {
35  secp256k1_ecmult_stop();
36  secp256k1_ecmult_gen_stop();
37  secp256k1_ecdsa_stop();
38  secp256k1_scalar_stop();
39  secp256k1_ge_stop();
40  secp256k1_fe_stop();
41 }
42 
43 int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen) {
44  DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
45  DEBUG_CHECK(msg != NULL);
46  DEBUG_CHECK(msglen <= 32);
47  DEBUG_CHECK(sig != NULL);
48  DEBUG_CHECK(pubkey != NULL);
49 
50  unsigned char msg32[32] = {0};
51  memcpy(msg32 + 32 - msglen, msg, msglen);
52  int ret = -3;
56  secp256k1_scalar_set_b32(&m, msg32, NULL);
57 
58  if (!secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen)) {
59  ret = -1;
60  goto end;
61  }
62  if (!secp256k1_ecdsa_sig_parse(&s, sig, siglen)) {
63  ret = -2;
64  goto end;
65  }
66  if (!secp256k1_ecdsa_sig_verify(&s, &q, &m)) {
67  ret = 0;
68  goto end;
69  }
70  ret = 1;
71 end:
72  return ret;
73 }
74 
75 int secp256k1_ecdsa_sign(const unsigned char *message, int messagelen, unsigned char *signature, int *signaturelen, const unsigned char *seckey, const unsigned char *nonce) {
76  DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
77  DEBUG_CHECK(message != NULL);
78  DEBUG_CHECK(messagelen <= 32);
79  DEBUG_CHECK(signature != NULL);
80  DEBUG_CHECK(signaturelen != NULL);
81  DEBUG_CHECK(seckey != NULL);
82  DEBUG_CHECK(nonce != NULL);
83 
84  secp256k1_scalar_t sec, non, msg;
85  secp256k1_scalar_set_b32(&sec, seckey, NULL);
86  int overflow = 0;
87  secp256k1_scalar_set_b32(&non, nonce, &overflow);
88  {
89  unsigned char c[32] = {0};
90  memcpy(c + 32 - messagelen, message, messagelen);
91  secp256k1_scalar_set_b32(&msg, c, NULL);
92  memset(c, 0, 32);
93  }
94  int ret = !secp256k1_scalar_is_zero(&non) && !overflow;
96  if (ret) {
97  ret = secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, NULL);
98  }
99  if (ret) {
100  secp256k1_ecdsa_sig_serialize(signature, signaturelen, &sig);
101  }
102  secp256k1_scalar_clear(&msg);
103  secp256k1_scalar_clear(&non);
104  secp256k1_scalar_clear(&sec);
105  return ret;
106 }
107 
108 int secp256k1_ecdsa_sign_compact(const unsigned char *message, int messagelen, unsigned char *sig64, const unsigned char *seckey, const unsigned char *nonce, int *recid) {
109  DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
110  DEBUG_CHECK(message != NULL);
111  DEBUG_CHECK(messagelen <= 32);
112  DEBUG_CHECK(sig64 != NULL);
113  DEBUG_CHECK(seckey != NULL);
114  DEBUG_CHECK(nonce != NULL);
115 
116  secp256k1_scalar_t sec, non, msg;
117  secp256k1_scalar_set_b32(&sec, seckey, NULL);
118  int overflow = 0;
119  secp256k1_scalar_set_b32(&non, nonce, &overflow);
120  {
121  unsigned char c[32] = {0};
122  memcpy(c + 32 - messagelen, message, messagelen);
123  secp256k1_scalar_set_b32(&msg, c, NULL);
124  memset(c, 0, 32);
125  }
126  int ret = !secp256k1_scalar_is_zero(&non) && !overflow;
128  if (ret) {
129  ret = secp256k1_ecdsa_sig_sign(&sig, &sec, &msg, &non, recid);
130  }
131  if (ret) {
132  secp256k1_scalar_get_b32(sig64, &sig.r);
133  secp256k1_scalar_get_b32(sig64 + 32, &sig.s);
134  }
135  secp256k1_scalar_clear(&msg);
136  secp256k1_scalar_clear(&non);
137  secp256k1_scalar_clear(&sec);
138  return ret;
139 }
140 
141 int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen, const unsigned char *sig64, unsigned char *pubkey, int *pubkeylen, int compressed, int recid) {
142  DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
143  DEBUG_CHECK(msg != NULL);
144  DEBUG_CHECK(msglen <= 32);
145  DEBUG_CHECK(sig64 != NULL);
146  DEBUG_CHECK(pubkey != NULL);
147  DEBUG_CHECK(pubkeylen != NULL);
148  DEBUG_CHECK(recid >= 0 && recid <= 3);
149 
150  int ret = 0;
151  unsigned char msg32[32] = {0};
152  memcpy(msg32 + 32 - msglen, msg, msglen);
155  int overflow = 0;
156  secp256k1_scalar_set_b32(&sig.r, sig64, &overflow);
157  if (overflow) {
158  return 0;
159  }
160  secp256k1_scalar_set_b32(&sig.s, sig64 + 32, &overflow);
161  if (overflow) {
162  return 0;
163  }
164  secp256k1_scalar_set_b32(&m, msg32, NULL);
165 
166  secp256k1_ge_t q;
167  if (secp256k1_ecdsa_sig_recover(&sig, &q, &m, recid)) {
168  ret = secp256k1_eckey_pubkey_serialize(&q, pubkey, pubkeylen, compressed);
169  }
170  return ret;
171 }
172 
173 int secp256k1_ec_seckey_verify(const unsigned char *seckey) {
174  DEBUG_CHECK(seckey != NULL);
175 
176  secp256k1_scalar_t sec;
177  int overflow;
178  secp256k1_scalar_set_b32(&sec, seckey, &overflow);
179  int ret = !secp256k1_scalar_is_zero(&sec) && !overflow;
180  secp256k1_scalar_clear(&sec);
181  return ret;
182 }
183 
184 int secp256k1_ec_pubkey_verify(const unsigned char *pubkey, int pubkeylen) {
185  DEBUG_CHECK(pubkey != NULL);
186 
187  secp256k1_ge_t q;
188  return secp256k1_eckey_pubkey_parse(&q, pubkey, pubkeylen);
189 }
190 
191 int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed) {
192  DEBUG_CHECK(secp256k1_ecmult_gen_consts != NULL);
193  DEBUG_CHECK(pubkey != NULL);
194  DEBUG_CHECK(pubkeylen != NULL);
195  DEBUG_CHECK(seckey != NULL);
196 
197  secp256k1_scalar_t sec;
198  secp256k1_scalar_set_b32(&sec, seckey, NULL);
199  secp256k1_gej_t pj;
200  secp256k1_ecmult_gen(&pj, &sec);
201  secp256k1_scalar_clear(&sec);
202  secp256k1_ge_t p;
203  secp256k1_ge_set_gej(&p, &pj);
204  return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, compressed);
205 }
206 
207 int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen) {
208  DEBUG_CHECK(pubkey != NULL);
209  DEBUG_CHECK(pubkeylen != NULL);
210 
211  secp256k1_ge_t p;
212  if (!secp256k1_eckey_pubkey_parse(&p, pubkey, *pubkeylen))
213  return 0;
214  return secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, 0);
215 }
216 
217 int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak) {
218  DEBUG_CHECK(seckey != NULL);
219  DEBUG_CHECK(tweak != NULL);
220 
221  secp256k1_scalar_t term;
222  int overflow = 0;
223  secp256k1_scalar_set_b32(&term, tweak, &overflow);
224  secp256k1_scalar_t sec;
225  secp256k1_scalar_set_b32(&sec, seckey, NULL);
226 
227  int ret = secp256k1_eckey_privkey_tweak_add(&sec, &term) && !overflow;
228  if (ret) {
229  secp256k1_scalar_get_b32(seckey, &sec);
230  }
231 
232  secp256k1_scalar_clear(&sec);
233  secp256k1_scalar_clear(&term);
234  return ret;
235 }
236 
237 int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
238  DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
239  DEBUG_CHECK(pubkey != NULL);
240  DEBUG_CHECK(tweak != NULL);
241 
242  secp256k1_scalar_t term;
243  int overflow = 0;
244  secp256k1_scalar_set_b32(&term, tweak, &overflow);
245  if (overflow) {
246  return 0;
247  }
248  secp256k1_ge_t p;
249  int ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
250  if (ret) {
251  ret = secp256k1_eckey_pubkey_tweak_add(&p, &term);
252  }
253  if (ret) {
254  int oldlen = pubkeylen;
255  ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
256  VERIFY_CHECK(pubkeylen == oldlen);
257  }
258 
259  return ret;
260 }
261 
262 int secp256k1_ec_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak) {
263  DEBUG_CHECK(seckey != NULL);
264  DEBUG_CHECK(tweak != NULL);
265 
266  secp256k1_scalar_t factor;
267  int overflow = 0;
268  secp256k1_scalar_set_b32(&factor, tweak, &overflow);
269  secp256k1_scalar_t sec;
270  secp256k1_scalar_set_b32(&sec, seckey, NULL);
271  int ret = secp256k1_eckey_privkey_tweak_mul(&sec, &factor) && !overflow;
272  if (ret) {
273  secp256k1_scalar_get_b32(seckey, &sec);
274  }
275 
276  secp256k1_scalar_clear(&sec);
277  secp256k1_scalar_clear(&factor);
278  return ret;
279 }
280 
281 int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak) {
282  DEBUG_CHECK(secp256k1_ecmult_consts != NULL);
283  DEBUG_CHECK(pubkey != NULL);
284  DEBUG_CHECK(tweak != NULL);
285 
286  secp256k1_scalar_t factor;
287  int overflow = 0;
288  secp256k1_scalar_set_b32(&factor, tweak, &overflow);
289  if (overflow) {
290  return 0;
291  }
292  secp256k1_ge_t p;
293  int ret = secp256k1_eckey_pubkey_parse(&p, pubkey, pubkeylen);
294  if (ret) {
295  ret = secp256k1_eckey_pubkey_tweak_mul(&p, &factor);
296  }
297  if (ret) {
298  int oldlen = pubkeylen;
299  ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, &pubkeylen, oldlen <= 33);
300  VERIFY_CHECK(pubkeylen == oldlen);
301  }
302 
303  return ret;
304 }
305 
306 int secp256k1_ec_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) {
307  DEBUG_CHECK(seckey != NULL);
308  DEBUG_CHECK(privkey != NULL);
309  DEBUG_CHECK(privkeylen != NULL);
310 
312  secp256k1_scalar_set_b32(&key, seckey, NULL);
313  int ret = secp256k1_eckey_privkey_serialize(privkey, privkeylen, &key, compressed);
314  secp256k1_scalar_clear(&key);
315  return ret;
316 }
317 
318 int secp256k1_ec_privkey_import(unsigned char *seckey, const unsigned char *privkey, int privkeylen) {
319  DEBUG_CHECK(seckey != NULL);
320  DEBUG_CHECK(privkey != NULL);
321 
323  int ret = secp256k1_eckey_privkey_parse(&key, privkey, privkeylen);
324  if (ret)
325  secp256k1_scalar_get_b32(seckey, &key);
326  secp256k1_scalar_clear(&key);
327  return ret;
328 }
field_impl.h
secp256k1_ecdsa_recover_compact
int secp256k1_ecdsa_recover_compact(const unsigned char *msg, int msglen, const unsigned char *sig64, unsigned char *pubkey, int *pubkeylen, int compressed, int recid)
Recover an ECDSA public key from a compact signature.
Definition: secp256k1.c:141
secp256k1_ec_privkey_tweak_add
int secp256k1_ec_privkey_tweak_add(unsigned char *seckey, const unsigned char *tweak)
Tweak a private key by adding tweak to it.
Definition: secp256k1.c:217
VERIFY_CHECK
#define VERIFY_CHECK(cond)
Definition: util.h:61
ecmult_gen_impl.h
ecmult_impl.h
eckey_impl.h
secp256k1_gej_t
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:21
secp256k1_ec_pubkey_create
int secp256k1_ec_pubkey_create(unsigned char *pubkey, int *pubkeylen, const unsigned char *seckey, int compressed)
Compute the public key for a secret key.
Definition: secp256k1.c:191
flags
int flags
Definition: prcycoin-tx.cpp:297
secp256k1_ec_privkey_tweak_mul
int secp256k1_ec_privkey_tweak_mul(unsigned char *seckey, const unsigned char *tweak)
Tweak a private key by multiplying it with tweak.
Definition: secp256k1.c:262
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:15
SECP256K1_START_VERIFY
#define SECP256K1_START_VERIFY
Flags to pass to secp256k1_start.
Definition: secp256k1.h:45
util.h
secp256k1_ecdsa_sign_compact
int secp256k1_ecdsa_sign_compact(const unsigned char *message, int messagelen, unsigned char *sig64, const unsigned char *seckey, const unsigned char *nonce, int *recid)
Create a compact ECDSA signature (64 byte + recovery id).
Definition: secp256k1.c:108
secp256k1.h
secp256k1_ec_seckey_verify
int secp256k1_ec_seckey_verify(const unsigned char *seckey)
Verify an ECDSA secret key.
Definition: secp256k1.c:173
secp256k1_ec_privkey_export
int secp256k1_ec_privkey_export(const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed)
Export a private key in DER format.
Definition: secp256k1.c:306
secp256k1_start
void secp256k1_start(unsigned int flags)
Initialize the library.
Definition: secp256k1.c:21
secp256k1_ec_pubkey_tweak_mul
int secp256k1_ec_pubkey_tweak_mul(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak)
Tweak a public key by multiplying it with tweak.
Definition: secp256k1.c:281
secp256k1_ecdsa_verify
int secp256k1_ecdsa_verify(const unsigned char *msg, int msglen, const unsigned char *sig, int siglen, const unsigned char *pubkey, int pubkeylen)
Verify an ECDSA signature.
Definition: secp256k1.c:43
group_impl.h
secp256k1_ec_pubkey_verify
int secp256k1_ec_pubkey_verify(const unsigned char *pubkey, int pubkeylen)
Just validate a public key.
Definition: secp256k1.c:184
SECP256K1_START_SIGN
#define SECP256K1_START_SIGN
Definition: secp256k1.h:46
DEBUG_CHECK
#define DEBUG_CHECK
Definition: util.h:52
key
CKey key
Definition: bip38tooldialog.cpp:173
secp256k1_ecdsa_sign
int secp256k1_ecdsa_sign(const unsigned char *message, int messagelen, unsigned char *signature, int *signaturelen, const unsigned char *seckey, const unsigned char *nonce)
Create an ECDSA signature.
Definition: secp256k1.c:75
secp256k1_ec_privkey_import
int secp256k1_ec_privkey_import(unsigned char *seckey, const unsigned char *privkey, int privkeylen)
Import a private key in DER format.
Definition: secp256k1.c:318
secp256k1_ec_pubkey_tweak_add
int secp256k1_ec_pubkey_tweak_add(unsigned char *pubkey, int pubkeylen, const unsigned char *tweak)
Tweak a public key by adding tweak times the generator to it.
Definition: secp256k1.c:237
secp256k1_ecdsa_sig_t::r
secp256k1_scalar_t r
Definition: ecdsa.h:17
secp256k1_ecdsa_sig_t::s
secp256k1_scalar_t s
Definition: ecdsa.h:17
num_impl.h
secp256k1_ec_pubkey_decompress
int secp256k1_ec_pubkey_decompress(unsigned char *pubkey, int *pubkeylen)
Decompress a public key.
Definition: secp256k1.c:207
secp256k1_scalar_t
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
secp256k1_ge_t
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:14
secp256k1_stop
void secp256k1_stop(void)
Free all memory associated with this library.
Definition: secp256k1.c:34
secp256k1_ecdsa_sig_t
Definition: ecdsa.h:16
ecdsa_impl.h
scalar_impl.h