-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathbankers_bonus.mpc
110 lines (80 loc) · 4.08 KB
/
bankers_bonus.mpc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# (C) 2018 University of Bristol. See License.txt
# coding: latin-1
"""
Solve Bankers bonus, aka Millionaires problem.
to deduce the maximum value from a range of integer input.
Demonstrate clients external to computing parties supplying input and receiving an authenticated result. See bankers-bonus-client.cpp for client (and setup instructions).
For an implementation with communications security see bankers_bonus_commsec.mpc.
Wait for MAX_NUM_CLIENTS to join the game or client finish flag to be sent
before calculating the maximum.
Note each client connects in a single thread and so is potentially blocked.
Each round / game will reset and so this runs indefinitiely.
"""
from Compiler.types import sint, regint, Array, MemValue
from Compiler.instructions import listen, acceptclientconnection
from Compiler.library import print_ln, do_while, for_range
from Compiler.util import if_else
PORTNUM = 14000
MAX_NUM_CLIENTS = 8
def accept_client_input():
"""
Wait for socket connection, send share of random value, receive input and deduce share.
Expect 3 inputs: unique id, bonus value and flag to indicate end of this round.
"""
client_socket_id = regint()
acceptclientconnection(client_socket_id, PORTNUM)
client_inputs = sint.receive_from_client(3, client_socket_id)
return client_socket_id, client_inputs[0], client_inputs[1], client_inputs[2]
def determine_winner(number_clients, client_values, client_ids):
"""Work out and return client_id which corresponds to max client_value"""
max_value = Array(1, sint)
max_value[0] = client_values[0]
win_client_id = Array(1, sint)
win_client_id[0] = client_ids[0]
@for_range(number_clients-1)
def loop_body(i):
# Is this client input a new maximum, will be sint(1) if true, else sint(0)
is_new_max = max_value[0] < client_values[i+1]
# Keep latest max_value
max_value[0] = if_else(is_new_max, client_values[i+1], max_value[0])
# Keep current winning client id
win_client_id[0] = if_else(is_new_max, client_ids[i+1], win_client_id[0])
return win_client_id[0]
def write_winner_to_clients(sockets, number_clients, winning_client_id):
"""Send share of winning client id to all clients who joined game."""
# Setup authenticate result using share of random.
# client can validate ∑ winning_client_id * ∑ rnd_from_triple = ∑ auth_result
rnd_from_triple = sint.get_random_triple()[0]
auth_result = winning_client_id * rnd_from_triple
@for_range(number_clients)
def loop_body(i):
sint.write_shares_to_socket(sockets[i], [winning_client_id, rnd_from_triple, auth_result])
def main():
"""Listen in while loop for players to join a game.
Once maxiumum reached or have notified that round finished, run comparison and return result."""
# Start listening for client socket connections
listen(PORTNUM)
print_ln('Listening for client connections on base port %s', PORTNUM)
@do_while
def game_loop():
print_ln('Starting a new round of the game.')
# Clients socket id (integer).
client_sockets = Array(MAX_NUM_CLIENTS, regint)
# Number of clients
number_clients = MemValue(regint(0))
# Clients secret input.
client_values = Array(MAX_NUM_CLIENTS, sint)
# Client ids to identity client
client_ids = Array(MAX_NUM_CLIENTS, sint)
# Loop round waiting for each client to connect
@do_while
def client_connections():
client_sockets[number_clients], client_ids[number_clients], client_values[number_clients], finish = accept_client_input()
number_clients.write(number_clients+1)
# continue while both expressions are false
return (number_clients >= MAX_NUM_CLIENTS) + finish.reveal() == 0
winning_client_id = determine_winner(number_clients, client_values, client_ids)
# print_ln('Found winner, index: %s.', winning_client_id.reveal())
write_winner_to_clients(client_sockets, number_clients, winning_client_id)
return True
main()