forked from defeo/ss-isogeny-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pqcrypto11.sage
795 lines (693 loc) · 65.2 KB
/
pqcrypto11.sage
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
# Copyright (c) 2011-2016 Luca De Feo.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 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 General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sage.misc.sage_timeit import sage_timeit
from sage.misc import misc
import paths
from pygost import gost34112012512
from pygost.gost3412 import GOST3412Kuznechik
from pygost.gost3413 import mac
from pygost.utils import hexdec
from pygost.utils import hexenc
load('pqcrypto11.spyx')
################################################################################
# EDIT PARAMETERS HERE
################################################################################
# These are some precomputed valid key sizes. The theoreical classical
# security is 1/4 of the bit size (third component of the name). The
# theoretical quantum security is 1/6 of the bit size.
parameters = {
'gost256' : {'lA':2, 'lB': 3, 'eA': 372, 'eB': 239, 'f': 7, 'pm1' : -1},
'gost128_1' : {'lA': 2, 'lB': 3, 'eA': 208, 'eB': 129, 'f': 5, 'pm1' : -1},
'gost128_2' : {'lA': 2, 'lB': 3, 'eA': 253, 'eB': 161, 'f': 7, 'pm1' : -1},
'grebnev' : {'lA':2, 'lB': 3, 'eA': 451, 'eB': 284, 'f': 1, 'pm1' : -1},
'sike434' : {'lA':2, 'lB': 3, 'eA': 216, 'eB': 137, 'f': 1, 'pm1' : -1},
'sike503' : {'lA':2, 'lB': 3, 'eA': 250, 'eB': 159, 'f': 1, 'pm1' : -1},
'sike610' : {'lA':2, 'lB': 3, 'eA': 305, 'eB': 192, 'f': 1, 'pm1' : -1},
'sike751' : {'lA':2, 'lB': 3, 'eA': 372, 'eB': 239, 'f': 1, 'pm1' : -1},
'2-3-8' : {'lA' : 2, 'lB' : 3, 'eA' : 6, 'eB' : 1, 'f' : 1, 'pm1' : -1},
'2-3-40' : {'lA' : 2, 'lB' : 3, 'eA' : 22, 'eB' : 15, 'f' : 1, 'pm1' : -1},
'2-3-256' : {'lA' : 2, 'lB' : 3, 'eA' : 130, 'eB' : 81, 'f' : 22, 'pm1' : -1},
'2-3-512' : {'lA' : 2, 'lB' : 3, 'eA' : 258, 'eB' : 161, 'f' : 186, 'pm1' : -1},
'2-3-678' : {'lA' : 2, 'lB' : 3, 'eA' : 341, 'eB' : 218, 'f' : 3, 'pm1' : -1},
'2-3-768' : {'lA' : 2, 'lB' : 3, 'eA' : 386, 'eB' : 242, 'f' : 2, 'pm1' : -1},
'2-3-1024' : {'lA' : 2, 'lB' : 3, 'eA' : 514, 'eB' : 323, 'f' : 353, 'pm1' : -1},
'3-5-512' : {'lA' : 3, 'lB' : 5, 'eA' : 161, 'eB' : 110, 'f' : 314, 'pm1' : 1},
'3-5-512:-1' : {'lA' : 3, 'lB' : 5, 'eA' : 161, 'eB' : 110, 'f' : 736, 'pm1' : -1},
'5-7-32' : {'lA' : 5, 'lB' : 7, 'eA' : 9, 'eB' : 7, 'f' : 16, 'pm1' : -1},
'5-7-32:-1' : {'lA' : 5, 'lB' : 7, 'eA' : 9, 'eB' : 7, 'f' : 18, 'pm1' : 1},
'5-7-128' : {'lA' : 5, 'lB' : 7, 'eA' : 55, 'eB' : 46, 'f' : 372, 'pm1' : -1},
'5-7-512' : {'lA' : 5, 'lB' : 7, 'eA' : 110, 'eB' : 91, 'f' : 284, 'pm1' : -1},
'5-7-768' : {'lA' : 5, 'lB' : 7, 'eA' : 165, 'eB' : 137, 'f' : 2968, 'pm1' : -1},
'5-7-1024' : {'lA' : 5, 'lB' : 7, 'eA' : 220, 'eB' : 182, 'f' : 538, 'pm1' : 1},
'11-13-512:+1' : {'lA' : 11, 'lB' : 13, 'eA' : 74, 'eB' : 69, 'f' : 1254, 'pm1' : 1},
'11-13-512' : {'lA' : 11, 'lB' : 13, 'eA' : 74, 'eB' : 69, 'f' : 384, 'pm1' : -1},
'11-13-768' : {'lA' : 11, 'lB' : 13, 'eA' : 111, 'eB' : 104, 'f' : 78, 'pm1' : +1},
'11-13-1024' : {'lA' : 11, 'lB' : 13, 'eA' : 148, 'eB' : 138, 'f' : 942, 'pm1' : +1},
'17-19-512' : {'lA' : 17, 'lB' : 19, 'eA' : 62, 'eB' : 60, 'f' : 120, 'pm1' : -1},
'17-19-512:+1' : {'lA' : 17, 'lB' : 19, 'eA' : 62, 'eB' : 60, 'f' : 210, 'pm1' : 1},
'17-19-768' : {'lA' : 17, 'lB' : 19, 'eA' : 94, 'eB' : 90, 'f' : 116, 'pm1' : -1},
'17-19-1024' : {'lA' : 17, 'lB' : 19, 'eA' : 125, 'eB' : 120, 'f' : 712, 'pm1' : -1},
'23-29-512:-1' : {'lA' : 23, 'lB' : 29, 'eA' : 56, 'eB' : 52, 'f' : 452, 'pm1' : -1},
'23-29-512' : {'lA' : 23, 'lB' : 29, 'eA' : 56, 'eB' : 52, 'f' : 286, 'pm1' : 1},
'23-29-768' : {'lA' : 23, 'lB' : 29, 'eA' : 85, 'eB' : 79, 'f' : 132, 'pm1' : -1},
'23-29-1024' : {'lA' : 23, 'lB' : 29, 'eA' : 113, 'eB' : 105, 'f' : 1004, 'pm1' : -1},
'31-41-512:+1' : {'lA' : 31, 'lB' : 41, 'eA' : 51, 'eB' : 47, 'f' : 1259, 'pm1' : 1},
'31-41-512' : {'lA' : 31, 'lB' : 41, 'eA' : 51, 'eB' : 47, 'f' : 564, 'pm1' : -1},
'31-41-768' : {'lA' : 31, 'lB' : 41, 'eA' : 77, 'eB' : 72, 'f' : 166, 'pm1' : +1},
'31-41-1024' : {'lA' : 31, 'lB' : 41, 'eA' : 103, 'eB' : 95, 'f' : 448, 'pm1' : -1},
}
# This dictionary is used to tune up the key exchange. To any prime
# ell is associated a pair giving the relative cost of one
# multiplication by ell and one evaluation of ell-isogeny
# respectively.
#
# We have some computed some experimental parameters for ell=2,3 using
# an Intel Core Duo U9400. We have left any other prime unoptimized.
#
# To optimize the key exchange for your computer, compile the file
# gfp2.c as follows
#
# > gcc -lgmp gfp2.c
#
# then run it and read the output.
weights = {2 : (2, 1), 3 : (2, 1), 5 : (1, 1), 7 : (1,1),
11 : (1, 1), 13 : (1, 1), 17 : (1, 1), 19 : (1, 1),
23 : (1, 1), 29 : (1, 1), 31 : (1, 1), 41 : (1, 1)}
control_value = {
#E_start y^2 = x^3 + (17493471122392659065800712772515557709670529132138038021277127480141235217844132013314393031224679806017205814419946118575015)*x^2 + x
#basis PA (7974449090525721801974633571774851594347647923032376651722153575558087644415543780532721143387922058519400515909870284072127*z + 38168307339521668599575642493333799629555127150707484184662988400952456947272877796696092116638708633223831527127031844443699 : 43842276912252336699236703619808165150778903564790358056222110564051430673421996260263000620476403511352447970722417541599006*z + 14161126763225416720585317354922447064503242894358192264272829681232974108235336883057296951912618840020835279063348557719232 : 48661681480986299289369530595866686599066199180249786106040186200794845770441225709096994085386040591147937337684139795514051*z + 51385990919708568648841537124475824730888110449943518879326722484482537510403150969773944400050140289636104233766445550108261)
#basis QA (41600623919745288349072134946304144578397127673074103488926879088997906431425535671372257830055094570685108257644471253704203*z + 2958371879322690731391363212899533387181915907703959541128008564216946043997127158058801627590625276210980850793377415446674 : 65983561267077378729989176406840820981290451053708248321502848493612701428655609870333969562092272028683143524393975577820887*z + 34624940742204533817742038510712997761608494190889365380433345191390763099794238798355338377094224294951235464301398479198035 : 33488352783056569297619323255727550295657326511257134454586355943643405781163401642997231008271151077768764107946363298003366*z + 39739471127875327313497723832087000493744507796773412493969756423229582355126143498172913090281991334145488684775120578790911)
#basis PB (71134133268090346668485704469379845529831078651123069862483993116274296200190944837010689589238850282050775343036954080909965*z + 4949013114206686260374003736588907477814029375446794115788756271130338431166270034424376452044775873016769239177768063827437 : 6955391531255658501026293577244750142578909592156539197180289177973140659632958019669598353814303860483077782643763954189235*z + 44587598669862461857594203354105598200104620612179803279870288480693202473140029081137822849793206609284332283441032169223116 : 43590226996047240516238648478840505409126284631629919981394654160051717903818417550738488934094296062325331405817905108336004*z + 7576410836004861465781407966536603356236263936555645084302203034481057996992397652113655221790869292267666893698998051465044)
#basis QB (42492554066871894709175714312989519762931258767708630885152277469702329079419081979887271082046721396402366502838077999738421*z + 64778583321933194083568078398791587384117911494623485631214445261807172077001278081967533871778811723354500973129188275920685 : 58354723570075462138669704063079802022023384644956902892346426639996922186185462808978828962190043881452534941471330526421338*z + 4515251453625604659269822982919769881476549731801986214098854630168940028702330761375697150463823832071446911759544939196756 : 23283854663744724403286884676936588287925512499868244974460685580565178180278928713749904621295475809427073991429140938717598*z + 59299202885517316120432533283640383425335690554469589394461487033065315250836059344041477610752150740452843076186811829917913)
#nA 375121851558675496452585532655202523022630679424182711019733544
#nB 32861487073955786794117591344418865324440155634955316073175816
#E_A (30234691665951344880917862759328189664816918970799213360061917879785908209367257376974885114474636078611229797887091946131218*z + 61463144801932172361256612963224033252113089338104329265976181838135470499103054761358315809719454090483049605960721636560376) y^2 = x^3 + (28938869683537349547257016671886647825655765739574163527495985370892897590386386632147183528546531306925546137045905389773173*z + 26652819384173045006231178630182468194963069698833644299224414911972663416827384380100359397026732866558185079719355054994246)*x^2 + x
#phiPB (1780853909047789122817209951395519816131972077810842284949542311068278012409661409560551278355476148681846920834700834126084*z + 66703240620485392311761152499229330695201867837750543204045070109234874840238507345664955451406671895469076489613528473353475 : 4882726185863873930783451305690168778135993535607477994985595717337574138004343830196584653222749768712919353317879917865480*z + 57686423636244300433257585539441470841411297480222055232356848016100464834533343271686528126707564177341319718211284489817487 : 61180394740883345101686725643924936953602003348788265815289170072322596965671478660157118029193124351968506472997662694268223*z + 54616015722860201353757737242727444254217471551742845062568582050964276292912533718039833606408930152449184637706325471677454)
#phiQB (16108313662703392199592218669280553529172776629832653690480155693005809547527955473340777165119572895298685357496559174128540*z + 8171246843271576812062532485779941138256975729453813448315124873863479983045188893716339279970408765493144688534145642055669 : 38704629851203875418223456271438684842428441487187543354303614386446403914792120608165029676753235170528468641305302495324156*z + 57277898590631285644104601800384677384175069648161785886783037641841275698470145168192435518731755467948780067721002693829028 : 58202383142857883301572758223737985993966686986740269158706949891331720280625617459898881688162286989198029039110911105926490*z + 5567907965927318124070836648750571554866768186015130363669150622854147379022838300009978795890448149875623075649846147844756)
#E_B (40611420361963429141642865464220516061987440842354674448604817907127138512839809904438188836369609528029991815149334691421984*z + 67318889054016794040185768414123909370240206737795831662753510393532503702353237777956991718363596811781315472704043085608413) y^2 = x^3 + (34161887828081559672533372539540632173285922028083643332743944869805706307906380578565836416733084870504549589653444670817702*z + 71035541456458971037973787238381279019657058487748964040226949674454632213237198236070980216482272938638302241108721449778384)*x^2 + x
#phiPA (36889664288362438175403544217170545821985293883846451339510805996364133972581068371513221200038720975221439107961030466962544*z + 12871492914304070437102224638332364176093675695381064048836279511875213777778585951881070359048933332085450100701630254978635 : 35601721917320296697308673195288339951014109497337178211271737061821241723228334400120233815218914208764586194257660676175563*z + 60306527848455600271625924588939695279372258026568311163431445415036811295838399550585060950880211344436762455458707058714709 : 9422863625831357711138249052837725468972505784024406723154759920498831935489944818351514429107105745210874478451748385903780*z + 48098432354067761973274977519991808974889091998697648161571568134436724646728723127798261950408341941341953153725343108611393)
#phiQA (10423225353254045593885165565244676021113024069297876788109307236056237987101860854447766312705248404007497581602097175098979*z + 56185496707564213439874222484678336563216565283925615254827747673067661389707198633069920108888472325898908991712414119676190 : 30101989866190487758242971589080805839246522193191822389003694962563660194805196057286274700069057083453677152491878171033776*z + 58685124134076217452082460431611087933464417649109340405794647007860893585490509094426516026144385508307984394779783671183447 : 19457852890535859215919357894754930265487649708691269181896322943757910486458026175285625751201428978065250443599888162637228*z + 33790441383655545265526510480904588712051968141424654168533666010359256074783778701938558241091312225946239567731621288128707)
#EsA (26483657271924125300454280084582119831308280296837504332523610772432657565054114782722922977293502240335883703222278020768300*z + 23295223772855255901547742550165110409251258382655821594435266028529929793405419190635914872709945140509157896739006566871250) y^2 = x^3 + (27774840173456807010685897796886692026166016103535092820017343299576737236925944163750349774758060096868447058223886314171604*z + 31804264096161362203402249252422878277426393245350488916030782555514087232177163647291787690541283274316428584623958267288075)*x^2 + x
#j 68623627958571917158803439055428926298849996512411040237639961245889311544671746821736439244311776180928200590031769898334562*z + 61323097278416335996585222962193003773694844705742843930823632282746480223694558143537188071030156850695002001594665283405433
'gost128':{
'nA': 375121851558675496452585532655202523022630679424182711019733544,
'nB': 32861487073955786794117591344418865324440155634955316073175816,
'EA_B_1': 30234691665951344880917862759328189664816918970799213360061917879785908209367257376974885114474636078611229797887091946131218,
'EA_B_2': 61463144801932172361256612963224033252113089338104329265976181838135470499103054761358315809719454090483049605960721636560376,
'EA_A_1': 28938869683537349547257016671886647825655765739574163527495985370892897590386386632147183528546531306925546137045905389773173,
'EA_A_2': 26652819384173045006231178630182468194963069698833644299224414911972663416827384380100359397026732866558185079719355054994246,
'phiPB_x_1': 1780853909047789122817209951395519816131972077810842284949542311068278012409661409560551278355476148681846920834700834126084,
'phiPB_x_2': 66703240620485392311761152499229330695201867837750543204045070109234874840238507345664955451406671895469076489613528473353475,
'phiPB_y_1': 4882726185863873930783451305690168778135993535607477994985595717337574138004343830196584653222749768712919353317879917865480,
'phiPB_y_2': 57686423636244300433257585539441470841411297480222055232356848016100464834533343271686528126707564177341319718211284489817487,
'phiPB_z_1': 61180394740883345101686725643924936953602003348788265815289170072322596965671478660157118029193124351968506472997662694268223,
'phiPB_z_2': 54616015722860201353757737242727444254217471551742845062568582050964276292912533718039833606408930152449184637706325471677454,
'phiQB_x_1': 16108313662703392199592218669280553529172776629832653690480155693005809547527955473340777165119572895298685357496559174128540,
'phiQB_x_2': 8171246843271576812062532485779941138256975729453813448315124873863479983045188893716339279970408765493144688534145642055669,
'phiQB_y_1': 38704629851203875418223456271438684842428441487187543354303614386446403914792120608165029676753235170528468641305302495324156,
'phiQB_y_2': 57277898590631285644104601800384677384175069648161785886783037641841275698470145168192435518731755467948780067721002693829028,
'phiQB_z_1': 58202383142857883301572758223737985993966686986740269158706949891331720280625617459898881688162286989198029039110911105926490,
'phiQB_z_2': 5567907965927318124070836648750571554866768186015130363669150622854147379022838300009978795890448149875623075649846147844756,
'EB_B_1': 40611420361963429141642865464220516061987440842354674448604817907127138512839809904438188836369609528029991815149334691421984,
'EB_B_2': 67318889054016794040185768414123909370240206737795831662753510393532503702353237777956991718363596811781315472704043085608413,
'EB_A_1': 34161887828081559672533372539540632173285922028083643332743944869805706307906380578565836416733084870504549589653444670817702,
'EB_A_2': 71035541456458971037973787238381279019657058487748964040226949674454632213237198236070980216482272938638302241108721449778384,
'phiPA_x_1': 36889664288362438175403544217170545821985293883846451339510805996364133972581068371513221200038720975221439107961030466962544,
'phiPA_x_2': 12871492914304070437102224638332364176093675695381064048836279511875213777778585951881070359048933332085450100701630254978635,
'phiPA_y_1': 35601721917320296697308673195288339951014109497337178211271737061821241723228334400120233815218914208764586194257660676175563,
'phiPA_y_2': 60306527848455600271625924588939695279372258026568311163431445415036811295838399550585060950880211344436762455458707058714709,
'phiPA_z_1': 9422863625831357711138249052837725468972505784024406723154759920498831935489944818351514429107105745210874478451748385903780,
'phiPA_z_2': 48098432354067761973274977519991808974889091998697648161571568134436724646728723127798261950408341941341953153725343108611393,
'phiQA_x_1': 10423225353254045593885165565244676021113024069297876788109307236056237987101860854447766312705248404007497581602097175098979,
'phiQA_x_2': 56185496707564213439874222484678336563216565283925615254827747673067661389707198633069920108888472325898908991712414119676190,
'phiQA_y_1': 30101989866190487758242971589080805839246522193191822389003694962563660194805196057286274700069057083453677152491878171033776,
'phiQA_y_2': 58685124134076217452082460431611087933464417649109340405794647007860893585490509094426516026144385508307984394779783671183447,
'phiQA_z_1': 19457852890535859215919357894754930265487649708691269181896322943757910486458026175285625751201428978065250443599888162637228,
'phiQA_z_2': 33790441383655545265526510480904588712051968141424654168533666010359256074783778701938558241091312225946239567731621288128707,
'j_1': 68623627958571917158803439055428926298849996512411040237639961245889311544671746821736439244311776180928200590031769898334562,
'j_2': 61323097278416335996585222962193003773694844705742843930823632282746480223694558143537188071030156850695002001594665283405433
},
#E_Start y^2 = x^3 + (19050032500236975951931278238128480632077445205188090822184044312147401018675525735727466316601557305179049693269373659397768054906005239733641763499719652314977061249270313147430408303992114668076300721615669341333544474198989)*x^2 + x
#basis PA (70746536086435957252162222395781065625609593766974983676568047020103931120029224051754397262072258533879728829593057235828837658399658226352637035985151964846332568496880842284747818464710071796581248650671185989908191271895919*z + 348779581580882247699441355776750578713077144639057727612745696855891203548321954270501743910398026019560538524453888595385146988601674416203576404727190227377798216251296370779954303202420297836184734160095008306053267071776 : 33503643254748777526135795304304336608475116192461267100040321519570213284669523235902631852874947051332492200480082771691731802744755238068701797471206549218535175759430602322147740490041169498526131962954791410872327511623000*z + 60453067796112960350617625807347831142580510716584932612140045366687415351414111681300105746961664286659468458524998472681600496372621015758353639717610874039376143804025207297673003083161554849301990827045762501638471186749152 : 5169962183835913906911897203638605259607314259459191403433855432531563340964400902074925239062316086339907521417911209566007753243250478554865956120039883560640901750333513507813492157307828952916378919626202758431590560130709*z + 7373656459139207639409041949091505990180783966814912638657173800361571299780913240750035871460853095577137003890569027029286713206325275999382719251424979516666628673161601582074192530117157745798510284151881191803053008699600)
#basis QA (26596984230535042413333540263771870135207968244008049932667216079370181509253300865283979678754862559434538414633309810102402012223568651464880797782764398585972509562673792894149481835266637464696184968378771953095522312329820*z + 17575413332150770901745946195367130763766727268442207253165706425801222165803482741986989741599555874034939860531861101861804670270655422056621543202457236350975839296370281015809956918395954731043086863215383924886729798607335 : 36798824750298658309797564552635141938044243413886056218645450515486084637578965465214913207240255274825101965310660142465111201050893839689744746434750222260889903351228568961240550273264243889153892974092487363888776767730243*z + 19040998857885789610202000901610311831201792210548369286843970510582580366474511580847922647997035637427419325479908979924556882441666034382026743707770271895828568821365227858576964313934450604949798155275454148656595003114747 : 52246633118273440017653585864797237252895245853091604376412108016266647983501186656950304805079187527891159973195007429232831971536480742286826305831826077563809932578120949723367865064020267705704253561958835456957612439500982*z + 7063020485368603937855976838937068818717145237134073924943678798562251657061471826491267256554365429470186330108943591378305640726339371893506998680311943518192140444803601890669697102715088309971704495787801703647592392274231)
#basis PB (51912591742010525037312735195820995106032261756299168853880630500874655073347600337796337217704506895002211621434100015563193153795548660545000763172483510104497373860526306266663802563111253561069349850292907911320569006149646*z + 25951007852228571740166987952192377933059169499230641374477835908109965590914986339649868468342118522309854644606405422507976974586128261831339856577787903980069442096753237490992054381266441249465623211518969498056950314312974 : 31016259735121358122473101393339731156510595354522568135050176397231453575476586719852411910038066382328590352581712999158623148322670510067521336024791098074923000739559924320325979537647093533437482178410891215133527072779006*z + 28579891128960370889561849526936704897223709306893697959110891071297471422183315348215383546600649419334135702651508539172509102484505430537603504060967670331103305307775364397274998654698990914377621458075330794844012282632837 : 31429098092535323097486069383598751323989807895481895384437905153014523412218082666224911376448750228682187888456419566464171302882401849283822109015959482574939508888303781226496970260097915750789752451319259790431335223468636*z + 6711946701189252472914915246340953226037616605659498979269229992695866917330678404655822678854766694362089441032634440390235566715580492695172491562619805588250299519610353354134711695145240022237577037571831581617359113603691)
#basis QB (68827162028124453959168899321861495627713779928709194865286610345170873610230473377922106293656162250111561505445200224749412497186910517347684303461590543067338846697123765466399029083158928596649062873544538144293289369479875*z + 35388380299037442260835457175439691114311165642943952932712454214305823212887043353034558898486483712794963646005422598556320359445916545323125423365923777839268257534340053264488899865780388555314828784014507330539471194536660 : 55232469442045337657680878316297750611815951164502083538349865070600431820608350475487232098852193592114983606879672103901700013651039802505265751539914751638287861430887515613931816612111755865737967015370597689083985099134469*z + 46964250168011064810305627069614374133457544317336320336203402993817055773158079809413452760490690578871062825049816881412002260052339496065342294891448429047687277774514045094459515051254983426376694840422075808450921978116666 : 43956148039234931651020793635479121610336485334670791327138179886202908936431195548521715879403350744886186899538192437312624960355140062340152133307544841685125620792955673471186805312443380257038271092281899807881853580251161*z + 35266868612363078002011083241101217195041505569383378654025998783017779381836390359834434962987453639228117697039814057959460971253817962503918960286258164579463677906716093075860967253226358882561837428609810552125826889353206)
#nA 346030325602274749947506991632267117788704366230898430312937428762956164070104248217698140860595323107396785733
#nB 619331982461998986588581917581292542593051512009994784953324219716485209241542985487547518060798667673335740707424
#E_A (31946167384652552035045203667688415699900148250373749065317356145083260220671623445360091002774798989585221626198990500688123268674649764786006838409037560233059659598728600267139061402372183432307214604899560341601099471671273*z + 48681932489965788667440351963473313399401294525673541021093276832784832683933188147236907841704252221970425176616090449137423022773313826409469109742274123024561440643182721193467360227334642385361881797130770075918886426193002) y^2 = x^3 + (4736940235082038018669029064642428753908669314534548631713565812019755676761481087819558168402456995084426730245805923003593796257215568172532742720864114481252883684266700523307452183650805931323768797529168172832333708426122*z + 31736714665719648514580234849819329283922185021056075831126848993387007329074281220419079386327391379078665331443670085026585545053922074344703170250236215344450702376186337731287160512437106189301085825582589387684888791992087)*x^2 + x
#phiPB (2736295828151928259574866416694423028421571134781885998410112696023695815911471356652225977192543959791658878545184166428008611088454415489269868093677615303800668691401200761277399636016842619066142849966332037037883092789817*z + 13785481778476885167398038926066918425416147852889746467478438802491278482523696381477174040500434181468382424144952926585976388295574264210275489476231996503420005139261916990425586595227463559192053860887674064878662545177094 : 47582179112010611587524772339382649689354687259940956916589069324228459065062783977206227590206931304324024155430425470126488495261100374130031849087760041341146653803603487279965395124290517967100863944892858647764270643726829*z + 27230700656060842047542258881376175804759660606099946989036373988343430764255719335602247070084618070501276974139507206549790715869299767828650238218553747202827440509187642120092587036040440361599431918836808579071346554811452 : 19620407006361287847158715023123510179080511457473822627943088834098727427169183462449695944791685886558357420848862813992962555387482081937315742303760755138097248708778544255006835285134978441842554408337228834900052347655260*z + 11182258572970614579770916393830386238496104156120953936535785151903191890199784574993212216689907592044727498135780180987412190172161111933897544791270217109613528944705941997797643983633148497342264682583611270313962417026727)
#phiQB (2519603271381457029869064668273804938023215691666159268968263684316640747738223428491685405098123559704280926353956870651103022448239541547585569191066666055235050667048332479798876556520737870780046235451131328745217689875704*z + 69412028374045841384169755427787441627361014978165058249448419084875564424713560924005631475913337320205515965443428589756630346027999290775400626644607946516195765738199041542114659189045194810110437613132565021639890827748948 : 65987470334020256117590308890593899761204884657203905391418008699528555764559920305013978654758934912961491168554911615040252503065544306317134905490358936833027931432799226698762860507385197895207614412691785973551542753363482*z + 61584760135795466411037496200649234794818738184873850445337254860733502261051166682053785957987878073561308058975791370944878570577414204538103119790870999178432938319278816661414357898072125803672751277376428659880124253529598 : 27416716685539688348512512713396184014110040421546580770850811229811257329721419887816012572977424189798842441518925834754621846017197245594558347273133649666903399607381732925459927587154005516293932228184451270317464363970634*z + 5192703416967117029027772365279137828170509583480963685653251774635537727617622733922369999865835726608293928782667282226760656240234359290654886399245180550602557185498996449636485910364386120505364352943776929148801766277443)
#E_B (43028594172519029522915328677172675514916638793589648362996911659513835172845623936424809884566192259037685499880073440964353748000527780996424423449834621392264096001383785201838982541741431761234074281852374101129182432073059*z + 51400103626311686078351126007167049117841536925343786825308044586627925289899083353000193552460067725576291563430060287924135679389289861404718470490581145284640934327766388146421325337737623874291171519751723100473516876708332) y^2 = x^3 + (17331481019780283784938421375590606535135384680294171307291965639011212001729426414559928467734551205680762529357491654877814207041196099591452169215242892811841480876019801498819484304017279551772339845930125874211437451919445*z + 71941288575588030659987312027372683271714780140751300470618150351097805695380450751333304862260562889565343201044332073598636052361575307525886870670596404719874168439878948690564079685094055146427306857303453587944245451043756)*x^2 + x
#phiPA (31212802572612725433662420838474630131225970678425230752258806693329548732197202769480516707824833693319145519214600750142096814275250582512530588493959066693539742343324945669863712794135054379972321085674516962587643756902636*z + 41650732537802824069990037172665000496318011288363955569022177544002477270642355683004880869586603174247226425483506546658460350772306461955349148479380320301481679199431589049874814757421603036496597668103698536652078041680822 : 3402389938843588745859922698465300058886642669729772451322942812101573921403360511082178904162804384125338918676632264013633393660703417729864564285532576870075721641274397881344380735729670559797273409956910318485986277204832*z + 34878063553601335642320740474810674659542854447790798205562595078726580231168898528125946618009741009154287985530561350630133303360633827874058798690360071543341619316936048487804181761487759019473179979249224468647757048566520 : 28170284264912405631118743097219669500778718095721064424609694204482520049459200741580917279947672763808764633458019165482893276566328945793034462373772331736619379506604998837509877124179732219466066642885149734618255719765874*z + 71509448737650569712116321003098109455247732525242929023966954555186344191375922619147044303744259402689257640779471437015713910697437090953129568649231876556898686428866276097917576712652710102031326807597016800167658062726227)
#phiQA (71038182195465593139387606851433730753431173063350049450337356716330869963910097575198412740182655419267282682493647756148481169427503358588489137860178196121864907478633924364870993092470840131508865751688381903204541535322992*z + 16532175887039885395794720464680180411200257144719189082461302079384953689495699833914784220407912427363068243026721943729431054579895811251916695111990963627589767847374779469935198201063052785266355865466795773797302556223048 : 57227688297029832962674224727565954817313058187636266260314081823082308882610649722093325455666076599194208363616568301953496861503023161924407116978277915966537254358261186314470612299590452759126567749362232380002574229912273*z + 37184924589169445375331736477552250782072037281914858535238458288071091259106829355178827183085828250609987728541739834752290781064537327039541852037554942154948310311927197814477984712719249363930426811888910078894822562243979 : 48566114613459196906579143328001930975802819400588117364390948966893513192708089330307034266495122882010896092730940828142935167003535341267213185927246851816381965942643054469270067290921943614655841816443424757942598973895567*z + 52447147574614478350960274889373077669737594900667203537618271704458333280422975700869082115842999019565431251566899706554999749098902512962778047115645262662074765838788794123996951862468087661861034317921787510156413745417671)
#EsA (14593837018481493722858184614081163955651874046219671381679749750095352246160070282372391681534708878200285858085964234575985104735524021169823368241936023062916004807312821098131305586093035487277566360731749227515163068285484*z + 19627478776651360188599105462452992901868530217572108719090435526738331656543743280842762123946308628602762332933395201840103367701035959050597848933076879576574070506329470068280049241351963359198911286633962369881261591601037) y^2 = x^3 + (70590451513352539713862092071708705732145111139297747322543152586098888111618191215880795877195142189702915686379033207597811941263227570639615462278995870514573063940865327301630468951839404829078352164906809293064466759593672*z + 49880687411022764436961106478220454604456400259593928583548466544664483186040871879836679051387055774083489882070401176710307252745452497123059498175655174932124556776427220337579127713727031154862202774049877974627679067981785)*x^2 + x
#j 26211438962201705501986852795701109732307383869819714052815057061375426355628724628252439435492780480848668297546461299017209078259898689895666286470331296576188859912850642600964404177103252209973430136612858554655860852231921*z + 53484509786277885857607071331758421976235930884260093731558721203187495275653241247286430914105597982603391384244550490750900019825098484781676915619089497313224223464209099018367506152927108509084162447011375263044820231231757
'gost256':{
'nA': 346030325602274749947506991632267117788704366230898430312937428762956164070104248217698140860595323107396785733,
'nB': 619331982461998986588581917581292542593051512009994784953324219716485209241542985487547518060798667673335740707424,
'EA_B_1': 31946167384652552035045203667688415699900148250373749065317356145083260220671623445360091002774798989585221626198990500688123268674649764786006838409037560233059659598728600267139061402372183432307214604899560341601099471671273,
'EA_B_2': 48681932489965788667440351963473313399401294525673541021093276832784832683933188147236907841704252221970425176616090449137423022773313826409469109742274123024561440643182721193467360227334642385361881797130770075918886426193002,
'EA_A_1': 4736940235082038018669029064642428753908669314534548631713565812019755676761481087819558168402456995084426730245805923003593796257215568172532742720864114481252883684266700523307452183650805931323768797529168172832333708426122,
'EA_A_2': 31736714665719648514580234849819329283922185021056075831126848993387007329074281220419079386327391379078665331443670085026585545053922074344703170250236215344450702376186337731287160512437106189301085825582589387684888791992087,
'phiPB_x_1': 2736295828151928259574866416694423028421571134781885998410112696023695815911471356652225977192543959791658878545184166428008611088454415489269868093677615303800668691401200761277399636016842619066142849966332037037883092789817,
'phiPB_x_2': 13785481778476885167398038926066918425416147852889746467478438802491278482523696381477174040500434181468382424144952926585976388295574264210275489476231996503420005139261916990425586595227463559192053860887674064878662545177094,
'phiPB_y_1': 47582179112010611587524772339382649689354687259940956916589069324228459065062783977206227590206931304324024155430425470126488495261100374130031849087760041341146653803603487279965395124290517967100863944892858647764270643726829,
'phiPB_y_2': 27230700656060842047542258881376175804759660606099946989036373988343430764255719335602247070084618070501276974139507206549790715869299767828650238218553747202827440509187642120092587036040440361599431918836808579071346554811452,
'phiPB_z_1': 19620407006361287847158715023123510179080511457473822627943088834098727427169183462449695944791685886558357420848862813992962555387482081937315742303760755138097248708778544255006835285134978441842554408337228834900052347655260,
'phiPB_z_2': 11182258572970614579770916393830386238496104156120953936535785151903191890199784574993212216689907592044727498135780180987412190172161111933897544791270217109613528944705941997797643983633148497342264682583611270313962417026727,
'phiQB_x_1': 2519603271381457029869064668273804938023215691666159268968263684316640747738223428491685405098123559704280926353956870651103022448239541547585569191066666055235050667048332479798876556520737870780046235451131328745217689875704,
'phiQB_x_2': 69412028374045841384169755427787441627361014978165058249448419084875564424713560924005631475913337320205515965443428589756630346027999290775400626644607946516195765738199041542114659189045194810110437613132565021639890827748948,
'phiQB_y_1': 65987470334020256117590308890593899761204884657203905391418008699528555764559920305013978654758934912961491168554911615040252503065544306317134905490358936833027931432799226698762860507385197895207614412691785973551542753363482,
'phiQB_y_2': 61584760135795466411037496200649234794818738184873850445337254860733502261051166682053785957987878073561308058975791370944878570577414204538103119790870999178432938319278816661414357898072125803672751277376428659880124253529598,
'phiQB_z_1': 27416716685539688348512512713396184014110040421546580770850811229811257329721419887816012572977424189798842441518925834754621846017197245594558347273133649666903399607381732925459927587154005516293932228184451270317464363970634,
'phiQB_z_2': 5192703416967117029027772365279137828170509583480963685653251774635537727617622733922369999865835726608293928782667282226760656240234359290654886399245180550602557185498996449636485910364386120505364352943776929148801766277443,
'EB_B_1': 43028594172519029522915328677172675514916638793589648362996911659513835172845623936424809884566192259037685499880073440964353748000527780996424423449834621392264096001383785201838982541741431761234074281852374101129182432073059,
'EB_B_2': 51400103626311686078351126007167049117841536925343786825308044586627925289899083353000193552460067725576291563430060287924135679389289861404718470490581145284640934327766388146421325337737623874291171519751723100473516876708332,
'EB_A_1': 17331481019780283784938421375590606535135384680294171307291965639011212001729426414559928467734551205680762529357491654877814207041196099591452169215242892811841480876019801498819484304017279551772339845930125874211437451919445,
'EB_A_2': 71941288575588030659987312027372683271714780140751300470618150351097805695380450751333304862260562889565343201044332073598636052361575307525886870670596404719874168439878948690564079685094055146427306857303453587944245451043756,
'phiPA_x_1': 31212802572612725433662420838474630131225970678425230752258806693329548732197202769480516707824833693319145519214600750142096814275250582512530588493959066693539742343324945669863712794135054379972321085674516962587643756902636,
'phiPA_x_2': 41650732537802824069990037172665000496318011288363955569022177544002477270642355683004880869586603174247226425483506546658460350772306461955349148479380320301481679199431589049874814757421603036496597668103698536652078041680822,
'phiPA_y_1': 3402389938843588745859922698465300058886642669729772451322942812101573921403360511082178904162804384125338918676632264013633393660703417729864564285532576870075721641274397881344380735729670559797273409956910318485986277204832,
'phiPA_y_2': 34878063553601335642320740474810674659542854447790798205562595078726580231168898528125946618009741009154287985530561350630133303360633827874058798690360071543341619316936048487804181761487759019473179979249224468647757048566520,
'phiPA_z_1': 28170284264912405631118743097219669500778718095721064424609694204482520049459200741580917279947672763808764633458019165482893276566328945793034462373772331736619379506604998837509877124179732219466066642885149734618255719765874,
'phiPA_z_2': 71509448737650569712116321003098109455247732525242929023966954555186344191375922619147044303744259402689257640779471437015713910697437090953129568649231876556898686428866276097917576712652710102031326807597016800167658062726227,
'phiQA_x_1': 71038182195465593139387606851433730753431173063350049450337356716330869963910097575198412740182655419267282682493647756148481169427503358588489137860178196121864907478633924364870993092470840131508865751688381903204541535322992,
'phiQA_x_2': 16532175887039885395794720464680180411200257144719189082461302079384953689495699833914784220407912427363068243026721943729431054579895811251916695111990963627589767847374779469935198201063052785266355865466795773797302556223048,
'phiQA_y_1': 57227688297029832962674224727565954817313058187636266260314081823082308882610649722093325455666076599194208363616568301953496861503023161924407116978277915966537254358261186314470612299590452759126567749362232380002574229912273,
'phiQA_y_2': 37184924589169445375331736477552250782072037281914858535238458288071091259106829355178827183085828250609987728541739834752290781064537327039541852037554942154948310311927197814477984712719249363930426811888910078894822562243979,
'phiQA_z_1': 48566114613459196906579143328001930975802819400588117364390948966893513192708089330307034266495122882010896092730940828142935167003535341267213185927246851816381965942643054469270067290921943614655841816443424757942598973895567,
'phiQA_z_2': 52447147574614478350960274889373077669737594900667203537618271704458333280422975700869082115842999019565431251566899706554999749098902512962778047115645262662074765838788794123996951862468087661861034317921787510156413745417671,
'j_1': 26211438962201705501986852795701109732307383869819714052815057061375426355628724628252439435492780480848668297546461299017209078259898689895666286470331296576188859912850642600964404177103252209973430136612858554655860852231921,
'j_2': 53484509786277885857607071331758421976235930884260093731558721203187495275653241247286430914105597982603391384244550490750900019825098484781676915619089497313224223464209099018367506152927108509084162447011375263044820231231757
}
}
def ss_isogeny(*args, **kw):
"""
Generate parameters and run a key exchange.
Input:
- If no argument is supplied, a default small prime is chosen;
- If a string is supplied, it is interpreted as the index of one of
the predefined primes in the parameters dictionary;
- Otherwise arguments defining a prime are accepted in the same format
as for ss_isogeny_gen().
Optional keywords:
- verbose (int): if >= 1, print informations and timings about each step
being executed;
- measure (bool): passed down to ss_isogeny_exchange().
Output: the output of ss_isogeny_exchange().
"""
if 'verbose' in kw:
old_verbose = misc.get_verbose()
misc.set_verbose(kw['verbose'])
if len(args) == 0:
params = ss_isogeny_gen(**parameters['5-7-32'])
elif len(args) == 1:
params = ss_isogeny_gen(**parameters[args[0]])
else:
params = ss_isogeny_gen(*args)
res = ss_isogeny_exchange(*params, measure='measure' in kw and kw['measure'])
if 'verbose' in kw:
misc.set_verbose(old_verbose)
return res
# We construct the bases of the lA^eA and lB^eB torsion
def torsion_point(E, cofactor, factor_div_p):
P = E.zero()
while (P * factor_div_p).is_zero():
P = E.random_point().lift() * cofactor
return P
def basis(E, cofactor, factor, p):
P = torsion_point(E, cofactor, factor // p)
assert((P * factor).is_zero())
Q = P
while P.weil_pairing(Q, factor)^(factor // p) == 1:
Q = torsion_point(E, cofactor, factor // p)
assert((P * factor).is_zero())
return (P,Q)
################################################################################
# SCHEME PARAMETERS (precomputations)
################################################################################
def ss_isogeny_gen(lA, lB, eA, eB, f, pm1):
"""
Generate public parameters for the given prime.
Input:
- lA, lB: prime integers;
- eA, eB, f: integers;
- pm1: +1 or -1;
- lA^eA * lB^eB * f + pm1 must be prime.
Output:
tuple (E, lA, eA, PA, QA, strA, lB, eB, PB, QB, strB), where:
- E is a supersingular curve of cardinality (lA^eA * lB^eB * f)^2;
- PA and QA are generators of E[lA^eA];
- PB and QB are generators of E[lB^eB];
- lA, eA, lB, eB are the same as the inputs;
- strA and str B are computational strategies to compute cyclic
isogenies of degree lA^eA and lB^eB, optimized according to the
global variable weights.
"""
prime = "%s^%s * %s^%s" % (lA, eA, lB, eB) + (f != 1)*(" * %s" % f) + (pm1 == 1)*" + 1" + (pm1 == -1)*" - 1"
p = sage_eval(prime)
misc.verbose("Using the %s-bits prime p = %s" % (p.nbits(), prime))
misc.verbose("")
# Intialize the base field GF(p^2)
misc.verbose("**** PUBLIC PARAMETERS SET UP ****")
ct = misc.verbose("Constructing the field GF(p^2)")
K.<z> = MyGFp2(p)
P.<X> = K[]
misc.verbose(t=ct)
misc.verbose()
# We compute a supersingular curve using complex multiplication.
# We need a CM field K such that p is inert in K, if D is the
# discriminant of K, this is equivalent to (D/p) = -1.
# By construction (-1/p) = -1, so the condition above is equivalent
# to (-D/p) = 1, thus we look for a (small) quadratic residue in GF(p).
ct = misc.verbose("Looking for a discriminant for which p is inert")
d = K.base()(-1)
while not d.is_square():
d = K.base()(randint(2, 1000))
D = -d.lift()
if D % 4 != 1:
D *= 4
misc.verbose(t=ct)
misc.verbose()
# Now we can compute the Hilbert class polynomial and factor it
# in GF(p^2)
ct = misc.verbose("Computing the Hilbert class polynomial for D=%s" % D)
H = P(hilbert_class_polynomial(D))
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Factoring the Hilbert class polynomial")
j = H.roots()[-1][0]
misc.verbose(t=ct)
misc.verbose()
# We finally can construct the supersingular elliptic curve
ct = misc.verbose("Constructing the elliptic curve and taking the right twist")
E = MontgomeryCurve_from_j(j)
if ((pm1 == -1 and not (E.A + 2).is_square) or
(pm1 == 1 and (E.A + 2).is_square)):
misc.verbose("The curve, has cardinality (p" + (pm1==-1)*"-" + (pm1==1)*"+" + "1)^2, twisting it.")
E = E.quadratic_twist()
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Checking (probabilistically) that the curve has the expected cardinality")
for i in range(10):
a = E.random_point()
assert((a*(p-pm1)).is_zero())
misc.verbose(t=ct)
misc.verbose()
# Alice's part
ct = misc.verbose("Constructing Alice's basis")
PA, QA = basis(E, lB^eB*f, lA^eA, lA)
misc.verbose(t=ct)
misc.verbose()
# Bob's part
ct = misc.verbose("Constructing Bob's basis")
PB, QB = basis(E, lA^eA*f, lB^eB, lB)
misc.verbose(t=ct)
misc.verbose()
# We compute the optimal straregies for Alice and Bob
ct = misc.verbose("Computing Alice's strategy")
height = eA
if lA == 2: height -= 2
strA = paths.optimal_paths(height, *weights[lA], construct=False)
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Computing Bob's strategy")
height = eB
if lB == 2: height -= 2
strB = paths.optimal_paths(height, *weights[lB], construct=False)
misc.verbose(t=ct)
misc.verbose()
return E, lA, eA, PA, QA, strA, lB, eB, PB, QB, strB
def gost_precompute(A, B, lA, lB, eA, eB, f, pm1):
"""
Generate public parameters for the given prime.
Input:
- A, B: Weierstrass form coefficients
- lA, lB: prime integers;
- eA, eB, f: integers;
- pm1: +1 or -1;
- lA^eA * lB^eB * f + pm1 must be prime.
Output:
tuple (E, lA, eA, PA, QA, strA, lB, eB, PB, QB, strB), where:
- E is a supersingular curve of cardinality in Montgomery form(lA^eA * lB^eB * f)^2;
- PA and QA are generators of E[lA^eA];
- PB and QB are generators of E[lB^eB];
- lA, eA, lB, eB are the same as the inputs;
- strA and str B are computational strategies to compute cyclic
isogenies of degree lA^eA and lB^eB, optimized according to the
global variable weights.
"""
prime = "%s^%s * %s^%s" % (lA, eA, lB, eB) + (f != 1)*(" * %s" % f) + (pm1 == 1)*" + 1" + (pm1 == -1)*" - 1"
p = sage_eval(prime)
misc.verbose("Using the %s-bits prime p = %s" % (p.nbits(), prime))
misc.verbose("")
# Intialize the base field GF(p^2)
misc.verbose("**** PUBLIC PARAMETERS SET UP ****")
ct = misc.verbose("Constructing the field GF(p^2)")
K.<z> = MyGFp2(p)
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Construct Montgomery curve from Weierstrass curve")
E_start = EllipticCurve(K, [A, B])
P = PolynomialRing(K.base(), 'X')
poly = P.gen()**3 + P.gen()*A + B
roots = poly.roots()
alpha = roots[0][0]
s = 3*alpha^2 + A
if kronecker(s, p) != 1:
raise RuntimeError, "ERROR: kronecker symbol not 1"
s = s.inverse_of_unit()
s = s.sqrt()
montA = 3*alpha*s
montB = s
mA = K._element_constructor_(int(montA))
# mB = K._element_constructor_(int(montB))
mB = K._element_constructor_(1)
E_start_mont = MontgomeryCurve(mA, mB)
if E_start.j_invariant() != E_start_mont.j_invariant():
raise RuntimeError, "ERROR, different curves"
misc.verbose(t=ct)
misc.verbose()
# Alice's part
ct = misc.verbose("Constructing Alice's basis")
PA, QA = basis(E_start_mont, lB^eB*f, lA^eA, lA)
misc.verbose(t=ct)
misc.verbose()
# Bob's part
ct = misc.verbose("Constructing Bob's basis")
PB, QB = basis(E_start_mont, lA^eA*f, lB^eB, lB)
misc.verbose(t=ct)
misc.verbose()
# We compute the optimal straregies for Alice and Bob
ct = misc.verbose("Computing Alice's strategy")
height = eA
if lA == 2: height -= 2
strA = paths.optimal_paths(height, *weights[lA], construct=False)
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Computing Bob's strategy")
height = eB
if lB == 2: height -= 2
strB = paths.optimal_paths(height, *weights[lB], construct=False)
misc.verbose(t=ct)
misc.verbose()
return E_start_mont, lA, eA, PA, QA, strA, lB, eB, PB, QB, strB
def rand_subgroup(l, e):
if randrange(0, l+1):
return (1, randrange(0, l^e))
else:
return (l*randrange(0,l^(e-1)), 1)
################################################################################
# KEY EXCHANGE
################################################################################
def ss_isogeny_exchange(E, lA, eA, PA, QA, strA, lB, eB, PB, QB, strB, control=None, measure=None):
"""
Perform a key exchange.
Input:
- lA, lB: prime integers;
- eA, eB, f: integers;
- E a supersingular curve of cardinality (lA^eA * lB^eB * f)^2;
- PA and QA are generators of E[lA^eA];
- PB and QB are generators of E[lB^eB];
- strA and str B are computational strategies to compute cyclic
isogenies of degree lA^eA and lB^eB;
- measure: use the sage_timeit module to measure performances
(default: False)
Output:
- The shared key (the j-invariant of a curve isogenous to E);
- If measure is True, timing information for each phase of the key
echange.
"""
# One run with checks, to see if everything works
misc.verbose('**** KEY EXCHANGE ****')
misc.verbose("We run some consistency checks to detect bugs,")
misc.verbose("so don't take the running times as being accurate.")
misc.verbose()
ct = misc.verbose("Randomly generating secret keys.")
if control is not None and control:
generator = PA.x.parent().gen()
if eA == 208 and eB == 129:
param = 'gost128'
else:
if eA == 372 and eB == 239:
param = 'gost256'
else:
raise RuntimeError, "ERROR:no control value for current paramset"
mA = 1
nA = control_value[param]['nA']
mB = 1
nB = control_value[param]['nB']
EA_A = control_value[param]['EA_A_1']*generator + control_value[param]['EA_A_2']
EA_B = control_value[param]['EA_B_1']*generator + control_value[param]['EA_B_2']
EA_control = MontgomeryCurve(EA_A, EA_B)
phiPB_x = control_value[param]['phiPB_x_1']*generator + control_value[param]['phiPB_x_2']
phiPB_y = control_value[param]['phiPB_y_1']*generator + control_value[param]['phiPB_y_2']
phiPB_z = control_value[param]['phiPB_z_1']*generator + control_value[param]['phiPB_z_2']
phiPB_control = MontgomeryPoint(EA_control, phiPB_x, phiPB_z, phiPB_y)
phiQB_x = control_value[param]['phiQB_x_1']*generator + control_value[param]['phiQB_x_2']
phiQB_y = control_value[param]['phiQB_y_1']*generator + control_value[param]['phiQB_y_2']
phiQB_z = control_value[param]['phiQB_z_1']*generator + control_value[param]['phiQB_z_2']
phiQB_control = MontgomeryPoint(EA_control, phiQB_x, phiQB_z, phiQB_y)
EB_A = control_value[param]['EB_A_1']*generator + control_value[param]['EB_A_2']
EB_B = control_value[param]['EB_B_1']*generator + control_value[param]['EB_B_2']
EB_control = MontgomeryCurve(EB_A, EB_B)
phiPA_x = control_value[param]['phiPA_x_1']*generator + control_value[param]['phiPA_x_2']
phiPA_y = control_value[param]['phiPA_y_1']*generator + control_value[param]['phiPA_y_2']
phiPA_z = control_value[param]['phiPA_z_1']*generator + control_value[param]['phiPA_z_2']
phiPA_control = MontgomeryPoint(EB_control, phiPA_x, phiPA_z, phiPA_y)
phiQA_x = control_value[param]['phiQA_x_1']*generator + control_value[param]['phiQA_x_2']
phiQA_y = control_value[param]['phiQA_y_1']*generator + control_value[param]['phiQA_y_2']
phiQA_z = control_value[param]['phiQA_z_1']*generator + control_value[param]['phiQA_z_2']
phiQA_control = MontgomeryPoint(EB_control, phiQA_x, phiQA_z, phiQA_y)
j_control = control_value[param]['j_1']*generator + control_value[param]['j_2']
else:
mA, nA = rand_subgroup(lA, eA)
mB, nB = rand_subgroup(lB, eB)
misc.verbose(t=ct)
misc.verbose()
misc.verbose("Generating Alice's public data")
EA, phiPB, phiQB = keygen_c(PA, QA, mA, nA, lA, strA, PB, QB)
if control is not None and control:
if EA.B==EA_control.B and EA.A==EA_control.A and phiPB.x==phiPB_control.x and phiPB.y==phiPB_control.y and phiPB.z==phiPB_control.z and phiQB.x == phiQB_control.x and phiQB.y == phiQB_control.y and phiQB.z == phiQB_control.z:
print "Control value for Alice is match!"
else:
raise RuntimeError, "ERROR: Alice's public data and control value is different"
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Generating Bob's public data")
EB, phiPA, phiQA = keygen_c(PB, QB, mB, nB, lB, strB, PA, QA)
if control is not None and control:
if EB.B==EB_control.B and EB.A==EB_control.A and phiPA.x==phiPA_control.x and phiPA.y==phiPA_control.y and phiPA.z==phiPA_control.z and phiQA.x == phiQA_control.x and phiQA.y == phiQA_control.y and phiQA.z == phiQA_control.z:
print "Control value for Bob is match!"
else:
raise RuntimeError, "ERROR: Bob's public data and control value is different"
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Computing shared key on Alice's side")
EsA, _, _ = keygen_c(phiPA, phiQA, mA, nA, lA, strA)
misc.verbose(t=ct)
misc.verbose()
ct = misc.verbose("Computing shared key on Bob's side")
EsB, _, _ = keygen_c(phiPB, phiQB, mB, nB, lB, strB)
misc.verbose(t=ct)
misc.verbose()
if EsA.j_invariant() != EsB.j_invariant():
raise RuntimeError, "ERROR: the shared keys don't match! Here's the secret keys:\n\tmA = %d\n\tnA = %d\n\tmB = %d\n\tnB = %d\n" % (mA,nA,mB,nB)
if control is not None and control:
if EsA.j_invariant() != j_control:
raise RuntimeError, "ERROR: the shared key doesn't match with control value!"
else:
print "Shared key matchs with control_value"
# Now we measure performances
timings = None
if measure is not None and measure:
misc.verbose('**** TIMINGS ****')
misc.verbose("Now we measure the real performances.")
misc.verbose("(This may take some time)")
misc.verbose()
context = globals()
context.update(locals())
if measure is True:
repeat = 3
else:
try:
repeat = int(measure)
except:
repeat = 3
misc.verbose("Alice round 1")
A1 = sage_timeit('keygen_c(PA, QA, mA, nA, lA, strA[:], PB, QB, checks=False)', context, repeat=repeat)
misc.verbose(A1)
misc.verbose("Alice round 2")
A2 = sage_timeit('keygen_c(phiPA, phiQA, mA, nA, lA, strA[:], checks=False)', context, repeat=repeat)
misc.verbose(A2)
misc.verbose()
misc.verbose("Bob round 1")
B1 = sage_timeit('keygen_c(PB, QB, mB, nB, lB, strB[:], PA, QA, checks=False)', context, repeat=repeat)
misc.verbose(B1)
misc.verbose("Bob round 2")
B2 = sage_timeit('keygen_c(phiPB, phiQB, mB, nB, lB, strB[:], checks=False)', context, repeat=repeat)
misc.verbose(B2)
misc.verbose()
timings = A1,A2,B1,B2
return EsA.j_invariant(), timings
def ss_isogeny_limonnitsa(E, l2, e2, P2, Q2, str2, l3, e3, P3, Q3, str3, measure=None):
"""
TODO COMMENTS
"""
misc.verbose('**** KEY EXCHANGE(Limonnitsa) ****')
misc.verbose()
AliceCurveE = E
AliceCurveBase_2 = l2
AliceCurveDeg_2 = e2
AliceCurveBase_3 = l3
AliceCurveDeg_3 = e3
AliceGeneratorP_2 = P2
AliceGeneratorP_3 = P3
AliceGeneratorQ_2 = Q2
AliceGeneratorQ_3 = Q3
BobCurveE = E
BobCurveBase_2 = l2
BobCurveDeg_2 = e2
BobCurveBase_3 = l3
BobCurveDeg_3 = e3
BobGeneratorP_2 = P2
BobGeneratorP_3 = P3
BobGeneratorQ_2 = Q2
BobGeneratorQ_3 = Q3
ID_A = "Alice"
ID_B = "Bob"
h2 = "0"
h3 = "1"
#on Alice side
misc.verbose("Generating Alice's secret key")
sA_1, sA_2 = rand_subgroup(l2, e2)
AliceSecretKey = sA_1 * sA_2
misc.verbose("Generating Alice's Cert")
AliceCurvePhiA, Alice_P, Alice_Q = keygen_c(AliceGeneratorP_2, AliceGeneratorQ_2, 1, AliceSecretKey, AliceCurveBase_2, str2, AliceGeneratorP_3, AliceGeneratorQ_3)
#on Bob side
misc.verbose("Generating Bob's secret key")
sB_1, sB_2 = rand_subgroup(l2, e2)
BobSecretKey = sB_1 * sB_2
misc.verbose("Generating Bob's Cert")
BobCurvePhiB, Bob_P, Bob_Q = keygen_c(BobGeneratorP_2, BobGeneratorQ_2, 1, BobSecretKey, BobCurveBase_2, str2, BobGeneratorP_3, BobGeneratorQ_3)
#beginning Limonnitsa
misc.verbose("Generating random k_A for Alice")
kA_1, kA_2 = rand_subgroup(l3, e3)
AliceK = kA_1 * kA_2
misc.verbose("Generating ephemerial piblic key on Alice side")
AliceCurveK_A, AlicePhi_AB_P2, AlicePhi_AB_Q2 = keygen_c(AliceGeneratorP_3, AliceGeneratorQ_3, 1, AliceK, AliceCurveBase_3, str3, BobGeneratorP_2, BobGeneratorQ_2)
AliceEphemerialString = str(AliceCurveK_A.A) + str(AliceCurveK_A.B) + str(AlicePhi_AB_P2.x) + str(AlicePhi_AB_P2.y) + str(AlicePhi_AB_P2.z) + str(AlicePhi_AB_Q2.x) + str(AlicePhi_AB_Q2.y) + str(AlicePhi_AB_Q2.z)
#send to Bob: ID_A, Cert_A, K_A
#on Bob side
misc.verbose("Generating random k_B for Bob")
kB_1, kB_2 = rand_subgroup(l3, e3)
BobK = kB_1 * kB_2
misc.verbose("Generating ephemerial piblic key on Bob side")
BobCurveK_B, BobPhi_BA_P2, BobPhi_BA_Q2 = keygen_c(BobGeneratorP_3, BobGeneratorQ_3, 1, BobK, BobCurveBase_3, str3, AliceGeneratorP_2, AliceGeneratorQ_2)
BobEphemerialString = str(BobCurveK_B.A) + str(BobCurveK_B.B) + str(BobPhi_BA_P2.x) + str(BobPhi_BA_P2.y) + str(BobPhi_BA_P2.z) + str(BobPhi_BA_Q2.x) + str(BobPhi_BA_Q2.y) + str(BobPhi_BA_Q2.z)
misc.verbose("Construct isogeny with kernel T_AB")
BobPsiE_AB, _, _ = keygen_c(Alice_P, Alice_Q, 1, BobK, BobCurveBase_3, str3)
misc.verbose("Construct isogeny with kernel T_AB'")
BobPsiE_AB_1, _, _ = keygen_c(AlicePhi_AB_P2, AlicePhi_AB_Q2, 1, BobSecretKey, BobCurveBase_2, str2)
str_BobSide = str(BobPsiE_AB.j_invariant()) + str(BobPsiE_AB_1.j_invariant()) + ID_A + ID_B
misc.verbose("KDF function on Bob side")
hash_BobSide = hash512(str_BobSide)
K_B, M_B = hash_BobSide[:len(hash_BobSide)/2], hash_BobSide[len(hash_BobSide)/2:]
misc.verbose("Calculate tag_B")
tag_B = Kuznechik_MAC(M_B, h2 + BobEphemerialString + AliceEphemerialString + ID_B + ID_A)
#send to Alice: ID_B, Cert_B, K_B, tag_b
#on Alice side
misc.verbose("Construct isogeny with kernel T_BA")
AlicePsiE_BA, _, _ = keygen_c(BobPhi_BA_P2, BobPhi_BA_Q2, 1, AliceSecretKey, AliceCurveBase_2, str2)
misc.verbose("Construct isogeny with kernel T_BA'")
AlicePsiE_BA_1, _, _ = keygen_c(Bob_P, Bob_Q, 1, AliceK, AliceCurveBase_3, str3)
str_AliceSide = str(AlicePsiE_BA.j_invariant()) + str(AlicePsiE_BA_1.j_invariant()) + ID_A + ID_B
misc.verbose("KDF function on Alice side")
hash_AliceSide = hash512(str_AliceSide)
K_A, M_A = hash_AliceSide[:len(hash_AliceSide)/2], hash_AliceSide[len(hash_AliceSide)/2:]
misc.verbose("Calculate tag_B'")
tag_B_1 = Kuznechik_MAC(M_A, h2 + BobEphemerialString + AliceEphemerialString + ID_B + ID_A)
if tag_B != tag_B_1:
raise RuntimeError, "ERROR: tag_B not match!"
misc.verbose("Calculate tag_A")
tag_A = Kuznechik_MAC(M_A, h3 + AliceEphemerialString + BobEphemerialString + ID_A + ID_B)
misc.verbose("Calculate tag_A'")
tag_A_1 = Kuznechik_MAC(M_B, h3 + AliceEphemerialString + BobEphemerialString + ID_A + ID_B)
if tag_A != tag_A_1:
raise RuntimeError, "ERROR: tag_A not match!"
misc.verbose("Congratulation! Limonnitsa finish successfully!")
timings = None
if measure is not None and measure:
misc.verbose('**** TIMINGS ****')
misc.verbose("Now we measure the real performances.")
misc.verbose("(This may take some time)")
misc.verbose()
context = globals()
context.update(locals())
if measure is True:
repeat = 3
else:
try:
repeat = int(measure)
except:
repeat = 3
misc.verbose("cert Alice Isogeny")
S1 = sage_timeit('AliceCurvePhiA, Alice_P, Alice_Q = keygen_c(AliceGeneratorP_2, AliceGeneratorQ_2, 1, AliceSecretKey, AliceCurveBase_2, str2, AliceGeneratorP_3, AliceGeneratorQ_3)', context, repeat=repeat)
misc.verbose(S1)
misc.verbose("cert Bob Isogeny")
S2 = sage_timeit('BobCurvePhiB, Bob_P, Bob_Q = keygen_c(BobGeneratorP_2, BobGeneratorQ_2, 1, BobSecretKey, BobCurveBase_2, str2, BobGeneratorP_3, BobGeneratorQ_3)', context, repeat=repeat)
misc.verbose(S2)
misc.verbose("Isogeny 1")
S3 = sage_timeit('AliceCurveK_A, AlicePhi_AB_P2, AlicePhi_AB_Q2 = keygen_c(AliceGeneratorP_3, AliceGeneratorQ_3, 1, AliceK, AliceCurveBase_3, str3, BobGeneratorP_2, BobGeneratorQ_2)', context, repeat=repeat)
misc.verbose(S3)
misc.verbose("Isogeny 2")
S4 = sage_timeit('BobCurveK_B, BobPhi_BA_P2, BobPhi_BA_Q2 = keygen_c(BobGeneratorP_3, BobGeneratorQ_3, 1, BobK, BobCurveBase_3, str3, AliceGeneratorP_2, AliceGeneratorQ_2)', context, repeat=repeat)
misc.verbose(S4)
misc.verbose("Isogeny 3")
S5 = sage_timeit('BobPsiE_AB, _, _ = keygen_c(Alice_P, Alice_Q, 1, BobK, BobCurveBase_3, str3)', context, repeat=repeat)
misc.verbose(S5)
misc.verbose("Isogeny 4")
S6 = sage_timeit('BobPsiE_AB_1, _, _ = keygen_c(AlicePhi_AB_P2, AlicePhi_AB_Q2, 1, BobSecretKey, BobCurveBase_2, str2)', context, repeat=repeat)
misc.verbose(S6)
misc.verbose("Isogeny 5")
S7 = sage_timeit('AlicePsiE_BA, _, _ = keygen_c(BobPhi_BA_P2, BobPhi_BA_Q2, 1, AliceSecretKey, AliceCurveBase_2, str2)', context, repeat=repeat)
misc.verbose(S7)
misc.verbose("Isogeny 6")
S8 = sage_timeit('AlicePsiE_BA_1, _, _ = keygen_c(Bob_P, Bob_Q, 1, AliceK, AliceCurveBase_3, str3)', context, repeat=repeat)
misc.verbose(S8)
misc.verbose("Bob KDF")
B1 = sage_timeit('hash_BobSide = hash512(str_BobSide)', context, repeat=repeat)
misc.verbose(B1)
misc.verbose("Bob tag_B")
B2 = sage_timeit('Kuznechik_MAC(M_B, h2 + BobEphemerialString + AliceEphemerialString + ID_B + ID_A)', context, repeat=repeat)
misc.verbose(B2)
misc.verbose()
misc.verbose("Alice KDF")
A1 = sage_timeit('hash512(str_AliceSide)', context, repeat=repeat)
misc.verbose(A1)
misc.verbose("Alice tag_B'")
A2 = sage_timeit('Kuznechik_MAC(M_A, h2 + BobEphemerialString + AliceEphemerialString + ID_B + ID_A)', context, repeat=repeat)
misc.verbose(A2)
misc.verbose("Alice tag_A")
A3 = sage_timeit('Kuznechik_MAC(M_A, h3 + AliceEphemerialString + BobEphemerialString + ID_A + ID_B)', context, repeat=repeat)
misc.verbose(A3)
misc.verbose("Bob tag_A'")
B3 = sage_timeit('Kuznechik_MAC(M_B, h3 + AliceEphemerialString + BobEphemerialString + ID_A + ID_B)', context, repeat=repeat)
misc.verbose(B3)
misc.verbose()
timings = B1, B2, A1, A2, A3, B3, S1, S2, S3,S4, S5, S6, S7, S8
return timings
def hash512(data):
return gost34112012512.new(data).hexdigest()
def Kuznechik_MAC(key, data):
ciph = GOST3412Kuznechik(hexdec(key))
return hexenc(mac(ciph.encrypt, 16, data))