-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_triples_test.py
79 lines (75 loc) · 2.82 KB
/
mock_triples_test.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
from shamir import Shamir
from mock_messaging import MockMessenger
from mock_triples import TripleGeneration
from multiprocessing import Queue, Process
import time
def consumer(mq, t, n, processes):
vals = []
while len(vals) < n:
if not mq.empty():
vals.append(mq.get())
n_triples = len(vals[0])
for i in [0, 1, n_triples-2, n_triples-1]:
trips = [vals[j][i] for j in range(n)]
a_shares = [t.a for t in trips]
b_shares = [t.b for t in trips]
c_shares = [t.c for t in trips]
a = Shamir(t, n).reconstruct_secret(a_shares)
b = Shamir(t, n).reconstruct_secret(b_shares)
c = Shamir(t, n).reconstruct_secret(c_shares)
assert a*b==c, "triples are not equal"
def run_triplegen_process(t, n, index, queues, main_queue, batch_size, n_batches):
shamir = Shamir(t, n)
messenger = MockMessenger(t, n, index, queues, "")
tg = TripleGeneration(index, shamir, messenger, batch_size=batch_size, n_batches=n_batches)
tg.run()
main_queue.put(tg.triples)
def test_triple_generation(t, n, batch_size, n_batches):
mq = Queue()
queues = [Queue() for _ in range(n)]
processes = []
for i in range(n):
p = Process(target=run_triplegen_process, args=(t, n, i+1, queues, mq, batch_size, n_batches))
processes.append(p)
start = time.time()
for p in processes:
p.start()
t1 = Process(target=consumer, args=(mq, t, n, processes))
t1.start()
for p in processes:
if p.is_alive():
p.join()
t1.join()
print(f"time: {round(time.time()-start, 4)} seconds")
for q in queues:
q.close()
q.join_thread()
mq.close()
mq.join_thread()
if __name__ == "__main__":
print("--Batch Size: 100, Batches: 10--")
test_triple_generation(1, 3, 100, 10)
print("--Batch Size: 1000, Batches: 1--")
test_triple_generation(1, 3, 1000, 1)
print("--Batch Size: 100, Batches: 20--")
test_triple_generation(1, 3, 100, 20)
print("--Batch Size: 1000, Batches: 2--")
test_triple_generation(1, 3, 1000, 2)
print("--Batch Size: 2000, Batches: 1--")
test_triple_generation(1, 3, 2000, 1)
print("--Batch Size: 100, Batches: 50--")
test_triple_generation(1, 3, 100, 50)
print("--Batch Size: 1000, Batches: 5--")
test_triple_generation(1, 3, 1000, 5)
print("--Batch Size: 5000, Batches: 1--")
test_triple_generation(1, 3, 5000, 1)
print("--Batch Size: 100, Batches: 100--")
test_triple_generation(1, 3, 100, 100)
print("--Batch Size: 1000, Batches: 10--")
test_triple_generation(1, 3, 1000, 10)
print("--Batch Size: 2000, Batches: 5--")
test_triple_generation(1, 3, 2000, 5)
print("--Batch Size: 5000, Batches: 2--")
test_triple_generation(1, 3, 5000, 2)
print("--Batch Size: 10000, Batches: 1--")
test_triple_generation(1, 3, 10000, 1)