def encode(message): if not all(x < p for x in message): raise Exception( "Message is improperly encoded as integers < p. It was:\n%r" % message) def row(i, b): return [Fp(i**(j)) for j in range(k)] + [Fp(b)] system = [row(i, message[i]) for i in range(k)] interpolated = someSolution(system, freeVariableValue=1) thePoly = Poly(interpolated) print("The polynomial encoding the message is:") print(thePoly) return [thePoly(Fp(i)) for i in range(n)]
def solveSystem(encodedMessage, debug=False): for e in range(maxE, 0, -1): ENumVars = e + 1 QNumVars = e + k def row(i, a, b): return ([b * a**j for j in range(ENumVars)] + [-1 * a**j for j in range(QNumVars)] + [0] ) # the "extended" part of the linear system system = ( [row(i, a, b) for (i, (a, b)) in enumerate(encodedMessage)] + [[0] * (ENumVars - 1) + [1] + [0] * (QNumVars) + [1]]) # ensure coefficient of x^e in E(x) is 1 if debug: print("\ne is %r" % e) print("\nsystem is:\n\n") for row in system: print("\t%r" % (row, )) solution = someSolution(system, freeVariableValue=1) E = Poly([solution[j] for j in range(e + 1)]) Q = Poly([solution[j] for j in range(e + 1, len(solution))]) if debug: print("\nreduced system is:\n\n") for row in system: print("\t%r" % (row, )) print("solution is %r" % (solution, )) print("Q is %r" % (Q, )) print("E is %r" % (E, )) P, remainder = Q.__divmod__(E) if remainder == 0: return Q, E raise Exception("found no divisors!")
def solveSystem(encodedMessage, debug=True): for e in range(maxE, 0, -1): ENumVars = e QNumVars = e + k def row(i, x, r): return ([r * x**j for j in range(ENumVars)] + [-1 * x**j for j in range(QNumVars)] + [-r * x**ENumVars] ) # the "extended" part of the linear system system = ([ row(i, a, b) for (i, (a, b)) in enumerate(encodedMessage) ]) # Add one more row in the end ensure coefficient of x^e in E(x) is 1 if debug: print("\nSystem of equations is:\n\n") for row in system: print("\t%r" % (row, )) solution = someSolution(system, freeVariableValue=1) E = Poly([solution[j] for j in range(ENumVars)] + [Fp(1)]) Q = Poly([solution[j] for j in range(ENumVars, len(solution))]) if debug: print("\nReduced system is:\n\n") for row in system: print("\t%r" % (row, )) print("Solution is %r" % (solution, )) print("Q is %r" % (Q, )) print("E is %r" % (E, )) P, remainder = Q.__divmod__(E) if remainder == 0: return Q, E raise Exception("found no divisors!")
def solveSystem(encodedMessage, debug=False): for e in range(maxE, 0, -1): ENumVars = e+1 QNumVars = e+k def row(i, a, b): return ([b * a**j for j in range(ENumVars)] + [-1 * a**j for j in range(QNumVars)] + [0]) # the "extended" part of the linear system system = ([row(i, a, b) for (i, (a,b)) in enumerate(encodedMessage)] + [[0] * (ENumVars-1) + [1] + [0] * (QNumVars) + [1]]) # ensure coefficient of x^e in E(x) is 1 if debug: print("\ne is %r" % e) print("\nsystem is:\n\n") for row in system: print("\t%r" % (row,)) solution = someSolution(system, freeVariableValue=1) E = Poly([solution[j] for j in range(e + 1)]) Q = Poly([solution[j] for j in range(e + 1, len(solution))]) if debug: print("\nreduced system is:\n\n") for row in system: print("\t%r" % (row,)) print("solution is %r" % (solution,)) print("Q is %r" % (Q,)) print("E is %r" % (E,)) P, remainder = Q.__divmod__(E) if remainder == 0: return Q, E raise Exception("found no divisors!")
from finitefield.finitefield import FiniteField from linearsolver.linearsolver import someSolution FF = FiniteField(13) A = [ [FF(4), FF(2), FF(9), FF(1)], [FF(4), FF(3), FF(6), FF(1)], [FF(2), FF(11), FF(5), FF(1)], ] B = someSolution(A)