示例#1
0
    def setUp(self) -> None:

        self.f2 = finite_field(2)
        self.f4 = FiniteField(2, 2, self.f2.polynomial(1, 1, 1))
        self.f4g = FiniteField(2,
                               2,
                               self.f2.polynomial(1, 1, 1),
                               generator=(0, 1))
示例#2
0
    def test4(self):
        """Parser test for a linear polynomial over the finite field of order 8
        """
        expr = '-(1+w+w^2) + X'
        f2 = PrimeField(2)
        g = f2.polynomial(1, 1, 0, 1)
        f8 = FiniteField(2, 3, g, root_symbol='w')

        p = symbolic_polynomial(expr, f8)
        self.assertTrue(p == f8.polynomial(f8(1, 1, 1), 1))
示例#3
0
    def test1(self):
        """Parser test over F4
        """
        f2 = PrimeField(2)
        f4 = FiniteField(2, 2, f2.polynomial(1, 1, 1))

        expr = '+(j+1)X + (1+j)X^2 + (j)'
        p = symbolic_polynomial(expr, f4)

        self.assertTrue(p == f4.polynomial(f4(0, 1), f4(1, 1), f4(1, 1)))
示例#4
0
    def testF9Product(self):
        """Check product of polynomials over F9
        """
        f3 = PrimeField(3)
        p = f3.polynomial(1, 0, 1)
        f9 = FiniteField(3, 2, p)
        a = symbolic_polynomial('-1 + X - (1+j)X^2', f9)
        b = symbolic_polynomial('1 + X^2 + (j)X', f9)

        self.assertTrue(
            a * b == f9.polynomial(-1, f9(1, -1), 1, f9(-1, -1), f9(-1, -1)))
示例#5
0
    def testF16gcd(self):
        """Check GCD of polynomials over F16
        """
        f2 = PrimeField(2)
        p = f2.polynomial(1, 1, 0, 0, 1)
        f16 = FiniteField(2, 4, p)

        a = symbolic_polynomial('X^6 + X^3 + 1', f16)
        b = symbolic_polynomial('X^3 + 1', f16)

        self.assertEqual(gcd(a, b), f16.polynomial(1))
示例#6
0
    def testF25Repr(self):
        """Check equivalence of symbolic representation and printable representation of a polynomial over F25
        """
        f5 = PrimeField(5)
        f25 = FiniteField(5, 2, f5.polynomial(2, 0, 1))

        p = symbolic_polynomial('(1+j)X^2 + (j)X^5 + X + 2X^3 + (2+j)', f25)
        self.assertEqual(
            p,
            eval(repr(f25.polynomial(f25(2, 1), 1, f25(1, 1), 2, 0, f25(0,
                                                                        1)))))
示例#7
0
    def testF4Str(self):
        """Check printable representation of a polynomial over F4
        """
        f2 = PrimeField(2)
        p_gen = f2.polynomial(1, 1, 1)
        f4 = FiniteField(2, 2, p_gen)

        p = f4.polynomial(f4(0, 1), f4(1, 0), f4(1, 1))
        self.assertTrue(str(p) == '(j) + X + (1+j)X^2')

        p *= f4(0, 1)
        self.assertTrue(str(p) == '(1+j) + (j)X + X^2')
示例#8
0
    def testF8Add(self):
        """Check addition of polynomials over F8
        """
        f2 = PrimeField(2)
        p = f2.polynomial(1, 1, 0, 1)
        f8 = FiniteField(2, 3, p, root_symbol='w')

        a = symbolic_polynomial('1 + X + (1+w^2)X^3', f8)
        b = symbolic_polynomial('(1+w)X^2 + X^3', f8)

        self.assertEqual(b.trailing, FFElement(f8, (1, 1, 0)))

        self.assertTrue(a + b == f8.polynomial(1, 1, f8(1, 1, 0), f8(0, 0, 1)))
示例#9
0
    def testF16Div(self):
        """Check long division of polynomials over F16
        """
        f2 = PrimeField(2)
        p = f2.polynomial(1, 1, 0, 0, 1)
        f16 = FiniteField(2, 4, p)

        a = symbolic_polynomial('X^6 + X^3 + 1', f16)
        b = symbolic_polynomial('X^3 + 1', f16)

        q = a / b
        r = a % b

        self.assertEqual(q, f16.polynomial(0, 0, 0, 1))
        self.assertEqual(r, a.unit)
示例#10
0
 def test5(self):
     """Check equivalence between multiplication with and w/o use of generator in F27
     """
     f27g = finite_field(27)
     f3 = finite_field(3)
     f27 = FiniteField(3, 3, f3.polynomial(-1, -1, 0, 1))
     for a in f27:
         for b in f27:
             self.assertEqual(a * b, f27g.element(a) * f27g.element(b))
             if not b.null:
                 self.assertEqual(a / b, f27g.element(a) / f27g.element(b))
示例#11
0
 def test4(self):
     """Check equivalence between multiplication with and w/o use of generator in F25
     """
     f25g = finite_field(25)
     f5 = finite_field(5)
     f25 = FiniteField(5, 2, f5.polynomial(2, 0, 1))
     for a in f25:
         for b in f25:
             self.assertEqual(a * b, f25g.element(a) * f25g.element(b))
             if not b.null:
                 self.assertEqual(a / b, f25g.element(a) / f25g.element(b))
示例#12
0
 def test3(self):
     """Check equivalence between multiplication with and w/o use of generator in F16
     """
     f16g = finite_field(16)
     f2 = finite_field(2)
     f16 = FiniteField(2, 4, f2.polynomial(1, 1, 0, 0, 1))
     for a in f16:
         for b in f16:
             self.assertEqual(a * b, f16g.element(a) * f16g.element(b))
             if not b.null:
                 self.assertEqual(a / b, f16g.element(a) / f16g.element(b))
示例#13
0
    def testPrimeAndFinite(self):
        """Checking correct transtyping int, PFElement to FFElement for + and - in F9
        """
        f3 = PrimeField(3)
        f9 = FiniteField(3, 2, f3.polynomial(-1, 1, 1))

        self.assertTrue(f9(1, 0) + 1 == f3(-1))
        self.assertTrue(1 + f3(1) == f9(-1, 0))
        self.assertTrue(f9(-1, 0) == -1)

        self.assertTrue(3 * f9(-1, 0) == f9(-1, 0) * 3)
        self.assertTrue(f3(1) * f9(1, 1) == f3(1) + f9(0, 1))
示例#14
0
    def test8(self):
        """Parser check of internal state automaton
        '(j+)X' is an invalid expression
        """
        expr = '(j+)X'

        f2 = PrimeField(2)
        p = f2.polynomial(1, 1, 1)
        f4 = FiniteField(2, 2, p)

        with self.assertRaises(SyntaxError):
            symbolic_polynomial(expr, f4)
示例#15
0
    def test1(self):
        """Check return type of power in F4 and F2
        """
        f2 = PrimeField(2)
        f4 = FiniteField(2, 2, f2.polynomial(1, 1, 1))

        b = f4(0, 1)**0
        self.assertIsInstance(b, FFElement)
        self.assertTrue(b == 1)

        b = f2(1)**0
        self.assertIsInstance(b, PFElement)
        self.assertTrue(b == 1)
示例#16
0
 def setUp(self):
     self.f4 = FiniteField(2, 2, PrimeField(2).polynomial(1, 1, 1))
示例#17
0
 def setUp(self):
     f2 = PrimeField(2)
     p_gen = f2.polynomial(1, 1, 1)
     self.f4 = FiniteField(2, 2, p_gen)
示例#18
0
class TestGenerator(TestCase):
    def setUp(self) -> None:

        self.f2 = finite_field(2)
        self.f4 = FiniteField(2, 2, self.f2.polynomial(1, 1, 1))
        self.f4g = FiniteField(2,
                               2,
                               self.f2.polynomial(1, 1, 1),
                               generator=(0, 1))

    def test0(self):
        """Check generator order
        """
        self.assertEqual(self.f4g.generator**(self.f4g.order - 1),
                         self.f4g.one)

    def test1(self):
        """Check equivalence between multiplication with and w/o use of generator
        """
        for a in self.f4:
            for b in self.f4:
                self.assertEqual(a * b,
                                 self.f4g.element(a) * self.f4g.element(b))

    def test2(self):
        """Check equivalence between true division with and w/o use of generator
        """
        for a in self.f4:
            for b in self.f4:
                if not b.null:
                    self.assertEqual(a / b,
                                     self.f4g.element(a) / self.f4g.element(b))

    def test3(self):
        """Check equivalence between multiplication with and w/o use of generator in F16
        """
        f16g = finite_field(16)
        f2 = finite_field(2)
        f16 = FiniteField(2, 4, f2.polynomial(1, 1, 0, 0, 1))
        for a in f16:
            for b in f16:
                self.assertEqual(a * b, f16g.element(a) * f16g.element(b))
                if not b.null:
                    self.assertEqual(a / b, f16g.element(a) / f16g.element(b))

    def test4(self):
        """Check equivalence between multiplication with and w/o use of generator in F25
        """
        f25g = finite_field(25)
        f5 = finite_field(5)
        f25 = FiniteField(5, 2, f5.polynomial(2, 0, 1))
        for a in f25:
            for b in f25:
                self.assertEqual(a * b, f25g.element(a) * f25g.element(b))
                if not b.null:
                    self.assertEqual(a / b, f25g.element(a) / f25g.element(b))

    def test5(self):
        """Check equivalence between multiplication with and w/o use of generator in F27
        """
        f27g = finite_field(27)
        f3 = finite_field(3)
        f27 = FiniteField(3, 3, f3.polynomial(-1, -1, 0, 1))
        for a in f27:
            for b in f27:
                self.assertEqual(a * b, f27g.element(a) * f27g.element(b))
                if not b.null:
                    self.assertEqual(a / b, f27g.element(a) / f27g.element(b))
示例#19
0
 def testPow(self):
     """Check exponentiation in F9
     """
     f9 = FiniteField(3, 2, PrimeField(3).polynomial(1, 0, 1))
     self.assertTrue(f9(1, 1)**2 == f9(0, -1))
示例#20
0
 def setUp(self):
     self.f8 = FiniteField(2, 3, PrimeField(2).polynomial(1, 1, 0, 1))
示例#21
0
 def setUp(self):
     self.f25 = FiniteField(5, 2,
                            Polynomial([1, 1, 1], base_field=PrimeField(5)))
示例#22
0
 def setUp(self):
     self.f27 = FiniteField(
         3, 3, Polynomial([-1, -1, 0, 1], base_field=PrimeField(3)))