-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
executable file
·85 lines (64 loc) · 2.19 KB
/
util.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
#!/usr/bin/python3
import random
import subprocess
import argparse
import os
steps_per_round_fsm1 = 2
steps_per_round_fsm2 = 2
def format_read_var(var_name, line):
l = line.split(" ")
variable_name = l[0]
equals = l[1]
value = int(l[2])
assert equals == "=", "incorrect config format"
assert var_name == variable_name, "incorrect config format with variable " + var_name
return value
def format_read_arr(arr_name, line, C):
l = line.split(" ")
variable_name = l[0]
equals = l[1]
assert equals == "=", "incorrect config format"
assert arr_name == variable_name, "incorrect config format with variable " + var_name
arr = []
for i in range(C):
arr.append(int(l[2+i]))
return arr
def read_config_file(config_file):
with open(config_file, "r") as f:
C = format_read_var("C", f.readline())
Bits = format_read_arr("Bits", f.readline(), C)
return C, Bits
def generateEntry(C, Bits):
return [random.randint(0, 2**bit - 1) for bit in Bits]
def format(entry):
s = ", ".join(str(component) for component in entry)
return s + "\n"
def GenerateTestCase(N, C, Bits, IN_DB, config_file, verbose = False):
if verbose:
print("Creating following test case:")
print("N =", N)
print("C =", C)
print("Config file =", config_file)
print("Bits per component", Bits)
print("In DB =", IN_DB)
subprocess.run(["./tests/GenerateOTs", str(N), config_file])
query = ""
entry = random.randint(0, N - 1) if IN_DB else -1
if not IN_DB:
query = generateEntry(C, Bits)
if verbose:
print("Generating database")
with open('./data/server/db.txt', 'w') as db_file:
for i in range(N):
if i == entry:
query = generateEntry(C, Bits)
db_file.write(format(query))
else:
db_file.write(format(generateEntry(C, Bits)))
if verbose:
print("Creating Query")
if IN_DB:
print("Query is in database at location: %d" % entry)
with open('./data/client/query.txt', 'w') as query_file:
query_file.write(format(query))
return entry