def testMonic(self): t1 = gfp(3, [2, 0, 1]) self.assertEqual(t1.monic(), gfp(3, [1, 0, 2])) t1 = gfp(3, [0, 0]) self.assertRaises(ValueError, t1.monic) t1 = gfp(7, [2, 3, 2, 6]) self.assertEqual(t1.monic(), gfp(7, [1, 5, 1, 3]))
def testInvTable(self): t = gfp(2) self.assertEqual(t.invTable, [0, 1]) t = gfp(3) self.assertEqual(t.invTable, [0, 1, 2]) t = gfp(7) self.assertEqual(t.invTable, [0, 1, 4, 5, 2, 3, 6])
def testInv(self): t = gfp(2) self.assertEqual(t.inv(1), 1) self.assertRaises(ValueError, t.inv, 0) t = gfp(7) self.assertEqual(t.inv(6), 6) self.assertEqual(t.inv(3), 5)
def testPolySub(self): t1 = gfp(3, [1, 0, 2]) t2 = gfp(3, [1, 1]) self.assertEqual(t1.polySub(t2), gfp(3, [1, 2, 1])) t1 = gfp(2, [0, 1, 0]) t2 = gfp(2, [1, 0, 0, 1]) self.assertEqual(t1.polySub(t2), gfp(2, [1, 0, 1, 1])) t1 = gfp(7, [3, 1, 4, 1]) t2 = gfp(7, [5, 2, 6, 0, 2]) self.assertEqual(t1.polySub(t2), gfp(7, [2, 1, 2, 4, 6]))
def testConv(self): t1 = gfp(2, [1, 0, 1]) t2 = gfp(2, [1, 1, 0]) self.assertEqual(t1.conv(t2), gfp(2, [1, 1, 1, 1, 0])) t1 = gfp(2, [0, 0, 1]) self.assertEqual(t1.conv(t2), gfp(2, [0, 0, 1, 1, 0])) t1 = gfp() t2 = gfp(2, [1, 0, 1]) self.assertEqual(t1.conv(t2), gfp())
def primitivePoly(p, n): """ primitive polynomials of characteristic-p of order n-1 """ iPoly = irreduciblePoly(p, n + 1) #print(iPoly) candidates = [gfp(p, v) for v in iPoly if len(v) == n + 1] #print(candidates) fieldPoly = [0 for i in range(p**n)] fieldPoly[0], fieldPoly[-1] = 1, -1 fieldPoly = gfp(p, fieldPoly) #print(fieldPoly) pPoly = [] for i in range(len(candidates)): q, r = fieldPoly.deconv(candidates[i]) if r == gfp(p): pPoly.append(candidates[i]) return pPoly
def findPrimitiveElement(p, n, genPoly): mPoly = [gfp(p, v) for v in enumerateMonicPoly(p, n)] order = p**n isPrimitive = [True for i in range(order - 2)] cyclicGroup = [gfp(p, [1])] i = 0 while len(cyclicGroup) < order - 1: cyclicGroup = [gfp(p, [1])] while not isPrimitive[i]: i += 1 nextPoly = mPoly[i] while not nextPoly == cyclicGroup[0]: isPrimitive[mPoly.index(nextPoly)] = False cyclicGroup.append(nextPoly) dummy, nextPoly = nextPoly.conv(mPoly[i]).deconv(genPoly) return cyclicGroup
def test__init__(self): t = gfp(2, 1) #self.assertRaises(ValueError, gfp, 2, []) t = gfp(2, 0) t = gfp(3, [1]) t = gfp(3, [1, 2, 0]) t = gfp() t = gfp(3, [1, -1])
def generateAdditionTable(p, n, genPoly): cyclicGroup = gf.findPrimitiveElement(p, n, genPoly) elem = cyclicGroup + [gfp(p)] # generator = gfp(p, [1, 1]) # elem = [gfp(p, 1), generator] # order = p**n # for i in range(2, order - 1): # nextPoly = elem[-1].conv(generator) # q, r = nextPoly.deconv(genPoly) # print(r) # if r in elem: # print('stop') # elem.append(r) # # elem.append(gfp(p)) addTab = [] order = p**n for i in range(order): addTab.append( [elem.index(elem[i].polyAdd(elem[j])) for j in range(order)]) return addTab
def testDeconv(self): t1 = gfp(2, [1, 0, 1, 1]) t2 = gfp(2, [0, 1, 1]) self.assertEqual(t1.deconv(t2), (gfp(2, [1, 1, 0]), gfp(2, [1]))) t1 = gfp(2, [1, 0, 1, 0, 0, 0, 1]) t2 = gfp(2, [1, 0, 1, 0, 1]) self.assertEqual(t1.deconv(t2), (gfp(2, [1, 0, 0]), gfp(2, [1, 0, 1]))) t1 = gfp(2, [1, 1, 0]) t2 = gfp(2, 1) self.assertEqual(t1.deconv(t2), (gfp(2, [1, 1, 0]), gfp())) t1 = gfp(2, [1, 1, 0]) t2 = gfp() self.assertRaises(ZeroDivisionError, t1.deconv, t2) t1 = gfp(7, [3, 1, 4, 1, 5, 2]) t2 = gfp(7, [2, 3, 5]) self.assertEqual(t1.deconv(t2), (gfp(7, [5, 0, 0, 4]), gfp(7, 3))) t1 = gfp() t2 = gfp(2, [1, 1]) self.assertEqual(t1.deconv(t2), (gfp(), gfp()))
def testProper(self): t1 = gfp(2, [0, 0, 1, 0]) self.assertEqual(t1.proper(), gfp(2, [1, 0])) t1 = gfp() self.assertEqual(t1, gfp(2, [0])) t1 = gfp(2) self.assertEqual(t1, gfp(2, [0])) t1 = gfp(2, [0]) self.assertEqual(t1, gfp()) t1 = gfp(2, [0, 0]) self.assertEqual(t1, gfp()) t1 = gfp(3, [1, -1]) t2 = gfp(3, [1, 2]) self.assertEqual(t1, t2)