def fieldAdd(array1,array2): """Given two field elements, defines the additive operation between them over GF(2), since we are working over GF(2), this is the same as subtraction """ a = truthtable.fromBinary(array1[::-1]) b = truthtable.fromBinary(array2[::-1]) added = a^b ans = truthtable.binary(added, max(len(array1),len(array2)))[::-1] return ans[:primpoly.polyDeg(ans)+1]
def divAlg(array1,array2): """ Given two polynomials, performs the division algorithm to find the quotient and remainder of the polynomial of first the polynomial divided by the second over the field GF(2) """ dif= polyDeg(array1) -polyDeg(array2) if dif < 0: return [0], array1 a = [0 for i in range(dif)] q = truthtable.binary(2**(dif),len(array1))[::-1] m = a + array2 r = truthtable.binary((truthtable.fromBinary(m[::-1]))^(truthtable.fromBinary(array1[::-1])), len(array1))[::-1] newQ,newR = divAlg(r,array2) return [truthtable.binary((truthtable.fromBinary(newQ[::-1]))^(truthtable.fromBinary(q[::-1])), len(q))[::-1],newR]
def bigFPolyDeg(array): """Given a polynomial in F_{2^{n}}[x], returns its degree """ for i in range(len(array)-1,-1,-1): if truthtable.fromBinary(array[i]) > 0: return i return 0
def fieldInv(array, dpoly): """Given a field element, finds its inverse in the field defined by dpoly """ if truthtable.fromBinary(array)==0: raise Exception("No inverse for zero element") field = fieldGen(len(dpoly)-1) for element in field: if fieldMult(element, array, dpoly) == [1]: return element[:primpoly.polyDeg(element)+1] break
def divChecker(array1, array2): """ Given two polynomials, determines whether one divides the other """ if polyDeg(array1) == polyDeg(array2): return False a = divAlg(array1, array2)[1] j = truthtable.fromBinary(a[::-1]) if j == 0: return True else: return False
def irrChecker(array): """ Given a particular polynomial, prints true if it is irreducible false if it is not """ d = polyDeg(array) for i in range(2,2**(d)+1): j = truthtable.binary(i,len(array))[::-1] a = divAlg(array,j)[1] b = truthtable.fromBinary(a[::-1]) if b == 0: return False return True