def decode_and_decrypt(HE, t, ret_dict, ind): temp_file = tempfile.NamedTemporaryFile() enc_res = np.array( [[[[decode_ciphertext(value, temp_file) for value in row] for row in column] for column in layer] for layer in t]) dec_res = decrypt_matrix(HE, enc_res) ret_dict[ind] = dec_res
def test_decrypt_matrix(self, HE): matrix = np.array([[ [[1, 2] , [3, 4] ], [[5, 6] , [7, 8] ] ] , [ [[10, 20] , [30, 40] ], [[50, 60] , [70, 80] ] ]]) enc_matrix = crypto.encrypt_matrix(HE, matrix) result = crypto.decrypt_matrix(HE, enc_matrix) assert result[0][0][0][0] == 1 assert result[0][0][0][1] == 2 assert result[0][0][1][0] == 3 assert result[0][0][1][1] == 4 assert result[0][1][0][0] == 5 assert result[0][1][0][1] == 6 assert result[0][1][1][0] == 7 assert result[0][1][1][1] == 8 assert result[1][0][0][0] == 10 assert result[1][0][0][1] == 20 assert result[1][0][1][0] == 30 assert result[1][0][1][1] == 40 assert result[1][1][0][0] == 50 assert result[1][1][0][1] == 60 assert result[1][1][1][0] == 70 assert result[1][1][1][1] == 80
def test_square_layer(self, HE): image = np.array([[ [[1, 1, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 1, 1, 1], [0, 0, 1, 1, 0], [0, 1, 1, 0, 0]], [[1, 0, 1, 1, 0], [0, 1, 2, 0, 0], [0, 0, 1, 0, 1], [0, 1, 1, 1, 0], [0, 1, 0, 0, 0]], ], [[[1, 1, 1, 0, 0], [0, 1, 2, 1, 1], [0, 0, 1, 1, 1], [1, 0, 2, 1, 0], [0, 0, 1, 0, 0]], [[-1, 0, 2, 0, 0], [0, 1, 1, 1, 2], [1, 0, 0, 1, 1], [0, 0, 1, 1, 0], [0, 0, 2, 0, 0]]]]) expected_result = np.array([[ [[1, 1, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 1, 1, 1], [0, 0, 1, 1, 0], [0, 1, 1, 0, 0]], [[1, 0, 1, 1, 0], [0, 1, 4, 0, 0], [0, 0, 1, 0, 1], [0, 1, 1, 1, 0], [0, 1, 0, 0, 0]], ], [[[1, 1, 1, 0, 0], [0, 1, 4, 1, 1], [0, 0, 1, 1, 1], [1, 0, 4, 1, 0], [0, 0, 1, 0, 0]], [[1, 0, 4, 0, 0], [0, 1, 1, 1, 4], [1, 0, 0, 1, 1], [0, 0, 1, 1, 0], [0, 0, 4, 0, 0]]]]) encrypted_image = cr.encrypt_matrix(HE, image) encrypted_result = sq.square(HE, encrypted_image) result = cr.decrypt_matrix(HE, encrypted_result) assert np.allclose(expected_result, result, rtol=1e-3)
def test__avg(self, HE): """ Procedure: 1. Create a image in the form [y, x] 2. Encrypt the image 3. Use __avg --------- Verification: 4. Verify the result is the expected """ # Shape of image is [1, 1, 5, 5]. Needed to use encrypt_matrix. image = np.array([[[[1, -1, 1, 0, 0], [0, 1, -2, 1, 0], [0, 0, 1, 1, 1], [0, -3, 1, 1, 0], [0, 1, 1, 0, 0]]]]) encrypted_image = cr.encrypt_matrix(HE, image) encrypted_result = avg._avg(HE, encrypted_image[0][0], kernel_size=(3, 3), stride=(2, 2)) result = cr.decrypt_matrix(HE, encrypted_result) expected_result = np.array([[0.1111, 0.3333], [0.1111, 0.6667]]) assert np.allclose(result, expected_result, 0.001)
def test_call(self): HE = Pyfhel() HE.contextGen(65537) HE.keyGen() HE.relinKeyGen(30, 100) new_weights = np.array([ [1, 2, 3, 4] ,[2, 3, 4, 5] ,[3, 4, 5, 6] ]) new_bias = np.array([1, 2, 3]) linear_layer = lin.LinearLayer(HE, new_weights, new_bias) flattened_input = np.array([ [1, 2, 3, 4] , [5, 6, 7, 8] ]) encrypted_input = cr.encrypt_matrix(HE, flattened_input) encrypted_result = linear_layer(encrypted_input) result = cr.decrypt_matrix(HE, encrypted_result) expected_result = np.array([ [31, 42, 53] ,[71, 98, 125] ]) assert np.allclose(result, expected_result)
def test_rencryption_layer(self, HE): image = np.array([[1, 1, -1, 0, 0], [1, 0, 1, 1, 0]]) enc_image = cr.encrypt_matrix(HE, image) rencryption_layer = RencryptionLayer(HE) enc_result = rencryption_layer(enc_image) result = cr.decrypt_matrix(HE, enc_result) assert np.allclose(image, result)
def test_square_layer2D(self, HE): image = np.array([[1, -2, 1, 3, 0], [0, 1, 4, 1, 0], [0, 2, 1, 1, 1]]) expected_result = np.array([[1, 4, 1, 9, 0], [0, 1, 16, 1, 0], [0, 4, 1, 1, 1]]) encrypted_image = cr.encrypt_matrix(HE, image) encrypted_result = sq.square(HE, encrypted_image) result = cr.decrypt_matrix(HE, encrypted_result) assert np.allclose(expected_result, result, rtol=1e-3)
def test_avg_pool2d(self, HE, image, kernel_size, stride, padding, expected_result): encrypted_image = cr.encrypt_matrix(HE, image) avg_layer = AveragePoolLayer(HE, kernel_size=kernel_size, stride=stride, padding=padding) result = cr.decrypt_matrix(HE, avg_layer(encrypted_image)) assert np.allclose(result, expected_result, rtol=1e-3)
def test_conv(self, HE, image, weights, bias, stride, padding, expected_result): encrypted_image = cr.encrypt_matrix(HE, image) conv_layer = ConvolutionalLayer(HE, weights, stride=stride, padding=padding, bias=bias) result = cr.decrypt_matrix(HE, conv_layer(encrypted_image)) assert np.allclose(result, expected_result, rtol=1e-2)
def decode_and_decrypt(HE, t, ret_dict=None, ind=None): temp_file = tempfile.NamedTemporaryFile() t = np.array(t) if len(t.shape) > 2: enc_res = np.array( [[[[decode_ciphertext(value, temp_file) for value in row] for row in column] for column in layer] for layer in t]) else: enc_res = np.array( [[decode_ciphertext(value, temp_file) for value in row] for row in t]) dec_res = decrypt_matrix(HE, enc_res) if ret_dict is not None: ret_dict[ind] = dec_res else: return dec_res
def test_decrypt_matrix_2d(self, HE): matrix = np.array([[1, 2] , [3, 4]]) encrypted_matrix = crypto.encrypt_matrix(HE, matrix) result = crypto.decrypt_matrix(HE, encrypted_matrix) assert np.allclose(result, matrix)