Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
tests.py
Go to the documentation of this file.
1import sys
2import time
3import functools
4from numpy import base_repr
5
6# SageMath imports
7from sage.all import (
8 log,
9 randrange,
10 GF,
11 QQ,
12 EllipticCurve,
13 set_verbose,
14)
15
16set_verbose(-1)
17
18from setup import arguments, setup_fp, setup_fq, ProgressBar
19from random_walks2 import cgl_hash2, to_mont
20from random_walks3 import cgl_hash3, get_order3_points, to_model, isogeny3
21from basefield_chain3 import radical_walk3
22
23runs = 25
24if __name__ == '__main__':
25 # main()
26 setup = setup_fq(arguments(sys.argv[1:]).bits)
27 Fq = setup['Fq']
28 p = Fq.characteristic()
29 A = Fq(6)
30
31 setup['j₋₁'] = EllipticCurve([0, A + 6, 0, 4 * (2 + A), 0]).j_invariant()
32 setup['j₀'] = EllipticCurve([0, A, 0, 1, 0]).j_invariant()
33
34 print('\033[93m#################################################### CGLHash2 with starting curve E₆ : y² = x³ + 6x² + x\033[00m\033[00m')
35 elapsed_time = 0
36 for tries in range(0, runs, 1):
37 ProgressBar(tries + 1, runs, f"Testing CGLHash2")
38 hash_input = hex(randrange(0, p))[2:].encode()
39 start_time = time.perf_counter()
40 j = cgl_hash2(hash_input, setup, verbose=False)
41 end_time = time.perf_counter()
42 elapsed_time += (end_time - start_time)
43
44 print(f'\nAverage time (CGLHash2): {elapsed_time / runs}\n')
45
46 print('\033[93m#################################################### CGLHash2 with random starting curve E : y² = x³ + Ax² + x\033[00m\033[00m')
47 elapsed_time = 0
48 for tries in range(0, runs, 1):
49 # Below three lines updates to use different curve at run
50 A = to_mont(j, setup)
51 setup['j₋₁'] = EllipticCurve([0, A + 6, 0, 4 * (2 + A), 0]).j_invariant()
52 setup['j₀'] = EllipticCurve([0, A, 0, 1, 0]).j_invariant()
53
54 ProgressBar(tries + 1, runs, f"Testing CGLHash2")
55 hash_input = hex(randrange(0, p))[2:].encode()
56 start_time = time.perf_counter()
57 j = cgl_hash2(hash_input, setup, verbose=False)
58 end_time = time.perf_counter()
59 elapsed_time += (end_time - start_time)
60
61 print(f'\nAverage time (CGLHash2): {elapsed_time / runs}\n')
62
63
64 print('\033[93m#################################################### CGLHash3 with starting curve E₆ : y² = x³ + 6x² + x\033[00m\033[00m')
65
66 A = Fq(6)
67 def test_3div_poly(a1, a3):
68 j = EllipticCurve(Fq, [a1, 0, a3, 0, 0]).j_invariant()
69 F_ = EllipticCurve(Fq, [a1, 0, a3, -5 * a1 * a3, -a1**3 * a3 - 7 * a3**2])
70 j_ = F_.j_invariant()
71 assert(j != j_)
72
73 alpha = setup['curt'](-a3)
74 assert alpha**3 == -a3
75 a1_, a3_ = isogeny3(alpha, a1, a3)
76 j0 = EllipticCurve(Fq, [a1_, 0, a3_, -5 * a1_ * a3_, -a1_**3 * a3_ - 7 * a3_**2]).j_invariant()
77
78 alpha *= setup['ζ₃']
79 assert alpha**3 == -a3
80 a1_, a3_ = isogeny3(alpha, a1, a3)
81 j1 = EllipticCurve(Fq, [a1_, 0, a3_, -5 * a1_ * a3_, -a1_**3 * a3_ - 7 * a3_**2]).j_invariant()
82
83 alpha *= setup['ζ₃']
84 assert alpha**3 == -a3
85 a1_, a3_ = isogeny3(alpha, a1, a3)
86 j2 = EllipticCurve(Fq, [a1_, 0, a3_, -5 * a1_ * a3_, -a1_**3 * a3_ - 7 * a3_**2]).j_invariant()
87
88 # Checking we have three non-isomorphic 3-isogenous curves
89 assert(j0 != j1)
90 assert(j0 != j2)
91 assert(j1 != j2)
92 # Checking non-backtracking
93 assert(j0 != j_)
94 assert(j1 != j_)
95 assert(j2 != j_)
96 # Checking non-backtracking
97 assert(j0 != j)
98 assert(j1 != j)
99 assert(j2 != j)
100
101 xs = get_order3_points(A, setup)
102 for xk in xs:
103 yk = setup['sqrt'](xk**3 + A * xk**2 + xk)
104 a1, a3 = to_model(A, xk)
105 test_3div_poly(a1, a3)
106
107 elapsed_time = 0
108 setup['A'] = A
109 for tries in range(0, runs, 1):
110 ProgressBar(tries + 1, runs, f"Testing CGLHash3")
111 hash_input = hex(randrange(0, p))[2:].encode()
112 start_time = time.perf_counter()
113 j = cgl_hash3(hash_input, setup, verbose=False)
114 end_time = time.perf_counter()
115 elapsed_time += (end_time - start_time)
116
117 print(f'\nAverage time (CGLHash3): {elapsed_time / runs}\n')
118
119 print('\033[93m#################################################### CGLHash3 with random starting curve E : y² = x³ + Ax² + x\033[00m\033[00m')
120 elapsed_time = 0
121 for tries in range(0, runs, 1):
122 # Below three lines updates to use different curve at run
123 A = to_mont(j, setup)
124 setup['A'] = A
125
126 # Test
127 xs = get_order3_points(A, setup)
128 for xk in xs:
129 yk = setup['sqrt'](xk**3 + A * xk**2 + xk)
130 a1, a3 = to_model(A, xk)
131 test_3div_poly(a1, a3)
132
133 ProgressBar(tries + 1, runs, f"Testing CGLHash3")
134 hash_input = hex(randrange(0, p))[2:].encode()
135 start_time = time.perf_counter()
136 j = cgl_hash3(hash_input, setup, verbose=False)
137 end_time = time.perf_counter()
138 elapsed_time += (end_time - start_time)
139
140 print(f'\nAverage time (CGLHash3): {elapsed_time / runs}\n')
141
142
143 setup = setup_fp(arguments(sys.argv[1:]).bits)
144 Fp = setup['Fp']
145 p = Fp.characteristic()
146 A = Fp(6)
147 print('\033[93m#################################################### Radical 3-isogeny chain with starting curve E₆ : y² = x³ + 6x² + x\033[00m\033[00m')
148 for value in range(0, 128, 1):
149 ProgressBar(value + 1, 128, f"Testing (+) Radical 3-isogeny over GF(p)")
150 A = radical_walk3(A, value, setup)
151 print('')
152 for value in range(0, 128, 1):
153 ProgressBar(value + 1, 128, f"Testing (-) Radical 3-isogeny over GF(p)")
154 A = radical_walk3(A, -value, setup)
155 print('')
assert(var1 eq var2)
test_3div_poly(a1, a3)
Definition tests.py:67
Fq
Definition tests.py:27