Пример #1
0
class BinaryTab(Tab):

    def __init__(self, master, name):
        Tab.__init__(self, master, name)

        self.a = Polynomial("z^8 + z") # initially E : y^2 + xy = x^3 + (z^8 + z)x^2 + (z + 1)
        self.b = Polynomial("z + 1")
        self.r = 9 # field is initially GF(2^9)
        self.modulus = Polynomial("z^9 + z^8 + 1") # initial irred poly
        self.Gx = Polynomial("z^8 + z^6 + z^2 + 1") # initially G = (z^8 + z^6 + z^2 + 1, z^7 + z^5 + z^4 + z + 1)
        self.Gy = Polynomial("z^7 + z^5 + z^4 + z + 1")
        self.Px = Polynomial("z^8 + z^5 + z^3 + z") # initially P = (z^8 + z^5 + z^3 + z, z^8 + z^4 + z + 1)
        self.Py = Polynomial("z^8 + z^4 + z + 1")
        self.Qx = Polynomial("z^6 + z^4 + z^3 + z") # initially Q = (z^6 + z^4 + z^3 + z, z^5 + 1)
        self.Qy = Polynomial("z^5 + 1")
        self.k = 179
        self.n = 5 # initially n = 5 random curves
        self.m = 10 # initially m = 10 random points

        outputFrame = Frame(self)
        Button(outputFrame, text="Clear Output", bg="blue", fg="white", command=(lambda: self.clear()), borderwidth=1).pack(side=BOTTOM, pady=3)
        self._output = Text(outputFrame, height=15, width=70)
        self._output.insert(END, "You are responsible for making sure the irreducible polynomial you use is actually irreducible. "
                                 "The default, z^9 + z^8 + 1, is irreducible.\n\n")
        self._output.pack(side=LEFT)
        scrollbar = Scrollbar(outputFrame)
        scrollbar.pack(side=RIGHT, fill=Y)
        scrollbar.config(command=self._output.yview)

        ecInfo = Frame(self)
        polyFrame = Frame(self)
        ptsInfo1 = Frame(self)
        ptsInfo2 = Frame(self)
        arithmeticFrame = Frame(self)
        orderFrame = Frame(self)
        listFrame = Frame(self)
        ptsFrame = Frame(self)

        self.curveString = StringVar()
        self.curveString.set("E : F_(2^" + str(self.r) + ") : y^2 + xy = x^3 + ")
        curveLabel = Label(ecInfo, textvariable=self.curveString, bg="orange", fg="black", borderwidth=1).grid(row=1, column=1)
        self._a = Entry(ecInfo, width=25)
        self._a.insert(0, self.a)
        self._a.bind("<Return>", (lambda event: self.changeA()))
        self._a.grid(row=1, column=2)
        Label(ecInfo, text="x^2 + ", bg="orange", fg="black", borderwidth=1).grid(row=1, column=3)
        self._b = Entry(ecInfo, width=25)
        self._b.insert(0, self.b)
        self._b.bind("<Return>", (lambda event: self.changeB()))
        self._b.grid(row=1, column=4)

        Label(polyFrame, text="Irreducible Polynomial: ", bg="orange", fg="black", borderwidth=1).grid(row=1, column=1, pady=3)
        self._irred = Entry(polyFrame, width=51)
        self._irred.insert(0, self.modulus)
        self._irred.bind("<Return>", (lambda event: self.changeModulus()))
        self._irred.grid(row=1, column=2, pady=3)

        Label(ptsInfo1, text="Generator G = (", bg="orange", fg="black", borderwidth=1).grid(row=1, column=1, pady=3)
        self._Gx = Entry(ptsInfo1, width=15)
        self._Gx.insert(0, self.Gx)
        self._Gx.bind("<Return>", (lambda event: self.changeG()))
        self._Gx.grid(row=1, column=2, pady=3)
        Label(ptsInfo1, text=", ", bg="orange", fg="black", borderwidth=1).grid(row=1, column=3, pady=3)
        self._Gy = Entry(ptsInfo1, width=15)
        self._Gy.insert(0, self.Gy)
        self._Gy.bind("<Return>", (lambda event: self.changeG()))
        self._Gy.grid(row=1, column=4, pady=3)
        Label(ptsInfo1, text="), k = ", bg="orange", fg="black", borderwidth=1).grid(row=1, column=5, pady=3)
        self._k = Entry(ptsInfo1, width=5)
        self._k.insert(0, self.k)
        self._k.bind("<Return>", (lambda event: self.changeK()))
        self._k.grid(row=1, column=6, pady=3)

        Label(ptsInfo2, text="P = ", bg="blue", fg="white", borderwidth=1).grid(row=1, column=1, pady=3)
        self._Px = Entry(ptsInfo2, width=15)
        self._Px.insert(0, self.Px)
        self._Px.bind("<Return>", (lambda event: self.changeP()))
        self._Px.grid(row=1, column=2, pady=3)
        Label(ptsInfo2, text=", ", bg="blue", fg="white", borderwidth=1).grid(row=1, column=3, pady=3)
        self._Py = Entry(ptsInfo2, width=15)
        self._Py.insert(0, self.Py)
        self._Py.bind("<Return>", (lambda event: self.changeP()))
        self._Py.grid(row=1, column=4, pady=3)
        Label(ptsInfo2, text="), Q = ", bg="blue", fg="white", borderwidth=1).grid(row=1, column=5, pady=3)
        self._Qx = Entry(ptsInfo2, width=15)
        self._Qx.insert(0, self.Qx)
        self._Qx.bind("<Return>", (lambda event: self.changeQ()))
        self._Qx.grid(row=1, column=6, pady=3)
        Label(ptsInfo2, text=", ", bg="blue", fg="white", borderwidth=1).grid(row=1, column=7, pady=3)
        self._Qy = Entry(ptsInfo2, width=15)
        self._Qy.insert(0, self.Qy)
        self._Qy.bind("<Return>", (lambda event: self.changeQ()))
        self._Qy.grid(row=1, column=8)
        Label(ptsInfo2, text=")", bg="blue", fg="white", borderwidth=1).grid(row=1, column=9, pady=3)

        Button(arithmeticFrame, text="P + Q", bg="blue", fg="white", command=(lambda: self.add())).grid(row=1, column=1, padx=5, pady=3)
        Button(arithmeticFrame, text="kP", bg="blue", fg="white", command=(lambda: self.mult())).grid(row=1, column=2, padx=5, pady=3)
        Button(arithmeticFrame, text="log_G(P)", bg="blue", fg="white", command=(lambda: self.log())).grid(row=1, column=3, padx=5, pady=3)

        Button(orderFrame, text="Order(E)", bg="blue", fg="white", command=(lambda: self.order())).grid(row=1, column=1, padx=5, pady=3)
        Button(orderFrame, text="Order(G)", bg="blue", fg="white", command=(lambda: self.pointOrder())).grid(row=1, column=2, padx=5, pady=3)

        Label(listFrame, text="Generate ", bg="orange", fg="black", borderwidth=1).grid(row=1, column=1, pady=3)
        self._n = Entry(listFrame, width=3)
        self._n.insert(0, self.n)
        self._n.bind("<Return>", (lambda event: self.changeN()))
        self._n.grid(row=1, column=2, pady=3)
        Button(listFrame, text="Random ECs", bg="orange", fg="black", command=(lambda: self.randomECs())).grid(row=1, column=3, padx=5, pady=3)

        Label(ptsFrame, text="List ", bg="orange", fg="black", borderwidth=1).grid(row=1, column=1, pady=3)
        self._m = Entry(ptsFrame, width=3)
        self._m.insert(0, self.m)
        self._m.bind("<Return>", (lambda event: self.changeM()))
        self._m.grid(row=1, column=2, pady=3)
        Button(ptsFrame, text="Random points on E", bg="orange", fg="black", command=(lambda: self.randomPoints())).grid(row=1, column=3, padx=5, pady=3)

        ecInfo.pack(side=TOP)
        polyFrame.pack(side=TOP)
        ptsInfo1.pack(side=TOP)
        ptsInfo2.pack(side=TOP)
        arithmeticFrame.pack(side=TOP)
        orderFrame.pack(side=TOP)
        listFrame.pack(side=TOP)
        ptsFrame.pack(side=TOP)
        outputFrame.pack(side=BOTTOM)

    def changeA(self):
        temp = self._a.get()
        if Polynomial.isValid(temp):
            self.a = Polynomial(temp) % self.modulus
            self._a.delete(0, END)
            self._a.insert(0, self.a)
            E = BinaryEllipticCurve(self.a, self.b, self.modulus)
            self._output.insert(END, str(E) + "\n")
        else:
            self._a.delete(0, END)
            self._a.insert(0, self.a)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def changeB(self):
        temp = self._b.get()
        if Polynomial.isValid(temp):
            self.b = Polynomial(temp) % self.modulus
            self._b.delete(0, END)
            self._b.insert(0, self.b)
            E = BinaryEllipticCurve(self.a, self.b, self.modulus)
            self._output.insert(END, str(E) + "\n")
        else:
            self._b.delete(0, END)
            self._b.insert(0, self.b)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def changeModulus(self):
        temp = self._irred.get()
        if Polynomial.isValid(temp):
            self.modulus = Polynomial(temp)
            self.r = self.modulus.degree()
            self.curveString.set("E : F_(2^" + str(self.r) + ") : y^2 + xy = x^3 + ")
            self._irred.delete(0, END)
            self._irred.insert(0, self.modulus)
            E = BinaryEllipticCurve(self.a, self.b, self.modulus)
            self._output.insert(END, "The irreducible polynomial is now set to " + str(self.modulus) + ".\n")
        else:
            self._irred.delete(0, END)
            self._irred.insert(0, self.modulus)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def changeG(self):
        tempx = self._Gx.get()
        tempy = self._Gy.get()
        if Polynomial.isValid(tempx) and Polynomial.isValid(tempy):
            self.Gx = Polynomial(tempx) % self.modulus
            self.Gy = Polynomial(tempy) % self.modulus
            self._Gx.delete(0, END)
            self._Gy.delete(0, END)
            self._Gx.insert(0, self.Gx)
            self._Gy.insert(0, self.Gy)
            self._output.insert(END, "G = " + str(PolynomialPoint(self.Gx, self.Gy, 1)) + "\n")
        else:
            self._Gx.delete(0, END)
            self._Gy.delete(0, END)
            self._Gx.insert(0, self.Gx)
            self._Gy.insert(0, self.Gy)
            self._output.insert(END, "There was an error with your input. Please try again.\n")


    def changeP(self):
        tempx = self._Px.get()
        tempy = self._Py.get()
        if Polynomial.isValid(tempx) and Polynomial.isValid(tempy):
            self.Px = Polynomial(tempx) % self.modulus
            self.Py = Polynomial(tempy) % self.modulus
            self._Px.delete(0, END)
            self._Py.delete(0, END)
            self._Px.insert(0, self.Px)
            self._Py.insert(0, self.Py)
            self._output.insert(END, "P = " + str(PolynomialPoint(self.Px, self.Py, 1)) + "\n")
        else:
            self._Px.delete(0, END)
            self._Py.delete(0, END)
            self._Px.insert(0, self.Px)
            self._Py.insert(0, self.Py)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def changeQ(self):
        tempx = self._Qx.get()
        tempy = self._Qy.get()
        if Polynomial.isValid(tempx) and Polynomial.isValid(tempy):
            self.Qx = Polynomial(tempx) % self.modulus
            self.Qy = Polynomial(tempy) % self.modulus
            self._Qx.delete(0, END)
            self._Qy.delete(0, END)
            self._Qx.insert(0, self.Qx)
            self._Qy.insert(0, self.Qy)
            self._output.insert(END, "Q = " + str(PolynomialPoint(self.Qx, self.Qy, 1)) + "\n")
        else:
            self._Qx.delete(0, END)
            self._Qy.delete(0, END)
            self._Qx.insert(0, self.Qx)
            self._Qy.insert(0, self.Qy)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def changeK(self):
        try:
            self.k = int(self._k.get())
            self._k.delete(0, END)
            self._k.insert(0, self.k)
            self._output.insert(END, "k = " + str(self.k) + "\n")
        except ValueError:
            self._k.delete(0, END)
            self._k.insert(0, self.k)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def changeN(self):
        try:
            self.n = int(self._n.get())
            self._n.delete(0, END)
            self._n.insert(0, self.n)
            self._output.insert(END, "Random curve selection will now generate " + str(self.n) + " curve")
            if self.n != 1:
                self._output.insert(END, "s")
            self._output.insert(END, ".\n")
        except ValueError:
            self._n.delete(0, END)
            self._n.insert(0, self.n)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def changeM(self):
        try:
            self.m = int(self._m.get())
            self._m.delete(0, END)
            self._m.insert(0, self.m)
            self._output.insert(END, "Random point selection will now generate " + str(self.m) + " point")
            if self.m != 1:
                self._output.insert(END, "s")
            self._output.insert(END, ".\n")
        except ValueError:
            self._m.delete(0, END)
            self._m.insert(0, self.m)
            self._output.insert(END, "There was an error with your input. Please try again.\n")

    def add(self):
        P = PolynomialPoint(self.Px, self.Py, Polynomial("1"))
        Q = PolynomialPoint(self.Qx, self.Qy, Polynomial("1"))
        R = P.add(Q, self.a, self.b, self.modulus)
        self._output.insert(END, str(P) + " + " + str(Q) + " = " + str(R) + "\n")

    def mult(self):
        P = PolynomialPoint(self.Px, self.Py, Polynomial("1"))
        R = P.mult(self.k, self.a, self.b, self.modulus)
        self._output.insert(END, str(self.k) + str(P) + " = " + str(R) + "\n")

    def log(self):
        E = BinaryEllipticCurve(self.a, self.b, self.modulus)
        P = PolynomialPoint(self.Px, self.Py, Polynomial("1"))
        G = PolynomialPoint(self.Gx, self.Gy, Polynomial("1"))
        R = E.log(P, G)
        self._output.insert(END, "log_G" + str(P) + " = " + str(R) + "\n")

    def order(self):
        ord = BinaryEllipticCurve(self.a, self.b, self.modulus).order()
        self._output.insert(END, "|E| = " + str(ord) + "\n")

    def pointOrder(self):
        ord = BinaryEllipticCurve(self.a, self.b, self.modulus).pointOrder(PolynomialPoint(self.Gx, self.Gy, Polynomial("1")))
        self._output.insert(END, "|G| = " + str(ord) + "\n")

    def randomECs(self):
        s = BinaryEllipticCurve.listRandomECs((1<<self.modulus.degree()), self.modulus, self.n)
        self._output.insert(END, s + "\n")

    def randomPoints(self):
        s = BinaryEllipticCurve.listRandomPoints(BinaryEllipticCurve(self.a, self.b, self.modulus), self.m)
        self._output.insert(END, s + "\n")

    def clear(self):
        self._output.delete(1.0, END)
        self._output.insert(END, "You are responsible for making sure the irreducible polynomial you use is actually irreducible. "
                                 "The default, z^9 + z^8 + 1, is irreducible.\n\n")
Пример #2
0
def polynomialFunctions(PolExpr):

	newpoli = 0

	for c in PolExpr:
		if c == '[':
			newpoli = 1
		elif c == ']':
			newpoli = 0
		if c == '+' and newpoli == 0:
			operation = PolExpr.split('+', 1)
			pol1 = strtoPolynomialArray(operation[0])
			pol2 = strtoPolynomialArray(operation[1])
			polynomial1 = Polynomial(pol1)
			polynomial2 = Polynomial(pol2)
			print (polynomial1.tostr())
			print (polynomial2.tostr())
			print ("+__________________")
			return (polynomial1.addition(polynomial2).tostr() + "\n")
		elif c == '-' and newpoli == 0:
			operation = PolExpr.split('-', 1)
			pol1 = strtoPolynomialArray(operation[0])
			pol2 = strtoPolynomialArray(operation[1])
			polynomial1 = Polynomial(pol1)
			polynomial2 = Polynomial(pol2)
			print (polynomial1.tostr())
			print (polynomial2.tostr())
			print ("-__________________")
			return (polynomial1.substraction(polynomial2).tostr() + "\n")
		elif c == '*' and newpoli == 0:
			operation = PolExpr.split('*', 1)
			pol1 = strtoPolynomialArray(operation[0])
			pol2 = strtoPolynomialArray(operation[1])
			polynomial1 = Polynomial(pol1)
			polynomial2 = Polynomial(pol2)
			print (polynomial1.tostr())
			print (polynomial2.tostr())
			print ("*__________________")
			return (polynomial1.multiplication(polynomial2).tostr() + "\n")
		elif c == '/' and newpoli == 0:
			operation = PolExpr.split('/', 1)
			pol1 = strtoPolynomialArray(operation[0])
			pol2 = strtoPolynomialArray(operation[1])
			polynomial1 = Polynomial(pol1)
			polynomial2 = Polynomial(pol2)
			print (polynomial1.tostr())
			print (polynomial2.tostr())
			print ("/__________________")
			if polynomial1.degree() < polynomial2.degree():
				return "Numerator must have a higher or equal degree compare with the denominator to be able divide both"
			if polynomial2.degree() == 0:
				return "Division by 0 error"
			result = []
			result = polynomial1.division(polynomial2)
			if len(result) == 3:
				return result[0].tostr() + " + " + result[2].tostr() + "/" + "(" + result[1].tostr() + ")" + "\n"
			return (result[0].tostr() + "\n")
		elif c == '#' and newpoli == 0:
			operation = PolExpr.split('#', 1)
			pol1 = strtoPolynomialArray(operation[0])
			polynomial1 = Polynomial(pol1)
			print (polynomial1.tostr())
			print ("#__________________")
			return (polynomial1.differentiate().tostr() + "\n")
		elif c == '@' and newpoli == 0:
			operation = PolExpr.split('@', 1)
			pol1 = strtoPolynomialArray(operation[0])
			evalnum = (operation[1].replace("(", "")).replace(")", "")
			polynomial1 = Polynomial(pol1)
			print (polynomial1.tostr())
			print ("@__________________")
			return (str(polynomial1.eval(int(evalnum)))+ "\n")