-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsecp256k1_libsecp256k1.c
559 lines (433 loc) · 13.1 KB
/
secp256k1_libsecp256k1.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#define USE_NUM_GMP
//#define USE_NUM_OPENSSL
#define USE_FIELD_5X52
#define USE_FIELD_5X52_ASM
//#define USE_FIELD_5X52_INT128
//#define USE_FIELD_GMP
//#define USE_FIELD_10X26
//#define USE_FIELD_INV_BUILTIN
#define USE_FIELD_INV_NUM
#define USE_ENDOMORPHISM
#include <Python.h>
#define HAVE___INT128
#define USE_SCALAR_4X64
//#define USE_SCALAR_8X32
//#define USE_SCALAR_INV_BUILTIN
#define USE_SCALAR_INV_NUM
//#define USE_ECMULT_STATIC_PRECOMPUTATION
#include "include/secp256k1.h"
#include "secp256k1.c"
#include "util.h"
#include "num.h"
//#include "num_openssl_impl.h"
#include "num_gmp_impl.h"
#include "field.h"
#include "field_impl.h"
#include "group.h"
#include "group_impl.h"
#include "scalar.h"
#include "scalar_impl.h"
#include "ecmult.h"
#include "ecmult_impl.h"
#include "ecmult_gen.h"
#include "ecmult_gen_impl.h"
#include "eckey_impl.h"
#include "eckey_impl.h"
static secp256k1_context *ctx = NULL;
static void secp256k1_fe_get_hex(char *, const secp256k1_fe *);
static int secp256k1_fe_set_hex(secp256k1_fe *, const char *);
static PyObject* mul(PyObject* self, PyObject* args)
{
PyObject *value, *list;
if (!PyArg_ParseTuple(args, "OO", &list, &value)) {
return NULL;
}
if (!PyInt_Check(value) && !PyLong_Check(value)) {
PyErr_SetString(PyExc_TypeError, "expected int or long");
return NULL;
}
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "expected list");
return NULL;
}
// load point
PyObject *valuex, *valuey;
valuex = PyList_GetItem(list, 0);
valuey = PyList_GetItem(list, 1);
// check if point at infinity received
if (valuex==Py_None) {
return Py_BuildValue("[O,O]", Py_None,Py_None);
}
// convert to hex string
PyObject* fmt = PyString_FromString("%064x");
PyObject* pyhex = PyString_Format(fmt, value);
char* hex = PyString_AsString(pyhex);
PyObject* pyhexPx = PyString_Format(fmt, valuex);
char* hexPx = PyString_AsString(pyhexPx);
PyObject* pyhexPy = PyString_Format(fmt, valuey);
char* hexPy = PyString_AsString(pyhexPy);
// load into bignum type
secp256k1_fe x, y;
secp256k1_fe_set_hex(&x, hexPx);
secp256k1_fe_set_hex(&y, hexPy);
// load scalar
char hbuf[32];
int b;
for (int i=0; i < 32; i++) {
sscanf(hex+2*i, "%02x", &b);
hbuf[i] = b;
}
secp256k1_scalar a;
secp256k1_scalar_set_b32(&a, hbuf, NULL);
Py_DECREF(fmt);
Py_DECREF(pyhex);
Py_DECREF(pyhexPx);
Py_DECREF(pyhexPy);
// load x and y to affine struct
secp256k1_ge p;
secp256k1_ge_set_xy(&p, &x, &y);
secp256k1_gej r;
// use optimization if multiplication is performed on the base point
if (secp256k1_fe_equal_var(&secp256k1_ge_const_g.x, &x) && secp256k1_fe_equal_var(&secp256k1_ge_const_g.y, &y)) {
// perform scalar multiplication of G
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &r, &a);
} else {
// convert point from affine to jacobian coordinates
secp256k1_gej_set_ge(&r, &p);
// perform scalar multiplication
secp256k1_scalar zero;
secp256k1_scalar_set_int(&zero, 0);
secp256k1_ecmult(&ctx->ecmult_ctx, &r, &r, &a, &zero);
}
// check if the result is point at infinity
if (secp256k1_gej_is_infinity(&r)) {
return Py_BuildValue("[O,O]", Py_None,Py_None);
}
// convert result from jacobian to affine coordinates
secp256k1_ge t;
secp256k1_ge_set_gej(&t, &r);
// convert x and y to hex
char cx[65];
char cy[65];
secp256k1_fe_get_hex(cx, &t.x);
secp256k1_fe_get_hex(cy, &t.y);
cx[64] = 0;
cy[64] = 0;
// convert hex string of x, y to python long
PyObject* xr = PyLong_FromString(cx, NULL, 16);
PyObject* yr = PyLong_FromString(cy, NULL, 16);
return Py_BuildValue("[NN]", xr, yr);
}
static PyObject* add(PyObject* self, PyObject* args)
{
PyObject *list1, *list2;
if (!PyArg_ParseTuple(args, "OO", &list1, &list2)) {
return NULL;
}
if (!PyList_Check(list1) || !PyList_Check(list2)) {
PyErr_SetString(PyExc_TypeError, "expected list");
return NULL;
}
PyObject *oAx, *oAy;
oAx = PyList_GetItem(list1, 0);
oAy = PyList_GetItem(list1, 1);
PyObject *oBx, *oBy;
oBx = PyList_GetItem(list2, 0);
oBy = PyList_GetItem(list2, 1);
// check if point at infinity received
if (oAx==Py_None) {
return Py_BuildValue("O", list2);
}
if (oBx==Py_None) {
return Py_BuildValue("O", list1);
}
// convert to hex string
PyObject* fmt = PyString_FromString("%064x");
PyObject* pyhexAx = PyString_Format(fmt, oAx);
char* hexAx = PyString_AsString(pyhexAx);
PyObject* pyhexAy = PyString_Format(fmt, oAy);
char* hexAy = PyString_AsString(pyhexAy);
PyObject* pyhexBx = PyString_Format(fmt, oBx);
char* hexBx = PyString_AsString(pyhexBx);
PyObject* pyhexBy = PyString_Format(fmt, oBy);
char* hexBy = PyString_AsString(pyhexBy);
// load into bignum type
secp256k1_fe Ax, Ay, Bx, By;
secp256k1_fe_set_hex(&Ax, hexAx);
secp256k1_fe_set_hex(&Ay, hexAy);
secp256k1_fe_set_hex(&Bx, hexBx);
secp256k1_fe_set_hex(&By, hexBy);
Py_DECREF(fmt);
Py_DECREF(pyhexAx);
Py_DECREF(pyhexAy);
Py_DECREF(pyhexBx);
Py_DECREF(pyhexBy);
secp256k1_gej r;
// load point A in jacobian struct
secp256k1_ge a;
secp256k1_ge_set_xy(&a, &Ax, &Ay);
secp256k1_gej A;
secp256k1_gej_set_ge(&A, &a);
// load point B in affine struct
secp256k1_ge b;
secp256k1_ge_set_xy(&b, &Bx, &By);
// perform addition
secp256k1_gej_add_ge_var(&r, &A, &b, NULL);
// check if the result is point at infinity
if (secp256k1_gej_is_infinity(&r)) {
return Py_BuildValue("[O,O]", Py_None,Py_None);
}
// convert result from jacobian to affine coordinates
secp256k1_ge t;
secp256k1_ge_set_gej(&t, &r);
// convert x and y to hex
char cx[65];
char cy[65];
secp256k1_fe_get_hex(cx, &t.x);
secp256k1_fe_get_hex(cy, &t.y);
cx[64] = 0;
cy[64] = 0;
// convert hex string of x, y to python long
PyObject* xr = PyLong_FromString(cx, NULL, 16);
PyObject* yr = PyLong_FromString(cy, NULL, 16);
return Py_BuildValue("[NN]", xr, yr);
}
static PyObject* inv(PyObject* self, PyObject* args)
{
PyObject *list;
if (!PyArg_ParseTuple(args, "O", &list)) {
return NULL;
}
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "expected list");
return NULL;
}
PyObject *Px, *Py;
Px = PyList_GetItem(list, 0);
Py = PyList_GetItem(list, 1);
// check if point at infinity received
if (Px==Py_None) {
return Py_BuildValue("[O,O]", Py_None,Py_None);
}
// convert to hex string
PyObject* fmt = PyString_FromString("%064x");
PyObject* pyhexPx = PyString_Format(fmt, Px);
char* hexPx = PyString_AsString(pyhexPx);
PyObject* pyhexPy = PyString_Format(fmt, Py);
char* hexPy = PyString_AsString(pyhexPy);
// load into bignum type
secp256k1_fe x, y;
secp256k1_fe_set_hex(&x, hexPx);
secp256k1_fe_set_hex(&y, hexPy);
Py_DECREF(fmt);
Py_DECREF(pyhexPx);
Py_DECREF(pyhexPy);
// load point in affine struct
secp256k1_ge P;
secp256k1_ge_set_xy(&P, &x, &y);
// perform inverting
secp256k1_ge t;
secp256k1_ge_neg(&t, &P);
// convert x and y to hex
char cx[65];
char cy[65];
secp256k1_fe_get_hex(cx, &t.x);
secp256k1_fe_get_hex(cy, &t.y);
cx[64] = 0;
cy[64] = 0;
// convert hex string of x, y to python long
PyObject* xr = PyLong_FromString(cx, NULL, 16);
PyObject* yr = PyLong_FromString(cy, NULL, 16);
return Py_BuildValue("[NN]", xr, yr);
}
static PyObject* compress(PyObject* self, PyObject* args)
{
PyObject *list;
if (!PyArg_ParseTuple(args, "O", &list)) {
return NULL;
}
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "expected list");
return NULL;
}
PyObject *Px, *Py;
Px = PyList_GetItem(list, 0);
Py = PyList_GetItem(list, 1);
// check if point at infinity received
if (Px==Py_None) {
char pub[33] = "";
return Py_BuildValue("s#", pub, 33);
}
// convert to hex string
PyObject* fmt = PyString_FromString("%064x");
PyObject* pyhexPx = PyString_Format(fmt, Px);
char* hexPx = PyString_AsString(pyhexPx);
PyObject* pyhexPy = PyString_Format(fmt, Py);
char* hexPy = PyString_AsString(pyhexPy);
// load into bignum type
secp256k1_fe x, y;
secp256k1_fe_set_hex(&x, hexPx);
secp256k1_fe_set_hex(&y, hexPy);
Py_DECREF(fmt);
Py_DECREF(pyhexPx);
Py_DECREF(pyhexPy);
// load point in affine struct
secp256k1_ge P;
secp256k1_ge_set_xy(&P, &x, &y);
size_t size; char pub[33];
secp256k1_eckey_pubkey_serialize(&P, pub, &size, 1);
// convert char array to python string object
PyObject* s = PyString_FromStringAndSize(pub,size);
return Py_BuildValue("N", s);
}
static PyObject* decompress(PyObject* self, PyObject* args)
{
PyObject *pubs;
if (!PyArg_ParseTuple(args, "O", &pubs)) {
return NULL;
}
if (!PyString_Check(pubs)) {
PyErr_SetString(PyExc_TypeError, "string expected");
return NULL;
}
char* pub = PyString_AsString(pubs);
// check if point at infinity received
if (pub[0]==0) {
return Py_BuildValue("[O,O]", Py_None,Py_None);
}
// perform decompression
secp256k1_ge t;
secp256k1_eckey_pubkey_parse(&t, pub, 33);
// convert x and y to hex
char cx[65];
char cy[65];
secp256k1_fe_get_hex(cx, &t.x);
secp256k1_fe_get_hex(cy, &t.y);
cx[64] = 0;
cy[64] = 0;
// convert hex string of x, y to python long
PyObject* xr = PyLong_FromString(cx, NULL, 16);
PyObject* yr = PyLong_FromString(cy, NULL, 16);
return Py_BuildValue("[NN]", xr, yr);
}
static PyObject* valid(PyObject* self, PyObject* args)
{
PyObject *list;
if (!PyArg_ParseTuple(args, "O", &list)) {
return NULL;
}
if (!PyList_Check(list)) {
PyErr_SetString(PyExc_TypeError, "expected list");
return NULL;
}
PyObject *Px, *Py;
Px = PyList_GetItem(list, 0);
Py = PyList_GetItem(list, 1);
// check if point at infinity received
if (Px==Py_None) {
return Py_BuildValue("O", Py_None);
}
// convert to hex string
PyObject* fmt = PyString_FromString("%064x");
PyObject* pyhexPx = PyString_Format(fmt, Px);
char* hexPx = PyString_AsString(pyhexPx);
PyObject* pyhexPy = PyString_Format(fmt, Py);
char* hexPy = PyString_AsString(pyhexPy);
// load into bignum type
secp256k1_fe x, y;
secp256k1_fe_set_hex(&x, hexPx);
secp256k1_fe_set_hex(&y, hexPy);
Py_DECREF(fmt);
Py_DECREF(pyhexPx);
Py_DECREF(pyhexPy);
// load point in affine struct
secp256k1_ge P;
secp256k1_ge_set_xy(&P, &x, &y);
if (secp256k1_ge_is_valid_var(&P)) {
return Py_BuildValue("i", 1);
}
return Py_BuildValue("i", 0);
}
static PyMethodDef Methods[] =
{
{"mul", mul, METH_VARARGS, "Perform point multiplication"},
{"add", add, METH_VARARGS, "Perform point addition"},
{"inv", inv, METH_VARARGS, "Perform point inversion"},
{"compress", compress, METH_VARARGS, "Compress point"},
{"decompress", decompress, METH_VARARGS, "Decompress point"},
{"valid", valid, METH_VARARGS, "Check whether the point is on curve"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initsecp256k1_libsecp256k1(void)
{
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
PyObject *module;
module = Py_InitModule("secp256k1_libsecp256k1", Methods);
// export base point g
// convert x and y to hex
char cx[65];
char cy[65];
secp256k1_fe_get_hex(cx, &secp256k1_ge_const_g.x);
secp256k1_fe_get_hex(cy, &secp256k1_ge_const_g.y);
cx[64] = 0;
cy[64] = 0;
// convert hex string of x, y to python long
PyObject* xr = PyLong_FromString(cx, NULL, 16);
PyObject* yr = PyLong_FromString(cy, NULL, 16);
// export base point G as module variable "g"
PyModule_AddObject(module, "g", Py_BuildValue("[NN]", xr, yr));
// export group order n
secp256k1_num order;
secp256k1_scalar_order_get_num(&order);
// convert n to hex
char hbuf[32];
char cn[65];
secp256k1_num_get_bin(hbuf, 32, &order);
secp256k1_fe fe;
secp256k1_fe_set_b32(&fe, hbuf);
secp256k1_fe_get_hex(cn, &fe);
cn[64] = 0;
// convert hex string of n to python long
PyObject* n = PyLong_FromString(cn, NULL, 16);
// export curve order as module variable "n"
PyModule_AddObject(module, "n", Py_BuildValue("N", n));
}
static void secp256k1_fe_get_hex(char *r64, const secp256k1_fe *a) {
secp256k1_fe b;
int i;
unsigned char tmp[32];
b = *a;
secp256k1_fe_normalize(&b);
secp256k1_fe_get_b32(tmp, &b);
for (i=0; i<32; i++) {
/* Hex character table. */
static const char *c = "0123456789ABCDEF";
r64[2*i] = c[(tmp[i] >> 4) & 0xF];
r64[2*i+1] = c[(tmp[i]) & 0xF];
}
}
static int secp256k1_fe_set_hex(secp256k1_fe *r, const char *a64) {
int i;
unsigned char tmp[32];
/* Byte to hex value table. */
static const int cvt[256] = {0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 1, 2, 3, 4, 5, 6,7,8,9,0,0,0,0,0,0,
0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,
0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0};
for (i=0; i<32; i++) {
tmp[i] = (cvt[(unsigned char)a64[2*i]] << 4) + cvt[(unsigned char)a64[2*i+1]];
}
return secp256k1_fe_set_b32(r, tmp);
}