예제 #1
0
def test_simple_raw_eq():

    eq = sycret.EqFactory(n_threads=6)

    for _ in range(16):
        keys_a, keys_b = eq.keygen(1)

        alpha_a = np.frombuffer(keys_a[0][0 : eq.N], dtype=np.uint32)
        alpha_b = np.frombuffer(keys_b[0][0 : eq.N], dtype=np.uint32)
        alpha = alpha_a + alpha_b

        x = alpha.astype(np.int64)

        r_a, r_b = (
            eq.eval(0, x, keys_a),
            eq.eval(1, x, keys_b),
        )
        assert (r_a + r_b) % (2 ** (eq.N * 8)) == 1

        x = alpha.astype(np.int64) + 31
        r_a, r_b = (
            eq.eval(0, x, keys_a),
            eq.eval(1, x, keys_b),
        )
        assert (r_a + r_b) % (2 ** (eq.N * 8)) == 0
예제 #2
0
def test_multiline(n_values, n_loops=16):

    eq = sycret.EqFactory(n_threads=6)

    for _ in range(n_loops):
        keys_a, keys_b = eq.keygen(n_values)

        # Reshape to a C-contiguous array (necessary for from_buffer)
        alpha = eq.alpha(keys_a, keys_b)

        x = alpha.astype(np.int64)

        # We just modify some input values, the rest is on the special path.
        x[1] = x[1] + 5
        x[2] = x[2] - 1
        x[4] = x[4] + 1

        r_a, r_b = (
            eq.eval(0, x, keys_a),
            eq.eval(1, x, keys_b),
        )

        # In PySyft, the AdditiveSharingTensor class will take care of the modulo
        result = (r_a + r_b) % (2**(eq.N * 8))

        expected_result = np.ones(n_values, dtype=np.uint64)
        expected_result[1] = 0
        expected_result[2] = 0
        expected_result[4] = 0

        assert (result == expected_result).all()
예제 #3
0
def test_simple_raw_eq():

    eq = sycret.EqFactory(n_threads=6)

    for _ in range(16):
        keys_a, keys_b = eq.keygen(1)
        alpha = eq.alpha(keys_a, keys_b)

        x = alpha.astype(np.int64)

        r_a, r_b = (
            eq.eval(0, x, keys_a),
            eq.eval(1, x, keys_b),
        )
        assert (r_a + r_b) % (2**(eq.N * 8)) == 1

        x = alpha.astype(np.int64) + 31
        r_a, r_b = (
            eq.eval(0, x, keys_a),
            eq.eval(1, x, keys_b),
        )
        assert (r_a + r_b) % (2**(eq.N * 8)) == 0
예제 #4
0
from sympc.store import CryptoPrimitiveProvider
from sympc.store import register_primitive_generator
from sympc.store import register_primitive_store_add
from sympc.store import register_primitive_store_get
from sympc.tensor import MPCTensor
from sympc.tensor import ShareTensor
from sympc.utils import parallel_execution

ttp_generator = csprng.create_random_device_generator()

λ = 127  # security parameter
n = 32  # bit precision

# number of processes
N_CORES = multiprocessing.cpu_count()
dpf = sycret.EqFactory(n_threads=N_CORES)
dif = sycret.LeFactory(n_threads=N_CORES)


# share level
def mask_builder(session: Session, x1: ShareTensor, x2: ShareTensor,
                 op: str) -> ShareTensor:
    """Mask the private inputs.

    Add the share of alpha (the mask) that is held in the crypto store to
    the difference x1 - x2.
    As we aim at comparing x1 <= x2, we actually compare x1 - x2 <= 0 and we hide
    x1 - x2 with alpha that is a random mask.

    Args:
        session (Session): MPC Session.