-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
zen_ecp.c
916 lines (839 loc) · 19.8 KB
/
zen_ecp.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/*
* This file is part of zenroom
*
* Copyright (C) 2017-2021 Dyne.org foundation
* designed, written and maintained by Denis Roio <jaromil@dyne.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3.0
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* Along with this program you should have received a copy of the
* GNU Affero General Public License v3.0
* If not, see http://www.gnu.org/licenses/agpl.txt
*
* Last modified by Denis Roio
* on Monday, 9th August 2021
*/
/// <h1>Elliptic Curve Point Arithmetic (ECP)</h1>
//
// Base arithmetical operations on elliptic curve point coordinates.
//
// ECP arithmetic operations are provided to implement existing and
// new encryption schemes: they are elliptic curve cryptographic
// primitives and work the same across different curves.
//
// It is possible to create ECP points instances using the @{new}
// method. The values of each coordinate can be imported using @{BIG}
// methods `BIG.hex()` or `BIG.base64()`.
//
// Once ECP numbers are created this way, the arithmetic operations
// of addition, subtraction and multiplication can be executed
// normally using overloaded operators (+ - *).
//
// @module ECP
// @author Denis "Jaromil" Roio
// @license AGPLv3
// @copyright Dyne.org foundation 2017-2019
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <zen_ecp.h>
#include <zen_ecp_factory.h>
#include <zen_error.h>
#include <zen_octet.h>
#include <zen_big.h>
#include <zen_fp12.h>
#include <zen_memory.h>
#include <lua_functions.h>
extern int _octet_to_big(lua_State *L, big *dst, octet *src);
ecp* ecp_new(lua_State *L) {
ecp *e = (ecp *)lua_newuserdata(L, sizeof(ecp));
if(!e) {
zerror(L, "Error allocating new ecp in %s", __func__);
return NULL; }
e->halflen = sizeof(BIG);
e->totlen = (MODBYTES*2)+1; // length of ECP.new(rng:modbig(o), 0):octet()
luaL_getmetatable(L, "zenroom.ecp");
lua_setmetatable(L, -2);
return(e);
}
void ecp_free(lua_State *L, ecp* e) {
Z(L);
if(e) {
free(e);
Z->memcount_ecp--;
}
}
ecp* ecp_arg(lua_State *L, int n) {
Z(L);
void *ud = luaL_testudata(L, n, "zenroom.ecp");
if(ud) {
ecp* result = (ecp*)malloc(sizeof(ecp));
*result = *(ecp*)ud;
Z->memcount_ecp++;
return result;
}
zerror(L, "invalid ECP in argument");
return NULL;
}
ecp* ecp_dup(lua_State *L, ecp* in) {
ecp *e = ecp_new(L);
if(e == NULL) {
zerror(L, "Error duplicating ECP in %s", __func__);
return NULL;
}
ECP_copy(&e->val, &in->val);
return(e);
}
int ecp_destroy(lua_State *L) {
(void)L;
return 0;
}
int _fp_to_big(big *dst, FP *src) {
FP_redc(dst->val, src);
return 1;
}
/***
Create a new ECP point from an @{OCTET} argument containing its coordinates.
@param[@{OCTET}] coordinates of the point on the elliptic curve
@return a new ECP point on the curve
@function new(octet)
@see ECP:octet
*/
static int lua_new_ecp(lua_State *L) {
BEGIN();
// unsafe parsing into BIG, only necessary for tests
// deactivate when not running tests
void *tx;
char *failed_msg = NULL;
octet *o = NULL;
tx = luaL_testudata(L, 1, "zenroom.big");
void *ty = luaL_testudata(L, 2, "zenroom.big");
if(tx && ty) {
ecp *e = ecp_new(L);
big *x = NULL, *y = NULL;
if(!e) {
failed_msg = "Could not create ECP";
goto end_big_big;
}
x = big_arg(L, 1);
y = big_arg(L, 2);
if(!x || !y) {
failed_msg = "Could not create BIGs";
goto end_big_big;
}
if(!ECP_set(&e->val, x->val, y->val))
warning(L, "new ECP value out of curve (points to infinity)");
end_big_big:
big_free(L,y);
big_free(L,x);
goto end;
}
#ifdef DEBUG
// If x is on the curve then y is calculated from the curve equation.
int tn;
lua_Number n = lua_tonumberx(L, 2, &tn);
if(tx && tn) {
big *x = NULL;
ecp *e = ecp_new(L);
if(!e) {
failed_msg = "Could not create ECP";
goto end_big_number;
}
x = big_arg(L, 1);
if(!x) {
failed_msg = "Could not create BIG";
goto end_big_number;
}
if(!ECP_setx(&e->val, x->val, (int)n))
warning(L, "new ECP value out of curve (points to infinity)");
end_big_number:
big_free(L,x);
goto end;
}
#endif
tx = luaL_testudata(L, 1, "zenroom.big");
if(tx) {
ecp *e = ecp_new(L);
big *x = NULL;
if(!e) {
failed_msg = "Could not create ECP";
goto end_big;
}
x = big_arg(L, 1);
if(!x) {
failed_msg = "Could not create BIG";
goto end_big;
}
if(!ECP_setx(&e->val, x->val, 0))
warning(L, "new ECP value out of curve (points to infinity)");
end_big:
big_free(L,x);
goto end;
}
// We protect well this entrypoint since parsing any input is at risk
// Milagro's _fromOctet() uses ECP_BLS_set(ECP_BLS *P, BIG x)
// then converts the BIG to an FP modulo using FP_BLS_nres.
o = o_arg(L, 1);
if(!o) {
failed_msg = "Could not allocate input";
goto end;
}
ecp *e = ecp_new(L); SAFE(e);
if(o->len == 2 && o->val[0] == SCHAR_MAX && o->val[1] == SCHAR_MAX) {
ECP_inf(&e->val);
goto end; } // ECP Infinity
if(o->len > e->totlen) { // quick and dirty safety
lua_pop(L, 1);
zerror(L, "Octet length %u instead of %u bytes", o->len, e->totlen);
failed_msg = "Invalid octet length to parse an ECP point";
goto end;
}
int res = ECP_validate(o);
if(res<0) { // test in Milagro's ecdh_*.h ECP_*_PUBLIC_KEY_VALIDATE
lua_pop(L, 1);
failed_msg = "Octet is not a valid ECP (point is not on this curve)";
goto end;
}
if(! ECP_fromOctet(&e->val, o) ) {
lua_pop(L, 1);
failed_msg = "Octet doesn't contains a valid ECP";
goto end;
}
end:
o_free(L,o);
if(failed_msg != NULL) {
lerror(L, failed_msg);
lua_pushnil(L);
}
END(1);
}
/***
Returns the generator of the curve: an ECP point that is multiplied by any @{BIG} number to obtain a correspondent point on the curve.
@function generator()
@return ECP point of the curve's generator.
*/
static int ecp_generator(lua_State *L) {
BEGIN();
ecp *e = ecp_new(L); SAFE(e);
/* if(!ECP_set(&e->val,
(chunk*)CURVE_Gx, (chunk*)CURVE_Gy)) {
lerror(L, "ECP generator value out of curve (stack corruption)");
return 0; }
*/
ECP_generator(&e->val);
END(1);
}
/***
Returns a new ECP infinity point that is definitely not on the curve.
@function infinity()
@return ECP pointing to infinity (out of the curve).
*/
static int ecp_get_infinity(lua_State *L) {
BEGIN();
ecp *e = ecp_new(L);
if(e) {
ECP_inf(&e->val);
} else {
THROW("Could not create ECP");
}
END(1);
}
/***
Gives the order of the curve, a @{BIG} number contained in an octet.
@function order()
@return a @{BIG} number containing the curve's order
*/
static int ecp_order(lua_State *L) {
BEGIN();
big *res = big_new(L);
if(res) {
big_init(L,res);
// BIG is an array of int32_t on chunk 32 (see rom_curve)
// curve order is ready-only so we need a copy for norm() to work
BIG_copy(res->val, (chunk*)CURVE_Order);
} else {
THROW("Could not create BIG");
}
END(1);
}
/***
Map an @{OCTET} of exactly 64 bytes length to a point on the curve: the OCTET should be the output of an hash function.
@param OCTET resulting from an hash function
@function mapit(OCTET)
@return an ECP that is univocally linked to the input OCTET
*/
static int ecp_mapit(lua_State *L) {
BEGIN();
octet *o = o_arg(L, 1);
if(!o) {
lerror(L, "Could not allocate ecp point");
lua_pushnil(L);
} else if(o->len != 64) {
o_free(L, o);
zerror(L, "octet length is %u instead of 64 (need to use sha512)", o->len);
lerror(L, "Invalid argument to ECP.mapit(), not an hash");
lua_pushnil(L);
} else {
ecp *e = ecp_new(L); SAFE(e);
func(L, "mapit on o->len %u", o->len);
ECP_mapit(&e->val, o);
o_free(L, o);
}
END(1);
}
/***
Verify that an @{OCTET} really corresponds to an ECP point on the curve.
@param OCTET point to be validated
@function validate(OCTET)
@return bool value: true if valid, false if not valid
*/
static int ecp_validate(lua_State *L) {
BEGIN();
octet *o = o_arg(L, 1);
if(o) {
int res = ECP_validate(o);
lua_pushboolean(L, res>=0);
o_free(L, o);
} else {
THROW("Could not allocate ECP point");
}
END(1);
}
/// Instance Methods
// @type ecp
/***
Make an existing ECP point affine with its curve
@function affine()
@return ECP point made affine
*/
static int ecp_affine(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *out = NULL;
ecp *in = ecp_arg(L, 1);
if(!in) {
failed_msg = "Could not create ECP";
goto end;
}
out = ecp_dup(L, in);
if(!out) {
failed_msg = "Could not create ECP";
goto end;
}
ECP_affine(&out->val);
end:
ecp_free(L,in);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Returns true if an ECP coordinate points to infinity (out of the curve) and false otherwise.
@function isinf()
@return false if point is on curve, true if its off curve into infinity.
*/
static int ecp_isinf(lua_State *L) {
BEGIN();
ecp *e = ecp_arg(L, 1);
if(e) {
lua_pushboolean(L, ECP_isinf(&e->val));
ecp_free(L,e);
} else {
THROW("Could not create ECP");
}
END(1);
}
/***
Add two ECP points to each other (commutative and associative operation). Can be made using the overloaded operator `+` between two ECP objects just like the would be numbers.
@param first number to be summed
@param second number to be summed
@function add(first, second)
@return sum resulting from the addition
*/
static int ecp_add(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *e = ecp_arg(L, 1);
ecp *q = ecp_arg(L, 2);
if(!e || !q) {
failed_msg = "Could not create ECP";
goto end;
}
ecp *p = ecp_dup(L, e); // push
if(!p) {
failed_msg = "Could not create ECP";
goto end;
}
ECP_add(&p->val, &q->val);
end:
ecp_free(L,q);
ecp_free(L,e);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Subtract an ECP point from another (commutative and associative operation). Can be made using the overloaded operator `-` between two ECP objects just like the would be numbers.
@param first number from which the second should be subtracted
@param second number to use in the subtraction
@function sub(first, second)
@return new ECP point resulting from the subtraction
*/
static int ecp_sub(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *e = ecp_arg(L, 1);
ecp *q = ecp_arg(L, 2);
if(!e || !q) {
failed_msg = "Could not create ECP";
goto end;
}
ecp *p = ecp_dup(L, e); // push
if(!p) {
failed_msg = "Could not create ECP";
goto end;
}
ECP_sub(&p->val, &q->val);
end:
ecp_free(L,q);
ecp_free(L,e);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Transforms an ECP point into its equivalent negative point on the elliptic curve.
@function negative()
*/
static int ecp_negative(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *out = NULL;
ecp *in = ecp_arg(L, 1);
if(!in) {
failed_msg = "Could not create ECP";
goto end;
}
out = ecp_dup(L, in);
if(!out) {
failed_msg = "Could not create ECP";
goto end;
}
ECP_neg(&out->val);
end:
ecp_free(L,in);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Transforms an ECP pointo into the double of its value, multiplying it by two. This works faster than multiplying it an arbitrary number of times.
@function double()
*/
static int ecp_double(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *out = NULL;
ecp *in = ecp_arg(L, 1);
if(!in) {
failed_msg = "Could not create ECP";
goto end;
}
out = ecp_dup(L, in);
if(!out) {
failed_msg = "Could not create ECP";
goto end;
}
ECP_dbl(&out->val);
end:
ecp_free(L,in);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Multiply an ECP point by a @{BIG} number. Can be made using the overloaded operator `*`
@function mul(ecp, num)
@param ecp point on the elliptic curve to be multiplied
@param number indicating how many times it should be multiplied
@return new ecp point resulting from the multiplication
*/
static int ecp_mul(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *out = NULL;
ecp *e = ecp_arg(L, 1);
big *b = big_arg(L, 2);
if(!e || !b) {
failed_msg = "Could not instantiate input";
goto end;
}
if(b->doublesize) {
failed_msg = "cannot multiply ECP point with double BIG numbers, need modulo";
goto end;
}
out = ecp_dup(L, e);
if(!out) {
failed_msg = "Could not create ECP";
goto end;
}
PAIR_G1mul(&out->val, b->val);
end:
ecp_free(L,e);
big_free(L,b);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Compares two ECP objects and returns true if they indicate the same point on the curve (they are equal) or false otherwise. It can also be executed by using the `==` overloaded operator.
@param first ecp point to be compared
@param second ecp point to be compared
@function eq(first, second)
@return bool value: true if equal, false if not equal
*/
static int ecp_eq(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *p = ecp_arg(L, 1);
ecp *q = ecp_arg(L, 2);
if(!p || !q) {
failed_msg = "Could not allocate ECP point";
goto end;
}
// TODO: is affine rly needed?
ECP_affine(&p->val);
ECP_affine(&q->val);
lua_pushboolean(L, ECP_equals(&p->val, &q->val));
end:
ecp_free(L,p);
ecp_free(L,q);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
// use shared internally with octet o_arg()
int _ecp_to_octet(octet *o, ecp *e) {
if (ECP_isinf(&e->val)) { // Infinity
o->val[0] = SCHAR_MAX; o->val[1] = SCHAR_MAX;
o->val[3] = 0x0; o->len = 2;
} else
ECP_toOctet(o, &e->val, 1);
return(1);
}
/***
Returns an octet containing the coordinate of an ECP point on the curve. It can be used to export the value of an ECP point into a string, using @{OCTET:hex} or @{OCTET:base64} encapsulation. It can be decoded back to an ECP point using @{ECP:new}.
@function octet()
@return the ECP point as an OCTET sequence
*/
static int ecp_octet(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *o = NULL;
ecp *e = ecp_arg(L, 1);
if(!e) {
failed_msg = "Could not instantiate ECP";
goto end;
}
o = o_new(L, e->totlen + 0x0f);
if(!o) {
failed_msg = "Could not instantiate ECP";
goto end;
}
_ecp_to_octet(o, e);
end:
ecp_free(L,e);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Gives the X coordinate of the ECP point as a single @{BIG} number.
@function x()
@return a BIG number indicating the X coordinate of the point on curve.
*/
static int ecp_get_x(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *e = ecp_arg(L, 1);
if(!e) {
failed_msg = "Could not read ECP";
goto end;
}
ECP_affine(&e->val);
big *x = big_new(L);
if(!x) {
failed_msg = "Could not read BIG";
goto end;
}
big_init(L,x);
_fp_to_big(x, &e->val.x);
end:
ecp_free(L,e);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/***
Gives the Y coordinate of the ECP point as a single @{BIG} number.
@function y()
@return a BIG number indicating the Y coordinate of the point on curve.
*/
static int ecp_get_y(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
big *y = NULL;
ecp *e = ecp_arg(L, 1);
if(!e) {
failed_msg = "Could not read ECP";
goto end;
}
ECP_affine(&e->val);
y = big_new(L);
if(!y) {
failed_msg = "Could not read BIG";
goto end;
}
big_init(L,y);
_fp_to_big(y, &e->val.y);
end:
ecp_free(L,e);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
static int ecp_prime(lua_State *L) {
BEGIN();
big *p = big_new(L); big_init(L,p); SAFE(p);
BIG_rcopy(p->val, CURVE_Prime);
END(1);
}
static int ecp_output(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *e = ecp_arg(L, 1);
if(!e) {
failed_msg = "Could not read ECP";
goto end;
}
if (ECP_isinf(&e->val)) { // Infinity
octet *o = o_new(L, 3);
if(!o) {
failed_msg = "Could not read OCTET";
goto end;
}
o->val[0] = SCHAR_MAX; o->val[1] = SCHAR_MAX;
o->val[3] = 0x0; o->len = 2;
goto end;
}
octet *o = o_new(L, e->totlen + 0x0f);
if(!o) {
failed_msg = "Could not read OCTET";
goto end;
}
_ecp_to_octet(o, e);
push_octet_to_hex_string(L, o);
end:
ecp_free(L,e);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
char gf_sign(BIG y) {
BIG p;
BIG_rcopy(p, CURVE_Prime);
BIG_dec(p, 1);
BIG_norm(p);
BIG_shr(p, 1);
if(BIG_comp(y, p) == 1)
return 1;
else
return 0;
}
static int ecp_zcash_export(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
ecp *e = ecp_arg(L, 1);
if(e == NULL) {
THROW("Could not create ECP point");
return 0;
}
octet *o = o_new(L, 48);
if(o == NULL) {
failed_msg = "Could not allocate ECP point";
goto end;
}
if(ECP_isinf(&e->val)) {
o->len = 48;
o->val[0] = (char)0xc0;
memset(o->val+1, 0, 47);
} else {
BIG x, y;
const char c_bit = 1;
const char i_bit = 0;
ECP_get(x, y, &e->val);
const char s_bit = gf_sign(y);
char m_byte = (char)((c_bit << 7)+(i_bit << 6)+(s_bit << 5));
BIG_toBytes(o->val, x);
o->len = 48;
o->val[0] |= m_byte;
}
end:
ecp_free(L, e);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
// See the generalised version commented inside zen_octet.c
static int ecp_zcash_import(lua_State *L){
BEGIN();
char *failed_msg = NULL;
octet *o = o_arg(L, 1);
if(o == NULL) {
failed_msg = "Could not allocate octet";
goto end;
}
ecp *e = NULL;
unsigned char m_byte = o->val[0] & 0xE0;
char c_bit;
char i_bit;
char s_bit;
if(m_byte == 0x20 || m_byte == 0x60 || m_byte == 0xE0) {
failed_msg = "Invalid octet header";
goto end;
}
c_bit = ((m_byte & 0x80) == 0x80);
i_bit = ((m_byte & 0x40) == 0x40);
s_bit = ((m_byte & 0x20) == 0x20);
if(c_bit) {
if(o->len != 48) {
failed_msg = "Invalid octet header";
goto end;
}
} else {
if(o->len != 96) {
failed_msg = "Invalid octet header";
goto end;
}
}
e = ecp_new(L);
o->val[0] = o->val[0] & 0x1F;
if(i_bit) {
// TODO: check o->val is all 0
ECP_inf(&e->val);
goto end;
}
if(c_bit) {
BIG xpoint, ypoint;
big* bigx = big_new(L);
_octet_to_big(L, bigx, o);
if(!ECP_setx(&e->val, bigx->val, 0)) {
failed_msg = "Invalid input octet: not a point on the curve";
goto end;
}
ECP_get(xpoint, ypoint, &e->val);
if(gf_sign(ypoint) != s_bit) {
ECP_neg(&e->val);
}
lua_pop(L,1);
} else {
failed_msg = "Not yet implemented";
goto end;
}
end:
o_free(L, o);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
static int ecp_rhs(lua_State *L){
BEGIN();
char *failed_msg = NULL;
big *rhs = NULL;
big *x = big_arg(L, 1);
if(!x) {
failed_msg = "Could not read BIG";
goto end;
}
FP X, Y;
FP_nres(&X , x->val);
ECP_rhs(&Y, &X);
rhs = big_new(L);
if(!rhs) {
failed_msg = "Could not create BIG";
goto end;
}
big_init(L,rhs);
_fp_to_big(rhs, &Y);
end:
big_free(L,x);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
int luaopen_ecp(lua_State *L) {
(void)L;
const struct luaL_Reg ecp_class[] = {
{"new", lua_new_ecp},
{"inf", ecp_get_infinity},
{"infinity", ecp_get_infinity},
{"isinf", ecp_isinf},
{"order", ecp_order},
{"mapit", ecp_mapit},
{"generator", ecp_generator},
{"G", ecp_generator},
{"add", ecp_add},
{"sub", ecp_sub},
{"mul", ecp_mul},
{"validate", ecp_validate},
{"prime", ecp_prime},
{"rhs", ecp_rhs},
{"to_zcash", ecp_zcash_export},
{"from_zcash", ecp_zcash_import},
{NULL, NULL}};
const struct luaL_Reg ecp_methods[] = {
{"affine", ecp_affine},
{"negative", ecp_negative},
{"double", ecp_double},
{"isinf", ecp_isinf},
{"isinfinity", ecp_isinf},
{"octet", ecp_octet},
{"add", ecp_add},
{"x", ecp_get_x},
{"y", ecp_get_y},
{"__add", ecp_add},
{"sub", ecp_sub},
{"__sub", ecp_sub},
{"mul", ecp_mul},
{"__mul", ecp_mul},
{"eq", ecp_eq},
{"__eq", ecp_eq},
{"__gc", ecp_destroy},
{"__tostring", ecp_output},
{"to_zcash", ecp_zcash_export},
{NULL, NULL}
};
zen_add_class(L, "ecp", ecp_class, ecp_methods);
act(L, "ECP curve is %s", ECP_CURVE_NAME);
return 1;
}