-
Notifications
You must be signed in to change notification settings - Fork 0
/
secretsharing.cpp
175 lines (134 loc) · 4.33 KB
/
secretsharing.cpp
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
/*
Testbed for empirical evaluation of KP-ABE schemes, according to Crampton, Pinto (CSF2014).
Code by: Alexandre Miranda Pinto
This file implements the classes defined in secretsharing.h
Since AccessPolicy and SecretSharing are abstract classes, they can not be directly instantiated.
Since furthermore ShareTuple is trivially simple, there are not tests for these classes and so there is not a corresponding testfile.
*/
#ifndef DEF_UTILS
#include "utils.h"
#endif
#include "secretsharing.h"
ShareTuple::ShareTuple():
partIndex(0), share(0), shareID("")
{}
ShareTuple::ShareTuple(const int pi, const Big s, const std::string si):
partIndex(pi), share(s), shareID(si)
{}
ShareTuple::ShareTuple(const ShareTuple& other) :
partIndex(other.partIndex),
share(other.share),
shareID(other.shareID)
{}
ShareTuple& ShareTuple::operator=(const ShareTuple& other)
{
partIndex = other.partIndex;
share = other.share;
shareID = other.shareID;
return *this;
}
void ShareTuple::setValues(const int pi, const Big s, const int si)
{
partIndex = pi;
share = s;
shareID = si;
}
bool ShareTuple::operator==(const ShareTuple& rhs) const {
if (partIndex != rhs.partIndex) return false;
if (share != rhs.share) return false;
if (shareID != rhs.shareID) return false;
return true;
}
string ShareTuple::to_string() const {
std::stringstream sstrm;
sstrm << "[" << partIndex << ";" << share << ";" << shareID << "] ";
return sstrm.str();
}
int ShareTuple::getPartIndex() const{
return partIndex;
}
Big ShareTuple::getShare() const {
return share;
}
std::string ShareTuple::getShareID() const{
return shareID;
}
std::ostream& operator<<(ostream& out, const ShareTuple &sp) {
return out << "[" << sp.getShareID() << ";" << sp.getPartIndex() << ";" << sp.getShare() << "] ";
}
//==================================================================
AccessPolicy::AccessPolicy()
{
m_participants.push_back(1);
}
AccessPolicy::AccessPolicy(const unsigned int n)
{
for (unsigned int i = 0; i < n; i++) {
m_participants.push_back(i+1);
}
}
AccessPolicy::AccessPolicy(const vector<int> &parts):
m_participants(parts)
{}
unsigned int AccessPolicy::getNumParticipants() const
{
return m_participants.size();
}
vector<int> AccessPolicy::getParticipants() const
{
return m_participants;
}
bool AccessPolicy::evaluate(const vector<ShareTuple> shares, vector<ShareTuple> &witnessShares) const{
witnessShares.clear();
vector<std::string> shareIDs;
for (unsigned int i = 0; i < shares.size(); i++) {
shareIDs.push_back(shares[i].getShareID());
}
ENHDEBUG("calling evaluateIDs");
vector<int> witnessSharesIndices;
bool success = evaluateIDs(shareIDs, witnessSharesIndices);
ENHDEBUG("returned from evaluateIDs");
debugVector("evalute: list of indices found", witnessSharesIndices);
for (unsigned int i = 0; i < witnessSharesIndices.size(); i++) {
witnessShares.push_back(shares[witnessSharesIndices[i]]);
}
return success;
}
//================================================================
SecretSharing::SecretSharing(shared_ptr<AccessPolicy> policy, PFC &pfc):
m_policy(policy), m_order(pfc.order()), m_pfc(pfc)
{}
SecretSharing::SecretSharing(shared_ptr<AccessPolicy> policy, const Big &order, PFC &pfc):
m_policy(policy), m_order(order), m_pfc(pfc)
{}
SecretSharing::~SecretSharing(){
}
Big SecretSharing::getOrder() const{
return m_order;
}
unsigned int SecretSharing::getNumParticipants() const{
return m_policy->getNumParticipants();
}
unsigned int SecretSharing::getNumShares() {
return m_policy->getNumShares();
}
vector<int> SecretSharing::getParticipants() const{
return m_policy->getParticipants();
}
vector<ShareTuple> SecretSharing::getSharesForParticipants(const vector<int> &parts, const vector<ShareTuple> &shares){
vector<ShareTuple> outShares;
outShares.reserve(shares.size());
int n; // this must be a signed int, otherwise contains always returns a positive value and we can not detect an error.
ShareTuple share;
for (unsigned int i = 0; i < shares.size(); i++) {
share = shares[i];
n = contains(parts, share.getPartIndex());
if ( n >= 0) {
outShares.push_back(share);
}
}
return outShares;
}
bool SecretSharing::evaluate(const vector<ShareTuple> shares, vector<ShareTuple> &witnessShares) const {
return m_policy->evaluate(shares, witnessShares);
}