def finish(result): self.progressbar.finish() print # This line goes on top of the progressbar dprint("Original array: %s", array) dprint("Sorted array: %s", result) print "Made %d comparisons" % self.comparisons runtime.shutdown()
def protocol(rt): l = 7 rand = dict([(i, random.Random(i)) for i in players]) inputs = [] for i in range(count): input = dict([(j, rand[j].randint(0, pow(2, l))) for j in players]) inputs.append(input) # Fixed input for easier debugging inputs.append({1: 20, 2: 25, 3: 0}) print "I am player %d, will compare %d numbers" % (id, len(inputs)) bits = [] for input in inputs: x, y, z = rt.shamir_share([1, 2, 3], Zp, input[id]) bit = rt.open(x >= y) bit.addCallback(lambda b: b == GF256(1)) bit.addCallback(lambda b, x, y: "%3d >= %3d: %-5s (%s)" \ % (x, y, b, b == (x >= y)), input[1], input[2]) dprint("%s", bit) bits.append(bit) results = gatherResults(bits) results.addCallback(lambda _: rt.shutdown())
def protocol(rt): print "-" * 64 print "Program started" print shares = rt.prss_share([1, 2, 3], GF256, input) while len(shares) > 1: a = shares.pop(0) b = shares.pop(0) shares.append(a ^ b) xor = rt.open(shares[0]) dprint("Result: %s", xor) rt.wait_for(xor)
def run(runtime): print "Connected." # Players 1 and 2 are doing a sharing over the field Zp. # Our input is number (None for other players). if runtime.id == 3: print "I have no number" else: print "My number: %d." % number (x, y) = runtime.shamir_share([1, 2], Zp, number) # Do the secret computation. result = divide(x, y, 10) # 10 bits for the result. # Now open the result so we can see it. dprint("The two numbers divided are: %s", runtime.open(result)) result.addCallback(lambda _: runtime.shutdown())
def protocol(runtime): print "-" * 64 print "Program started" print # Each of the players [1, 2, 3] shares his input -- resulting in # three shares. The a share is the input from P1, b is from P2, # and c is from P3. a, b, c = runtime.input([1, 2, 3], Zp, input) # It is possible to make the players do different things by # branching on the players ID. In this case only P1 inputs a # number. The other two players must still participate in order to # get the hold of their share. if runtime.id == 1: s1 = runtime.input([1], Zp, 42) else: s1 = runtime.input([1], Zp, None) # Secure arithmetic works like normal. a = b + c b = c * s1 # Outputting shares convert them from secret shared form into # cleartext. By default every player receives the cleartext. a = runtime.output(a) b = runtime.output(b) c = runtime.output(c) # Output s1 to player 2. The other players will receive None. s1 = runtime.output(s1, [2]) dprint("### Output a to all: %s ###", a) dprint("### Output b to all: %s ###", b) dprint("### Output c to all: %s ###", c) # We only print the value of s1 for player 2, # since only player 2 has the value of s1. if runtime.id == 2: dprint("### opened s1: %s ###", s1) # We wait for the evaluation of deferred a, b, c. runtime.wait_for(a, b, c)
def protocol(runtime): print "Program started" start_time = time() num_accounts = 50 if runtime.id == 1: from_id = runtime.input([1], Zp, 9) # other_id = runtime.input([1], Zp, 999) # gf_256 = runtime.input([1], GF256, 21) from_pubkey = runtime.input([1], Zp, 0x3134f954AFf7F5F8EB849a80Fb85447E5b2a3696) arr = get_accounts(num_accounts, runtime) else: from_id = runtime.input([1], Zp, None) # other_id = runtime.input([1], Zp, None) # gf_256 = runtime.input([1], GF256, None) from_pubkey = runtime.input([1], Zp, None) arr = [runtime.input([1], Zp, None) for x in range(num_accounts * 3)] owner_pubkey = 0 value = 0 for i in range(num_accounts): if runtime.id == 1: id_share = runtime.shamir_share([1], Zp, i) else: id_share = runtime.shamir_share([1], Zp, None) equality = runtime.equal(from_id, id_share) dprint("%s == %s: %s", runtime.open(from_id), runtime.open(id_share), runtime.open(equality)) owner_pubkey += arr[i * 3] * equality value += arr[i * 3 + 2] * equality output = (from_pubkey == owner_pubkey) * value output = runtime.open(output) dprint("output %s", output) actual = runtime.open(arr[9 * 3 + 2]) dprint("actual %s", actual) print "Time taken: %.2f sec" % (time() - start_time) runtime.wait_for(output)
def protocol(runtime): print "-" * 64 print "Program started" print a, b, c = runtime.prss_share([1, 2, 3], GF256, input) a = runtime.open(a) b = runtime.open(b) c = runtime.open(c) dprint("### opened a: %s ###", a) dprint("### opened b: %s ###", b) dprint("### opened c: %s ###", c) runtime.wait_for(a, b, c)
def protocol(rt): print "-" * 64 print "Program started" print a, b, c = rt.shamir_share([1, 2, 3], Zp, input) a = rt.open(a) b = rt.open(b) c = rt.open(c) dprint("### opened a: %s ###", a) dprint("### opened b: %s ###", b) dprint("### opened c: %s ###", c) rt.wait_for(a, b, c)
def protocol(rt): elements = [rt.open(rt.prss_share_random(Z31)) for _ in range(10)] result = gatherResults(elements) dprint("bits: %s", result) rt.wait_for(result)
def protocol(runtime): print "-" * 64 print "Program started" print a, b = runtime.share([1, 2], Zp, input) c = a * b dprint("a%d: %s", runtime.id, a) dprint("b%d: %s", runtime.id, b) dprint("c%d: %s", runtime.id, c) a = runtime.open(a) b = runtime.open(b) c = runtime.open(c) dprint("### opened a: %s ###", a) dprint("### opened b: %s ###", b) dprint("### opened c: %s ###", c) runtime.wait_for(a, b, c)