Пример #1
0
def reconstruct_polynomial(alpha, poly_alpha, field_value=257):
    temp_poly = Polynomial([0])
    t = len(alpha)
    for i in range(t):
        poly = Polynomial([1])
        temp_prod = 1
        alpha_i = alpha[i]
        for j in range(t):
            if (i != j):
                poly = poly.multiply(
                    Polynomial([1, (field_value - alpha[j]) % field_value]))
                temp_prod = temp_prod * (
                    (alpha_i - alpha[j] + field_value) % field_value)
                temp_prod %= field_value
        poly = poly.divide_by_constant(temp_prod)
        poly = poly.multiply(Polynomial([poly_alpha[i]]))
        temp_poly = temp_poly.add(poly)
    return temp_poly
	def generate_shadow_images(self, store_shadows = True):
		print ("Encryption begins!")
		img_info = self.img_info
		n, t, k = self.n, self.t, self.k

		alpha = self.alpha
		e = self.e
		poly_q = self.poly_q

		shadow_image_size = None
		shadow_images = []

		for i in range(n):
			print ("Shadow Image no :- {}".format(i))
			x_secrets = []
			y_secrets = []
			lagrange = get_Lagrange_Polynomials(e)
			prod_fun = get_prod_funs(e)

			temp_poly = Polynomial(poly_q)
			temp_poly = temp_poly.multiply(prod_fun)


			if self.debug == True:
				#For debugging whether the first term of the generated polynomial is correct
				for mmm in range(len(e)):
					value = temp_poly.eval(e[mmm])
					if value != 0:
						raise ValueError("First term of the encrypting polynomial is wrong")
						quit()

				#For debugging whether the generated Lagrange Polynomial is correct
				for nnn in range(len(e)):
					for j in range(len(lagrange)):
						if nnn != j:
							if (lagrange[j].eval(e[nnn]) != 0):
								raise ValueError("Lagrange Polynmial is wrong")
						else:
							if (lagrange[j].eval(e[nnn]) != 1):
								raise ValueError("Lagrange Polynomial is wrong")

			for x in img_info.keys():
				for y in range(len(img_info[x][1])):
					bar = img_info[x][1][y]
					sum_polyx = Polynomial([0])
					sum_polyy = Polynomial([0])
					for u in range(k):
						s_x = img_info[x][1][y][u][0]
						s_y = img_info[x][1][y][u][1]
						tempx = (lagrange[u]).multiply(Polynomial([s_x]))
						tempy = (lagrange[u]).multiply(Polynomial([s_y]))
						sum_polyx = sum_polyx.add(tempx)
						sum_polyy = sum_polyy.add(tempy)
					x_poly = temp_poly.add(sum_polyx)
					y_poly = temp_poly.add(sum_polyy)
					x_secrets += [x_poly.eval(alpha[i])]
					y_secrets += [y_poly.eval(alpha[i])]
			temp_shadow = x_secrets + y_secrets
			if (shadow_image_size is None):
				temp_size = len(temp_shadow)
				while(1):
					if (floor(sqrt(temp_size)) == ceil(sqrt(temp_size))):
						break
					else:
						temp_size += 1
				shadow_image_size = temp_size
			temp_shadow = temp_shadow + [np.random.randint(0, 256) for u in range(len(x_secrets + y_secrets), shadow_image_size)]
			temp_shadow = np.array(temp_shadow).astype(int)
			temp_shadow = temp_shadow.reshape((int(sqrt(shadow_image_size)), int(sqrt(shadow_image_size))))
			invalid_positions = []
			for x in range(temp_shadow.shape[0]):
				for y in range(temp_shadow.shape[1]):
					if temp_shadow[x, y] == 256:
						invalid_positions += [(x, y)]
						temp_shadow[x, y] = 255
			if store_shadows == True:
				cv2.imwrite("Shadows/{}.jpg".format(i), temp_shadow)
			shadow_images += [(temp_shadow, invalid_positions)]
		print ("Encryption ends!")

		return shadow_images
Пример #3
0
from Polynomial import Polynomial
from Monomial import Monomial
from LinearInterpolater import LinearInterpolator

# polynomial
mon0 = Monomial(1, 2)
mon1 = Monomial(1, 1)
mon2 = Monomial(-1, 0)

pol0 = Polynomial([mon0, mon1])
pol0.add(mon2)
pol1 = Polynomial([Monomial(1, 2), Monomial(1, 0)])

print(pol0.pretty_print())
print(pol0.print_eval(5))
print(pol0 - pol1)
print(pol1 * pol0)

#linear interpolation
x = list(range(0, 10))
li = LinearInterpolator(x, [i**2 for i in x])
Пример #4
0
from itertools import zip_longest

from EducationEmployee import EducationEmployee
from Polynomial import Polynomial
from ScienceEmployee import ScienceEmployee
from Student import Student

if __name__ == '__main__':
    p = Polynomial(1, -2, 2)  # this is 1 - 2x + 2x^2
    p2 = Polynomial(1, 0, 5)  # this is 1 + 5x^2

    print(p(3))
    print(p + p2)
    print(p.add(p2))
    print(p - p2)
    print(p * p2)
    (p2 * p).print_polynomial()  # more "mathematical" way of print
    print(bool(p(3)))
    print("###############################")
    student = Student("Ania", "Urban", "*****@*****.**", "298005",
                      {'history': 4})
    student2 = Student("Beata", "Kozidrak", "*****@*****.**", "298006")
    scienceEmployee = ScienceEmployee("Jakub", "Nowak",
                                      "*****@*****.**", 412,
                                      ["First publication"])
    educationEmployee = EducationEmployee('Adam', 'Radosny',
                                          '*****@*****.**', 523,
                                          {'Friday': '14;00 - '
                                           '15;00'})
    scienceEmployee.add_publication("some publication")
    educationEmployee.add_learning_subject("history")