-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbn128.c
1973 lines (1662 loc) · 57.6 KB
/
bn128.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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <gmp.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#ifndef CPUCYCLES_amd64cpuinfo_h
#define CPUCYCLES_amd64cpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_amd64cpuinfo(void);
extern long long cpucycles_amd64cpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "amd64cpuinfo"
#define cpucycles cpucycles_amd64cpuinfo
#define cpucycles_persecond cpucycles_amd64cpuinfo_persecond
#endif
#endif
#if(GMP_LIMB_BITS == 32)
#define N_LIMBS 8
#elif(GMP_LIMB_BITS == 64)
#define N_LIMBS 4
#else
#error "Only 32 and 64 bit architectures are supported"
#endif
// Parameters used in fp.c
#define BN_P "21888242871839275222246405745257275088696311157297823662689037894645226208583"
#define BN_PINV32 3834012553UL
#define BN_PINV64 9786893198990664585UL
// Parameters used in fp2.c
#define ALPHA (-1) // constant coefficient in the irreducible polynomial x^2 - alpha, used to construct F_{p^2}
// Parameters used in curve.c
#define BN_X "4965661367192848881" // parameter x used to generate the curve (see "Pairing-Friendly Elliptic Curves of Prime Order")
#define BN_N "21888242871839275222246405745257275088548364400416034343698204186575808495617" // prime order of E(F_p)
#define BN_TRACE "147946756881789318990833708069417712967" // trace of Frobenius of the curve
#define BN_CHI "552484233613224096312617126783173147097382103762957654188882734314196910839907541213974502761540629817009608548654680343627701153829446747810907373256841551006201639677726139946029199968412598804882391702273019083653272047566316584365559776493027495458238373902875937659943504873220554161550525926302303331747463515644711876653177129578303191095900909191624817826566688241804408081892785725967931714097716709526092261278071952560171111444072049229123565057483750161460024353346284167282452756217662335528813519139808291170539072125381230815729071544861602750936964829313608137325426383735122175229541155376346436093930287402089517426973178917569713384748081827255472576937471496195752727188261435633271238710131736096299798168852925540549342330775279877006784354801422249722573783561685179618816480037695005515426162362431072245638324744480" // exponent of final exponentiation
#define BN_CHICOMP "4965661367192848881" // exponent of final exponentiation for compressed pairing
#define BN_LOOPLENGTH_ETA "11916685325773193009570696613837024235910666865622157670373"
#define BN_B "3" // parameter b in the curve equation x^2 = y^3 + b
// Parameters used in fp6.c
#define BN_XI "1", "9"
#define BN_YPMINUS1 "10307601595873709700152284273816112264069230130616436755625194854815875713954", "21575463638280843010398324269430826099269044274347216827212613867836435027261"
#define BN_ZETA "2203960485148121921418603742825762020974279258880205651966"
// #define BN_XI2 "2", "0"
// #define BN_1O27XI3 "5674729633439812094656475563585219467439784374114250579215676491204317905929", "16213513238399463127589930181672055621256526783183573083473361403440908302654"
// #define BN_1O3XI3 "7296080957279758407415468581752425029565437052432607887563012631548408736195", "14592161914559516814830937163504850059130874104865215775126025263096817472388"
// #define BN_1O3XI "14592161914559516814830937163504850059130874104865215775126025263096817472389", "14592161914559516814830937163504850059130874104865215775126025263096817472389"
// #define BN_1O3MODP "14592161914559516814830937163504850059130874104865215775126025263096817472389"
// #define BN_COMETA_C0_CONST "82434016654300679717245125061265641166427769693017678351449950981238451124296"
// #define BN_COMETA_C1_CONST "82434016654300679719231239282227840001499775752201953636308636697028740308739"
// Parameters used in fp12.c
#define BN_TAU "0", "0", "0", "1", "0", "0" // constant tau used to construct F_p^12 as F_p^6[Z]/ (Z^2 - tau)
#define BN_ZPMINUS1 "16469823323077808223889137241176536799009286646108169935659301613961712198316", "8376118865763821496583973867626364092589906065868298776909617916018768340080" // Z^(p-1)
#define BN_ZPMINUS1INV "5722266937896532885780051958958348231143373700109372999374820235121374419868", "18566938241244942414004596690298913868373833782006617400804628704885040364344" // Z^(1-p)
// Parameters used in points.c
#define BN_CURVEGEN "1", "2", "1"
#define BN_TWISTGEN_X "11559732032986387107991004021392285783925812861821192530917403151452391805634", "10857046999023057135944570762232829481370756359578518086990519993285655852781"
#define BN_TWISTGEN_Y "4082367875863433681332203403145435568316851327593401208105741076214120093531", "8495653923123431417604973247489272438418190587263600148770280649306958101930"
// Parameters used for OptAte computation in ate_optate.c
#define BN_ZETA2 "21888242871839275220042445260109153167277707414472061641714758635765020556616" // zeta^2
#define BN_Z2P "10307601595873709700152284273816112264069230130616436755625194854815875713954", "21575463638280843010398324269430826099269044274347216827212613867836435027261" // Z^(2p)
#define BN_Z3P "3505843767911556378687030309984248845540243509899259641013678093033130930403", "2821565182194536844548159561693502659359617185244120367078079554186484126554" // Z^(3p)
mpz_t p;
unsigned long p_inv; // -p^{-1} mod 2^{GMP_LIMB_BITS} used in Montgomery reduction
typedef struct fpe_struct fpe_struct_t;
struct fpe_struct
{
mp_limb_t m_value[N_LIMBS];
};
void fp_init()
{
mpz_init_set_str(p, BN_P, 10);
// Set the value m', used in Montgomery reduction, see HAC, alg. 14.32
#if(GMP_LIMB_BITS == 32)
p_inv = BN_PINV32;
#elif(GMP_LIMB_BITS == 64)
p_inv = BN_PINV64;
#else
#error "Only 32 and 64 Bit architectures are supported"
#endif
}
typedef fpe_struct_t fpe_t[1];
// Montgomery reduction, see HAC, alg 14.32, size of op must be 2 * N_LIMBS:
static void fpe_montgomery_reduce(fpe_t rop, mp_limb_t *op)
{
mp_limb_t u, t, c, d; // dummies
int i;
c = 0;
for(i = 0; i < N_LIMBS; i++)
{
u = op[i] * p_inv;
d = mpn_addmul_1(op + i, p->_mp_d, N_LIMBS, u);
t = op[N_LIMBS + i] + d;
op[N_LIMBS + i] = t + c;
if(t < d || (c == 1 && t + c == 0))
c = 1;
else
c = 0;
}
if(c || mpn_cmp(op + N_LIMBS, p->_mp_d, N_LIMBS) >= 0)
// Result is larger than p, subtract p from the result:
mpn_sub_n(rop->m_value, op + N_LIMBS, p->_mp_d, N_LIMBS);
else
{
for(i = 0; i < N_LIMBS; i++)
rop->m_value[i] = op[N_LIMBS + i];
}
}
// Montgomery transformation:
static void fpe_montgomery_trans(fpe_t rop)
{
mp_limb_t tmp1[2 * N_LIMBS], tmp2[2 * N_LIMBS]; // needed for intermediate results
int i;
for(i = 0; i < N_LIMBS; i++)
tmp1[i] = 0;
for(i = 0; i < N_LIMBS; i++)
tmp1[N_LIMBS + i] = rop->m_value[i];
mpn_tdiv_qr(tmp2, rop->m_value, 0, tmp1, 2 * (N_LIMBS), p->_mp_d, N_LIMBS);
}
// Montgomery retransformation:
static void fpe_montgomery_retrans(fpe_t rop, const fpe_t op)
{
mp_limb_t tmp[2 * N_LIMBS]; // needed for intermediate results
int i;
for(i = 0; i < N_LIMBS; i++)
tmp[N_LIMBS + i] = 0;
for(i = 0; i < N_LIMBS; i++)
tmp[i] = op->m_value[i];
fpe_montgomery_reduce(rop, tmp);
}
void fpe_set(fpe_t rop, const fpe_t op)
{
int i;
for(i = 0; i < N_LIMBS; i++)
rop->m_value[i] = op->m_value[i];
}
// Set fpe_t rop to given value:
void fpe_set_ui(fpe_t rop, const unsigned int op)
{
int i;
for(i = 1; i < N_LIMBS; i++)
rop->m_value[i] = 0;
rop->m_value[0] = op;
fpe_montgomery_trans(rop);
}
// Set fpe_t rop to given value given as (ASCII) string
void fpe_set_str(fpe_t rop, const char* op)
{
// Initialize all limbs with 0:
int i;
for(i = 0; i < N_LIMBS; i++)
{
rop->m_value[i] = 0;
}
// Determine the length of op:
const char *scan = op;
int size = 0;
while(*scan != 0)
{
++scan;
++size;
}
unsigned char str[size];
memcpy(str, op, size);
// Convert from ASCII:
for(i = 0; i < size; ++i)
str[i] -= '0';
mpn_set_str(rop->m_value, str, size, 10);
fpe_montgomery_trans(rop);
}
// Set rop to one
void fpe_setone(fpe_t rop)
{
int i;
for(i = 1; i < N_LIMBS; i++)
rop->m_value[i] = 0;
rop->m_value[0] = 1;
fpe_montgomery_trans(rop);
}
// Set rop to zero
void fpe_setzero(fpe_t rop)
{
int i;
for(i = 0; i < N_LIMBS; i++)
rop->m_value[i] = 0;
}
// Return 1 if op is zero, 0 otherwise
int fpe_iszero(const fpe_t op)
{
int i;
for(i = 0; i < N_LIMBS; i++)
if(op->m_value[i]) return 0;
return 1;
}
// Return 1 if op is one, 0 otherwise
int fpe_isone(const fpe_t op)
{
int i;
for(i = 1; i < N_LIMBS; i++)
if(!op->m_value[i]) return 0;
// Retransform from Montgomery domain:
fpe_t dummy;
fpe_montgomery_retrans(dummy, op);
return dummy->m_value[0] == 1;
}
// Compute the negative of an fpe
void fpe_neg(fpe_t rop, const fpe_t op)
{
mpn_sub_n(rop->m_value, p->_mp_d, op->m_value, N_LIMBS);
}
// Double an fpe:
void fpe_double(fpe_t rop, const fpe_t op)
{
mp_limb_t c;
c = mpn_lshift(rop->m_value, op->m_value, N_LIMBS, 1);
// Reduce if result is larger than p:
if(c || mpn_cmp(rop->m_value, p->_mp_d, N_LIMBS) > 0)
mpn_sub_n(rop->m_value, rop->m_value, p->_mp_d, N_LIMBS);
}
// Halve an fpe:
void fpe_halve(fpe_t rop, const fpe_t op)
{
if((op->m_value[0] % 2) == 0)
mpn_rshift(rop->m_value, op->m_value, N_LIMBS, 1);
else
{
int c;
c = (mpn_add_n(rop->m_value, op->m_value, p->_mp_d, N_LIMBS)) << (GMP_LIMB_BITS - 1);
mpn_rshift(rop->m_value, rop->m_value, N_LIMBS, 1);
rop->m_value[N_LIMBS - 1] ^= c;
}
}
// Triple an fpe:
void fpe_triple(fpe_t rop, const fpe_t op)
{
mp_limb_t tmp[2 * N_LIMBS]; // needed for intermediate results
int i, c;
for(i = 2 * N_LIMBS - 1; i >= N_LIMBS; i--)
tmp[i] = 0;
// Double
c = mpn_lshift(tmp, op->m_value, N_LIMBS, 1);
if(c || mpn_cmp(tmp, p->_mp_d, N_LIMBS) >= 0)
mpn_sub_n(tmp, tmp, p->_mp_d, N_LIMBS);
// Add
c = mpn_add_n(rop->m_value, tmp, op->m_value, N_LIMBS);
if(c || mpn_cmp(rop->m_value, p->_mp_d, N_LIMBS) >= 0)
mpn_sub_n(rop->m_value, rop->m_value, p->_mp_d, N_LIMBS);
}
// Add two fpe, store result in rop:
void fpe_add(fpe_t rop, const fpe_t op1, const fpe_t op2)
{
mp_limb_t c;
c = mpn_add_n(rop->m_value, op1->m_value, op2->m_value, N_LIMBS);
// Reduce if result is larger than p:
if(c || mpn_cmp(rop->m_value, p->_mp_d, N_LIMBS) >= 0)
mpn_sub_n(rop->m_value, rop->m_value, p->_mp_d, N_LIMBS);
}
// Subtract op2 from op1, store result in rop:
void fpe_sub(fpe_t rop, const fpe_t op1, const fpe_t op2)
{
mp_limb_t b;
b = mpn_sub_n(rop->m_value, op1->m_value, op2->m_value, N_LIMBS);
if(b)
mpn_add_n(rop->m_value, rop->m_value, p->_mp_d, N_LIMBS);
}
// Multiply two fpe, store result in rop:
void fpe_mul(fpe_t rop, const fpe_t op1, const fpe_t op2)
{
#ifdef BENCH
nummultp++;
multpcycles -= cpucycles();
#endif
mp_limb_t tmp[2 * N_LIMBS]; // needed for intermediate results
if(fpe_iszero(op1) || fpe_iszero(op2))
fpe_setzero(rop);
else
{
mpn_mul_n(tmp, op1->m_value, op2->m_value, N_LIMBS);
fpe_montgomery_reduce(rop, tmp);
}
#ifdef BENCH
multpcycles += cpucycles();
#endif
}
// Compute inverse of an fpe, store result in rop:
void fpe_invert(fpe_t rop, const fpe_t op)
{
#ifdef BENCH
numinvp++;
invpcycles -= cpucycles();
#endif
/*
* FIXME: This code doesn't work they way it should
// Using mpn_gcdext:
mp_limb_t mp_limb_t d1[N_LIMBS + 1], mp_limb_t d2[N_LIMBS + 1], tmp1[2 * N_LIMBS], tmp2[2 * N_LIMBS]; // needed for intermediate results
mp_size_t n1, n2;
int i;
for(i = 0; i < N_LIMBS; i++)
{
d1[i] = p->_mp_d[i];
d2[i] = op->m_value[i];
}
n2 = mpn_gcdext(tmp1, tmp2, &n1, d2, N_LIMBS, d1, N_LIMBS);
for(i = 0; i < N_LIMBS; i++)
rop->m_value[i] = tmp2[i];
fpe_montgomery_trans(rop);
fpe_montgomery_trans(rop);
*/
// Partial Montgomery inversion, see Guide to ECC, alg. 2.23
mp_limb_t u[N_LIMBS], v[N_LIMBS], s[N_LIMBS];
unsigned long i, k, c;
c = k = 0;
for(i = 0; i < N_LIMBS; i++)
{
u[i] = p->_mp_d[i];
v[i] = op->m_value[i];
rop->m_value[i] = 0;
s[i] = 0;
}
s[0] = 1;
int v_zero = 1;
for(i = 0; i < N_LIMBS && v_zero; i++)
if(v[i]) v_zero = 0;
// Don't try to invert 0:
assert(!v_zero);
while(!v_zero)
{
if(u[0] % 2 == 0)
{
mpn_rshift(u, u, N_LIMBS, 1);
mpn_lshift(s, s, N_LIMBS, 1);
}
else
{
if(v[0] % 2 == 0)
{
mpn_rshift(v, v, N_LIMBS, 1);
mpn_lshift(rop->m_value, rop->m_value, N_LIMBS, 1);
}
else
{
if(mpn_cmp(u, v, N_LIMBS) > 0)
{
mpn_sub_n(u, u, v, N_LIMBS);
mpn_rshift(u, u, N_LIMBS, 1);
c = mpn_add_n(rop->m_value, rop->m_value, s, N_LIMBS);
mpn_lshift(s, s, N_LIMBS, 1);
}
else
{
mpn_sub_n(v, v, u, N_LIMBS);
mpn_rshift(v, v, N_LIMBS, 1);
mpn_add_n(s, s, rop->m_value, N_LIMBS);
c = mpn_lshift(rop->m_value, rop->m_value, N_LIMBS, 1);
}
}
}
++k;
// Check, whether v is zero:
v_zero = 1;
for(i = 0; i < N_LIMBS; i++)
{
if(v[i]) v_zero = 0;
}
}
if(c || mpn_cmp(rop->m_value, p->_mp_d, N_LIMBS) >= 0)
mpn_sub_n(rop->m_value, rop->m_value, p->_mp_d, N_LIMBS);
mpn_sub_n(rop->m_value, p->_mp_d, rop->m_value, N_LIMBS);
// Make the Montgomery Inversion complete:
for(; k < N_LIMBS * sizeof(mp_limb_t) * 16; k++)
{
c = mpn_lshift(rop->m_value, rop->m_value, N_LIMBS, 1);
if(c || mpn_cmp(rop->m_value, p->_mp_d, N_LIMBS) >= 0)
mpn_sub_n(rop->m_value, rop->m_value, p->_mp_d, N_LIMBS);
}
#ifdef BENCH
invpcycles += cpucycles();
#endif
}
// Print the element to stdout:
void fpe_print(FILE *outfile, const fpe_t op)
{
int i;
// Retransform from Montgomery domain:
fpe_t dummy;
fpe_montgomery_retrans(dummy, op);
i = 0;
while(i < N_LIMBS && !dummy->m_value[N_LIMBS - 1 - i])
i++;
// Print '0' if op is zero:
if(i == N_LIMBS && !dummy->m_value[0])
{
fputc('0', outfile);
fputc(0, outfile);
}
else
{
unsigned char *str;
double log2 = M_LOG10E * M_LN2; // log_{10}2
size_t str_size = log2 * (GMP_LIMB_BITS * (N_LIMBS)) + 2;
str = malloc(str_size);
str_size = mpn_get_str(str, 10, dummy->m_value, N_LIMBS - i);
// Strip leading zeros:
while(*str == 0 && str_size > 1)
{
str++;
str_size--;
}
for(i = 0; i < str_size; i++)
{
fputc(*(str + i) + '0', outfile);
}
fputc(0, outfile);
free(str);
}
}
/// Structure describing a point on a BN-curve
typedef struct curvepoint_fp_struct curvepoint_fp_struct_t;
struct curvepoint_fp_struct
{
fpe_t m_x; // X-Coordinate (Jacobian Coordinate system)
fpe_t m_y; // Y-Coordinate (Jacobian Coordinate system)
fpe_t m_z; // Y-Coordinate (Jacobian Coordinate system)
fpe_t m_t; // T = Z^2, only used during pairing computation, set to zero if not set
};
typedef curvepoint_fp_struct_t curvepoint_fp_t[1];
#define fpe_square(rop, op) fpe_mul(rop, op, op)
mpz_t b; /* parameter b in the curve equation y^2 = x^3 + b */
mpz_t n; /* order of the curve */
mpz_t x;
mpz_t trace; /* trace of Frobenius */
mpz_t chi; /* p^12 / n */
mpz_t chicomp;
mpz_t looplength_eta;
mpz_t ate_loop_count;
void curve_init()
{
/* Curve parameters */
mpz_init_set_str(x, BN_X, 10);
mpz_init_set_str(n, BN_N, 10);
mpz_init_set_str(trace, BN_TRACE,10);
mpz_init_set_str(chi, BN_CHI, 10); // (p^k - 1) / n
mpz_init_set_str(chicomp, BN_CHICOMP, 10);
mpz_init_set_str(looplength_eta, BN_LOOPLENGTH_ETA, 10);
mpz_init_set_str(b, BN_B, 10);
}
// Global dummies usable by all curvepoints:
fpe_t curvepoint_dummy_fpe1;
void curvepoint_fp_init_set_str(curvepoint_fp_t rop, const char* x, const char* y, const char* z)
{
fpe_set_str(rop->m_x, x);
fpe_set_str(rop->m_y, y);
fpe_set_str(rop->m_z, z);
fpe_set_ui(rop->m_t, 0);
}
// Set the coordinates of a curvepoint_fp_t by copying the coordinates from another curvepoint_fp
void curvepoint_fp_set(curvepoint_fp_t rop, const curvepoint_fp_t op)
{
fpe_set(rop->m_x, op->m_x);
fpe_set(rop->m_y, op->m_y);
fpe_set(rop->m_z, op->m_z);
fpe_set_ui(rop->m_t, 0);
}
// Set the coordinates of a curvepoint_fp:
void curvepoint_fp_set_str(curvepoint_fp_t rop, const char* x, const char* y, const char* z)
{
fpe_set_str(rop->m_x, x);
fpe_set_str(rop->m_y, y);
fpe_set_str(rop->m_z, z);
fpe_set_ui(rop->m_t, 0);
}
// Addition of two points, op2 is assumed to be in affine coordinates
// For the algorithm see e.g. DA Peter Schwabe
void curvepoint_fp_mixadd(curvepoint_fp_t rop, const curvepoint_fp_t op1, const curvepoint_fp_t op2)
{
fpe_t tfpe1, tfpe2, tfpe3, tfpe4, tfpe5, tfpe6, tfpe7, tfpe8, tfpe9; // Temporary variables needed for intermediary results
fpe_square(tfpe1, op1->m_z);
fpe_mul(tfpe2, op1->m_z, tfpe1);
fpe_mul(tfpe3, op2->m_x, tfpe1);
fpe_mul(tfpe4, op2->m_y, tfpe2);
fpe_sub(tfpe5, tfpe3, op1->m_x);
fpe_sub(tfpe6, tfpe4, op1->m_y);
fpe_square(tfpe7, tfpe5);
fpe_mul(tfpe8, tfpe7, tfpe5);
fpe_mul(tfpe9, op1->m_x, tfpe7);
fpe_double(tfpe1, tfpe9);
fpe_add(tfpe1, tfpe1, tfpe8);
fpe_square(rop->m_x, tfpe6);
fpe_sub(rop->m_x, rop->m_x, tfpe1);
fpe_sub(tfpe1, tfpe9, rop->m_x);
fpe_mul(tfpe2, tfpe1, tfpe6);
fpe_mul(tfpe3, op1->m_y, tfpe8);
fpe_sub(rop->m_y, tfpe2, tfpe3);
fpe_mul(rop->m_z, op1->m_z, tfpe5);
}
void curvepoint_fp_double(curvepoint_fp_t rop, const curvepoint_fp_t op)
{
fpe_t tfpe1, tfpe2, tfpe3, tfpe4; // Temporary variables needed for intermediary results
fpe_square(tfpe1, op->m_y);
fpe_mul(tfpe2, tfpe1, op->m_x);
fpe_double(tfpe2, tfpe2);
fpe_double(tfpe2, tfpe2);
fpe_square(tfpe3, tfpe1);
fpe_double(tfpe3, tfpe3);
fpe_double(tfpe3, tfpe3);
fpe_double(tfpe3, tfpe3);
fpe_square(tfpe4, op->m_x);
fpe_triple(tfpe4, tfpe4);
fpe_square(rop->m_x, tfpe4);
fpe_double(tfpe1, tfpe2);
fpe_sub(rop->m_x, rop->m_x, tfpe1);
fpe_sub(tfpe1, tfpe2, rop->m_x);
fpe_mul(rop->m_z, op->m_y, op->m_z);
fpe_double(rop->m_z, rop->m_z);
fpe_mul(rop->m_y, tfpe4, tfpe1);
fpe_sub(rop->m_y, rop->m_y, tfpe3);
}
void curvepoint_fp_mul(curvepoint_fp_t rop, const curvepoint_fp_t op, const mpz_t scalar)
{
size_t i;
curvepoint_fp_t r;
curvepoint_fp_set(r, op);
for(i = mpz_sizeinbase(scalar, 2) - 1; i > 0; i--)
{
curvepoint_fp_double(r, r);
if(mpz_tstbit(scalar, i - 1))
curvepoint_fp_mixadd(r, r, op);
}
curvepoint_fp_set(rop, r);
}
// Negate a point, store in rop:
void curvepoint_fp_neg(curvepoint_fp_t rop, const curvepoint_fp_t op)
{
fpe_neg(curvepoint_dummy_fpe1, op->m_y);
fpe_set(rop->m_x, op->m_x);
fpe_set(rop->m_y, curvepoint_dummy_fpe1);
fpe_set(rop->m_z, op->m_z);
}
// Transform to Affine Coordinates (z=1)
void curvepoint_fp_makeaffine(curvepoint_fp_t point)
{
if(fpe_iszero(point->m_z))
{
fpe_setzero(point->m_x);
fpe_setone(point->m_y);
fpe_setzero(point->m_z);
}
else
{
fpe_invert(curvepoint_dummy_fpe1, point->m_z);
fpe_mul(point->m_x, point->m_x, curvepoint_dummy_fpe1);
fpe_mul(point->m_x, point->m_x, curvepoint_dummy_fpe1);
fpe_mul(point->m_y, point->m_y, curvepoint_dummy_fpe1);
fpe_mul(point->m_y, point->m_y, curvepoint_dummy_fpe1);
fpe_mul(point->m_y, point->m_y, curvepoint_dummy_fpe1);
fpe_setone(point->m_z);
}
}
// Print a point:
void curvepoint_fp_print(FILE *outfile, const curvepoint_fp_t point)
{
fprintf(outfile, "[");
fpe_print(outfile, point->m_x);
fprintf(outfile, ", ");
fpe_print(outfile, point->m_y);
fprintf(outfile, ", ");
fpe_print(outfile, point->m_z);
fprintf(outfile, "]");
}
// Elements from F_{p^2}= F_p[X] / (x^2 - alpha)F_p[X] are represented as aX + b
typedef struct fp2e_struct fp2e_struct_t;
struct fp2e_struct
{
fpe_t m_a;
fpe_t m_b;
};
typedef fp2e_struct_t fp2e_t[1];
// Set fp2e_t rop to given value:
void fp2e_set(fp2e_t rop, const fp2e_t op)
{
fpe_set(rop->m_a, op->m_a);
fpe_set(rop->m_b, op->m_b);
}
// Set fp2e_t rop to given value:
void fp2e_set_fpe(fp2e_t rop, const fpe_t op)
{
fpe_set_ui(rop->m_a, 0);
fpe_set(rop->m_b, op);
}
// Set rop to one
void fp2e_setone(fp2e_t rop)
{
fpe_setzero(rop->m_a);
fpe_setone(rop->m_b);
}
// Set rop to zero
void fp2e_setzero(fp2e_t rop)
{
fpe_setzero(rop->m_a);
fpe_setzero(rop->m_b);;
}
// Set an fp2e_t to value given in two strings
void fp2e_set_str(fp2e_t rop, const char* a_str, const char* b_str)
{
fpe_set_str(rop->m_a, a_str);
fpe_set_str(rop->m_b, b_str);
}
// Double an fp2e:
void fp2e_double(fp2e_t rop, const fp2e_t op)
{
fpe_double(rop->m_a, op->m_a);
fpe_double(rop->m_b, op->m_b);
}
// Triple an fp2e:
void fp2e_triple(fp2e_t rop, const fp2e_t op)
{
fpe_triple(rop->m_a, op->m_a);
fpe_triple(rop->m_b, op->m_b);
}
// Add two fp2e, store result in rop:
void fp2e_add(fp2e_t rop, const fp2e_t op1, const fp2e_t op2)
{
fpe_add(rop->m_a, op1->m_a, op2->m_a);
fpe_add(rop->m_b, op1->m_b, op2->m_b);
}
// Subtract op2 from op1, store result in rop:
void fp2e_sub(fp2e_t rop, const fp2e_t op1, const fp2e_t op2)
{
fpe_sub(rop->m_a, op1->m_a, op2->m_a);
fpe_sub(rop->m_b, op1->m_b, op2->m_b);
}
// Negate op
void fp2e_neg(fp2e_t rop, const fp2e_t op)
{
fpe_neg(rop->m_a, op->m_a);
fpe_neg(rop->m_b, op->m_b);
}
// Multiply two fp2e, store result in rop:
void fp2e_mul(fp2e_t rop, const fp2e_t op1, const fp2e_t op2)
{
#ifdef BENCH
nummultp2++;
multp2cycles -= cpucycles();
#endif
fpe_t tmp1, tmp2, tmp3; // Needed for intermediary results
if((fpe_iszero(op1->m_a) && fpe_iszero(op1->m_b)) || (fpe_iszero(op2->m_a) && fpe_iszero(op2->m_b)))
fp2e_setzero(rop);
else
{
fpe_mul(tmp1, op1->m_a, op2->m_a);
fpe_mul(tmp2, op1->m_b, op2->m_b);
fpe_add(tmp3, op2->m_a, op2->m_b);
fpe_add(rop->m_a, op1->m_a, op1->m_b);
fpe_set(rop->m_b, tmp2);
fpe_mul(rop->m_a, rop->m_a, tmp3);
fpe_sub(rop->m_a, rop->m_a, tmp1);
fpe_sub(rop->m_a, rop->m_a, rop->m_b);
#if(ALPHA == 2)
fpe_double(tmp1, tmp1);
fpe_add(rop->m_b, rop->m_b, tmp1);
#elif(ALPHA == -2)
fpe_double(tmp1, tmp1);
fpe_sub(rop->m_b, rop->m_b, tmp1);
#elif(ALPHA == -1)
fpe_sub(rop->m_b, rop->m_b, tmp1);
#else
#error "ALPHA must be -1, 2 or -2"
#endif
}
#ifdef BENCH
multp2cycles += cpucycles();
#endif
}
// Square an fp2e, store result in rop:
void fp2e_square(fp2e_t rop, const fp2e_t op)
{
#ifdef BENCH
numsqp2++;
sqp2cycles -= cpucycles();
#endif
fpe_t tmp1, tmp2, tmp3; // Needed for intermediary results
fpe_mul(tmp1, op->m_a, op->m_b);
fpe_add(tmp2, op->m_a, op->m_b);
#if(ALPHA == 2)
fpe_double(tmp3, op->m_a);
fpe_add(rop->m_b, op->m_b, tmp3);
#elif(ALPHA == -2)
fpe_double(tmp3, op->m_a);
fpe_sub(rop->m_b, op->m_b, tmp3);
#elif(ALPHA == -1)
fpe_sub(rop->m_b, op->m_b, op->m_a);
#else
#error "ALPHA must be -1, 2 or -2"
#endif
fpe_mul(rop->m_b, rop->m_b, tmp2);
fpe_sub(rop->m_b, rop->m_b, tmp1);
#if(ALPHA == 2)
fpe_double(tmp2, tmp1);
fpe_sub(rop->m_b, rop->m_b, tmp2);
#elif(ALPHA == -2)
fpe_double(tmp2, tmp1);
fpe_add(rop->m_b, rop->m_b, tmp2);
#elif(ALPHA == -1)
fpe_add(rop->m_b, rop->m_b, tmp1);
#else
#error "ALPHA must be -1, 2 or -2"
#endif
fpe_double(rop->m_a, tmp1);
#ifdef BENCH
sqp2cycles += cpucycles();
#endif
}
// Multiply by xi which is used to construct F_p^6
void fp2e_mulxi(fp2e_t rop, const fp2e_t op)
{
//TODO Check for XI and ALPHA
fpe_t tmp1, tmp2;
fpe_sub(tmp1, op->m_b, op->m_a);
fpe_sub(tmp2, tmp1, op->m_a);
fpe_add(rop->m_a, op->m_b, op->m_a);
fpe_set(rop->m_b, tmp2);
// MODIFIED
// fp2e_mul(rop, op, xi);
}
// Multiply an fpe by xi which is used to construct F_p^6
void fp2e_mulxi_fpe(fp2e_t rop, const fpe_t op)
{
//TODO Check for XI
fpe_set(rop->m_a, op);
fpe_set(rop->m_b, op);
}
// Scalar multiple of an fp2e, store result in rop:
void fp2e_mul_fpe(fp2e_t rop, const fp2e_t op1, const fpe_t op2)
{
fpe_mul(rop->m_a, op1->m_a, op2);
fpe_mul(rop->m_b, op1->m_b, op2);
}
// Inverse multiple of an fp2e, store result in rop:
void fp2e_invert(fp2e_t rop, const fp2e_t op)
{
#ifdef BENCH
numinvp2++;
invp2cycles -= cpucycles();
#endif
fpe_t tmp1, tmp2; // Needed for intermediary results
fpe_mul(tmp1, op->m_a, op->m_a);
fpe_mul(tmp2, op->m_b, op->m_b);
#if(ALPHA == 2)
fpe_double(tmp1, tmp1);
fpe_sub(tmp2, tmp2, tmp1);
#elif(ALPHA == -2)
fpe_double(tmp1, tmp1);
fpe_add(tmp2, tmp2, tmp1);
#elif(ALPHA == -1)
fpe_add(tmp2, tmp2, tmp1);
#else
#error "ALPHA must be -1, 2 or -2"
#endif
fpe_invert(tmp2, tmp2);
fpe_mul(rop->m_b, op->m_b, tmp2);
fpe_neg(tmp2, tmp2);
fpe_mul(rop->m_a, op->m_a, tmp2);
#ifdef BENCH
invp2cycles += cpucycles();
#endif
}
// Print the fp2e:
void fp2e_print(FILE *outfile, const fp2e_t op)
{
fprintf(outfile, "(");
fpe_print(outfile, op->m_a);
fprintf(outfile, " * a + \n");
fpe_print(outfile, op->m_b);
fprintf(outfile, ")");
}
typedef struct twistpoint_fp2_struct twistpoint_fp2_struct_t;
struct twistpoint_fp2_struct
{
fp2e_t m_x; // X-Coordinate (Jacobian Coordinate system)
fp2e_t m_y; // Y-Coordinate (Jacobian Coordinate system)
fp2e_t m_z; // Z-Coordinate (Jacobian Coordinate system)
fp2e_t m_t; // T = Z^2, only used during pairing computation, set to zero if not set
};
typedef twistpoint_fp2_struct_t twistpoint_fp2_t[1];
curvepoint_fp_t curve_gen; // generator of E(\F_p)
twistpoint_fp2_t twist_gen; // generator of the subgroup of order n of E'(\F_{p^2})
void twistpoint_fp2_set(twistpoint_fp2_t rop, const twistpoint_fp2_t op)
{
fp2e_set(rop->m_x, op->m_x);
fp2e_set(rop->m_y, op->m_y);
fp2e_set(rop->m_z, op->m_z);
fp2e_setzero(rop->m_t);
}
void twistpoint_fp2_set_fp2e(twistpoint_fp2_t rop, const fp2e_t x, const fp2e_t y, const fp2e_t z)
{
fp2e_set(rop->m_x, x);
fp2e_set(rop->m_y, y);
fp2e_set(rop->m_z, z);
fp2e_setzero(rop->m_t);
}
void twistpoint_fp2_init_set_str(twistpoint_fp2_t rop, const char* xx, const char* xy, const char* yx, const char* yy){
fp2e_t x,y,z;
fp2e_set_str(x,xx,xy);
fp2e_set_str(y,yx,yy);
fp2e_setone(z);
fp2e_set(rop->m_x, x);
fp2e_set(rop->m_y, y);
fp2e_set(rop->m_z, z);
fp2e_setzero(rop->m_t);
}
void twistpoint_fp2_affineset_fp2e(twistpoint_fp2_t rop, const fp2e_t x, const fp2e_t y)
{
fp2e_set(rop->m_x, x);
fp2e_set(rop->m_y, y);
fp2e_setone(rop->m_z);
fp2e_setzero(rop->m_t);
}
void twistpoint_fp2_mixadd(twistpoint_fp2_t rop, const twistpoint_fp2_t op1, const twistpoint_fp2_t op2)
{
fp2e_t tfp2e1, tfp2e2, tfp2e3, tfp2e4, tfp2e5, tfp2e6, tfp2e7, tfp2e8, tfp2e9; // Temporary variables needed for intermediary results
fp2e_square(tfp2e1, op1->m_z);
fp2e_mul(tfp2e2, op1->m_z, tfp2e1);
fp2e_mul(tfp2e3, op2->m_x, tfp2e1);
fp2e_mul(tfp2e4, op2->m_y, tfp2e2);
fp2e_sub(tfp2e5, tfp2e3, op1->m_x);
fp2e_sub(tfp2e6, tfp2e4, op1->m_y);
fp2e_square(tfp2e7, tfp2e5);
fp2e_mul(tfp2e8, tfp2e7, tfp2e5);
fp2e_mul(tfp2e9, op1->m_x, tfp2e7);
fp2e_double(tfp2e1, tfp2e9);
fp2e_add(tfp2e1, tfp2e1, tfp2e8);
fp2e_square(rop->m_x, tfp2e6);
fp2e_sub(rop->m_x, rop->m_x, tfp2e1);
fp2e_sub(tfp2e1, tfp2e9, rop->m_x);
fp2e_mul(tfp2e2, tfp2e1, tfp2e6);
fp2e_mul(tfp2e3, op1->m_y, tfp2e8);
fp2e_sub(rop->m_y, tfp2e2, tfp2e3);
fp2e_mul(rop->m_z, op1->m_z, tfp2e5);
}
void twistpoint_fp2_double(twistpoint_fp2_t rop, const twistpoint_fp2_t op)
{
fp2e_t tfp2e1, tfp2e2, tfp2e3, tfp2e4; // Temporary variables needed for intermediary results
fp2e_square(tfp2e1, op->m_y);
fp2e_mul(tfp2e2, tfp2e1, op->m_x);
fp2e_double(tfp2e2, tfp2e2);
fp2e_double(tfp2e2, tfp2e2);
fp2e_square(tfp2e3, tfp2e1);
fp2e_double(tfp2e3, tfp2e3);
fp2e_double(tfp2e3, tfp2e3);
fp2e_double(tfp2e3, tfp2e3);
fp2e_square(tfp2e4, op->m_x);
fp2e_triple(tfp2e4, tfp2e4);