-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.py
160 lines (134 loc) · 4.39 KB
/
point.py
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
from utils import modinv
import utils
class Curve:
pass
class Point:
def __init__(self, xx=0, yy=0, zz=0):
self.x = xx
self.y = yy
if((xx != 0 or yy != 0) and zz == 0):
self.z = 1
else:
self.z = zz
def __eq__(self, other):
return (self.x == other.x and self.y == other.y and self.z == other.z)
def __ne__(self, other):
return not self.__eq__(other)
# Point addition, does p1 + p2 on the curve c, points already converted to montgomery representation
def add_montgomery(p1, p2, c):
inf = Point()
if(p1 == inf):
return p2
if(p2 == inf):
return p1
bm = utils.montgomery
am = utils.add_mod
sm = utils.sub_mod
r, r2, p = c.r, c.r2, c.p
x1, y1, z1 = p1.x, p1.y, p1.z
x2, y2, z2 = p2.x, p2.y, p2.z
p3 = Point()
z12 = bm(z1, z1, c)
z22 = bm(z2, z2, c)
u1 = bm(x1, z22, c)
u2 = bm(x2, z12, c)
s1 = bm(y1, bm(z22, z2, c), c)
s2 = bm(y2, bm(z12, z1, c), c)
if(u1 == u2):
if(s1 != s2):
return p3
else: # point double
if(p1.y == 0):
return p3
s = bm(x1, bm(y1, y1, c), c)
s = bm(bm(4, r2, c), s, c)
m = bm(bm(3, r2, c), bm(x1, x1, c), c)
m = am(m, bm(c.a, bm(z12, z12, c), c), p)
p3.x = sm(bm(m, m, c), am(s, s, c.pt), p)
y12 = bm(y1, y1, c)
y14 = bm(y12, y12, c)
p3.y = bm(m, sm(s, p3.x, p), c)
p3.y = sm(p3.y, bm(bm(8, r2, c), y14, c), p)
p3.z = am(bm(y1, z1, c), bm(y1, z1, c), p)
return p3
h = sm(u2, u1, p)
s = sm(s2, s1, p)
p3.x = sm(bm(s, s, c), bm(h, bm(h, h, c), c), p)
tmp = bm(bm(2, r2, c), bm(u1, bm(h, h, c), c), c)
p3.x = sm(p3.x, tmp, p)
tmp = sm(bm(u1, bm(h, h, c), c), p3.x, p)
tmp = bm(s, tmp, c)
p3.y = sm(tmp, bm(s1, bm(h, bm(h, h, c), c), c), p)
p3.z = bm(h, bm(z1, z2, c), c)
return p3
# Does point multiplication p*r on the curve c, both p and c are
# already converted to montgomery representation
# Returns the value in Jacobian coordinates and montgomery representation
def multiply(p, r, c):
P = Point(p.x, p.y, p.z)
pr = Point()
while(r > 0):
if(r & 1):
pr = add(pr, P, c)
r = (r >> 1)
P = add(P, P, c)
return pr
# Point addition, does p1 + p2 on the curve c, points already converted to montgomery representation
def add(p1, p2, c):
inf = Point()
if(p1 == inf):
return p2
if(p2 == inf):
return p1
bm = utils.binary_montgomery
am = utils.add_mod
sm = utils.sub_mod
r, r2, p = c.r, c.r2, c.p
x1, y1, z1 = p1.x, p1.y, p1.z
x2, y2, z2 = p2.x, p2.y, p2.z
p3 = Point()
z12 = bm(z1, z1, p, r)
z22 = bm(z2, z2, p, r)
u1 = bm(x1, z22, p, r)
u2 = bm(x2, z12, p, r)
s1 = bm(y1, bm(z22, z2, p, r), p, r)
s2 = bm(y2, bm(z12, z1, p, r), p, r)
if(u1 == u2):
if(s1 != s2):
return p3
else: # point double
if(p1.y == 0):
return p3
s = bm(x1, bm(y1, y1, p, r), p, r)
s = bm(bm(4, r2, p, r), s, p, r)
m = bm(bm(3, r2, p, r), bm(x1, x1, p, r), p, r)
m = am(m, bm(c.a, bm(z12, z12, p, r), p, r), p)
p3.x = sm(bm(m, m, p, r), am(s, s, p), p)
y12 = bm(y1, y1, p, r)
y14 = bm(y12, y12, p, r)
p3.y = bm(m, sm(s, p3.x, p), p, r)
p3.y = sm(p3.y, bm(bm(8, r2, p, r), y14, p, r), p)
p3.z = am(bm(y1, z1, p, r), bm(y1, z1, p, r), p)
return p3
h = sm(u2, u1, p)
s = sm(s2, s1, p)
p3.x = sm(bm(s, s, p, r), bm(h, bm(h, h, p, r), p, r), p)
tmp = bm(bm(2, r2, p, r), bm(u1, bm(h, h, p, r), p, r), p, r)
p3.x = sm(p3.x, tmp, p)
tmp = sm(bm(u1, bm(h, h, p, r), p, r), p3.x, p)
tmp = bm(s, tmp, p, r)
p3.y = sm(tmp, bm(s1, bm(h, bm(h, h, p, r), p, r), p, r), p)
p3.z = bm(h, bm(z1, z2, p, r), p, r)
return p3
# Does point multiplication p*r on the curve c, both p and c are
# already converted to montgomery representation
# Returns the value in Jacobian coordinates and montgomery representation
def multiply_montgomery(p, r, c):
P = Point(p.x, p.y, p.z)
pr = Point()
while(r > 0):
if(r & 1):
pr = add_montgomery(pr, P, c)
r = (r >> 1)
P = add_montgomery(P, P, c)
return pr