def ecdsa_sign_parallel(players, need_check=False): global elliptic_time cn, cp, cq, g, n = Player.cn, Player.cp, Player.cq, Player.g, Player.n for p in players: p.k = p.share_values.pop() p.gamma = p.share_values.pop() p.set_multiply_x([p.k, p.k]) p.set_multiply_y([p.gamma, p.private_key]) multiply_beaver_parallel(players, 2, need_check) for p in players: p.delta = p.list_product[0] p.sigma = p.list_product[1] p.set_broadcast(p.delta) open_share_with_mac(players) for p in players: p.delta_inv = inv(p.open_value, n) p.k_inv = tuple((p.delta_inv * p.gamma[i]) % n for i in range(2)) t0 = time.time() for p in players: p.k_inv_point = mulp(cp, cq, cn, g, p.k_inv[0]) p.set_broadcast((p.k_inv_point, p.k_inv[1])) t1 = time.time() elliptic_time += t1 - t0 open_point_with_mac(players) for p in players: p.r_open = p.open_point[0] p.s = tuple(p.hash_message * p.k[i] + p.r_open * p.sigma[i] for i in range(2)) p.set_broadcast(tuple(p.s)) open_share_with_mac(players) for p in players: p.sign = (p.r_open, p.open_value) validate(players[0].sign, players[0].hash_message, players[0].overall_public_key)
def validate(sign, hash_message, overall_public_key): cn, cp, cq, g, n = Player.cn, Player.cp, Player.cq, Player.g, Player.n r, s = sign s_inv = inv(s, n) p1 = mulp(cp, cq, cn, g, (s_inv * hash_message) % n) p2 = mulp(cp, cq, cn, overall_public_key, (s_inv * r) % n) p = add(cp, cq, cn, p1, p2) assert p[0] == r
def sign(h, dk): '''Sign the numeric value h using private key dk''' bits, d = dk bits, cn, n, cp, cq, g = get_curve(bits) h = truncate(h, cn) r = s = 0 while r == 0 or s == 0: k = randkey(bits, cn) kinv = inv(k, n) kg = mulp(cp, cq, cn, g, k) r = kg[0] % n if r == 0: continue s = (kinv * (h + r * d)) % n return r, s
def verify(h, sig, qk): '''Verify that 'sig' is a valid signature of h using public key qk''' bits, q = qk try: bits, cn, n, cp, cq, g = get_curve(bits) except KeyError: return False h = truncate(h, cn) r, s = sig if 0 < r < n and 0 < s < n: w = inv(s, n) u1 = (h * w) % n u2 = (r * w) % n x, y = muladdp(cp, cq, cn, g, u1, q, u2) return r % n == x % n return False
#all_shares[j].append(tuple(shares[j][:3])) if method == 'memory': for i in range(ComputePlayer.ComputeNum): ComputePlayer.ComputeList[i].set_beaver_triples(all_shares[i]) elif method == 'file': for i in range(ComputePlayer.ComputeNum): with open('beaver_triple_P{}.txt'.format(i), 'w') as file: print(len(all_shares[i])) file.write(str(len(all_shares[i]))) file.write('\n') for j in all_shares[i]: file.write(str(j)) file.write('\n') if __name__ == '__main__': a = InputPlayer(10000) b = TrustedThirdParty(20000) c = ComputePlayer(30000) d = ComputePlayer(40000) a.set_message("hello world") b.generate_mac() b.generate_mac_share(100, 'file') c.read_shares() b.generate_beaver_triples(100, 'file') c.read_beaver_triples() print(c.alpha, c.hash_message, c.beaver_triples) #print(d.alpha, d.hash_message,d.share_values) print(b.mac_sum, inv(b.mac_sum, Player.n))