32 bit direct field now works correctly.

This commit is contained in:
Ethan L. Miller 2015-11-13 00:52:13 -08:00
parent a6ab689d84
commit 5b186d41c4

View File

@ -37,7 +37,7 @@ class GaloisNumber:
the field with which the GaloisNumber instance was defined. the field with which the GaloisNumber instance was defined.
''' '''
if v > self.field.size: if v > self.field.size:
raise GaloisException ("Value {0} is outside field".format (value)) raise GaloisException ("Value {0} is outside field".format (v))
self.value = v self.value = v
def __add__ (self, other): def __add__ (self, other):
@ -171,9 +171,9 @@ class GaloisFieldDirect:
There only needs to be one instantiation of the field for a given set of parameters, There only needs to be one instantiation of the field for a given set of parameters,
but elements from different field instances with the same parameters may be mixed. but elements from different field instances with the same parameters may be mixed.
''' '''
field_widths = (4, 8, 12, 16) field_widths = (4, 8, 12, 16, 32)
poly_defaults = {4: 0x13, 8: 0x11d, 12:0x1053, 16: 0x1100b} poly_defaults = {4: 0x13, 8: 0x11d, 12:0x1053, 16: 0x1100b, 32: 0x1000000c5}
multiply_test_size = 10000 max_test_size = 5000
def __init__ (self, bits, primitive_polynomial = None, repr_prefix = 'G', alpha = 1): def __init__ (self, bits, primitive_polynomial = None, repr_prefix = 'G', alpha = 1):
''' '''
Create a Galois field using direct arithmetic. No log tables or inverses to Create a Galois field using direct arithmetic. No log tables or inverses to
@ -241,14 +241,15 @@ class GaloisFieldDirect:
v = GaloisNumber (self) v = GaloisNumber (self)
g_0 = GaloisNumber (self, 0) g_0 = GaloisNumber (self, 0)
g_1 = GaloisNumber (self, 1) g_1 = GaloisNumber (self, 1)
for i in range (self.size): small_field = self.size < self.max_test_size
v.assign (i) n_tests = (self.size - 1) if small_field else self.max_test_size
if i == 0: continue for i in range (0, n_tests):
v.assign ((i if small_field else random.randint (0, self.size - 2)) + 1)
assert v * ~v == mul_identity, "Multiplicative inverse failed at {}".format (i) assert v * ~v == mul_identity, "Multiplicative inverse failed at {}".format (i)
assert g_0 - v == -v, "Additive inverse failed at {}".format (i) assert g_0 - v == -v, "Additive inverse failed at {}".format (i)
assert v * g_1 == v, "Multiplicative identity failed at {}".format (i) assert v * g_1 == v, "Multiplicative identity failed at {}".format (i)
vb = GaloisNumber (self) vb = GaloisNumber (self)
for a in range (1, self.multiply_test_size): for a in range (1, self.max_test_size):
v.assign (random.randint (1, self.size - 1)) v.assign (random.randint (1, self.size - 1))
vb.assign (random.randint (1, self.size - 1)) vb.assign (random.randint (1, self.size - 1))
product = v * vb product = v * vb
@ -257,17 +258,19 @@ class GaloisFieldDirect:
return True return True
if __name__ == '__main__': if __name__ == '__main__':
print '\nTesting direct fields...........'
for width in GaloisFieldDirect.field_widths: for width in GaloisFieldDirect.field_widths:
field = GaloisFieldDirect (width) field = GaloisFieldDirect (width)
g0 = GaloisNumber (field, 5) g0 = GaloisNumber (field, 2)
g1 = GaloisNumber (field, 7) g1 = GaloisNumber (field, 7)
print g0 + g1 print '{0} + {1} = {2}'.format (g0, g1, g0 + g1)
if field.self_test (): if field.self_test ():
print "{0} bit field (direct) passed!".format (width) print "{0} bit field (direct) passed!".format (width)
print '\nTesting log fields...........'
for width in GaloisFieldLog.field_widths: for width in GaloisFieldLog.field_widths:
field = GaloisFieldLog (width) field = GaloisFieldLog (width)
g0 = GaloisNumber (field, 2)
g1 = GaloisNumber (field, 7)
print '{0} + {1} = {2}'.format (g0, g1, g0 + g1)
if field.self_test (): if field.self_test ():
print "{0} bit field (log) passed!".format (width) print "{0} bit field (log) passed!".format (width)
g0 = GaloisNumber (field, 5)
g1 = GaloisNumber (field, 7)
print g0 + g1