Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self._original_provider = crypten.mpc.get_default_provider()
crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedThirdParty)
super(TestTTP, self).setUp()
def setUp(self):
self._original_provider = crypten.mpc.get_default_provider()
crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedFirstParty)
super(TestTFP, self).setUp()
def setUp(self):
self._original_provider = crypten.mpc.get_default_provider()
crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedThirdParty)
super(TestTTP, self).setUp()
def setUp(self):
self._original_provider = crypten.mpc.get_default_provider()
crypten.mpc.set_default_provider(crypten.mpc.provider.TrustedThirdParty)
super(TestTTP, self).setUp()
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
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
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)