-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsecp256k1.py
172 lines (132 loc) · 4.23 KB
/
secp256k1.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
161
162
163
164
165
166
167
168
169
170
171
172
class Fp:
# Galois field. In mathematics, a finite field or Galois field is a field that contains a finite number of elements.
# As with any field, a finite field is a set on which the operations of multiplication, addition, subtraction and
# division are defined and satisfy certain basic rules.
#
# https://www.cs.miami.edu/home/burt/learning/Csc609.142/ecdsa-cert.pdf
# Don Johnson, Alfred Menezes and Scott Vanstone, The Elliptic Curve Digital Signature Algorithm (ECDSA)
# 3.1 The Finite Field Fp
p = 0
def __init__(self, x):
self.x = x % self.p
def __repr__(self):
return f'Fp(0x{self.x:064x})'
def __eq__(self, data):
assert self.p == data.p
return self.x == data.x
def __add__(self, data):
assert self.p == data.p
return self.__class__((self.x + data.x) % self.p)
def __sub__(self, data):
assert self.p == data.p
return self.__class__((self.x - data.x) % self.p)
def __mul__(self, data):
assert self.p == data.p
return self.__class__((self.x * data.x) % self.p)
def __truediv__(self, data):
return self * data ** -1
def __pow__(self, data):
return self.__class__(pow(self.x, data, self.p))
def __pos__(self):
return self
def __neg__(self):
return self.__class__(self.p - self.x)
@classmethod
def nil(cls):
return cls(0)
@classmethod
def one(cls):
return cls(1)
if __name__ == '__main__':
Fp.p = 23
assert Fp(12) + Fp(20) == Fp(9)
assert Fp(8) * Fp(9) == Fp(3)
assert Fp(8) ** -1 == Fp(3)
Fp.p = 0
# Prime of finite field.
P = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
# The order n of G.
N = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
class Fq(Fp):
p = P
def __repr__(self):
return f'Fq(0x{self.x:064x})'
class Fr(Fp):
p = N
def __repr__(self):
return f'Fr(0x{self.x:064x})'
A = Fq(0)
B = Fq(7)
class Pt:
def __init__(self, x, y):
if x != Fq(0) or y != Fq(0):
assert y ** 2 == x ** 3 + A * x + B
self.x = x
self.y = y
def __repr__(self):
return f'Pt({self.x}, {self.y})'
def __eq__(self, data):
return self.x == data.x and self.y == data.y
def __add__(self, data):
# https://www.cs.miami.edu/home/burt/learning/Csc609.142/ecdsa-cert.pdf
# Don Johnson, Alfred Menezes and Scott Vanstone, The Elliptic Curve Digital Signature Algorithm (ECDSA)
# 4.1 Elliptic Curves Over Fp
x1, x2 = self.x, data.x
y1, y2 = self.y, data.y
if x1 == Fq(0) and y1 == Fq(0):
return data
if x2 == Fq(0) and y2 == Fq(0):
return self
if x1 == x2 and y1 == +y2:
sk = (x1 * x1 + x1 * x1 + x1 * x1 + A) / (y1 + y1)
x3 = sk * sk - x1 - x2
y3 = sk * (x1 - x3) - y1
return Pt(x3, y3)
if x1 == x2 and y1 == -y2:
return I
sk = (y2 - y1) / (x2 - x1)
x3 = sk * sk - x1 - x2
y3 = sk * (x1 - x3) - y1
return Pt(x3, y3)
def __sub__(self, data):
return self + data.__neg__()
def __mul__(self, k):
# Point multiplication: Double-and-add
# https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication
n = k.x
result = I
addend = self
while n:
b = n & 1
if b == 1:
result += addend
addend = addend + addend
n = n >> 1
return result
def __truediv__(self, k):
return self.__mul__(k ** -1)
def __pos__(self):
return self
def __neg__(self):
return Pt(self.x, -self.y)
# Identity element
I = Pt(
Fq(0),
Fq(0),
)
# Generator point
G = Pt(
Fq(0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798),
Fq(0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8),
)
if __name__ == '__main__':
p = G * Fr(42)
q = G * Fr(24)
r = Pt(p.x, -p.y)
assert p + q == G * Fr(66)
assert p + p == G * Fr(84)
assert p - q == G * Fr(18)
assert r == -p
assert p + r == I
assert p + I == p
assert p * Fr(42) == G * Fr(1764)