-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dumbp256k1.py
111 lines (96 loc) · 3.8 KB
/
test_dumbp256k1.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
# Testing Dumbp256k1
# Only "more complex" functions are tested because lazy
import pytest, secrets
import dumbp256k1
# parse test vectors
data = []
with open('test_vectors.txt','r') as fp:
while True:
line = fp.readline()
if not line:
break
if line[:4] == 'k = ':
k = int(line.strip()[4:]) # int
line = fp.readline()
x = line.strip()[4:] # uppercase hex string
line = fp.readline()
y = line.strip()[4:] # uppercase hex string
data.append((k, x, y))
def test_keypair():
# test point at infinity
test = dumbp256k1.Scalar(dumbp256k1.n) * dumbp256k1.G
assert str(test) == '00'
# test through test vectors
for k, x, y in data:
test = dumbp256k1.Scalar(k) * dumbp256k1.G
hex_x = f'{test.x:0{dumbp256k1.b // 4}X}'
hex_y = f'{test.y:0{dumbp256k1.b // 4}X}'
assert (hex_x, hex_y) == (x, y)
def test_addition():
# test point at infinity
assert dumbp256k1.Z + dumbp256k1.Z == dumbp256k1.Z
assert dumbp256k1.Z + dumbp256k1.G == dumbp256k1.G
assert dumbp256k1.G + dumbp256k1.Z == dumbp256k1.G
assert dumbp256k1.G + (-dumbp256k1.G) == dumbp256k1.Z
# randomized testing
for i in range(20):
a = dumbp256k1.random_scalar()
b = dumbp256k1.random_scalar()
c = a + b
aG = a * dumbp256k1.G
bG = b * dumbp256k1.G
cG = c * dumbp256k1.G
assert aG + bG == cG
assert bG + aG == cG
def test_subtraction():
# test point at infinity
assert -dumbp256k1.Z == dumbp256k1.Z
assert dumbp256k1.Z - dumbp256k1.Z == dumbp256k1.Z
assert dumbp256k1.Z - dumbp256k1.G == -dumbp256k1.G
assert dumbp256k1.G - dumbp256k1.Z == dumbp256k1.G
assert dumbp256k1.G - dumbp256k1.G == dumbp256k1.Z
# randomized testing
for i in range(20):
a = dumbp256k1.random_scalar()
b = dumbp256k1.random_scalar()
c = a - b
aG = a * dumbp256k1.G
bG = b * dumbp256k1.G
cG = c * dumbp256k1.G
assert aG - bG == cG
assert bG - aG == -cG
def test_scalarvector_invert():
# test Scalar(0)
svector = [dumbp256k1.Scalar(0), dumbp256k1.Scalar(1), dumbp256k1.Scalar(2)]
svector = dumbp256k1.ScalarVector(svector)
with pytest.raises(ZeroDivisionError):
svector.invert()
assert (svector.invert(allow_zero=True)).invert(allow_zero=True) == svector
# randomized testing
for i in range(20):
svector = []
for j in range(secrets.randbelow(4) + 1):
svector.append(dumbp256k1.random_scalar())
svector = dumbp256k1.ScalarVector(svector)
assert (svector.invert()).invert() == svector
def test_vector_products():
# test Scalar(0) and point at infinity
svector1 = [dumbp256k1.Scalar(0)] * 3
svector1 = dumbp256k1.ScalarVector(svector1)
svector2 = [dumbp256k1.random_scalar(), dumbp256k1.random_scalar(), dumbp256k1.random_scalar()]
svector2 = dumbp256k1.ScalarVector(svector2)
pvector = dumbp256k1.PointVector([dumbp256k1.G] * 3)
assert svector1 ** (svector2 * pvector) == dumbp256k1.Point('00')
# randomized testing
for i in range(20):
svector1 = []
svector2 = []
length = secrets.randbelow(4) + 1
pvector = dumbp256k1.PointVector([dumbp256k1.G] * length)
for j in range(length):
svector1.append(dumbp256k1.random_scalar(zero=True))
svector1 = dumbp256k1.ScalarVector(svector1)
for j in range(length):
svector2.append(dumbp256k1.random_scalar(zero=True))
svector2 = dumbp256k1.ScalarVector(svector2)
assert svector1 ** (svector2 * pvector) == (svector1 ** svector2) * dumbp256k1.G