def test_bin_xor(get_clients, bit, security) -> None: parties = get_clients(3) protocol = Falcon(security) session = Session(protocol=protocol, parties=parties) session.ring_size = 2 SessionManager.setup_mpc(session) ring_size = 2 sh_x = torch.tensor([[0, 1, 0], [1, 0, 1]], dtype=torch.bool) shares_x = [sh_x, sh_x, sh_x] rst_list_x = ReplicatedSharedTensor.distribute_shares(shares=shares_x, session=session, ring_size=ring_size) x = MPCTensor(shares=rst_list_x, session=session) x.shape = sh_x.shape sh_b = torch.tensor([bit], dtype=torch.bool) shares_b = [sh_b, sh_b, sh_b] rst_list_b = ReplicatedSharedTensor.distribute_shares(shares=shares_b, session=session, ring_size=ring_size) b = MPCTensor(shares=rst_list_b, session=session) b.shape = sh_b.shape secret_x = ReplicatedSharedTensor.shares_sum(shares_x, ring_size) secret_b = ReplicatedSharedTensor.shares_sum(shares_b, ring_size) result = operator.xor(x, b) expected_res = secret_x ^ secret_b assert (result.reconstruct(decode=False) == expected_res).all()
def test_prime_xor(get_clients, security, bit) -> None: parties = get_clients(3) protocol = Falcon(security) session = Session(protocol=protocol, parties=parties) session.ring_size = PRIME_NUMBER SessionManager.setup_mpc(session) ring_size = PRIME_NUMBER x_sh1 = torch.tensor([[17, 44], [8, 20]], dtype=torch.uint8) x_sh2 = torch.tensor([[8, 51], [27, 52]], dtype=torch.uint8) x_sh3 = torch.tensor([[42, 40], [32, 63]], dtype=torch.uint8) bit_sh_1, bit_sh_2, bit_sh_3 = bit b_sh1 = torch.tensor([bit_sh_1], dtype=torch.uint8) b_sh2 = torch.tensor([bit_sh_2], dtype=torch.uint8) b_sh3 = torch.tensor([bit_sh_3], dtype=torch.uint8) shares_x = [x_sh1, x_sh2, x_sh3] shares_b = [b_sh1, b_sh2, b_sh3] rst_list_x = ReplicatedSharedTensor.distribute_shares(shares=shares_x, session=session, ring_size=ring_size) rst_list_b = ReplicatedSharedTensor.distribute_shares(shares=shares_b, session=session, ring_size=ring_size) x = MPCTensor(shares=rst_list_x, session=session) b = MPCTensor(shares=rst_list_b, session=session) x.shape = x_sh1.shape b.shape = b_sh1.shape secret_x = ReplicatedSharedTensor.shares_sum(shares_x, ring_size) secret_b = ReplicatedSharedTensor.shares_sum(shares_b, ring_size) result = operator.xor(x, b) expected_res = secret_x ^ secret_b assert (result.reconstruct(decode=False) == expected_res).all()