Tweaks to galois.py

This commit is contained in:
Ethan L. Miller 2020-09-02 15:44:15 -07:00
parent 0d92601702
commit 043dbbb1a2

View File

@ -46,6 +46,16 @@ class GaloisNumber:
raise ValueError ("Value {0} is outside field".format (v)) raise ValueError ("Value {0} is outside field".format (v))
self.value = v self.value = v
def mul_region (self, arr, init_value = None):
if not init_value:
init_value = [GaloisNumber (0, self.field)] * len (arr)
if any ([type(x) != GaloisNumber or type(x.field) != type(self.field) for x in arr]):
raise ValueError ("All items must be Galois numbers in this field!")
return [x * self + i for x, i in zip (arr, init_value)]
def copy (self):
return GaloisNumber (self.value, self.field)
def __add__ (self, other): def __add__ (self, other):
if self.field != other.field: if self.field != other.field:
raise GaloisException ("Field elements from different fields") raise GaloisException ("Field elements from different fields")
@ -54,13 +64,13 @@ class GaloisNumber:
def __iadd__ (self, other): def __iadd__ (self, other):
if self.field != other.field: if self.field != other.field:
raise GaloisException ("Field elements from different fields") raise GaloisException ("Field elements from different fields")
self.value ^= other.value return self + other
def __sub__ (self, other): def __sub__ (self, other):
return self + other return self + other
def __isub__ (self, other): def __isub__ (self, other):
self += other return self + other
def __invert__ (self): def __invert__ (self):
return self.field.invert (self) return self.field.invert (self)
@ -76,7 +86,7 @@ class GaloisNumber:
def __imul__ (self, other): def __imul__ (self, other):
if self.field != other.field: if self.field != other.field:
raise GaloisException ("Field elements from different fields") raise GaloisException ("Field elements from different fields")
self.value = self.field.direct_multiply (self.value, other.value) return self.field.multiply (self, other)
def __floordiv__ (self, other): def __floordiv__ (self, other):
if self.field != other.field: if self.field != other.field:
@ -94,6 +104,9 @@ class GaloisNumber:
def __repr__ (self): def __repr__ (self):
return self.field.fmt (self.value) return self.field.fmt (self.value)
def to_bytes (self):
return (self.value).to_bytes ((self.field.bits + 7) // 8, byteorder='big')
class GaloisFieldLog: class GaloisFieldLog:
''' '''
Pure python implementation of Galois (finite) field arithmetic routines using log/antilog Pure python implementation of Galois (finite) field arithmetic routines using log/antilog