-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathecc_test.py
352 lines (312 loc) · 13.7 KB
/
ecc_test.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from binascii import unhexlify
from random import randint
from unittest import TestCase
from ecc import (
FieldElement,
Point,
PrivateKey,
S256Point,
Signature,
G,
N,
)
class FieldElementTest(TestCase):
def test_add(self):
a = FieldElement(2, 31)
b = FieldElement(15, 31)
self.assertEqual(a+b, FieldElement(17, 31))
a = FieldElement(17, 31)
b = FieldElement(21, 31)
self.assertEqual(a+b, FieldElement(7, 31))
def test_sub(self):
a = FieldElement(29, 31)
b = FieldElement(4, 31)
self.assertEqual(a-b, FieldElement(25, 31))
a = FieldElement(15, 31)
b = FieldElement(30, 31)
self.assertEqual(a-b, FieldElement(16, 31))
def test_mul(self):
a = FieldElement(24, 31)
b = FieldElement(19, 31)
self.assertEqual(a*b, FieldElement(22, 31))
def test_rmul(self):
a = FieldElement(24, 31)
b = 2
self.assertEqual(b*a, a+a)
def test_pow(self):
a = FieldElement(17, 31)
self.assertEqual(a**3, FieldElement(15, 31))
a = FieldElement(5, 31)
b = FieldElement(18, 31)
self.assertEqual(a**5 * b, FieldElement(16, 31))
def test_div(self):
a = FieldElement(3, 31)
b = FieldElement(24, 31)
self.assertEqual(a/b, FieldElement(4, 31))
a = FieldElement(17, 31)
self.assertEqual(a**-3, FieldElement(29, 31))
a = FieldElement(4, 31)
b = FieldElement(11, 31)
self.assertEqual(a**-4*b, FieldElement(13, 31))
class PointTest(TestCase):
def test_on_curve(self):
with self.assertRaises(RuntimeError):
Point(x=-2, y=4, a=5, b=7)
# these should not raise an error
Point(x=3, y=-7, a=5, b=7)
Point(x=18, y=77, a=5, b=7)
def test_add0(self):
a = Point(x=None, y=None, a=5, b=7)
b = Point(x=2, y=5, a=5, b=7)
c = Point(x=2, y=-5, a=5, b=7)
self.assertEqual(a+b, b)
self.assertEqual(b+a, b)
self.assertEqual(b+c, a)
def test_add1(self):
a = Point(x=3, y=7, a=5, b=7)
b = Point(x=-1, y=-1, a=5, b=7)
self.assertEqual(a+b, Point(x=2, y=-5, a=5, b=7))
def test_add2(self):
a = Point(x=-1, y=1, a=5, b=7)
self.assertEqual(a+a, Point(x=18, y=-77, a=5, b=7))
class ECCTest(TestCase):
def test_on_curve(self):
# tests the following points whether they are on the curve or not
# on curve y^2=x^3-7 over F_223:
# (192,105) (17,56) (200,119) (1,193) (42,99)
# the ones that aren't should raise a RuntimeError
prime = 223
a = FieldElement(0, prime)
b = FieldElement(7, prime)
valid_points = ((192, 105), (17, 56), (1, 193))
invalid_points = ((200, 119), (42, 99))
# iterate over valid points
for x_raw, y_raw in valid_points:
# Initialize points this way:
# x = FieldElement(192, prime)
# y = FieldElement(105, prime)
# Point(x, y, a, b)
x = FieldElement(x_raw, prime)
y = FieldElement(y_raw, prime)
# Creating the point should not result in an error
Point(x, y, a, b)
# iterate over invalid points
for x_raw, y_raw in invalid_points:
# Initialize points this way:
# x = FieldElement(192, prime)
# y = FieldElement(105, prime)
# Point(x, y, a, b)
x = FieldElement(x_raw, prime)
y = FieldElement(y_raw, prime)
# check that creating the point results in a RuntimeError
# with self.assertRaises(RuntimeError):
# Point(x, y, a, b)
with self.assertRaises(RuntimeError):
Point(x, y, a, b)
def test_add1(self):
# tests the following additions on curve y^2=x^3-7 over F_223:
# (192,105) + (17,56)
# (47,71) + (117,141)
# (143,98) + (76,66)
prime = 223
a = FieldElement(0, prime)
b = FieldElement(7, prime)
additions = (
# (x1, y1, x2, y2, x3, y3)
(192, 105, 17, 56, 170, 142),
(47, 71, 117, 141, 60, 139),
(143, 98, 76, 66, 47, 71),
)
# iterate over the additions
for x1_raw, y1_raw, x2_raw, y2_raw, x3_raw, y3_raw in additions:
# Initialize points this way:
# x = FieldElement(192, prime)
# y = FieldElement(105, prime)
# p1 = Point(x, y, a, b)
x1 = FieldElement(x1_raw, prime)
y1 = FieldElement(y1_raw, prime)
p1 = Point(x1, y1, a, b)
x2 = FieldElement(x2_raw, prime)
y2 = FieldElement(y2_raw, prime)
p2 = Point(x2, y2, a, b)
x3 = FieldElement(x3_raw, prime)
y3 = FieldElement(y3_raw, prime)
p3 = Point(x3, y3, a, b)
# check that p1 + p2 == p3
self.assertEqual(p1+p2, p3)
def test_rmul(self):
# tests the following scalar multiplications
# 2*(192,105)
# 2*(143,98)
# 2*(47,71)
# 4*(47,71)
# 8*(47,71)
# 21*(47,71)
prime = 223
a = FieldElement(0, prime)
b = FieldElement(7, prime)
multiplications = (
# (coefficient, x1, y1, x2, y2)
(2, 192, 105, 49, 71),
(2, 143, 98, 64, 168),
(2, 47, 71, 36, 111),
(4, 47, 71, 194, 51),
(8, 47, 71, 116, 55),
(21, 47, 71, None, None),
)
# iterate over the multiplications
for s, x1_raw, y1_raw, x2_raw, y2_raw in multiplications:
# Initialize points this way:
# x = FieldElement(192, prime)
# y = FieldElement(105, prime)
# p = Point(x, y, a, b)
x1 = FieldElement(x1_raw, prime)
y1 = FieldElement(y1_raw, prime)
p1 = Point(x1, y1, a, b)
# initialize the second point based on whether it's the point at infinity
if x2_raw is None:
p2 = Point(None, None, a, b)
else:
x2 = FieldElement(x2_raw, prime)
y2 = FieldElement(y2_raw, prime)
p2 = Point(x2, y2, a, b)
# check that the product is equal to the expected point
self.assertEqual(s*p1, p2)
class S256Test(TestCase):
def test_order(self):
point = N*G
self.assertIsNone(point.x)
def test_pubpoint(self):
# write a test that tests the public point for the following
points = (
# secret, x, y
(7, 0x5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc, 0x6aebca40ba255960a3178d6d861a54dba813d0b813fde7b5a5082628087264da),
(1485, 0xc982196a7466fbbbb0e27a940b6af926c1a74d5ad07128c82824a11b5398afda, 0x7a91f9eae64438afb9ce6448a1c133db2d8fb9254e4546b6f001637d50901f55),
(2**128, 0x8f68b9d2f63b5f339239c1ad981f162ee88c5678723ea3351b7b444c9ec4c0da, 0x662a9f2dba063986de1d90c2b6be215dbbea2cfe95510bfdf23cbf79501fff82),
(2**240+2**31, 0x9577ff57c8234558f293df502ca4f09cbc65a6572c842b39b366f21717945116, 0x10b49c67fa9365ad7b90dab070be339a1daf9052373ec30ffae4f72d5e66d053),
)
# iterate over points
for secret, x, y in points:
# initialize the secp256k1 point (S256Point)
point = S256Point(x, y)
# check that the secret*G is the same as the point
self.assertEqual(secret*G, point)
def test_sec(self):
coefficient = 999**3
uncompressed = '049d5ca49670cbe4c3bfa84c96a8c87df086c6ea6a24ba6b809c9de234496808d56fa15cc7f3d38cda98dee2419f415b7513dde1301f8643cd9245aea7f3f911f9'
compressed = '039d5ca49670cbe4c3bfa84c96a8c87df086c6ea6a24ba6b809c9de234496808d5'
point = coefficient*G
self.assertEqual(point.sec(compressed=False), unhexlify(uncompressed))
self.assertEqual(point.sec(compressed=True), unhexlify(compressed))
coefficient = 123
uncompressed = '04a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5204b5d6f84822c307e4b4a7140737aec23fc63b65b35f86a10026dbd2d864e6b'
compressed = '03a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5'
point = coefficient*G
self.assertEqual(point.sec(compressed=False), unhexlify(uncompressed))
self.assertEqual(point.sec(compressed=True), unhexlify(compressed))
coefficient = 42424242
uncompressed = '04aee2e7d843f7430097859e2bc603abcc3274ff8169c1a469fee0f20614066f8e21ec53f40efac47ac1c5211b2123527e0e9b57ede790c4da1e72c91fb7da54a3'
compressed = '03aee2e7d843f7430097859e2bc603abcc3274ff8169c1a469fee0f20614066f8e'
point = coefficient*G
self.assertEqual(point.sec(compressed=False), unhexlify(uncompressed))
self.assertEqual(point.sec(compressed=True), unhexlify(compressed))
def test_address(self):
secret = 888**3
mainnet_address = '148dY81A9BmdpMhvYEVznrM45kWN32vSCN'
testnet_address = 'mieaqB68xDCtbUBYFoUNcmZNwk74xcBfTP'
point = secret*G
self.assertEqual(
point.address(compressed=True, prefix=b'\x00'), mainnet_address)
self.assertEqual(
point.address(compressed=True, prefix=b'\x6f'), testnet_address)
secret = 321
mainnet_address = '1S6g2xBJSED7Qr9CYZib5f4PYVhHZiVfj'
testnet_address = 'mfx3y63A7TfTtXKkv7Y6QzsPFY6QCBCXiP'
point = secret*G
self.assertEqual(
point.address(compressed=False, prefix=b'\x00'), mainnet_address)
self.assertEqual(
point.address(compressed=False, prefix=b'\x6f'), testnet_address)
secret = 4242424242
mainnet_address = '1226JSptcStqn4Yq9aAmNXdwdc2ixuH9nb'
testnet_address = 'mgY3bVusRUL6ZB2Ss999CSrGVbdRwVpM8s'
point = secret*G
self.assertEqual(
point.address(compressed=False, prefix=b'\x00'), mainnet_address)
self.assertEqual(
point.address(compressed=False, prefix=b'\x6f'), testnet_address)
def test_verify(self):
point = S256Point(
0x887387e452b8eacc4acfde10d9aaf7f6d9a0f975aabb10d006e4da568744d06c,
0x61de6d95231cd89026e286df3b6ae4a894a3378e393e93a0f45b666329a0ae34)
z = 0xec208baa0fc1c19f708a9ca96fdeff3ac3f230bb4a7ba4aede4942ad003c0f60
r = 0xac8d1c87e51d0d441be8b3dd5b05c8795b48875dffe00b7ffcfac23010d3a395
s = 0x68342ceff8935ededd102dd876ffd6ba72d6a427a3edb13d26eb0781cb423c4
self.assertTrue(point.verify(z, Signature(r, s)))
z = 0x7c076ff316692a3d7eb3c3bb0f8b1488cf72e1afcd929e29307032997a838a3d
r = 0xeff69ef2b1bd93a66ed5219add4fb51e11a840f404876325a1e8ffe0529a2c
s = 0xc7207fee197d27c618aea621406f6bf5ef6fca38681d82b2f06fddbdce6feab6
self.assertTrue(point.verify(z, Signature(r, s)))
def test_parse(self):
sec = unhexlify('0349fc4e631e3624a545de3f89f5d8684c7b8138bd94bdd531d2e213bf016b278a')
point = S256Point.parse(sec)
want = 0xa56c896489c71dfc65701ce25050f542f336893fb8cd15f4e8e5c124dbf58e47
self.assertEqual(point.y.num, want)
class SignatureTest(TestCase):
def test_der(self):
testcases = (
(1, 2),
(randint(0, 2**256), randint(0, 2**255)),
(randint(0, 2**256), randint(0, 2**255)),
)
for r, s in testcases:
sig = Signature(r, s)
der = sig.der()
sig2 = Signature.parse(der)
self.assertEqual(sig2.r, r)
self.assertEqual(sig2.s, s)
class PrivateKeyTest(TestCase):
def test_sign(self):
pk = PrivateKey(randint(0, 2**256))
z = randint(0, 2**256)
sig = pk.sign(z)
self.assertTrue(pk.point.verify(z, sig))
def test_wif(self):
pk = PrivateKey(2**256-2**199, compressed=True)
want = 'L5oLkpV3aqBJ4BgssVAsax1iRa77G5CVYnv9adQ6Z87te7TyUdSC'
self.assertEqual(pk.wif(prefix=b'\x80'), want)
pk = PrivateKey(2**256-2**201, compressed=False)
want = '93XfLeifX7Jx7n7ELGMAf1SUR6f9kgQs8Xke8WStMwUtrDucMzn'
self.assertEqual(pk.wif(prefix=b'\xef'), want)
pk = PrivateKey(
0x0dba685b4511dbd3d368e5c4358a1277de9486447af7b3604a69b8d9d8b7889d,
compressed=False,
)
want = '5HvLFPDVgFZRK9cd4C5jcWki5Skz6fmKqi1GQJf5ZoMofid2Dty'
self.assertEqual(pk.wif(prefix=b'\x80'), want)
pk = PrivateKey(
0x1cca23de92fd1862fb5b76e5f4f50eb082165e5191e116c18ed1a6b24be6a53f,
compressed=True,
)
want = 'cNYfWuhDpbNM1JWc3c6JTrtrFVxU4AGhUKgw5f93NP2QaBqmxKkg'
self.assertEqual(pk.wif(prefix=b'\xef'), want)
def test_parse(self):
pk = PrivateKey.parse('L5oLkpV3aqBJ4BgssVAsax1iRa77G5CVYnv9adQ6Z87te7TyUdSC')
want = 2**256-2**199
self.assertEqual(pk.secret, want)
pk = PrivateKey.parse('93XfLeifX7Jx7n7ELGMAf1SUR6f9kgQs8Xke8WStMwUtrDucMzn')
want = 2**256-2**201
self.assertEqual(pk.secret, want)
pk = PrivateKey.parse('5HvLFPDVgFZRK9cd4C5jcWki5Skz6fmKqi1GQJf5ZoMofid2Dty')
want = 0x0dba685b4511dbd3d368e5c4358a1277de9486447af7b3604a69b8d9d8b7889d
self.assertEqual(pk.secret, want)
pk = PrivateKey.parse('cNYfWuhDpbNM1JWc3c6JTrtrFVxU4AGhUKgw5f93NP2QaBqmxKkg')
want = 0x1cca23de92fd1862fb5b76e5f4f50eb082165e5191e116c18ed1a6b24be6a53f
self.assertEqual(pk.secret, want)
def test_deterministic_k(self):
secret = 0x1111111111111111111111111111111111111111111111111111111111111111
z = 0x2222222222222222222222222222222222222222222222222222222222222222
pk = PrivateKey(secret=secret)
k = pk.deterministic_k(z)
want = 0x6931e1828ba0afba580ed7c833bfe082c84f1331afa33a6b98ad8c493cc5edd0
self.assertEqual(k, want)