def Inv_gcd(a, b): r0, r1, s0, s1 = 1, 0, 0, 1 while b: qt, rt = division(a, b) q, a, b = qt, b, rt r0, r1 = r1, r0 ^ int(mul(hex(q)[2:], hex(r1)[2:], '11B'), 16) s0, s1 = s1, s0 ^ int(mul(hex(q)[2:], hex(s1)[2:], '11B'), 16) return s0
def inv(column): fixedmatrix = bytearray([0x0e, 0x0b, 0x0d, 0x09]) mixcolumnmatrix = bytearray() for i in range(0, 4): resultinglist = [] rijndael = gf.makeblist(0x11b) for j in range(0, 4): fixedbitlist = gf.makeblist(fixedmatrix[j]) columnvector = gf.makeblist(column[j]) resultingbyte = gf.mul(fixedbitlist, columnvector) while gf.value(resultingbyte) > 255: # note the inverted division function! resultingbyte = gf.div(rijndael, resultingbyte)[1] resultinglist.append(resultingbyte) finalvalue = gf.add(resultinglist[0], resultinglist[1]) finalvalue = gf.add(finalvalue, resultinglist[2]) finalvalue = gf.add(finalvalue, resultinglist[3]) mixcolumnmatrix.append(gf.value(finalvalue)) fixedmatrix = gf.circrotateright(fixedmatrix) return mixcolumnmatrix
def mix(column): fixedmatrix = bytearray([0x02, 0x03, 0x01, 0x01]) mixcolumnmatrix = bytearray() for i in range(4): resultinglist = [] rijndael = gf.makeblist(0x11b) for j in range(4): fixedbytelist = gf.makeblist(fixedmatrix[j]) columnvector = gf.makeblist(column[j]) resultingbyte = gf.mul(fixedbytelist, columnvector) if gf.value(resultingbyte) > 255: resultingbyte = gf.div(resultingbyte, rijndael)[1] resultinglist.append(resultingbyte) finalvalue = gf.add(resultinglist[0], resultinglist[1]) finalvalue = gf.add(finalvalue, resultinglist[2]) finalvalue = gf.add(finalvalue, resultinglist[3]) mixcolumnmatrix.append(gf.value(finalvalue)) fixedmatrix = gf.circrotateright(fixedmatrix) return mixcolumnmatrix
def recover2d_s0s1(D, failed): n = len(D) - ns j = failed[0] k = failed[1] old_s0 = D[-2] old_s1 = D[-1] new_s0 = calc_s0(D) new_s1 = calc_s1(D) x1 = gf.inv(gf.pow(n - k - 1)) x2 = gf.inv(gf.pow(k - j) ^ 1) Dj = gf.mul((gf.mul(old_s1 ^ new_s1, x1) ^ old_s0 ^ new_s0), x2) Dk = old_s0 ^ new_s0 ^ Dj return Dj, Dk
def recover1d_s1(D, failed): n = len(D) - ns new_s1 = calc_s1(D) old_s1 = D[-1] x = gf.pow(n - failed - 1) x1 = gf.inv(x) return gf.mul(new_s1 ^ old_s1, x1)
def sdc_detect(D): n = len(D) - ns old_s0 = D[-2] old_s1 = D[-1] new_s0 = calc_s0(D) new_s1 = calc_s1(D) if new_s0 == old_s0 and new_s1 == old_s1: return -1 j = n - 1 - gf.log( gf.mul((old_s1 ^ new_s1), gf.inv(old_s0 ^ new_s0)) ) D[j] = old_s0 ^ new_s0 ^ D[j] return j
def maketable(): rcontable = [] rcontable.append([1, 0, 0, 0, 1, 1, 0, 1]) # rcon[0] rcon = gf.makeblist(0x01) rijn = gf.makeblist(0x11b) for i in range(52): rcontable.append(rcon) # this is actually a leftwise shift, multiply by 2 rcon = gf.mul(rcon, [0, 0, 0, 0, 0, 0, 1, 0]) if gf.value(rcon) > 255: # take the remainder from division operation (modulus) rcon = gf.div(rcon, rijn)[1] return rcontable
\t\t\t[4] A(x) / B(x) \t\t\t[5] Change Values \t\t\t[6] Exit \t\t-------------------------------- """ while True: try: op = input("\t\t\t\tCHOOSE AN OPERATION: ") break except SyntaxError: print "\n\t\t\t\tPlease input a valid choice.\n" if op == 1 or op == 2: gf.printGiven(Ax, Bx, Px) gf.printPoly(gf.addOrSub(Ax, Bx, op, 1)) elif op == 3: gf.printGiven(Ax, Bx, Px) gf.printPoly(gf.mul(Ax, Bx, Px, 1)) elif op == 4: gf.printGiven(Ax, Bx, Px) quo, rem = gf.div(Ax, Bx, Px, 1) gf.printPoly(quo) print "\t\t\t\tRemainder = ", gf.printPoly(rem) elif op == 5: break elif op == 6: print "\n\t\t\t\t\tAu Revoir!\n" exit() else: print "\n\t\t\t\tInvalid choice. Input 1 - 6 only.\n"