def from_point(context: Mpc, p: Point) -> SharedPoint: """ Given a local point and a context, created a shared point """ if not isinstance(p, Point): raise Exception(f"Could not create shared point-- p ({p}) is not a Point!") return SharedPoint( context, context.Share(p.x), context.Share(p.y), curve=p.curve )
def _compute_x(context: Mpc, r_bits: list, c_bits: list): """ Section 5.2 Computing X Computes [x] from equation 7 The least significant bit of [x], written [x_0] is equal to the value [r_i], where i is the most significant bit where [r_i] != c_i [x_0] == ([r]_B > c) TODO: precompute PRODUCT(1 + [r_j]) Compute PRODUCT(1 + c_j) without MPC See final further work points in paper section 6 """ power_bits = [ context.field(1) + LessThan._xor_bits(r, c) for r, c in zip(r_bits[1:], c_bits[1:]) ] powers = [context.Share(1)] for b in reversed(power_bits): powers.insert(0, b * powers[0]) # TODO: make this log(n) x = context.field(0) for (r_i, c_i, p) in zip(r_bits, c_bits, powers): x += r_i * (context.field(1) - c_i) * p return x
async def gen_test_bit(context: Mpc, diff: Share): cj, bj = await Equality._gen_test_bit(context, diff) while cj == 0: cj, bj = await Equality._gen_test_bit(context, diff) legendre = Equality.legendre_mod_p(cj) if legendre == 0: return Equality.gen_test_bit(context, diff) return (legendre / context.field(2)) * (bj + context.Share(legendre))
async def _gen_test_bit(context: Mpc, diff: Share): # # b \in {0, 1} b = context.preproc.get_bit(context) # # _b \in {5, 1}, for p = 1 mod 8, s.t. (5/p) = -1 # # so _b = -4 * b + 5 _b = (-4 * b) + context.Share(5) _r = context.preproc.get_rand(context) _rp = context.preproc.get_rand(context) # c = a * r + b * rp * rp # If b_i == 1, c_i is guaranteed to be a square modulo p if a is zero # and with probability 1/2 otherwise (except if rp == 0). # If b_i == -1 it will be non-square. c = await ((diff * _r) + (_b * _rp * _rp)).open() return c, _b
async def _prog(context: Mpc, x: Share, y: Share): xy_2t = context.Share(x.v * y.v, context.t * 2) xy_t = await DoubleSharingMultiply.reduce_degree_share(context, xy_2t) return xy_t