Untitled

1118 days ago by pub

class FF: def __init__(self, n, irredpoly = None): assert(n in NN and is_prime(n)) self.elements = [] self.n = n self.irredpoly = irredpoly ##--SET RING--## if irredpoly is None: self.ring = Integers(n) elif irredpoly is not None: self.ring = PolynomialRing(Integers(n),'x').quotient(irredpoly, 'x') else: return "invalid input" self.gen = self.ring.gen() ##--SET MODULUS--## if irredpoly is None: self.modulus = self.n else: self.modulus = self.irredpoly def createAllElements(self): if self.irredpoly == None: self.elements = [((self.gen)*i).mod(self.modulus) for i in [1,..,self.n-1]] self.elements.append(0) else: self.elements = [((self.gen)^i).mod(self.modulus) for i in [1,..,(self.n)^self.irredpoly.degree(x)-1]] self.elements.append(0) #-------------------------------------------------------------------------------# class Element: def __init__(self,FF,m = None): self.FF = FF self.m = m if self.m == None: self.content = FF.ring.random_element() else: self.content = FF.elements[self.m] def Plus(self, other): assert(self.FF == other.FF) new = copy(self) new.content = (self.content + other.content).mod(self.FF.modulus) return new def Mult(self, other): assert(self.FF == other.FF) new = copy(self) new.content = (self.content * other.content).mod(self.FF.modulus) return new def Power(p): new = copy(self) new.content = ((self.content)^p).mod(self.FF.modulus) return new def frob(self, other, p): assert(self.FF == other.FF) new = copy(self) new.content = ((self.content + other.content)^p).mod(self.FF.modulus) return new
f8 = FF(2,x^3+x^2+1) #f8.createAllElements() e = Element(f8) print e.content
 x^2 + x + 1 x^2 + x + 1
f5 = FF(5) f5.createAllElements() print f5.elements el1 = Element(f5) el2 = Element(f5) print el1.content, el2.content, el1.Plus(el2).content
 [1, 2, 3, 4, 0] 2 0 2 [1, 2, 3, 4, 0] 2 0 2