Exemplo n.º 1
0
def test_multiline(n_values, n_loops=16):

    le = sycret.LeFactory(n_threads=6)

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

        # Reshape to a C-contiguous array (necessary for from_buffer)

        alpha_a = np.frombuffer(np.ascontiguousarray(keys_a[:, 0:4]),
                                dtype=np.uint32)
        alpha_b = np.frombuffer(np.ascontiguousarray(keys_b[:, 0:4]),
                                dtype=np.uint32)
        alpha = alpha_a + alpha_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
        x[8] = x[8] - 635435

        x[9] = x[9] + 1

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

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

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

        assert (result == expected_result).all()
Exemplo n.º 2
0
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.
        x1 (ShareTensor): Share of the first private value.