-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttp.py
84 lines (64 loc) · 2.24 KB
/
ttp.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
"""
Trusted parameters generator.
MODIFY THIS FILE.
"""
import collections
import random
import sys
from secret_sharing import *
from typing import (
Dict,
Set,
Tuple,
)
from communication import Communication
from secret_sharing import (
share_secret,
Share,
)
# Feel free to add as many imports as you want.
class TrustedParamGenerator:
"""
A trusted third party that generates random values for the Beaver triplet multiplication scheme.
"""
def __init__(self):
self.participant_ids: Set[str] = set()
self.triplets_shares: Dict[str, Dict[str, Tuple[Share, Share, Share]]] = dict()
def add_participant(self, participant_id: str) -> None:
"""
Add a participant.
"""
self.participant_ids.add(participant_id)
def retrieve_share(self, client_id: str, op_id: str) -> Tuple[Share, Share, Share]:
"""
Retrieve a triplet of shares for a given client_id. And operation id.
"""
if client_id not in self.participant_ids:
return None
# first check if dict not already generated
if op_id in self.triplets_shares.keys():
return self.triplets_shares[op_id][client_id]
else:
# generate the dict corresponding to this new operation and return result
self.gen_beaver(op_id)
return self.triplets_shares[op_id][client_id]
def gen_beaver(self, op_id):
"""Generates a new dictionary containing all beaver triplets for each client for a specific operation
indexed by op_id """
a = random.randint(0, get_mod())
b = random.randint(0, get_mod())
c = mul_mod(a, b)
# generate shares of beaver triplets
a_shares = share_secret(a, len(self.participant_ids))
b_shares = share_secret(b, len(self.participant_ids))
c_shares = share_secret(c, len(self.participant_ids))
# store shares in a dict
res = dict()
for i, name in enumerate(self.participant_ids):
l = list()
l.append(a_shares[i])
l.append(b_shares[i])
l.append(c_shares[i])
res[name] = tuple(l)
# index dict w.r.t the operation id
self.triplets_shares[op_id] = res