def test_field_3(): f = Field(3) assert f.elements == [ RemainderPoly.constant(0), RemainderPoly.constant(1), RemainderPoly.constant(2) ]
def test_field_5(): f = Field(5) assert f.elements == [ RemainderPoly.constant(0), RemainderPoly.constant(1), RemainderPoly.constant(2), RemainderPoly.constant(3), RemainderPoly.constant(4) ]
def __call__(self, xval): """ Evaluate Galois field polynomial at field element value. Returns an integer representing the result of evaluation. The integer is the "enumeration" value of the Galois field element, which is its index in the field (range 0...order - 1). If the field is {x[0], ..., x[n-1]}, and the polynomial evaluates to x[j], this function returns the value of j. The type of 'xval' is expected to be a RemainderPoly. """ result = RemainderPoly.constant(0) term = RemainderPoly.constant(1) for coef in self: result = result + coef * term term = term * xval return result.enumerate()
def __mul__(self, operand): result_degree = self.degree + operand.degree result = GFPolynomial( coef_list=[RemainderPoly.constant(0)] * (result_degree + 1)) for k in range(result_degree + 1): min_index = k - operand.degree if k - operand.degree > 0 else 0 max_index = k if k < self.degree else self.degree for m in range(min_index, max_index + 1): # pylint: disable=invalid-name result[k] = result[k] + self[m] * operand[k - m] return result
def __add__(self, operand): coef_tuples = itertools.zip_longest( self, operand, fillvalue=RemainderPoly.constant(0)) result_coefs = [a + b for a, b in coef_tuples] result = GFPolynomial(coef_list=result_coefs) return result