def test_softmax_with_cross_entropy(self):

        data_1 = np.array([1, 1]).astype('float32')
        data_2 = np.array([1, 0]).astype('float32')

        expected_out = np.array([0.5, 0.5]).astype('float32')
        #print("input data_1: {} \n".format(data_1))

        data_1_shares = aby3.make_shares(data_1)
        data_2_shares = aby3.make_shares(data_2)

        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])
        data_2_all3shares = np.array(
            [aby3.get_aby3_shares(data_2_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.softmax_with_cross_entropy,
                                   data_1=data_1_all3shares,
                                   data_2=data_2_all3shares,
                                   return_results=return_results)

        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        #print("revealed: {} \n".format(revealed))
        #print("expected: {} \n".format(expected_out))
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-4))
Пример #2
0
    def test_dyanmic_gru_op(self):
        data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0],
                         [-1.0, -2.0, -3.0]]).astype('float32')
        data_share = aby3.make_shares(data)
        data_all3shares = np.array(
            [aby3.get_aby3_shares(data_share, i) for i in range(3)])

        weight = np.array([[0.0, 0.0, 0.0]]).astype('float32')
        weight_share = aby3.make_shares(weight)
        weight_all3shares = np.array(
            [aby3.get_aby3_shares(weight_share, i) for i in range(3)])

        return_results = Manager().list()
        return_results_cheb = Manager().list()
        expect_results = Manager().list()
        ret = self.multi_party_run(target=self.dyanmic_gru_op,
                                   data=data,
                                   data_share=data_all3shares,
                                   weight=weight,
                                   weight_share=weight_all3shares,
                                   return_results=return_results,
                                   return_results_cheb=return_results_cheb,
                                   expect_results=expect_results)
        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        revealed_cheb = aby3.reconstruct(np.array(return_results_cheb))
        print("expected:", expect_results[0])
        print("reveal: ", revealed)
        print("reveal_cheb: ", revealed_cheb)
        self.assertTrue(np.allclose(revealed, expect_results[0],
                                    atol=1e-1 * 5))
        self.assertTrue(
            np.allclose(revealed_cheb, expect_results[0], atol=1e-1 * 5))
Пример #3
0
    def test_diff_dim_mul_mid(self):
        data_1 = np.full((3, 4, 2), fill_value=2)
        data_2 = np.full((
            3,
            4,
        ), fill_value=1.5)
        #data_2 = np.ones((4,))
        # print(data_1)
        # print(data_2)
        data_1_shares = aby3.make_shares(data_1)
        data_2_shares = aby3.make_shares(data_2)
        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])
        data_2_all3shares = np.array(
            [aby3.get_aby3_shares(data_2_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.diff_dim_mul_mid,
                                   data_1=data_1_all3shares,
                                   data_2=data_2_all3shares,
                                   return_results=return_results)
        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        expected_out = np.array([[[3, 3], [3, 3], [3, 3], [3, 3]],
                                 [[3, 3], [3, 3], [3, 3], [3, 3]],
                                 [[3, 3], [3, 3], [3, 3], [3, 3]]])
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-4))
Пример #4
0
    def test_embedding_op(self):
        data = np.array([[1, 0, 0], [0, 1, 0]])
        data_normal = np.array([0, 1]).astype('int64')
        w_data = np.array([[1, 2], [2, 3], [3, 4]])

        # data = self.gen_one_hot(data_normal, w_data.shape[0]).astype('int64')

        data_share = aby3.make_shares(np.array(data))
        data_all3shares = np.array([aby3.get_aby3_shares(data_share, i) for i in range(3)])
        w_data_share = aby3.make_shares(w_data)
        w_data_all3shares = np.array([aby3.get_aby3_shares(w_data_share, i) for i in range(3)])

        return_results = Manager().list()
        expect_results = Manager().list()
        ret = self.multi_party_run(target=self.embedding_op,
                                   data=data,
                                   data_normal=data_normal,
                                   w_data=w_data,
                                   data_share=data_all3shares,
                                   w_data_share=w_data_all3shares,
                                   return_results=return_results,
                                   expect_results=expect_results)
        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        # print("reveal: ", revealed)
        self.assertTrue(np.allclose(revealed, expect_results[0], atol=1e-4))
Пример #5
0
 def encrypted_mnist_labels():
     """
     label reader
     """
     for instance in test_reader():
         yield aby3.make_shares(
             np.array(1) if instance[1] == 0 else np.array(0))
Пример #6
0
 def dummy_share_reader(self):
     """
     Dummy share_reader for share_reader in aby3.save_aby3_shares()
     :return:
     """
     test_data = np.arange(1, 10).reshape((3, 3)).astype(np.float32)
     yield aby3.make_shares(test_data)
Пример #7
0
 def test_reconstruct(self):
     num_arr = np.arange(0, 4).reshape((2, 2)).astype(np.float32)
     shares = aby3.make_shares(num_arr)
     all_3shares = np.array(
         [aby3.get_aby3_shares(shares, i) for i in range(3)])
     recon = aby3.reconstruct(all_3shares)
     self.assertTrue(np.allclose(num_arr, recon))
Пример #8
0
    def test_pool2d(self):

        data_1 = np.array([[[[1, 2, 3, 4, 0, 100], [5, 6, 7, 8, 0, 100],
                             [9, 10, 11, 12, 0, 200],
                             [13, 14, 15, 16, 0, 200]]]]).astype('float32')

        expected_out = np.array([[[[6, 8, 100], [14, 16,
                                                 200]]]]).astype('float32')
        print("input data_1: {} \n".format(data_1))

        data_1_shares = aby3.make_shares(data_1)

        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.pool2d,
                                   data_1=data_1_all3shares,
                                   return_results=return_results)

        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        #print("revealed: {} \n".format(revealed))
        #print("expected: {} \n".format(expected_out))
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-2))
Пример #9
0
 def encrypt_label():
     for instance in reader():
         #print(np.array(instance[2]))
         if label_filepath != None:
             with open(label_filepath, 'a+') as f:
                 f.write(str(instance[2][0]) + '\n')
         yield aby3.make_shares(np.array(instance[2]))
Пример #10
0
 def encrypt_feature_idx():
     for instance in reader():
         global count
         feature_idx_ = np.array(instance[0])
         feature_idx = np.eye(args.sparse_feature_number +
                              1)[feature_idx_.reshape(-1)]
         yield aby3.make_shares(feature_idx)
Пример #11
0
    def test_elementwise_mul(self):
        data_1 = np.full(shape=(4), fill_value=2)
        data_2 = np.full(shape=(4), fill_value=3)
        data_1_shares = aby3.make_shares(data_1)
        data_2_shares = aby3.make_shares(data_2)
        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])
        data_2_all3shares = np.array(
            [aby3.get_aby3_shares(data_2_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.elementwise_mul,
                                   data_1=data_1_all3shares,
                                   data_2=data_2_all3shares,
                                   return_results=return_results)
        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        expected_out = np.array([[6, 6, 6, 6]])
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-4))
Пример #12
0
    def test_square_error_cost(self):
        data_1 = np.arange(0, 4).reshape((2, 2))
        data_2 = np.full(shape=(2, 2), fill_value=2)
        data_1_shares = aby3.make_shares(data_1)
        data_2_shares = aby3.make_shares(data_2)
        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])
        data_2_all3shares = np.array(
            [aby3.get_aby3_shares(data_2_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.square_error_cost,
                                   data_1=data_1_all3shares,
                                   data_2=data_2_all3shares,
                                   return_results=return_results)
        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        expected_out = np.array([[4, 1], [0, 1]])
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-4))
Пример #13
0
def save_cypher(cypher_file, vec):
    """
    save cypertext to file
    """
    shares = aby3.make_shares(vec)
    exts = ['.part0', '.part1', '.part2']
    with open(cypher_file + exts[0], 'ab') as file0, \
            open(cypher_file + exts[1], 'ab') as file1, \
            open(cypher_file + exts[2], 'ab') as file2:
        files = [file0, file1, file2]
        for idx in six.moves.range(0, 3):  # 3 parts
            share = aby3.get_aby3_shares(shares, idx)
            files[idx].write(share.tostring())
Пример #14
0
    def test_relu(self):
        data_1 = np.arange(-3, 3).reshape((3, 2))
        data_1_shares = aby3.make_shares(data_1)
        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.relu,
                                   data_1=data_1_all3shares,
                                   return_results=return_results)
        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        expected_out = np.array([[0, 0], [0, 0], [1, 2]])
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-4))
Пример #15
0
 def encrypted_mnist_labels():
     """
     label reader
     """
     for instance in sample_reader():
         if class_num == 2:
             label = np.array(1) if instance[1] == 0 else np.array(0)
         elif class_num == 10:
             label = np.eye(N=1, M=10, k=instance[1],
                            dtype=float).reshape(10)
         else:
             raise ValueError(
                 "class_num should be 2 or 10, but received {}.".format(
                     class_num))
         yield aby3.make_shares(label)
Пример #16
0
 def test_mean(self):
     """
     Test normal case.
     :return:
     """
     data_1 = np.array([[1, 2, 3, 4],
                        [5, 6, 7, 8]])
     data_1_shares = aby3.make_shares(data_1)
     data_1_all3shares = np.array([aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])
     return_results = Manager().list()
     ret = self.multi_party_run(target=self.mean,
                                data_1=data_1_all3shares,
                                return_results=return_results)
     self.assertEqual(ret[0], True)
     revealed = aby3.reconstruct(np.array(return_results))
     expected_out = np.array([4.5])
     self.assertTrue(np.allclose(revealed, expected_out, atol=1e-4))
Пример #17
0
    def test_square(self):
        """
        Test normal case.
        :return:
        """
        data_1 = np.full(shape=(2, 2), fill_value=3)
        data_1_shares = aby3.make_shares(data_1)
        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.square,
                                   data_1=data_1_all3shares,
                                   return_results=return_results)
        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        expected_out = np.array([[9, 9], [9, 9]])
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-4))
Пример #18
0
 def encrypted_mnist_labels():
     """
     label reader
     """
     for instance in test_reader():
         if class_num == 2:
             label = np.array(1) if instance[1] == 0 else np.array(0)
             with open(label_mnist_filepath, 'a+') as f:
                 f.write(str(1 if instance[1] == 0 else 0) + '\n')
         elif class_num == 10:
             label = np.eye(N=1, M=10, k=instance[1],
                            dtype=float).reshape(10)
             with open(label_mnist_filepath, 'a+') as f:
                 f.write(str(instance[1]) + '\n')
         else:
             raise ValueError(
                 "class_num should be 2 or 10, but received {}.".format(
                     class_num))
         yield aby3.make_shares(label)
Пример #19
0
    def test_batch_norm(self):

        data_1 = np.array([[10, 10, 10], [50, 50, 50]]).astype('float32')

        expected_out = np.array([[-1, -1, -1], [1, 1, 1]]).astype('float32')
        # print("input data_1: {} \n".format(data_1))

        data_1_shares = aby3.make_shares(data_1)

        data_1_all3shares = np.array(
            [aby3.get_aby3_shares(data_1_shares, i) for i in range(3)])

        return_results = Manager().list()
        ret = self.multi_party_run(target=self.batch_norm,
                                   data_1=data_1_all3shares,
                                   return_results=return_results)

        self.assertEqual(ret[0], True)
        revealed = aby3.reconstruct(np.array(return_results))
        # print("revealed: {} \n".format(revealed))
        # print("expected: {} \n".format(expected_out))
        self.assertTrue(np.allclose(revealed, expected_out, atol=1e-2))
Пример #20
0
 def encrypt_sample():
     """
     encrypt sample
     """
     for sample in sample_reader():
         yield aby3.make_shares(sample)
Пример #21
0
 def test_mpc_one_hot(self):
   data = np.array([0, 1]).astype('int64')
   ret = self.gen_one_hot(data, 3)
   mpc_one_hot = aby3.make_shares(ret)
Пример #22
0
 def encrypt_feature_value():
     for instance in reader():
         #print(np.array(instance[1]).shape)
         yield aby3.make_shares(np.array(instance[1]))
Пример #23
0
 def test_make_shares_3dim(self):
     num_arr = np.arange(0, 8).reshape((2, 2, 2))
     shares = aby3.make_shares(num_arr)
     self.assertEqual((3, 2, 2, 2), shares.shape)
Пример #24
0
 def func():
     yield aby3.make_shares(data)
Пример #25
0
 def encrypted_housing_labels():
     """
     label reader
     """
     for instance in sample_reader():
         yield aby3.make_shares(instance[1])
Пример #26
0
 def encrypted_housing_features():
     """
     feature reader
     """
     for instance in sample_reader():
         yield aby3.make_shares(instance[0])
Пример #27
0
 def encrypted_mnist_features():
     """
     feature reader
     """
     for instance in test_reader():
         yield aby3.make_shares(instance[0])
Пример #28
0
 def func():
     """
     make shares
     """
     yield aby3.make_shares(data)