-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlibrary.m
More file actions
342 lines (314 loc) · 7.38 KB
/
library.m
File metadata and controls
342 lines (314 loc) · 7.38 KB
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
/*!
* Find a usable SIDH prime.
*/
FindPrime := function(NumBits)
TwoPow := Ceiling(NumBits/2);
ThreePow := Ceiling(NumBits/Log(2,3));
PrimeList := PrimesUpTo(500);
for ind in [1..5] do
for f in PrimeList do
p := Power(2, TwoPow)*Power(3, ThreePow)*f - 1;
if IsPrime(p) then
return p;
end if;
end for;
TwoPow +:= 1;
end for;
return 0;
end function;
/*!
* Generate a random supersingular elliptic curve over L.
*/
Random_SS_Curve := function(L)
E := EllipticCurveWithjInvariant(L!1728);
if not IsSupersingular(E) then
E := EllipticCurveWithjInvariant(L!0);
end if;
if not IsSupersingular(E) then
"Failed to generate supersingular curve!";
return 0;
end if;
NumIter := Ceiling(Log(2, Characteristic(L)));
_<x> := PolynomialRing(L);
for i in [0..NumIter] do
S := DivisionPoints(E!0, 2)[Random([2,3,4])];
ker := (x-S[1]);
E, _ := IsogenyFromKernel(E, ker);
end for;
return E;
end function;
/*!
* Generate SIDH generators.
*/
GetSIDHGenerators := function(J : ComputeBPts:=true)
p := Characteristic(BaseRing(J));
Ord_fact := Factorisation(p+1);
pA := Ord_fact[1, 1];
eA := Ord_fact[1, 2];
pB := Ord_fact[2, 1];
eB := Ord_fact[2, 2];
AOrd := Power(pA, eA);
BOrd := Power(pB, eB);
// Generating points PA, QA
ATor := DivisionPoints(J!0, AOrd);
for P in ATor do
if Order(P) ne AOrd then continue; end if;
for Q in ATor do
if Order(Q) ne AOrd then continue; end if;
if not IsLinearlyIndependent(P, Q, AOrd) then continue; end if;
A_Pts := [P, Q];
break P;
end for;
end for;
// Generating points PB, QB
if ComputeBPts then
BTor := DivisionPoints(J!0, BOrd);
for P in BTor do
if Order(P) ne BOrd then continue; end if;
for Q in BTor do
if Order(Q) ne BOrd then continue; end if;
if not IsLinearlyIndependent(P, Q, BOrd) then continue; end if;
B_Pts := [P, Q];
break P;
end for;
end for;
else
B_Pts := [];
end if;
return A_Pts, B_Pts;
end function;
/*!
* Get canonical supersingular representative.
*/
GetSSRepFromjInv := function(jInv)
p := Characteristic(Parent(jInv));
E := EllipticCurveFromjInvariant(jInv);
P := [Random(E) : ind in [1..10]];
if &and[(p+1)*Pind eq E!0 : Pind in P] then
return E;
else
ind := 1;
alp := PrimitiveElement(BaseRing(E));
repeat
ind +:= 1;
Et := QuadraticTwist(E, alp^ind);
P := [ Random(Et) : jnd in [1..10] ];
until &and[ (p + 1)*Pind eq Et!0 : Pind in P ];
return Et;
end if;
end function;
/*!
* Compute 'small' isogeny.
*/
SmallIsogeny := function(P, deg)
E := Curve(P);
if deg eq 1 then
return E;
elif deg*P ne E!0 then
return 0;
end if;
K := BaseRing(E);
_<x> := PolynomialRing(K);
C, phi := IsogenyFromKernel(E, &*[ x - (ind*P)[1] : ind in [1..deg-1] ]);
//We would like to ensure that the the Codomain is the EXACT curve we want to work in.
Cj := GetSSRepFromjInv(jInvariant(C));
if IsIsomorphic(C, Cj) then
_, theta := IsIsomorphic(C, Cj);
phi := phi*theta;
return Cj, phi;
else
"WARNING: [SmallIsogeny] Cannot make codomain the canonical one.";
return C, phi;
end if;
end function;
/*!
* Compute 'long' isogeny.
*/
LongIsogeny := function(P, deg)
pA := Factorisation(deg)[1,1];
eA := Factorisation(deg)[1,2];
phis := [**];
for ind in [1..eA] do
C, phi := SmallIsogeny(pA^(eA-ind)*P, pA);
P := phi(P);
Append(~phis, phi);
end for;
return C, phis;
end function;
/*!
* Evaluate 'long' isogeny.
*/
EvaluateLongIsogeny := function(phis, P)
for phi in phis do
P := phi(P);
end for;
return P;
end function;
/*!
* Converts a p-ary representation to a decimal.
*/
pListToDec := function(a, p)
val := 0;
for ind in [1..#a] do
val +:= Power(p, ind - 1)*a[ind];
end for;
return val;
end function;
/*!
* Find all 2-neighbours.
*/
TwoNeighbours := function(jInv)
E := GetSSRepFromjInv(jInv);
TorsPts := DivisionPoints(E!0, 2);
Exclude(~TorsPts, E!0);
res := [];
for P in TorsPts do
N, _ := SmallIsogeny(P, 2);
Append(~res, jInvariant(N));
end for;
return res;
end function;
/*!
* Find all 4-neighbours.
*/
FourNeighbours := function(jInv)
E := GetSSRepFromjInv(jInv);
res := [];
OneNbh := TwoNeighbours(jInv);
for jind in OneNbh do
t := TwoNeighbours(jind);
Exclude(~t, jInv);
res cat:= t;
end for;
return res;
end function;
/*!
* Find all (2^n)-neighbours.
*/
NthNeighbours := function(jInv, n)
if n eq 1 then
return TwoNeighbours(jInv);
elif n eq 2 then
return FourNeighbours(jInv);
end if;
PreviousLayer := [jInv];
CurrentLayer := [jInv];
NextLayer := [];
for ind in [1..n] do
for jnd in [1..#CurrentLayer] do
NextLayer cat:= TwoNeighbours(CurrentLayer[jnd]);
end for;
for jnd in [1..#PreviousLayer] do
while PreviousLayer[jnd] in NextLayer do
Exclude(~NextLayer, PreviousLayer[jnd]);
end while;
end for;
PreviousLayer := CurrentLayer;
CurrentLayer := NextLayer;
NextLayer := [];
end for;
return CurrentLayer;
end function;
/*!
* Get all common (2^n)-neighbours
*/
GetCommonNeighbour := function(j1, j2, n)
if n eq 1 then
Nbh1 := TwoNeighbours(j1);
Nbh2 := TwoNeighbours(j2);
for jind in Nbh1 do
for jjnd in Nbh2 do
if jind eq jjnd then
return jind;
end if;
end for;
end for;
elif n eq 2 then
Nbh1 := FourNeighbours(j1);
Nbh2 := FourNeighbours(j2);
for jind in Nbh1 do
for jjnd in Nbh2 do
if jind eq jjnd then
return jind;
end if;
end for;
end for;
elif n ge 3 then
Nbh1 := NthNeighbours(j1, n);
Nbh2 := NthNeighbours(j2, n);
for jind in Nbh1 do
for jjnd in Nbh2 do
if jind eq jjnd then
return jind;
end if;
end for;
end for;
end if;
return 0;
end function;
/*!
* Finds kernel for the 2-isogeny E1 --> E2.
*/
GetConnectingKernels := function(E1, jE2)
E1_2Tor := DivisionPoints(E1!0, 2);
Exclude(~E1_2Tor, E1!0);
for ind in [1..3] do
CandidatejE2 := jInvariant(SmallIsogeny(E1_2Tor[ind], 2));
if CandidatejE2 eq jE2 then
return E1_2Tor[ind];
end if;
end for;
return 0;
end function;
/*!
* Given a j-invariant and all-but-one of its neighbours, return the final neighbour
*/
GetLastNeighbour := function(j, AlmostAllNbh)
Nbh := TwoNeighbours(j);
Exclude(~Nbh, AlmostAllNbh[1]);
Exclude(~Nbh, AlmostAllNbh[2]);
return Nbh[1];
end function;
/*!
* Given a 2-isogeny phi: E1 --> E2, and a point P in E2,
* we find the entire preimage of P under phi.
*/
Preimage := function(phi, P)
E2 := Curve(P);
j1 := jInvariant(Domain(phi));
DualphiKernel := GetConnectingKernels(E2, j1);
ED,Dualphi := SmallIsogeny(DualphiKernel, 2);
Candidates := DivisionPoints(Dualphi(P), 2);
// Candidates are all on the canonical curve ED, not exactly E1 = Domain(phi)
_,theta := IsIsomorphic(Curve(Candidates[1]), Domain(phi));
res := [];
for Q in Candidates do
tQ := theta(Q);
if phi(tQ) eq -P then
Append(~res, -tQ);
elif phi(tQ) eq P then
Append(~res, tQ);
end if;
end for;
return res;
end function;
/*!
* Compute the isogeny phi : E2 --> E1, given the kernel of the dual (P in E1).
*/
deg2_isogeny_from_dual_ker := function(P)
E1 := Curve(P);
E2, dual_phi := SmallIsogeny(P, 2);
ker := GetConnectingKernels(E2, jInvariant(E1));
_, phi := SmallIsogeny(ker, 2);
return phi;
end function;
/*!
* Calculate a hash of 2 messages, where the messages cannot commute and "order" specifies the ordering of the two messages
*/
positional_hash := function (m1, m2 : order:=1)
if order eq 1 then
return Hash(ElementToSequence(m1) cat ElementToSequence(m2));
else
return Hash(ElementToSequence(m2) cat ElementToSequence(m1));
end if;
end function;