weighted_shamir.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Ilias Khairullin <ilias@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_PUBKEY_WEIGHTED_SHAMIR_SSS_HPP
27 #define CRYPTO3_PUBKEY_WEIGHTED_SHAMIR_SSS_HPP
28 
30 
31 namespace nil {
32  namespace crypto3 {
33  namespace pubkey {
34  template<typename Group>
35  struct weighted_shamir_sss : public shamir_sss<Group> {
38  };
39 
40  template<typename Group>
44  typedef std::pair<std::size_t, std::vector<part_public_share_type>> public_share_type;
46  typedef typename public_share_type::first_type index_type;
47  typedef typename public_share_type::second_type value_type;
49 
50  public_share_sss() = default;
51 
52  public_share_sss(std::size_t i, std::size_t w, std::size_t threshold_number) : t(threshold_number) {
53  public_share.first = i;
54  assert(scheme_type::check_participant_index(get_index()));
55  assert(scheme_type::check_weight(i, w));
56  for (std::size_t j = 1; j <= w; ++j) {
57  public_share.second.emplace_back(i * t + j);
58  assert(indexes.emplace(public_share.second.back().get_index()).second);
59  }
60  }
61 
62  template<typename PartPublicShares>
63  public_share_sss(std::size_t i, std::size_t threshold_number, const PartPublicShares &i_public_shares) :
64  public_share_sss(i, threshold_number, std::cbegin(i_public_shares), std::cend(i_public_shares)) {
65  }
66 
67  template<typename PartPublicShareIt>
68  public_share_sss(const std::size_t i, std::size_t threshold_number, PartPublicShareIt first,
69  PartPublicShareIt last) :
70  t(threshold_number) {
71  public_share.first = i;
72  assert(scheme_type::check_participant_index(get_index()));
73  for (auto iter = first; iter != last; ++iter) {
74  public_share.second.emplace_back(*iter);
75  assert(indexes.emplace(public_share.second.back().get_index()).second);
76  }
77  }
78 
79  inline index_type get_index() const {
80  return public_share.first;
81  }
82 
83  inline const value_type &get_value() const {
84  return public_share.second;
85  }
86 
87  inline const data_type &get_data() const {
88  return public_share.second;
89  }
90 
91  inline const indexes_type &get_indexes() const {
92  return indexes;
93  }
94 
95  inline std::size_t get_threshold_number() const {
96  return t;
97  }
98 
99  inline std::size_t get_weight() const {
100  return std::size(indexes);
101  }
102 
103  bool operator==(const public_share_sss &other) const {
104  return this->public_share == other.public_share;
105  }
106 
107  bool operator<(const public_share_sss &other) const {
108  return this->get_index() < other.get_index();
109  }
110 
111  inline part_public_share_type
112  to_shamir(const typename scheme_type::weights_type &confirmed_weights) const {
113  auto confirmed_indexes = scheme_type::get_indexes(confirmed_weights, t);
114 
115  typename scheme_type::public_element_type part_share = scheme_type::public_element_type::zero();
116  for (const auto &public_share_j : public_share.second) {
117  part_share = part_share +
118  public_share_j.get_value() *
119  scheme_type::eval_basis_poly(confirmed_indexes, public_share_j.get_index());
120  }
121 
122  return part_public_share_type(public_share.first, part_share);
123  }
124 
125  private:
126  std::size_t t;
127  indexes_type indexes;
128  public_share_type public_share;
129  };
130 
131  template<typename Group>
135  typedef std::pair<std::size_t, std::vector<part_share_type>> share_type;
137  typedef typename share_type::first_type index_type;
138  typedef typename share_type::second_type value_type;
140 
141  share_sss() = default;
142 
143  share_sss(std::size_t i, std::size_t w, std::size_t threshold_number) : t(threshold_number) {
144  share.first = i;
145  assert(scheme_type::check_participant_index(get_index()));
146  assert(scheme_type::check_weight(i, w));
147  for (std::size_t j = 1; j <= w; ++j) {
148  share.second.emplace_back(i * t + j);
149  assert(indexes.emplace(share.second.back().get_index()).second);
150  }
151  }
152 
153  inline index_type get_index() const {
154  return share.first;
155  }
156 
157  inline const value_type &get_value() const {
158  return share.second;
159  }
160 
161  inline const data_type &get_data() const {
162  return share;
163  }
164 
165  inline const indexes_type &get_indexes() const {
166  return indexes;
167  }
168 
169  inline std::size_t get_threshold_number() const {
170  return t;
171  }
172 
173  inline std::size_t get_weight() const {
174  return std::size(indexes);
175  }
176 
177  operator public_share_sss<scheme_type>() const {
179 
180  return To(share.first, t, share.second);
181  }
182 
183  bool operator==(const share_sss &other) const {
184  return this->share == other.share;
185  }
186 
187  bool operator<(const share_sss &other) const {
188  return this->get_index() < other.get_index();
189  }
190 
191  //
192  // 0 <= k < t
193  //
194  inline void update(const typename scheme_type::coeff_type &coeff, std::size_t exp) {
195  for (auto &share_j : share.second) {
196  share_j.update(coeff, exp);
197  }
198  }
199 
200  inline part_share_type to_shamir(const typename scheme_type::weights_type &confirmed_weights) const {
201  auto confirmed_indexes = scheme_type::get_indexes(confirmed_weights, t);
202 
203  typename scheme_type::private_element_type part_share = scheme_type::private_element_type::zero();
204  for (const auto &share_j : share.second) {
205  part_share = part_share + share_j.get_value() * scheme_type::eval_basis_poly(
206  confirmed_indexes, share_j.get_index());
207  }
208 
209  return part_share_type(share.first, part_share);
210  }
211 
212  private:
213  std::size_t t;
214  indexes_type indexes;
215  share_type share;
216  };
217 
218  template<typename Group>
224 
225  template<typename Shares>
226  secret_sss(const Shares &shares) : secret_sss(std::cbegin(shares), std::cend(shares)) {
227  }
228 
229  template<typename ShareIt>
230  secret_sss(ShareIt first, ShareIt last) : secret(reconstruct_secret(first, last)) {
231  }
232 
233  template<typename Shares>
234  secret_sss(const Shares &shares, const indexes_type &indexes) :
235  secret_sss(std::cbegin(shares), std::cend(shares), indexes) {
236  }
237 
238  template<typename ShareIt>
239  secret_sss(ShareIt first, ShareIt last, const indexes_type &indexes) :
240  secret(reconstruct_secret(first, last, indexes)) {
241  }
242 
243  inline const value_type &get_value() const {
244  return secret;
245  }
246 
247  bool operator==(const secret_sss &other) const {
248  return this->secret == other.secret;
249  }
250 
251  private:
252  template<typename ShareIt,
253  typename std::enable_if<
254  std::is_same<typename std::remove_cv<typename std::remove_reference<
255  typename std::iterator_traits<ShareIt>::value_type>::type>::type,
256  share_sss<scheme_type>>::value,
257  bool>::type = true>
258  static inline secret_type reconstruct_secret(ShareIt first, ShareIt last) {
259  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<ShareIt>));
260 
262  for (auto iter = first; iter != last; iter++) {
263  std::copy(std::cbegin(iter->get_value()), std::cend(iter->get_value()),
264  std::back_inserter(_shares));
265  }
266 
267  return reconstruct_secret(std::cbegin(_shares), std::cend(_shares),
268  scheme_type::get_indexes(std::cbegin(_shares), std::cend(_shares)));
269  }
270 
271  template<typename ShareIt,
272  typename std::enable_if<
273  std::is_same<typename std::remove_cv<typename std::remove_reference<
274  typename std::iterator_traits<ShareIt>::value_type>::type>::type,
275  typename share_sss<scheme_type>::part_share_type>::value,
276  bool>::type = true>
277  static inline secret_type reconstruct_secret(ShareIt first, ShareIt last, const indexes_type &indexes) {
278  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<ShareIt>));
279 
280  secret_type secret = secret_type::zero();
281  for (auto it = first; it != last; it++) {
282  secret = secret + it->get_value() * scheme_type::eval_basis_poly(indexes, it->get_index());
283  }
284 
285  return secret;
286  }
287 
288  secret_type secret;
289  };
290 
291  template<typename Group>
295  typedef std::vector<share_type> shares_type;
298 
299  static inline void init_accumulator(internal_accumulator_type &acc, std::size_t n, std::size_t t,
300  const typename scheme_type::weights_type &weights) {
301  assert(n == std::distance(std::cbegin(weights), std::cend(weights)));
302  assert(scheme_type::check_threshold_value(t, n));
303 
304  for (const auto &w_i : weights) {
305  acc.emplace_back(w_i.first, w_i.second, t);
306  }
307  }
308 
309  static inline void update(internal_accumulator_type &acc, std::size_t exp,
310  const typename scheme_type::coeff_type &coeff) {
311  for (auto shares_iter = std::begin(acc); shares_iter != std::end(acc); ++shares_iter) {
312  shares_iter->update(coeff, exp);
313  }
314  }
315 
317  return acc;
318  }
319  };
320 
321  template<typename Group>
326  typedef std::vector<share_type> internal_accumulator_type;
328 
329  public:
330  static inline void init_accumulator() {
331  }
332 
333  static inline void update(internal_accumulator_type &acc, const share_type &share) {
334  acc.emplace_back(share);
335  }
336 
338  return acc;
339  }
340  };
341  } // namespace pubkey
342  } // namespace crypto3
343 } // namespace nil
344 
345 #endif // CRYPTO3_PUBKEY_WEIGHTED_SHAMIR_SSS_HPP
std::enable_if<!boost::accumulators::detail::is_accumulator_set< OutputIterator >::value, OutputIterator >::type reconstruct_secret(InputIterator first, InputIterator last, OutputIterator out)
Reconstruct secret using passed shares.
Definition: reconstruct_secret.hpp:69
boost::mpl::apply< AccumulatorSet, tag::pubkey< ProcessingMode > >::type::result_type pubkey(const AccumulatorSet &acc)
Definition: accumulators/pubkey.hpp:106
Definition: pair.hpp:31
shares_type internal_accumulator_type
Definition: weighted_shamir.hpp:296
share_sss< scheme_type > share_type
Definition: weighted_shamir.hpp:294
static void update(internal_accumulator_type &acc, std::size_t exp, const typename scheme_type::coeff_type &coeff)
Definition: weighted_shamir.hpp:309
weighted_shamir_sss< Group > scheme_type
Definition: weighted_shamir.hpp:293
std::vector< share_type > shares_type
Definition: weighted_shamir.hpp:295
static void init_accumulator(internal_accumulator_type &acc, std::size_t n, std::size_t t, const typename scheme_type::weights_type &weights)
Definition: weighted_shamir.hpp:299
shares_type result_type
Definition: weighted_shamir.hpp:297
static result_type process(internal_accumulator_type &acc)
Definition: weighted_shamir.hpp:316
Definition: deal_shares_op.hpp:33
std::size_t get_threshold_number() const
Definition: weighted_shamir.hpp:95
const value_type & get_value() const
Definition: weighted_shamir.hpp:83
weighted_shamir_sss< Group > scheme_type
Definition: weighted_shamir.hpp:42
public_share_sss< shamir_sss< Group > > part_public_share_type
Definition: weighted_shamir.hpp:43
part_public_share_type to_shamir(const typename scheme_type::weights_type &confirmed_weights) const
Definition: weighted_shamir.hpp:112
std::size_t get_weight() const
Definition: weighted_shamir.hpp:99
const data_type & get_data() const
Definition: weighted_shamir.hpp:87
std::pair< std::size_t, std::vector< part_public_share_type > > public_share_type
Definition: weighted_shamir.hpp:44
public_share_sss(const std::size_t i, std::size_t threshold_number, PartPublicShareIt first, PartPublicShareIt last)
Definition: weighted_shamir.hpp:68
public_share_type::first_type index_type
Definition: weighted_shamir.hpp:46
bool operator<(const public_share_sss &other) const
Definition: weighted_shamir.hpp:107
public_share_type data_type
Definition: weighted_shamir.hpp:48
public_share_sss(std::size_t i, std::size_t threshold_number, const PartPublicShares &i_public_shares)
Definition: weighted_shamir.hpp:63
bool operator==(const public_share_sss &other) const
Definition: weighted_shamir.hpp:103
public_share_sss(std::size_t i, std::size_t w, std::size_t threshold_number)
Definition: weighted_shamir.hpp:52
public_share_type::second_type value_type
Definition: weighted_shamir.hpp:47
const indexes_type & get_indexes() const
Definition: weighted_shamir.hpp:91
scheme_type::indexes_type indexes_type
Definition: weighted_shamir.hpp:45
index_type get_index() const
Definition: weighted_shamir.hpp:79
Definition: public_share_sss.hpp:33
share_sss< scheme_type > share_type
Definition: weighted_shamir.hpp:324
static void update(internal_accumulator_type &acc, const share_type &share)
Definition: weighted_shamir.hpp:333
secret_sss< scheme_type > secret_type
Definition: weighted_shamir.hpp:325
static void init_accumulator()
Definition: weighted_shamir.hpp:330
std::vector< share_type > internal_accumulator_type
Definition: weighted_shamir.hpp:326
weighted_shamir_sss< Group > scheme_type
Definition: weighted_shamir.hpp:323
static result_type process(internal_accumulator_type &acc)
Definition: weighted_shamir.hpp:337
Definition: reconstruct_secret_op.hpp:33
secret_type value_type
Definition: weighted_shamir.hpp:223
const value_type & get_value() const
Definition: weighted_shamir.hpp:243
secret_sss(const Shares &shares)
Definition: weighted_shamir.hpp:226
secret_sss(const Shares &shares, const indexes_type &indexes)
Definition: weighted_shamir.hpp:234
scheme_type::indexes_type indexes_type
Definition: weighted_shamir.hpp:222
scheme_type::private_element_type secret_type
Definition: weighted_shamir.hpp:221
bool operator==(const secret_sss &other) const
Definition: weighted_shamir.hpp:247
weighted_shamir_sss< Group > scheme_type
Definition: weighted_shamir.hpp:220
secret_sss(ShareIt first, ShareIt last, const indexes_type &indexes)
Definition: weighted_shamir.hpp:239
secret_sss(ShareIt first, ShareIt last)
Definition: weighted_shamir.hpp:230
Definition: secret_sss.hpp:33
Definition: shamir.hpp:58
void update(const typename scheme_type::coeff_type &coeff, std::size_t exp)
Definition: weighted_shamir.hpp:194
share_type::second_type value_type
Definition: weighted_shamir.hpp:138
share_sss< shamir_sss< Group > > part_share_type
Definition: weighted_shamir.hpp:134
weighted_shamir_sss< Group > scheme_type
Definition: weighted_shamir.hpp:133
share_sss(std::size_t i, std::size_t w, std::size_t threshold_number)
Definition: weighted_shamir.hpp:143
share_type::first_type index_type
Definition: weighted_shamir.hpp:137
scheme_type::indexes_type indexes_type
Definition: weighted_shamir.hpp:136
bool operator<(const share_sss &other) const
Definition: weighted_shamir.hpp:187
const data_type & get_data() const
Definition: weighted_shamir.hpp:161
part_share_type to_shamir(const typename scheme_type::weights_type &confirmed_weights) const
Definition: weighted_shamir.hpp:200
bool operator==(const share_sss &other) const
Definition: weighted_shamir.hpp:183
index_type get_index() const
Definition: weighted_shamir.hpp:153
const indexes_type & get_indexes() const
Definition: weighted_shamir.hpp:165
std::pair< std::size_t, std::vector< part_share_type > > share_type
Definition: weighted_shamir.hpp:135
share_type data_type
Definition: weighted_shamir.hpp:139
std::size_t get_threshold_number() const
Definition: weighted_shamir.hpp:169
std::size_t get_weight() const
Definition: weighted_shamir.hpp:173
const value_type & get_value() const
Definition: weighted_shamir.hpp:157
Definition: share_sss.hpp:35
typename Group::curve_type::scalar_field_type::value_type private_element_type
Definition: pubkey/include/nil/crypto3/pubkey/secret_sharing/basic_policy.hpp:47
private_element_type coeff_type
Definition: pubkey/include/nil/crypto3/pubkey/secret_sharing/basic_policy.hpp:55
typename Group::value_type public_element_type
Definition: pubkey/include/nil/crypto3/pubkey/secret_sharing/basic_policy.hpp:48
std::set< std::size_t > indexes_type
Definition: pubkey/include/nil/crypto3/pubkey/secret_sharing/basic_policy.hpp:57
Definition: weighted_basic_policy.hpp:37
std::map< std::size_t, std::size_t > weights_type
Definition: weighted_basic_policy.hpp:45
Definition: weighted_shamir.hpp:35
sss_weighted_basic_policy< Group > basic_policy
Definition: weighted_shamir.hpp:36
shamir_sss< Group > base_type
Definition: weighted_shamir.hpp:37