Exemple #1
0
def test_xor_implementation(workers):
    alice, bob, james = workers["alice"], workers["bob"], workers["james"]
    r = decompose(torch.LongTensor([3]), 2 ** 64).send(alice, bob).child
    x_bit_sh = (
        decompose(torch.LongTensor([23]), 2 ** 64)
        .share(alice, bob, crypto_provider=james, dtype="long")
        .child
    )
    j0 = torch.zeros(x_bit_sh.shape).long().send(bob)
    j1 = torch.ones(x_bit_sh.shape).long().send(alice)
    j = MultiPointerTensor(children=[j0.child, j1.child])
    w = (j * r) + x_bit_sh - (2 * x_bit_sh * r)

    r_real = r.virtual_get()[0]
    x_real = x_bit_sh.virtual_get()
    w_real = r_real + x_real - 2 * r_real * x_real
    assert (w.virtual_get() == w_real).all()

    # For dtype int
    r = decompose(torch.IntTensor([3]), 2 ** 32).send(alice, bob).child
    x_bit_sh = (
        decompose(torch.IntTensor([23]), 2 ** 32)
        .share(alice, bob, crypto_provider=james, dtype="int")
        .child
    )
    assert x_bit_sh.field == 2 ** 32 and x_bit_sh.dtype == "int"
    j0 = torch.zeros(x_bit_sh.shape).type(torch.int32).send(bob)
    j1 = torch.ones(x_bit_sh.shape).type(torch.int32).send(alice)
    j = MultiPointerTensor(children=[j0.child, j1.child])
    w = (j * r) + x_bit_sh - (2 * x_bit_sh * r)

    r_real = r.virtual_get()[0]
    x_real = x_bit_sh.virtual_get()
    w_real = r_real + x_real - 2 * r_real * x_real
    assert (w.virtual_get() == w_real).all()
Exemple #2
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
Exemple #3
0
def test_xor_implementation(workers):
    alice, bob, james = workers["alice"], workers["bob"], workers["james"]
    r = decompose(th.tensor([3])).send(alice, bob).child
    x_bit_sh = decompose(th.tensor([23])).share(alice,
                                                bob,
                                                crypto_provider=james).child
    j0 = torch.zeros(x_bit_sh.shape).long().send(bob)
    j1 = torch.ones(x_bit_sh.shape).long().send(alice)
    j = MultiPointerTensor(children=[j0.child, j1.child])
    w = (j * r) + x_bit_sh - (2 * x_bit_sh * r)

    r_real = r.virtual_get()[0]
    x_real = x_bit_sh.virtual_get()
    w_real = r_real + x_real - 2 * r_real * x_real
    assert (w.virtual_get() == w_real).all()
Exemple #4
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()