@@ -6,7 +6,7 @@ struct EllipticCurve
6
6
end
7
7
8
8
9
- mutable struct EllipticPoint
9
+ struct EllipticPoint
10
10
x:: BigInt
11
11
y:: BigInt
12
12
end
@@ -17,17 +17,17 @@ function isInfinityPoint(P::EllipticPoint)
17
17
end
18
18
19
19
20
- function double (P:: EllipticPoint , E:: EllipticCurve )
20
+ function double (P:: EllipticPoint , E:: EllipticCurve ):: EllipticPoint
21
21
if isInfinityPoint (P)
22
22
return P
23
23
end
24
24
25
25
g:: BigInt , inv:: BigInt , _ = gcdx (2 * P. y, E. n)
26
- if g != 1
27
- return g
28
- elseif g == E. n
29
- return InfinityPoint ()
30
- end
26
+ # if g != 1
27
+ # return g
28
+ # elseif g == E.n
29
+ # return InfinityPoint()
30
+ # end
31
31
32
32
lambda:: BigInt = mod ((3 * P. x^ 2 + E. a) * inv, E. n)
33
33
new_x:: BigInt = mod (lambda^ 2 - 2 * P. x, E. n)
@@ -37,7 +37,7 @@ function double(P::EllipticPoint, E::EllipticCurve)
37
37
end
38
38
39
39
40
- function add (P:: EllipticPoint , Q:: EllipticPoint , E:: EllipticCurve )
40
+ function add (P:: EllipticPoint , Q:: EllipticPoint , E:: EllipticCurve ):: EllipticPoint
41
41
if isInfinityPoint (P)
42
42
return Q
43
43
elseif isInfinityPoint (Q)
@@ -48,18 +48,18 @@ function add(P::EllipticPoint, Q::EllipticPoint, E::EllipticCurve)
48
48
return double (P, E)
49
49
end
50
50
51
- delta_x = mod (Q. x - P. x, E. n)
52
- g , inv, _ = gcdx (delta_x, E. n)
53
- if 1 < g < E. n
54
- return g
55
- elseif g == E. n
56
- return InfinityPoint ()
57
- end
51
+ @time delta_x = mod (Q. x - P. x, E. n)
52
+ @time g :: BigInt , inv:: BigInt , _ = gcdx (delta_x, E. n)
53
+ # if 1 < g < E.n
54
+ # return g
55
+ # elseif g == E.n
56
+ # return InfinityPoint()
57
+ # end
58
58
59
- delta_y = mod (Q. y - P. y, E. n)
60
- lambda = mod (delta_y * inv, E. n)
61
- new_x = mod (lambda^ 2 - P. x - Q. x, E. n)
62
- new_y = mod (lambda * (P. x - new_x) - P. y, E. n)
59
+ @time delta_y = mod (Q. y - P. y, E. n)
60
+ @time lambda = mod (delta_y * inv, E. n)
61
+ @time new_x = mod (lambda^ 2 - P. x - Q. x, E. n)
62
+ @time new_y = mod (lambda * (P. x - new_x) - P. y, E. n)
63
63
64
64
return EllipticPoint (new_x, new_y)
65
65
end
@@ -71,19 +71,19 @@ function InfinityPoint()
71
71
end
72
72
73
73
74
- function multiply (k:: BigInt , P:: EllipticPoint , E:: EllipticCurve )
74
+ function multiply (k:: BigInt , P:: EllipticPoint , E:: EllipticCurve ):: EllipticPoint
75
75
Q = InfinityPoint ()
76
76
77
- for b in string (k, base = 2 )
77
+ @inbounds for b in string (k, base = 2 )
78
78
Q = double (Q, E)
79
- if typeof (Q) != EllipticPoint
80
- return Q
81
- end
79
+ # if typeof(Q) != EllipticPoint
80
+ # return Q
81
+ # end
82
82
if b == ' 1'
83
83
Q = add (Q, P, E)
84
- if typeof (Q) != EllipticPoint
85
- return Q
86
- end
84
+ # if typeof(Q) != EllipticPoint
85
+ # return Q
86
+ # end
87
87
end
88
88
end
89
89
return Q
@@ -101,9 +101,12 @@ function test_add()
101
101
gy = big " 36134250956749795798585127919587881956611106672985015071877198253568414405109"
102
102
P = EllipticPoint (gx, gy)
103
103
104
- for i= 1 : 100000
105
- P = add (P, P, E)
106
- end
104
+ P = add (P, P, E)
105
+ P = add (P, P, E)
106
+ P = add (P, P, E)
107
+ # for i=1:100000
108
+ # P = add(P, P, E)
109
+ # end
107
110
end
108
111
109
112
128
131
# 21.950572 seconds (112.61 M allocations: 2.961 GiB, 12.85% gc time)
129
132
130
133
@time test_add ()
131
- @time test_mult ()
134
+ # @time test_mult()
0 commit comments