Files
@ 37a1df17b9a1
Branch filter:
Location: Shamira/src/tests/test_gf256.py - annotation
37a1df17b9a1
1.7 KiB
text/x-python
changed naming to snake case. sadly no love for camel case in this world
8968eab714bc 8968eab714bc c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 37a1df17b9a1 cc4182acd584 cc4182acd584 cc4182acd584 cc4182acd584 cc4182acd584 cc4182acd584 c1686e15f8d1 37a1df17b9a1 c1686e15f8d1 c1686e15f8d1 cc4182acd584 c1686e15f8d1 37a1df17b9a1 c1686e15f8d1 cc4182acd584 cc4182acd584 cc4182acd584 cc4182acd584 c1686e15f8d1 37a1df17b9a1 37a1df17b9a1 c1686e15f8d1 c1686e15f8d1 37a1df17b9a1 c1686e15f8d1 cc4182acd584 c1686e15f8d1 c1686e15f8d1 37a1df17b9a1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 37a1df17b9a1 c1686e15f8d1 c1686e15f8d1 c1686e15f8d1 37a1df17b9a1 c1686e15f8d1 37a1df17b9a1 37a1df17b9a1 c1686e15f8d1 37a1df17b9a1 cc4182acd584 cc4182acd584 c1686e15f8d1 37a1df17b9a1 c1686e15f8d1 c1686e15f8d1 cc4182acd584 c1686e15f8d1 | # GNU GPLv3, see LICENSE
import random
import unittest
from unittest import TestCase
from gf256 import _gfmul
from gf256 import *
class TestGF256(TestCase):
def test__gfmul(self):
self.assertEqual(_gfmul(0, 0), 0)
self.assertEqual(_gfmul(1, 1), 1)
self.assertEqual(_gfmul(2, 2), 4)
self.assertEqual(_gfmul(0, 21), 0)
self.assertEqual(_gfmul(0x53, 0xca), 0x01)
self.assertEqual(_gfmul(0xff, 0xff), 0x13)
def test_gfmul(self):
for a in range(256):
for b in range(256):
self.assertEqual(_gfmul(a, b), gfmul(a, b))
def test_evaluate(self):
for x in range(256):
(a0, a1, a2, a3) = (x, x>>1, x>>2, x>>3)
self.assertEqual(evaluate([17], x), 17) # constant polynomial
self.assertEqual(evaluate([a0, a1, a2, a3], 0), x) # any polynomial at 0
self.assertEqual(evaluate([a0, a1, a2, a3], 1), a0^a1^a2^a3) # polynomial at 1 == sum of coefficients
def test_get_constant_coef(self):
self.assertEqual(get_constant_coef((1, 1), (2, 2), (3, 3)), 0)
random.seed(17)
random_matches = 0
for i in range(10):
k = random.randint(2, 255)
# exact
res = self.check_coefs_match(k, k)
self.assertEqual(res[0], res[1])
# overdetermined
res = self.check_coefs_match(k, 256)
self.assertEqual(res[0], res[1])
# underdetermined => random
res = self.check_coefs_match(k, k-1)
if res[0]==res[1]:
random_matches += 1
self.assertLess(random_matches, 2) # with a chance (255/256)**10=0.96 there should be no match
def check_coefs_match(self, k, m):
coefs = [random.randint(0, 255) for i in range(k)]
points = [(j, evaluate(coefs, j)) for j in range(1, 256)]
random.shuffle(points)
return (get_constant_coef(*points[:m]), coefs[0])
if __name__=='__main__':
unittest.main()
|