def test_reduce(self): fp3 = FF(3) # X^2 - 1 self.assertEqual(fp3.reduce_poly([5]), [2]) self.assertEqual(fp3.reduce_poly([6]), []) fp27 = FF(3, pivot=[-1, -1, 0, 1]) self.assertEqual(fp27.reduce_poly([0, 0, 0, 0, 1]), [0, 1, 1])
def test_derivative(self): field = FF(p=3) polyring = Poly(field=field) f = [field.uni(1) for i in range(11)] self.assertEqual(polyring.derivative(f), [field.uni(i + 1) for i in range(10)])
def test_add(self): field = FF(p=5) polyring = Poly(field=field) f = [[0], [1]] g = [[], [], [2]] self.assertEqual(polyring.add(f, g), [[], [1], [2]])
def test_gcd(self): field = FF(p=3) polyring = Poly(field=field) f = [[0], [1]] g = [[], [], [2]] self.assertEqual(polyring.gcd(f, g), f)
def test_div_mod(self): field = FF(p=3) polyring = Poly(field=field) f = [[0], [1]] g = [[], [], [2]] self.assertEqual(polyring.div_mod(g, f), ([[], [2]], []))
def test_frobenius(self): fp27 = FF(3, pivot=[-1, -1, 0, 1]) for i in range(3): for j in range(3): for k in range(3): f = [i, j, k] f3 = fp27.mult(f, fp27.mult(f, f)) self.assertEqual(f3, fp27.frobenius(f))
def test_inverse_long(self): fp27 = FF(3, pivot=[-1, -1, 0, 1]) for i in range(3): for j in range(3): for k in range(3): if not (i == 0 and j == 0 and k == 0): a = fp27.prune([i, j, k]) b = fp27.inv(a) self.assertEqual(fp27.mult(a, b), [1])
def on_click(self): errormsg = "" # Message to append to in case of bad behaviour. # Ensure poly_to_factor parses correctly. p = 1 to_factor = None pivot = None try: str = self.polynomialInput.toPlainText() to_factor = parse_unbracketed(str) except: errormsg += "Error, cannot parse polynomial to factor.\n" # Ensure p pares correctly. try: p = int(self.charpInput.toPlainText()) except: errormsg += "Error, p is not a valid integer.\n" # Ensure p is prime. if not (prime(p)): errormsg += "Error, p is not prime.\n" # Ensure pivot parses correctly. try: pivot = parse_unbracketed(self.minPolyEntry.toPlainText(), as_integer=True) except: errormsg += "Error, cannot parse minimal polynomial." # Ensure pivot is irreducible. if errormsg == "": try: field = FF(p, pivot=pivot) polyring = Poly(field) msg = factorise(to_factor, polyring) self.outputDisplay.setText(msg) except: self.outputDisplay.setText( "An error occured during factoring, please try again.") else: self.outputDisplay.setText(errormsg)
def test_inverse2(self): fp16 = FF(2, pivot=[1, 1, 0, 0, 1]) f = [1, 1, 1] g = fp16.inv(f) self.assertEqual(fp16.mult(f, g), [1])
def test_inverse1(self): fp27 = FF(3, pivot=[-1, -1, 0, 1]) a = fp27.inv([1, 1, 1]) self.assertEqual(fp27.mult(a, [1, 1, 1]), [1])
def test_inverse_Fp_error(self): fp27 = FF(3, pivot=[-1, -1, 0, 1]) self.assertRaises(ValueError, fp27.inverse_f_p, 3)
def test_inverse_Fp(self): fp27 = FF(3, pivot=[-1, -1, 0, 1]) self.assertEqual(fp27.inverse_f_p(2), 2)
def test_mult(self): fp27 = FF(3, pivot=[-1, -1, 0, 1]) self.assertEqual(fp27.mult([], [1, 2, 3]), []) self.assertEqual(fp27.mult([1, 2, 3], []), []) self.assertEqual(fp27.mult([2], [0, 2, 1]), [0, 1, 2]) self.assertEqual(fp27.mult([1, 1], [1, 1]), [1, 2, 1])
def test_element_add(self): fp27 = FF(3, pivot=[-1, -1, 0, 1]) self.assertEqual(fp27.add([1], [1]), [2]) self.assertEqual(fp27.add([1], [2]), []) self.assertEqual(fp27.add([1, 1], [0, 1, 2]), [1, 2, 2])
def test_inverse_short(self): fp7 = FF(53) for i in range(1, 7): self.assertEqual(fp7.mult([i], fp7.inv([i])), [1])