Example #1
0
def test_private_compare(workers):
    """
    Test private compare which returns: β′ = β ⊕ (x > r).
    """
    alice, bob, james = workers["alice"], workers["bob"], workers["james"]

    x_bit_sh = decompose(torch.LongTensor([13])).share(
        alice, bob, crypto_provider=james).child
    r = torch.LongTensor([12]).send(alice, bob).child

    beta = torch.LongTensor([1]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert not beta_p

    beta = torch.LongTensor([0]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert beta_p

    # Big values
    x_bit_sh = decompose(torch.LongTensor([2**60])).share(
        alice, bob, crypto_provider=james).child
    r = torch.LongTensor([2**61]).send(alice, bob).child

    beta = torch.LongTensor([1]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert beta_p

    beta = torch.LongTensor([0]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert not beta_p

    # Multidimensional tensors
    x_bit_sh = (decompose(torch.LongTensor([[13, 44], [1, 28]])).share(
        alice, bob, crypto_provider=james).child)
    r = torch.LongTensor([[12, 44], [12, 33]]).send(alice, bob).child

    beta = torch.LongTensor([1]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert (beta_p == torch.tensor([[0, 1], [1, 1]])).all()

    beta = torch.LongTensor([0]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert (beta_p == torch.tensor([[1, 0], [0, 0]])).all()

    # Negative values
    x_val = -105 % 2**62
    r_val = -52 % 2**62  # The protocol works only for values in Zq
    x_bit_sh = decompose(torch.LongTensor([x_val])).share(
        alice, bob, crypto_provider=james).child
    r = torch.LongTensor([r_val]).send(alice, bob).child

    beta = torch.LongTensor([1]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert beta_p

    beta = torch.LongTensor([0]).send(alice, bob).child
    beta_p = private_compare(x_bit_sh, r, beta)
    assert not beta_p
Example #2
0
def test_private_compare(workers):
    alice, bob, james = (
        workers["alice"],
        workers["bob"],
        workers["james"],
    )
    x_bits = (securenn.decompose(torch.tensor([3, 3]),
                                 2**64).share(alice,
                                              bob,
                                              dtype="custom",
                                              field=67,
                                              crypto_provider=james).child)
    r = torch.tensor([1, 5]).send(alice, bob).child
    b = torch.tensor([0]).send(alice, bob).child
    compare = securenn.private_compare(x_bits, r, b, 2**64)
    assert (compare == torch.tensor([1, 0])).all()