How to use the crypten.mpc.get_default_provider function in crypten

To help you get started, we’ve selected a few crypten examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebookresearch / CrypTen / test / test_autograd.py View on Github external
def setUp(self):
        self._original_provider = crypten.mpc.get_default_provider()
        crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedThirdParty)
        super(TestTTP, self).setUp()
github facebookresearch / CrypTen / test / test_mpc.py View on Github external
def setUp(self):
        self._original_provider = crypten.mpc.get_default_provider()
        crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedFirstParty)
        super(TestTFP, self).setUp()
github facebookresearch / CrypTen / test / test_nn.py View on Github external
def setUp(self):
        self._original_provider = crypten.mpc.get_default_provider()
        crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedThirdParty)
        super(TestTTP, self).setUp()
github facebookresearch / CrypTen / test / test_gradients.py View on Github external
def setUp(self):
        self._original_provider = crypten.mpc.get_default_provider()
        crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedThirdParty)
        super(TestTTP, self).setUp()
github facebookresearch / CrypTen / crypten / mpc / primitives / beaver.py View on Github external
def B2A_single_bit(xB):
    """Converts a single-bit BinarySharedTensor xB into an
        ArithmeticSharedTensor. This is done by:

    1. Generate ArithmeticSharedTensor [rA] and BinarySharedTensor =rB= with
        a common 1-bit value r.
    2. Hide xB with rB and open xB ^ rB
    3. If xB ^ rB = 0, then return [rA], otherwise return 1 - [rA]
        Note: This is an arithmetic xor of a single bit.
    """
    if comm.get().get_world_size() < 2:
        from .arithmetic import ArithmeticSharedTensor

        return ArithmeticSharedTensor(xB._tensor, precision=0, src=0)

    provider = crypten.mpc.get_default_provider()
    rA, rB = provider.B2A_rng(xB.size())

    z = (xB ^ rB).reveal()
    rA = rA * (1 - 2 * z) + z
    return rA
github facebookresearch / CrypTen / crypten / mpc / primitives / beaver.py View on Github external
def wraps(x):
    """Privately computes the number of wraparounds for a set a shares

    To do so, we note that:
        [theta_x] = theta_z + [beta_xr] - [theta_r] - [eta_xr]

    Where [theta_i] is the wraps for a variable i
          [beta_ij] is the differential wraps for variables i and j
          [eta_ij]  is the plaintext wraps for variables i and j

    Note: Since [eta_xr] = 0 with probability 1 - |x| / Q for modulus Q, we
    can make the assumption that [eta_xr] = 0 with high probability.
    """
    provider = crypten.mpc.get_default_provider()
    r, theta_r = provider.wrap_rng(x.size())
    beta_xr = theta_r.clone()
    beta_xr._tensor = count_wraps([x._tensor, r._tensor])

    z = x + r
    theta_z = comm.get().gather(z._tensor, 0)
    theta_x = beta_xr - theta_r

    # TODO: Incorporate eta_xr
    if x.rank == 0:
        theta_z = count_wraps(theta_z)
        theta_x._tensor += theta_z
    return theta_x
github facebookresearch / CrypTen / crypten / mpc / primitives / beaver.py View on Github external
def __beaver_protocol(op, x, y, *args, **kwargs):
    """Performs Beaver protocol for additively secret-shared tensors x and y

    1. Obtain uniformly random sharings [a],[b] and [c] = [a * b]
    2. Additively hide [x] and [y] with appropriately sized [a] and [b]
    3. Open ([epsilon] = [x] - [a]) and ([delta] = [y] - [b])
    4. Return [z] = [c] + (epsilon * [b]) + ([a] * delta) + (epsilon * delta)
    """
    assert op in ["mul", "matmul", "conv2d", "conv_transpose2d"]

    provider = crypten.mpc.get_default_provider()
    a, b, c = provider.generate_additive_triple(x.size(), y.size(), op, *args, **kwargs)

    # Stack to vectorize reveal if possible
    if x.size() == y.size():
        from .arithmetic import ArithmeticSharedTensor

        eps_del = ArithmeticSharedTensor.stack([x - a, y - b]).reveal()
        epsilon = eps_del[0]
        delta = eps_del[1]
    else:
        epsilon = (x - a).reveal()
        delta = (y - b).reveal()

    # z = c + (a * delta) + (epsilon * b) + epsilon * delta
    # TODO: Implement crypten.mul / crypten.matmul / crypten.conv{_transpose}2d
    c._tensor += getattr(torch, op)(epsilon, b._tensor, *args, **kwargs)