def test_field_3(): f = Field(3) assert f.elements == [ RemainderPoly.constant(0), RemainderPoly.constant(1), RemainderPoly.constant(2) ]
def test_create_with_quotient(): IntMod.modulus = 7 qp = PolyMod([IntMod(1), IntMod(2), IntMod(1)]) RemainderPoly.quotient = qp rp = RemainderPoly([IntMod(1), IntMod(4), IntMod(6), IntMod(4), IntMod(1)]) rp._residue() # pylint: disable=protected-access x = str(rp) assert x == '0'
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 test_field_4(): f = Field(4) assert [str(e) for e in f.elements] == ['0', '1', 'x', 'x + 1'] assert f.elements == [ RemainderPoly(coef_list=[IntMod(0)]), RemainderPoly(coef_list=[IntMod(1)]), RemainderPoly(coef_list=[IntMod(0), IntMod(1)]), RemainderPoly(coef_list=[IntMod(1), IntMod(1)]) ]
def test_plus_different_degrees(): IntMod.modulus = 2 rp1 = RemainderPoly(coef_list=[IntMod(0), IntMod(1)]) rp2 = RemainderPoly(coef_list=[IntMod(1), IntMod(1), IntMod(1)]) rp = rp1 + rp2 rp_expected = RemainderPoly(coef_list=[IntMod(1), IntMod(0), IntMod(1)]) assert rp == rp_expected
def test_plus_no_quotient(): IntMod.modulus = 2 rp1 = RemainderPoly(coef_list=[IntMod(0), IntMod(1)]) rp2 = RemainderPoly(coef_list=[IntMod(1), IntMod(1)]) rp = rp1 + rp2 rp_expected = RemainderPoly(coef_list=[IntMod(1)]) assert rp == rp_expected
def test_times_no_residue_needed(): IntMod.modulus = 3 qp = PolyMod([IntMod(1), IntMod(1), IntMod(1), IntMod(1)]) RemainderPoly.quotient = qp rp1 = RemainderPoly(coef_list=[IntMod(1), IntMod(1)]) rp2 = RemainderPoly(coef_list=[IntMod(1), IntMod(1)]) rp = rp1 * rp2 rp_expected = RemainderPoly(coef_list=[IntMod(1), IntMod(2), IntMod(1)]) assert rp == rp_expected
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 test_create_no_quotient(): IntMod.modulus = 0 rp = RemainderPoly(coef_list=[ IntMod(1), IntMod(4), IntMod(6), IntMod(4), IntMod(1) ]) x = str(rp) assert x == 'x^4 + 4x^3 + 6x^2 + 4x + 1'
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