accumulators/deal_shares.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 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_ACCUMULATORS_PUBKEY_SSS_DEAL_SHARES_HPP
27 #define CRYPTO3_ACCUMULATORS_PUBKEY_SSS_DEAL_SHARES_HPP
28 
29 #include <cstddef>
30 #include <set>
31 #include <utility>
32 #include <algorithm>
33 #include <iterator>
34 
35 #include <boost/concept_check.hpp>
36 
37 #include <boost/accumulators/framework/accumulator_base.hpp>
38 #include <boost/accumulators/framework/parameters/sample.hpp>
39 
43 
48 
50 
51 namespace nil {
52  namespace crypto3 {
53  namespace pubkey {
54  namespace accumulators {
55  namespace impl {
56  template<typename ProcessingMode, typename = void>
58 
59  template<typename ProcessingMode>
60  struct deal_shares_impl<ProcessingMode> : boost::accumulators::accumulator_base {
61  protected:
62  typedef ProcessingMode processing_mode_type;
63  typedef typename processing_mode_type::scheme_type scheme_type;
64  typedef typename processing_mode_type::op_type op_type;
65  typedef typename processing_mode_type::internal_accumulator_type internal_accumulator_type;
66 
67  public:
68  typedef typename processing_mode_type::result_type result_type;
69 
70  //
71  // boost::accumulators::sample -- participants number
72  //
73  // nil::crypto3::accumulators::threshold_value -- threshold number of participants
74  //
75  template<typename Args>
76  deal_shares_impl(const Args &args) :
77  seen_coeffs(0), n(args[boost::accumulators::sample]),
78  t(args[nil::crypto3::accumulators::threshold_value]) {
80  scheme_type>::value) {
81  processing_mode_type::init_accumulator(
82  acc, n, t, args[nil::crypto3::accumulators::weights]);
83  } else {
84  processing_mode_type::init_accumulator(acc, n, t);
85  }
86  }
87 
88  inline result_type result(boost::accumulators::dont_care) const {
89  assert(t == seen_coeffs);
90 
91  return processing_mode_type::process(acc);
92  }
93 
94  //
95  // boost::accumulators::sample -- polynomial coefficients
96  // input coefficients should be supplied in increasing term degrees order
97  //
98  template<typename Args>
99  inline void operator()(const Args &args) {
100  resolve_type(args[boost::accumulators::sample],
101  args[::nil::crypto3::accumulators::iterator_last | nullptr]);
102  }
103 
104  protected:
105  inline void resolve_type(const typename scheme_type::coeff_type &coeff,
106  std::nullptr_t = nullptr) {
107  if (t == seen_coeffs) {
108  return;
109  }
110 
111  processing_mode_type::update(acc, seen_coeffs, coeff);
112  seen_coeffs++;
113  }
114 
115  template<typename InputRange>
116  inline void resolve_type(const InputRange &range, std::nullptr_t) {
117  for (const auto &c : range) {
118  resolve_type(c);
119  }
120  }
121 
122  template<typename InputIterator>
123  inline void resolve_type(InputIterator first, InputIterator last) {
124  for (auto it = first; it != last; it++) {
125  resolve_type(*it);
126  }
127  }
128 
129  std::size_t n;
130  std::size_t t;
131  std::size_t seen_coeffs;
133  };
134 
135  // template<typename ProcessingMode>
136  // struct deal_shares_impl<
137  // ProcessingMode,
138  // typename std::enable_if<std::is_same<
139  // typename ProcessingMode::scheme_type,
140  // pubkey::weighted_shamir_sss<typename
141  // ProcessingMode::scheme_type::group_type>>::value>::type>
142  // : boost::accumulators::accumulator_base {
143  // protected:
144  // typedef typename ProcessingMode::scheme_type scheme_type;
145  // typedef typename ProcessingMode::op_type op_type;
146  //
147  // typedef typename op_type::coeffs_type coeffs_type;
148  // typedef typename op_type::weight_type weight_type;
149  // typedef typename op_type::weights_type weights_type;
150  // typedef typename op_type::shares_type shares_type;
151  //
152  // public:
153  // typedef shares_type result_type;
154  //
155  // //
156  // // boost::accumulators::sample -- participants number
157  // //
158  // // nil::crypto3::accumulators::threshold_value -- threshold number of participants
159  // //
160  // template<typename Args>
161  // deal_shares_impl(const Args &args) : seen_coeffs(0) {
162  // assert(op_type::check_t(args[nil::crypto3::accumulators::threshold_value],
163  // args[boost::accumulators::sample]));
164  // t = args[nil::crypto3::accumulators::threshold_value];
165  // n = args[boost::accumulators::sample];
166  // std::size_t i = 1;
167  // std::generate_n(std::inserter(shares_weights, shares_weights.end()), n, [&i]() {
168  // return weight_type(i++, 1);
169  // });
170  // }
171  //
172  // inline result_type result(boost::accumulators::dont_care) const {
173  // assert(t == seen_coeffs);
174  // return op_type::deal_shares(coeffs, shares_weights);
175  // }
176  //
177  // //
178  // // boost::accumulators::sample -- participant weight
179  // // or
180  // // boost::accumulators::sample -- polynomial coefficients
181  // // input coefficients should be supplied in increasing term degrees order
182  // //
183  // template<typename Args>
184  // inline void operator()(const Args &args) {
185  // resolve_type(
186  // args[boost::accumulators::sample],
187  // args[::nil::crypto3::accumulators::iterator_last | typename
188  // coeffs_type::iterator()]);
189  // }
190  //
191  // protected:
192  // template<typename Coeff,
193  // typename InputIterator,
194  // typename op_type::template check_coeff_type<Coeff> = true>
195  // inline void resolve_type(const Coeff &coeff, InputIterator) {
196  // assert(t > seen_coeffs);
197  // coeffs.emplace_back(coeff);
198  // seen_coeffs++;
199  // }
200  //
201  // template<typename Coeffs,
202  // typename InputIterator,
203  // typename op_type::template check_coeff_type<typename Coeffs::value_type> = true>
204  // inline void resolve_type(const Coeffs &coeffs, InputIterator dont_care) {
205  // for (const auto &c : coeffs) {
206  // resolve_type(c, dont_care);
207  // }
208  // }
209  //
210  // template<typename InputIterator,
211  // typename op_type::template check_coeff_type<
212  // typename std::iterator_traits<InputIterator>::value_type> = true>
213  // inline void resolve_type(InputIterator first, InputIterator last) {
214  // for (auto it = first; it != last; it++) {
215  // resolve_type(*it, last);
216  // }
217  // }
218  //
219  // template<typename Weight,
220  // typename InputIterator,
221  // typename op_type::template check_weight_type<Weight> = true>
222  // inline void resolve_type(const Weight &w, InputIterator) {
223  // assert(op_type::check_weight(w, n));
224  // shares_weights.insert_or_assign(w.first, w.second);
225  // }
226  //
227  // template<typename InputIterator,
228  // typename op_type::template check_weight_type<
229  // typename std::iterator_traits<InputIterator>::value_type> = true>
230  // inline void resolve_type(InputIterator first, InputIterator last) {
231  // for (auto it = first; it != last; it++) {
232  // resolve_type(*it, last);
233  // }
234  // }
235  //
236  // std::size_t t;
237  // std::size_t n;
238  // std::size_t seen_coeffs;
239  // coeffs_type coeffs;
240  // weights_type shares_weights;
241  // };
242  } // namespace impl
243 
244  namespace tag {
245  template<typename ProcessingMode>
246  struct deal_shares : boost::accumulators::depends_on<> {
247  typedef ProcessingMode mode_type;
248 
251 
252  typedef boost::mpl::always<accumulators::impl::deal_shares_impl<mode_type>> impl;
253  };
254  } // namespace tag
255 
256  namespace extract {
257  template<typename ProcessingMode, typename AccumulatorSet>
258  typename boost::mpl::apply<AccumulatorSet, tag::deal_shares<ProcessingMode>>::type::result_type
259  deal_shares(const AccumulatorSet &acc) {
260  return boost::accumulators::extract_result<tag::deal_shares<ProcessingMode>>(acc);
261  }
262  } // namespace extract
263  } // namespace accumulators
264  } // namespace pubkey
265  } // namespace crypto3
266 } // namespace nil
267 
268 #endif // CRYPTO3_ACCUMULATORS_PUBKEY_SSS_DEAL_SHARES_HPP
boost::mpl::apply< AccumulatorSet, tag::deal_shares< ProcessingMode > >::type::result_type deal_shares(const AccumulatorSet &acc)
Definition: accumulators/deal_shares.hpp:259
boost::mpl::apply< AccumulatorSet, tag::pubkey< ProcessingMode > >::type::result_type pubkey(const AccumulatorSet &acc)
Definition: accumulators/pubkey.hpp:106
Definition: pair.hpp:31
std::size_t n
Definition: accumulators/deal_shares.hpp:129
void resolve_type(const InputRange &range, std::nullptr_t)
Definition: accumulators/deal_shares.hpp:116
ProcessingMode processing_mode_type
Definition: accumulators/deal_shares.hpp:62
deal_shares_impl(const Args &args)
Definition: accumulators/deal_shares.hpp:76
void operator()(const Args &args)
Definition: accumulators/deal_shares.hpp:99
std::size_t t
Definition: accumulators/deal_shares.hpp:130
std::size_t seen_coeffs
Definition: accumulators/deal_shares.hpp:131
processing_mode_type::internal_accumulator_type internal_accumulator_type
Definition: accumulators/deal_shares.hpp:65
void resolve_type(const typename scheme_type::coeff_type &coeff, std::nullptr_t=nullptr)
Definition: accumulators/deal_shares.hpp:105
processing_mode_type::op_type op_type
Definition: accumulators/deal_shares.hpp:64
internal_accumulator_type acc
Definition: accumulators/deal_shares.hpp:132
processing_mode_type::scheme_type scheme_type
Definition: accumulators/deal_shares.hpp:63
processing_mode_type::result_type result_type
Definition: accumulators/deal_shares.hpp:68
void resolve_type(InputIterator first, InputIterator last)
Definition: accumulators/deal_shares.hpp:123
result_type result(boost::accumulators::dont_care) const
Definition: accumulators/deal_shares.hpp:88
Definition: accumulators/deal_shares.hpp:57
Definition: accumulators/deal_shares.hpp:246
boost::mpl::always< accumulators::impl::deal_shares_impl< mode_type > > impl
Definition: accumulators/deal_shares.hpp:252
ProcessingMode mode_type
Definition: accumulators/deal_shares.hpp:247
Definition: weighted_shamir.hpp:35