def test_party_a_gradient_checking_test(self):

        U_A = np.array([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11],
                        [4, 5, 6, 7, 8]])
        U_B = np.array([[4, 2, 3, 1, 2], [6, 5, 1, 4, 5], [7, 4, 1, 9, 10],
                        [6, 5, 1, 4, 5]])
        y = np.array([[1], [-1], [1], [-1]])

        overlap_indexes = [1, 2]
        non_overlap_indexes = [0, 3]

        Wh = np.ones((4, U_A.shape[1]))
        bh = np.zeros(U_A.shape[1])

        autoencoderA = FakeAutoencoder(0)
        autoencoderA.build(U_A.shape[1], Wh, bh)
        autoencoderB = FakeAutoencoder(1)
        autoencoderB.build(U_B.shape[1], Wh, bh)

        partyA, _ = run_one_party_msg_exchange(autoencoderA, autoencoderB, U_A,
                                               U_B, y, overlap_indexes,
                                               non_overlap_indexes,
                                               self.public_key,
                                               self.private_key, True)
        loss_grads_A_1 = partyA.get_loss_grads()
        loss1 = partyA.send_loss()

        U_A_prime = np.array([[1, 2, 3, 4, 5], [4, 5.001, 6, 7, 8],
                              [7, 8, 9, 10, 11], [4, 5, 6, 7, 8]])
        partyA, _ = run_one_party_msg_exchange(
            autoencoderA, autoencoderB, U_A_prime, U_B, y, overlap_indexes,
            non_overlap_indexes, self.public_key, self.private_key, True)
        loss_grads_A_2 = partyA.get_loss_grads()
        loss2 = partyA.send_loss()

        loss_grads_A_1 = np.array(
            encryption.decrypt_matrix(self.private_key, loss_grads_A_1))
        loss_grads_A_2 = np.array(
            encryption.decrypt_matrix(self.private_key, loss_grads_A_2))

        loss1 = encryption.decrypt(self.private_key, loss1)
        loss2 = encryption.decrypt(self.private_key, loss2)

        grad_approx = (loss2 - loss1) / 0.001
        grad_real = loss_grads_A_1[1, 1]
        grad_diff = np.abs(grad_approx - grad_real)
        assert grad_diff < 0.001
Example #2
0
def distribute_decrypt(private_key, X):
    """
    decrypt X
    :param X: DTable
    :return: a dictionary
    """

    X2 = X.mapValues(lambda x: decrypt_matrix(private_key, x))
    val = X2.collect()
    val = dict(val)
    return val
    def test_encrypt_matmul_2_dim(self):

        X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float64)
        Y = np.array([[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                     dtype=np.float64)

        Z = np.matmul(X, Y)

        encrypt_Y = self.encrypt_2d_matrix(Y)
        res = distribute_encrypt_matmul_2_ob(X, encrypt_Y)

        # decrypt res
        decrypt_res = decrypt_matrix(self.privatekey, res)
        assert_matrix(Z, decrypt_res)
    def test_encrypt_matmul_3_dim_3(self):

        X = np.array([[[1, 2, 3]], [[10, 11, 12]]], dtype=np.float64)
        Y = np.array([[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
                      [[19, 20, 21], [22, 23, 24], [25, 26, 27]]],
                     dtype=np.float64)

        Z = np.matmul(X, Y)

        encrypt_Y = self.encrypt_3d_matrix(Y)
        res = distribute_encrypt_matmul_3(X, encrypt_Y)

        decrypt_res = decrypt_matrix(self.privatekey, res)
        assert_matrix(Z, decrypt_res)
Example #5
0
 def __decrypt_gradients(self, encrypt_gradients):
     return decrypt_matrix(self.private_key, encrypt_gradients[0]), decrypt_array(self.private_key, encrypt_gradients[1])