diff --git a/galois.py b/galois.py index ff9460e..e2298c3 100755 --- a/galois.py +++ b/galois.py @@ -37,7 +37,7 @@ class GaloisNumber: the field with which the GaloisNumber instance was defined. ''' 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 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, but elements from different field instances with the same parameters may be mixed. ''' - field_widths = (4, 8, 12, 16) - poly_defaults = {4: 0x13, 8: 0x11d, 12:0x1053, 16: 0x1100b} - multiply_test_size = 10000 + field_widths = (4, 8, 12, 16, 32) + poly_defaults = {4: 0x13, 8: 0x11d, 12:0x1053, 16: 0x1100b, 32: 0x1000000c5} + max_test_size = 5000 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 @@ -241,14 +241,15 @@ class GaloisFieldDirect: v = GaloisNumber (self) g_0 = GaloisNumber (self, 0) g_1 = GaloisNumber (self, 1) - for i in range (self.size): - v.assign (i) - if i == 0: continue + small_field = self.size < self.max_test_size + n_tests = (self.size - 1) if small_field else self.max_test_size + 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 g_0 - v == -v, "Additive inverse failed at {}".format (i) assert v * g_1 == v, "Multiplicative identity failed at {}".format (i) 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)) vb.assign (random.randint (1, self.size - 1)) product = v * vb @@ -257,17 +258,19 @@ class GaloisFieldDirect: return True if __name__ == '__main__': + print '\nTesting direct fields...........' for width in GaloisFieldDirect.field_widths: field = GaloisFieldDirect (width) - g0 = GaloisNumber (field, 5) + g0 = GaloisNumber (field, 2) g1 = GaloisNumber (field, 7) - print g0 + g1 + print '{0} + {1} = {2}'.format (g0, g1, g0 + g1) if field.self_test (): print "{0} bit field (direct) passed!".format (width) + print '\nTesting log fields...........' for width in GaloisFieldLog.field_widths: field = GaloisFieldLog (width) + g0 = GaloisNumber (field, 2) + g1 = GaloisNumber (field, 7) + print '{0} + {1} = {2}'.format (g0, g1, g0 + g1) if field.self_test (): print "{0} bit field (log) passed!".format (width) - g0 = GaloisNumber (field, 5) - g1 = GaloisNumber (field, 7) - print g0 + g1