Esempio n. 1
0
    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)
Esempio n. 2
0
    def test_convolute2d(self, HE):
        """ Procedure:
                    1. Create a image in the form
                        [y, x]
                    2. Create a filter in the form
                        [y, x]
                    3. Encrypt the image
                    4. Encode the filter
                    5. Convolute
                    ---------
                    Verification:
                    6. 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, 1, 1, 0], [0, 0, 1, 1, 1],
                            [0, 0, 1, 1, 0], [0, 1, 1, 0, 0]]]])

        # Shape of filter is [1, 1, 3, 3]. Needed to use encode_matrix.
        filter_matrix = np.array([[[[1, 0, 1], [0, 1, 0], [1, 0, 1]]]])

        encrypted_image = cr.encrypt_matrix(HE, image)
        encoded_filter = cr.encode_matrix(HE, filter_matrix)

        encrypted_result = conv.convolute2d(encrypted_image[0][0],
                                            encoded_filter[0][0], (1, 1))

        assert HE.decryptFrac(encrypted_result[0][0]) == 4
        assert HE.decryptFrac(encrypted_result[0][1]) == 3
        assert HE.decryptFrac(encrypted_result[0][2]) == 4
        assert HE.decryptFrac(encrypted_result[1][0]) == 2
        assert HE.decryptFrac(encrypted_result[1][1]) == 4
        assert HE.decryptFrac(encrypted_result[1][2]) == 3
        assert HE.decryptFrac(encrypted_result[2][0]) == 2
        assert HE.decryptFrac(encrypted_result[2][1]) == 3
        assert HE.decryptFrac(encrypted_result[2][2]) == 4
Esempio n. 3
0
 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
Esempio n. 4
0
    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)
Esempio n. 5
0
    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)
Esempio n. 6
0
    def crypt_and_encode(HE, t, ret_dict, ind):
        temp_file = tempfile.NamedTemporaryFile()
        enc_image = encrypt_matrix(HE, t)

        data = [[[[encode_ciphertext(value, temp_file) for value in row]
                  for row in column] for column in layer]
                for layer in enc_image]
        ret_dict[ind] = data
Esempio n. 7
0
    def test_encrypt_matrix_2d(self, HE):
        matrix = np.array([[1, 2],
                           [3, 4]])

        result = crypto.encrypt_matrix(HE, matrix)
        assert HE.decryptFrac(result[0][0]) == 1
        assert HE.decryptFrac(result[0][1]) == 2
        assert HE.decryptFrac(result[1][0]) == 3
        assert HE.decryptFrac(result[1][1]) == 4
Esempio n. 8
0
    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)
Esempio n. 9
0
    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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    def test_get_min_noise(self):

        HE = Pyfhel()
        HE.contextGen(65537)
        HE.keyGen()
        plain_image = np.array([[[1, 2, 3], [1, 3, 4], [1, 3, 4]]])

        cipher_image = encrypt_matrix(HE, plain_image)
        cipher_image[0][1][1] = cipher_image[0][1][1] * HE.encryptFrac(2)

        assert get_min_noise(HE, cipher_image) == HE.noiseLevel(
            cipher_image[0][1][1])
Esempio n. 12
0
    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)
Esempio n. 13
0
    def test_get_max_error(self):

        HE = Pyfhel()
        HE.contextGen(65537)
        HE.keyGen()
        plain_image = np.array([[1, 2, 3], [1, 3, 4], [1, 3, 4]])

        cipher_image = encrypt_matrix(HE, plain_image)

        cipher_image[2][0] = HE.encryptFrac(-1)

        max_error, position = get_max_error(HE, plain_image, cipher_image)

        assert max_error == 2
        assert position, (2 == 0)
Esempio n. 14
0
    def crypt_and_encode(HE, t, ret_dict=None, ind=None):
        temp_file = tempfile.NamedTemporaryFile()
        enc_image = encrypt_matrix(HE, t)

        if len(enc_image.shape) > 2:
            data = [[[[encode_ciphertext(value, temp_file) for value in row]
                      for row in column] for column in layer]
                    for layer in enc_image]
        else:
            data = [[encode_ciphertext(value, temp_file) for value in row]
                    for row in enc_image]

        data = np.array(data)

        if ret_dict is not None:
            ret_dict[ind] = data
        else:
            return data
Esempio n. 15
0
 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)