-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecc.py
More file actions
executable file
·89 lines (74 loc) · 2.51 KB
/
ecc.py
File metadata and controls
executable file
·89 lines (74 loc) · 2.51 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
#!/usr/local/bin/python3
""" Module for chapter 2 of Programming Bitcoin
Usage:
python3 ecc.py
"""
class Point:
def __init__(self, x, y, a, b):
self.a = a
self.b = b
self.x = x
self.y = y
# end::source1[]
# tag::source2[]
if self.x is None and self.y is None: # <1>
return
# end::source2[]
# tag::source1[]
if self.y**2 != self.x**3 + a * x + b: # <1>
raise ValueError('({}, {}) is not on the curve'.format(x, y))
def __eq__(self, other): # <2>
return self.x == other.x and self.y == other.y \
and self.a == other.a and self.b == other.b
# end::source1[]
def __ne__(self, other):
# this should be the inverse of the == operator
return self.x != other.x or self.y != other.y \
or self.a != other.a or self.b != other.b
def __repr__(self):
if self.x is None:
return 'Point(infinity)'
else:
return 'Point({},{})_{}_{}'.format(self.x, self.y, self.a, self.b)
# tag::source3[]
def __add__(self, other): # <2>
if self.a != other.a or self.b != other.b:
raise TypeError('Points {}, {} are not on the same curve'.format
(self, other))
if self.x is None: # <3>
return other
if other.x is None: # <4>
return self
# vertical line (additive inverse)
if self.x == other.x and self.y != other.y:
return self.__class__(None, None, self.a, self.b)
# find the slope of the line
if self.x != other.x:
s = (other.y - self.y) / (other.x - self.x)
x = s**2 - self.x - other.x
y = s * (self.x - x) - self.y
return self.__class__(x, y, self.a, self.b)
if self == other and self.y == 0 * self.x:
return self.__class__(None, None, self.a, self.b)
if self == other:
s = (3 * self.x**2 + self.a) / (2 * self.y)
x = s**2 - 2 * self.x
y = s * (self.x - x) - self.y
return self.__class__(x, y, self.a, self.b)
def test_online(x, y):
""" Test if point is on the curve
Args: x, y: the point coordinates
Return: true or false
"""
print ({x, y}," ", y**2 == x**3 +7)
test_online(2,4)
test_online(-1,-1)
test_online(0,11)
test_online(5,7)
# test_online(2,2)
p1 = Point(-1, -1, 5, 7)
p2 = Point(-1, 1, 5, 7)
inf = Point(None, None, 5, 7)
print(p1 + inf)
print(inf + p2)
print(p1 + p2)