-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gf256.py
287 lines (240 loc) · 9.2 KB
/
test_gf256.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
"""
test_gf256
~~~~~~~~~~
:copyright: 2016 by Daniel Neuhäuser
:license: BSD, see LICENSE.rst for details
"""
import pytest
from hypothesis import assume, given
from hypothesis.strategies import integers
from gf256 import GF256, GF256LT, _polydiv
def test_polydiv():
# _polydiv should never be called with 0 as a divisor in the regular code,
# so the case won't be covered by any other tests. We do nevertheless want
# the right thing to happen here, if something goes wrong.
with pytest.raises(ZeroDivisionError):
_polydiv(1, 0)
def create_test_class(GF256):
gf256s = integers(min_value=0, max_value=255).map(GF256)
class TestGF256:
@given(integers())
def test_init_fails_outside_of_range(self, a):
"""
For all `a` such that `a < 0` or `a > 255`, `GF256(a)` should raise
a :exc:`ValueError`.
"""
assume(not (0 <= a < 256))
with pytest.raises(ValueError):
GF256(a)
@pytest.mark.parametrize(('a', 'polynomial'), [
(0b0, '0'),
(0b1, '1'),
(0b10, 'x'),
(0b11, 'x + 1'),
(0b11111111, 'x**7 + x**6 + x**5 + x**4 + x**3 + x**2 + x + 1')
])
def test_to_polynomial_string(self, a, polynomial):
assert GF256(a).to_polynomial_string() == polynomial
@given(gf256s, gf256s)
def test_distinct_hashes(self, a, b):
"""
For all `a` and `b` in `GF(2 ** 8)` such that `a != b` the equation
`hash(a) != hash(b)` should all, that is their hashes should be
distinct.
"""
assume(a != b)
assert hash(a) != hash(b)
@given(gf256s)
def test_repr_evals_to_equal_obj(self, a):
evaluated = eval(repr(a), {}, {GF256.__name__: GF256})
assert evaluated == a
@given(gf256s, gf256s)
def test_closure_under_addition(self, a, b):
"""
For all `a` and `b` in `GF(2 ** 8)`, `a + b` should be in
`GF(2 ** 8)`.
"""
c = a + b
assert isinstance(c, GF256)
assert 0 <= int(c) < 256
@given(gf256s, gf256s)
def test_closure_under_multiplication(self, a, b):
"""
For all `a` and `b` in `GF(2 ** 8)`, `a * b` should be in
`GF(2 ** 8)`.
"""
c = a * b
assert isinstance(c, GF256)
assert 0 <= int(c) < 256
@given(gf256s, gf256s, gf256s)
def test_associativity_of_addition(self, a, b, c):
"""
For all `a`, `b`, and, `c` in `GF(2 ** 8)` the equation
`a + (b + c) == (a + b) + c` should hold.
"""
assert a + (b + c) == (a + b) + c
@given(gf256s, gf256s, gf256s)
def test_associativity_of_multiplication(self, a, b, c):
"""
For all `a`, `b`, and, `c` in `GF(2 ** 8)` the equation
`a * (b * c) == (a * b) * c` should hold.
"""
assert a * (b * c) == (a * b) * c
@given(gf256s, gf256s)
def test_commutativity_of_addition(self, a, b):
"""
For all `a` and `b` in `GF(2 ** 8)` the equation `a + b == b + a`
should hold.
"""
assert a + b == b + a
@given(gf256s, gf256s)
def test_commutativity_of_multiplication(self, a, b):
"""
For all `a` and `b` in `GF(2 ** 8)` `a * b == b * a` holds.
"""
assert a * b == b * a
@given(gf256s)
def test_existence_of_additive_identity_element(self, a):
"""
There exists exactly one element in `GF(2 ** 8)`, called the
*additive identity* element, which is denotated by `0`. For all `a`
in `GF(2 ** 8)` `a + 0 == a` holds.
"""
zeros = {zero for zero in map(GF256, range(256)) if a + zero == a}
assert len(zeros) == 1
zero = zeros.pop()
assert zero == GF256(0)
@given(gf256s)
def test_existence_of_multiplicative_identity_element(self, a):
"""
There exists an element in `GF(2 ** 8)`, called the
*multiplicative identity* element, which is denotated by `1`. For
all `a` in `GF(2 ** 8)` the equation `a * 1 = a` should hold.
"""
assert any(a * one == a for one in map(GF256, range(256)))
@given(gf256s)
def test_existence_of_additive_inverses(self, a):
"""
For every `a` in `GF(2 ** 8)`, there exists an element `-a` in
`GF(2 ** 8)` such that `a + (-a) = 0`.
(This is required for subtraction to work.)
"""
assert any(
a + inverse == GF256(0) for inverse in map(GF256, range(256))
)
@given(gf256s)
def test_existence_of_multiplicative_inverses(self, a):
"""
For every `a` in `GF(2 ** 8)`, there exists an element
`a**(-1)` in `GF(2 ** 8)` such that `a * (a ** (-1)) = 1`.
(This is required for division to work.)
"""
assume(a != GF256(0)) # You can't divide by zero
assert any(
a * inverse == GF256(1) for inverse in map(GF256, range(256))
)
@given(gf256s, gf256s, gf256s)
def test_left_distributivity(self, a, b, c):
"""
For all `a`, `b`, and `c` in `GF(2 ** 8)`,
`a * (b + c) == (a * b) + (a * c)` holds.
"""
assert a * (b + c) == (a * b) + (a * c)
@given(gf256s, gf256s, gf256s)
def test_right_distributivity(self, a, b, c):
"""
For all `a`, `b`, and `c` in `GF(2 ** 8)`,
`(b + c) * a == (b * a) + (c * a)` holds.
"""
assert (b + c) * a == (b * a) + (c * a)
@given(gf256s, gf256s)
def test_subtraction(self, a, b):
"""
For all `a` and `b` in `GF(2 ** 8)` with `c = a + b` the equation
`c - b == a` holds.
"""
c = a + b
assert c - b == a
@given(gf256s, gf256s)
def test_division(self, a, b):
"""
For all `a` and `b` in `GF(2 ** 8)` with `c = a * b` the equation
`c / b == a` holds.
"""
assume(b != GF256(0))
c = a * b
d = c / b
assert d == a
@given(gf256s)
def test_divide_by_zero(self, a):
with pytest.raises(ZeroDivisionError):
a / GF256(0)
@given(gf256s, gf256s)
def test_pow(self, a, b):
power_through_op = a ** b
power_manually = GF256(1)
for _ in range(int(b)):
power_manually *= a
assert power_through_op == power_manually
@given(gf256s)
def test_pow_of_one(self, a):
assert a ** GF256(0) == GF256(1)
@given(gf256s, gf256s)
def test_equality(self, a, b):
if a == b:
assert int(a) == int(b)
else:
assert int(a) != int(b)
@given(gf256s, gf256s)
def test_inequality(self, a, b):
if a != b:
assert int(a) != b
else:
assert int(a) == int(b)
@given(gf256s, gf256s)
def test_equal_and_unequal_are_inverse(self, a, b):
if a == b:
assert not (a != b)
else:
assert a != b
if a != b:
assert not (a == b)
else:
assert a == b
@given(integers(min_value=0, max_value=255))
def test_int_coercion(self, a):
assert int(GF256(a)) == a
def test_addition_with_different_type(self):
with pytest.raises(TypeError):
GF256(1) + 1
def test_subtraction_with_different_type(self):
with pytest.raises(TypeError):
GF256(1) - 1
def test_multiplication_with_different_type(self):
with pytest.raises(TypeError):
GF256(1) * 1
def test_pow_with_different_type(self):
with pytest.raises(TypeError):
GF256(1) ** 1
def test_truediv_with_different_type(self):
with pytest.raises(TypeError):
GF256(1) / 1
TestGF256.__name__ = 'Test{}'.format(GF256.__name__)
return TestGF256
TestGF256 = create_test_class(GF256)
TestGF256LT = create_test_class(GF256LT)
class TestImplementationEquality:
gf256s = integers(min_value=0, max_value=255)
@given(gf256s, gf256s)
def test_addition(self, a, b):
assert int(GF256(a) + GF256(b)) == int(GF256LT(a) + GF256LT(b))
@given(gf256s, gf256s)
def test_subtraction(self, a, b):
assert int(GF256(a) - GF256(b)) == int(GF256LT(a) - GF256LT(b))
@given(gf256s, gf256s)
def test_multiplication(self, a, b):
assert int(GF256(a) * GF256(b)) == int(GF256LT(a) * GF256LT(b))
@given(gf256s, gf256s)
def test_division(self, a, b):
assume(b != 0)
assert int(GF256(a) / GF256(b)) == int(GF256LT(a) / GF256LT(b))