-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlocal_demo.py
executable file
·305 lines (248 loc) · 8.74 KB
/
local_demo.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import string
from collections import Counter
from itertools import chain
from pathlib import Path
from subprocess import Popen
from subprocess import call as subprocess_call
logging.basicConfig(level=logging.INFO, filemode="w", filename="local_demo.log", format="%(asctime)s;%(levelname)s;%(name)s;%(message)s")
logger = logging.getLogger('admin')
DEMO_ELECTION = Path(os.path.dirname(__file__)).parent.joinpath('demoElection')
def main(args):
print(args)
if args.vmni:
vmni(args)
if args.vmn:
vmn(args)
if args.vbt and not args.dry_run:
print(vbt(args))
return 0
def vmni(args):
"""See:
2 - Info File Generator
2.1 - Basic Usage
"""
vmni_common_parameters(args)
vmni_individual_protocol_info_files(args)
vmni_merge_protocol_info_files(args)
def vmni_common_parameters(args):
"""See:
1. Agree on common parameters
"""
for idx,_ in enumerate(args.ips):
if not args.dry_run:
os.makedirs(os.path.join(DEMO_ELECTION, str(idx)), exist_ok=True)
args.call(
[
"vmni",
"-prot",
"-sid",
args.session_id,
"-name",
args.name,
"-nopart",
args.num_part,
"-thres",
args.threshold,
"stub.xml",
], cwd=os.path.join(DEMO_ELECTION, str(idx))
)
def vmni_individual_protocol_info_files(args):
"""See:
2. Generate individual info files
"""
for idx, ip in enumerate(args.ips):
name = args.party_format.format(idx=idx)
priv = "privInfo.xml"
prot = "protInfo.xml"
http = args.http_format.format(ip=ip, idx=idx, port=args.http_port + idx)
hint = args.hint_format.format(ip=ip, idx=idx, port=args.hint_port + idx)
if not args.dry_run:
os.makedirs(os.path.join(DEMO_ELECTION, str(idx)), exist_ok=True)
args.call(
[
"vmni",
"-party",
"-name",
name,
"-http",
http,
"-hint",
hint,
"stub.xml",
priv,
prot,
], cwd=os.path.join(DEMO_ELECTION, str(idx))
)
def vmni_merge_protocol_info_files(args):
"""See:
3. Merge protocol info files.
"""
args.call(
["vmni", "-merge"]
+ [f"{idx}/protInfo.xml" for idx in range(args.num_parties)]
+ ["merged.xml"], cwd=DEMO_ELECTION
)
def vmn(args):
"""See:
3.Mix-Net
"""
processes = [
# add logging script to vmn
args.call(
["vmn", "-keygen", "privInfo.xml", "../merged.xml", "publicKey"],
popen=True,
cwd=os.path.join(DEMO_ELECTION, str(idx)),
)
for idx in range(args.num_parties)
]
logger.info(f'3 -> (receive) Public key received by mix-net')
for p in processes:
p.communicate()
if args.demo:
args.call(["vmnd", "-ciphs", "0/publicKey", 100, "ciphertexts"])
elif args.dry_run:
pass
elif args.post:
with open(os.path.join(DEMO_ELECTION, "0/publicKey"), "rb") as f:
request("POST", f"{args.post}/publicKey", files={"publicKey": f})
logger.info('3 -> (send) Public key sent')
input("Vote and press Enter ")
logger.info('25 -> (send) End of voting')
with open(os.path.join(DEMO_ELECTION, "ciphertexts"), "wb") as f:
r = request("GET", f"{args.post}/ciphertexts")
f.write(r.content)
else:
while not os.path.exists("ciphertexts"):
input("Please collect ciphertexts and press Enter ")
logger.info(f'27 -> (send) Start shuffle for party')
processes = [
args.call(
[
"vmn",
"-mix",
"privInfo.xml",
"../merged.xml",
"../ciphertexts",
"plaintexts",
],
popen=True,
cwd=os.path.join(DEMO_ELECTION, str(idx)),
)
for idx in range(args.num_parties)
]
logger.info(f'27 -> (receive) End shuffle for party')
for p in processes:
p.communicate()
def request(method, *args, **kwargs):
import requests
method = {"post": requests.post, "get": requests.get}[method.lower()]
print(method.__name__, args, kwargs)
r = method(*args, **kwargs)
r.raise_for_status()
return r
VALID_CHARS = " -_.,()" + string.ascii_letters + string.digits
def import_bytetree():
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../webdemo')
try:
sys.path.append(path)
from bytetree import byte_array_byte_tree_to_json
def vbt_call(fname):
with open(fname, "rb") as f:
return json.loads(byte_array_byte_tree_to_json(bytearray(f.read())))
return vbt_call
except ImportError as e:
print(f"Could not load bytetree.py that should have been located in {path}")
raise e
def vbt(args):
"""
Output & tallying
"""
logging.info('28 -> (send) Signal from mix-net to decrypt votes')
vbt_json = Counter(
map(
lambda x: "".join(
# Plaintexts is a byte tree with N children where each child is
# a byte tree with 2 children. The first of the inner children
# is the vote in ASCII bytes.
c
for c in map(chr, bytes.fromhex(x[0]))
if c in VALID_CHARS
),
# vbt converts the RAW plaintexts to JSON.
import_bytetree()(os.path.join(DEMO_ELECTION, "1", "plaintexts")),
)
)
logging.info(f'28 -> (receive) Decrypted votes {vbt_json}')
# Post results to GUI
request("POST", f"{args.post}/results", json=vbt_json)
return vbt_json
def call(cmd, popen=False, **kwargs):
cmd_strings = [str(x) for x in cmd]
print(" ".join(cmd_strings))
out_basename = os.path.join(
kwargs.get("cwd", "."), str_to_fname(chain.from_iterable(cmd_strings))
)
with open(out_basename + "-stdout.txt", "w") as out, open(
out_basename + "-stderr.txt", "w"
) as err:
kwargs.setdefault("stdout", out)
kwargs.setdefault("stderr", err)
if popen:
return Popen(cmd_strings, **kwargs)
assert subprocess_call(cmd_strings, **kwargs) == 0, "subprocess.call failed"
return None
def call_print(cmd, popen=False, **_):
cmd_strings = [str(x) for x in cmd]
print(" ".join(cmd_strings))
if popen:
class FakePopen:
def communicate(self):
pass
return FakePopen()
return 0
def str_to_fname(iterable):
return "".join(c for c in iterable if c in VALID_CHARS)
def parse_args():
parser = argparse.ArgumentParser()
# TODO: Which phases to run
parser.add_argument("--no-vmni", action="store_false", dest="vmni")
parser.add_argument("--no-vmn", action="store_false", dest="vmn")
parser.add_argument("--no-vbt", action="store_false", dest="vbt")
parser.add_argument("--demo", action="store_true")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument(
"--post", nargs="?", default=None, const="https://vmn-webapp.azurewebsites.net/"
)
# 2.1.1 common parameters
parser.add_argument("-sid", "--session-id", default="Session1")
parser.add_argument("-name", "--name", default="myElection")
parser.add_argument("-nopart", "--num-part", default=3, type=int)
parser.add_argument("-thres", "--threshold", default=0, type=int)
# 2.1.2 Individual info files
parser.add_argument("-n", "--num-parties", default=3, type=int)
parser.add_argument("--http-format", default="http://{ip}:{port}")
parser.add_argument("--http-port", default=25432, type=int)
parser.add_argument("--hint-format", default="{ip}:{port}")
# parser.add_argument("--hint-format", default="http://verificatum.assert-team.eu:{port}")
parser.add_argument("--hint-port", default=24321, type=int)
parser.add_argument("--party-format", default="party{idx}")
parser.add_argument("--ip", "-i", action="append", dest="ips")
args = parser.parse_args()
if args.threshold <= 0:
args.threshold = args.num_part
if args.ips is None:
args.ips = ["localhost"] * args.num_parties
elif len(args.ips) != args.num_parties:
raise ValueError("Wrong number of IPs passed")
args.call = call_print if args.dry_run else call
if args.post:
args.post = args.post.rstrip("/")
return args
if __name__ == "__main__":
import sys
sys.exit(main(parse_args()))