-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_graph.mpc
132 lines (102 loc) · 3.74 KB
/
test_graph.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from Compiler.mpc_graph_lib import dijkstra_optimized, T
from random import randint
print_ln("----------------------------------- Testing graph_lib -----------------------------------")
class bcolors:
HEADER = '\033[95m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def test_print_vector_secret(V):
[print_str("%s ", (v.reveal() if type(v) == sint else v)) for v in V]
print_ln(" ")
def test_print_vector_public(V):
[print_str("%s ", v) for v in V]
print_ln(" ")
def cast_to_sint(value, public_structure= False ):
return sint(value) if public_structure and value != T else value
def test(actual, expected):
total_tests = cint.load_mem(3000)
total_tests += 1
total_tests.store_in_mem(3000)
if_then(actual != expected)
print_ln(bcolors.FAIL + 'FAILURE: expected %s, got %s' + bcolors.ENDC, expected, actual)
failed_tests = cint.load_mem(6000)
failed_tests += 1
failed_tests.store_in_mem(6000)
else_then()
print_ln(bcolors.OKGREEN + "TEST: %s equals %s" + bcolors.ENDC, expected, actual)
end_if()
def get_random_adjacency_matrix(dimension):
matrix_c = []
matrix_s = []
for i in range(dimension):
row_c = []
row_s = []
a= T
for j in range(dimension):
if (j > i):
a = randint(0,10)
else:
a= T
row_c.append(a)
row_s.append(sint(a))
matrix_c.append(row_c)
matrix_s.append(row_s)
return matrix_c, matrix_s
def dijkstra_clear(weights, source):
n = len(weights)
distance = [T] * n
alpha = [T] * n
vertex_id = [i for i in range(n)]
distance[source] = 0
for i in range(n):
d_prime = T
for j in range(n-1, i-1, -1):
if(distance[j]< d_prime):
v = j
d_prime = distance[j]
row_temp = weights[i]
weights[i] = weights[v]
weights[v] = row_temp
distance_temp = distance[i]
distance[i] = distance[v]
distance[v] = distance_temp
vertex_id_temp = vertex_id[i]
vertex_id[i] = vertex_id[v]
vertex_id[v] = vertex_id_temp
for j in range(i+1, n):
a = distance[i] + weights[i][j]
if (a < distance[j]):
distance[j] = a
alpha[j] = vertex_id[i]
return alpha
def test_connected_acyclic_graph(matrix_c):
size = len(matrix_c)
matrix_s = [[cast_to_sint(element) for element in row] for row in matrix_c]
for i in range(size):
test_print_vector_secret(matrix_s[i])
expected = dijkstra_clear(matrix_c, 0)
actual = dijkstra_optimized(matrix_s, 0)
actual[0]= sint(actual[0])
print_ln("Expected:")
test_print_vector_public(expected)
print_ln("Actual:")
test_print_vector_secret(expected)
a = 0
for i in range(size):
a += randint(1,T)*(expected[i] - actual[i])
test(a.reveal(), 0)
def test_connected_acyclic_graph_4v4():
matrix_c= [[T,1,7,T],[T,T,1,1],[T,T,T,5],[T,T,T,T]]
test_connected_acyclic_graph(matrix_c)
def test_connected_acyclic_graph_5v5():
matrix_c= [[T,1,7,5,T],[T,T,1,1,T],[T,T,T,1,8],[T,T,T,T,1],[T,T,T,T,T]]
test_connected_acyclic_graph(matrix_c)
def test_connected_acyclic_graph_6v6():
matrix_c= [[T,1,7,5,T,T],[T,T,1,4,T,T],[T,T,T,1,8,T],[T,T,T,T,1,T],[T,T,T,T,T,3],[T,T,T,T,T,T]]
test_connected_acyclic_graph(matrix_c)
test_connected_acyclic_graph_4v4()
test_connected_acyclic_graph_5v5()
test_connected_acyclic_graph_6v6()
print_str("\n \n TESTS:" + bcolors.OKGREEN + " %s" + bcolors.ENDC + "/" + bcolors.FAIL + "%s failed" + bcolors.ENDC + "\n \n", cint.load_mem(5000), cint.load_mem(6000))