pubkey_value.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_PUBKEY_VALUE_HPP
27 #define CRYPTO3_PUBKEY_VALUE_HPP
28 
29 #include <type_traits>
30 #include <iterator>
31 
32 #include <boost/assert.hpp>
33 #include <boost/concept_check.hpp>
34 
35 #include <boost/range/concepts.hpp>
36 
37 #include <boost/mpl/front.hpp>
38 #include <boost/mpl/apply.hpp>
39 
43 
44 namespace nil {
45  namespace crypto3 {
46  namespace pubkey {
47  namespace detail {
48  template<typename SchemeAccumulator>
49  struct ref_pubkey_impl {
50  typedef SchemeAccumulator accumulator_set_type;
51  typedef
52  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
53 
55  }
56 
58  };
59 
60  template<typename SchemeAccumulator>
62  typedef SchemeAccumulator accumulator_set_type;
63  typedef
64  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
65 
67  accumulator_set(std::forward<accumulator_set_type>(acc)) {
68  }
69 
71  };
72 
73  template<typename SchemeStateImpl>
74  struct range_pubkey_impl : public SchemeStateImpl {
75  typedef SchemeStateImpl scheme_state_impl_type;
76 
77  typedef typename scheme_state_impl_type::accumulator_type accumulator_type;
78  typedef typename scheme_state_impl_type::accumulator_set_type accumulator_set_type;
79 
80  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
82 
84  SchemeStateImpl(std::forward<accumulator_set_type>(ise)) {
85  this->accumulator_set();
86  }
87 
88  template<typename SinglePassRange>
89  range_pubkey_impl(const SinglePassRange &range, accumulator_set_type &&ise) :
90  SchemeStateImpl(std::forward<accumulator_set_type>(ise)) {
91  BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
92 
93  this->accumulator_set(range);
94  }
95 
96  template<typename InputIterator>
97  range_pubkey_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise) :
98  SchemeStateImpl(std::forward<accumulator_set_type>(ise)) {
99  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
100 
101  this->accumulator_set(first, ::nil::crypto3::accumulators::iterator_last = last);
102  }
103 
104  template<typename InputIterator1, typename InputIterator2>
105  range_pubkey_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
106  InputIterator2 last2, accumulator_set_type &&ise) :
107  SchemeStateImpl(std::forward<accumulator_set_type>(ise)) {
108  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator1>));
109  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator2>));
110 
111  this->accumulator_set(first1, ::nil::crypto3::accumulators::iterator_last = last1);
112  this->accumulator_set(first2, ::nil::crypto3::accumulators::iterator_last = last2);
113  }
114 
115  template<typename SinglePassRange, typename Scheme>
116  range_pubkey_impl(const SinglePassRange &range, accumulator_set_type &&ise,
117  const public_key<Scheme> &pubkey) :
118  SchemeStateImpl(std::forward<accumulator_set_type>(ise)) {
119  BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
120 
121  this->accumulator_set(range, ::nil::crypto3::accumulators::key = pubkey);
122  }
123 
124  template<typename InputIterator, typename Scheme>
125  range_pubkey_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise,
126  const public_key<Scheme> &pubkey) :
127  SchemeStateImpl(std::forward<accumulator_set_type>(ise)) {
128  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
129 
130  this->accumulator_set(first, ::nil::crypto3::accumulators::iterator_last = last,
131  ::nil::crypto3::accumulators::key = pubkey);
132  }
133 
134  // TODO: fix
135  template<typename Scheme>
137  const typename public_key<Scheme>::signature_type &signature) :
138  SchemeStateImpl(std::forward<accumulator_set_type>(ise)) {
139  this->accumulator_set(signature);
140  }
141 
142  template<typename OutputRange>
143  operator OutputRange() const {
144  result_type result =
145  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
146  return OutputRange(result.cbegin(), result.cend());
147  }
148 
149  operator result_type() const {
150  return boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
151  }
152 
153  operator accumulator_set_type &() const {
154  return this->accumulator_set;
155  }
156 
157 #ifdef CRYPTO3_ASCII_STRING_CODEC_OUTPUT
158  template<typename Char, typename CharTraits, typename Alloc>
159  operator std::basic_string<Char, CharTraits, Alloc>() const {
160  return std::to_string(
161  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set));
162  }
163 #endif
164  };
165 
166  template<typename SchemeStateImpl, typename OutputIterator>
167  struct itr_pubkey_impl : public SchemeStateImpl {
168  private:
169  mutable OutputIterator out;
170 
171  public:
172  typedef SchemeStateImpl scheme_state_impl_type;
173 
174  typedef typename scheme_state_impl_type::accumulator_type accumulator_type;
175  typedef typename scheme_state_impl_type::accumulator_set_type accumulator_set_type;
176 
177  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
179 
180  itr_pubkey_impl(OutputIterator out, accumulator_set_type &&ise) :
181  SchemeStateImpl(std::forward<accumulator_set_type>(ise)), out(std::move(out)) {
182  }
183 
184  template<typename SinglePassRange>
185  itr_pubkey_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise) :
186  SchemeStateImpl(std::forward<accumulator_set_type>(ise)), out(std::move(out)) {
187  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
188  BOOST_CONCEPT_ASSERT((boost::OutputIteratorConcept<OutputIterator, result_type>));
189 
190  this->accumulator_set(range);
191  }
192 
193  template<typename InputIterator>
194  itr_pubkey_impl(InputIterator first, InputIterator last, OutputIterator out,
195  accumulator_set_type &&ise) :
196  SchemeStateImpl(std::forward<accumulator_set_type>(ise)),
197  out(std::move(out)) {
198  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
199  BOOST_CONCEPT_ASSERT((boost::OutputIteratorConcept<OutputIterator, result_type>));
200 
201  this->accumulator_set(first, ::nil::crypto3::accumulators::iterator_last = last);
202  }
203 
204  template<typename InputIterator1, typename InputIterator2>
205  itr_pubkey_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
206  InputIterator2 last2, OutputIterator out, accumulator_set_type &&ise) :
207  SchemeStateImpl(std::forward<accumulator_set_type>(ise)),
208  out(std::move(out)) {
209  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator1>));
210  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator2>));
211  BOOST_CONCEPT_ASSERT((boost::OutputIteratorConcept<OutputIterator, result_type>));
212 
213  this->accumulator_set(first1, ::nil::crypto3::accumulators::iterator_last = last1);
214  this->accumulator_set(first2, ::nil::crypto3::accumulators::iterator_last = last2);
215  }
216 
217  template<typename SinglePassRange, typename Scheme>
218  itr_pubkey_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise,
219  const public_key<Scheme> &pubkey) :
220  SchemeStateImpl(std::forward<accumulator_set_type>(ise)),
221  out(std::move(out)) {
222  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
223  BOOST_CONCEPT_ASSERT((boost::OutputIteratorConcept<OutputIterator, result_type>));
224 
225  this->accumulator_set(range, ::nil::crypto3::accumulators::key = pubkey);
226  }
227 
228  template<typename InputIterator, typename Scheme>
229  itr_pubkey_impl(InputIterator first, InputIterator last, OutputIterator out,
231  SchemeStateImpl(std::forward<accumulator_set_type>(ise)),
232  out(std::move(out)) {
233  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
234  BOOST_CONCEPT_ASSERT((boost::OutputIteratorConcept<OutputIterator, result_type>));
235 
236  this->accumulator_set(first, ::nil::crypto3::accumulators::iterator_last = last,
237  ::nil::crypto3::accumulators::key = pubkey);
238  }
239 
240  operator OutputIterator() const {
241  *out++ = boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
242  return out;
243  }
244  };
245  } // namespace detail
246  } // namespace pubkey
247  } // namespace crypto3
248 } // namespace nil
249 
250 #endif // CRYPTO3_PUBKEY_VALUE_HPP
boost::accumulators::accumulator_set< digest< ProcessingMode::block_bits >, boost::accumulators::features< accumulators::tag::block< ProcessingMode > >, std::size_t > accumulator_set
Accumulator set with pre-defined block cipher accumulator params.
Definition: block/include/nil/crypto3/block/cipher_state.hpp:51
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
boost::mpl::apply< AccumulatorSet, tag::pubkey< ProcessingMode > >::type::result_type pubkey(const AccumulatorSet &acc)
Definition: accumulators/pubkey.hpp:106
Definition: pair.hpp:31
Definition: hash_state.hpp:43
Definition: pubkey_value.hpp:167
itr_pubkey_impl(OutputIterator out, accumulator_set_type &&ise)
Definition: pubkey_value.hpp:180
itr_pubkey_impl(InputIterator first, InputIterator last, OutputIterator out, accumulator_set_type &&ise, const public_key< Scheme > &pubkey)
Definition: pubkey_value.hpp:229
SchemeStateImpl scheme_state_impl_type
Definition: pubkey_value.hpp:172
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: pubkey_value.hpp:178
itr_pubkey_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator out, accumulator_set_type &&ise)
Definition: pubkey_value.hpp:205
itr_pubkey_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise)
Definition: pubkey_value.hpp:185
scheme_state_impl_type::accumulator_type accumulator_type
Definition: pubkey_value.hpp:174
itr_pubkey_impl(const SinglePassRange &range, OutputIterator out, accumulator_set_type &&ise, const public_key< Scheme > &pubkey)
Definition: pubkey_value.hpp:218
itr_pubkey_impl(InputIterator first, InputIterator last, OutputIterator out, accumulator_set_type &&ise)
Definition: pubkey_value.hpp:194
scheme_state_impl_type::accumulator_set_type accumulator_set_type
Definition: pubkey_value.hpp:175
scheme_state_impl_type::accumulator_type accumulator_type
Definition: pubkey_value.hpp:77
scheme_state_impl_type::accumulator_set_type accumulator_set_type
Definition: pubkey_value.hpp:78
range_pubkey_impl(const SinglePassRange &range, accumulator_set_type &&ise, const public_key< Scheme > &pubkey)
Definition: pubkey_value.hpp:116
range_pubkey_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, accumulator_set_type &&ise)
Definition: pubkey_value.hpp:105
range_pubkey_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise, const public_key< Scheme > &pubkey)
Definition: pubkey_value.hpp:125
SchemeStateImpl scheme_state_impl_type
Definition: pubkey_value.hpp:75
range_pubkey_impl(InputIterator first, InputIterator last, accumulator_set_type &&ise)
Definition: pubkey_value.hpp:97
range_pubkey_impl(accumulator_set_type &&ise, const typename public_key< Scheme >::signature_type &signature)
Definition: pubkey_value.hpp:136
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: pubkey_value.hpp:81
range_pubkey_impl(const SinglePassRange &range, accumulator_set_type &&ise)
Definition: pubkey_value.hpp:89
range_pubkey_impl(accumulator_set_type &&ise)
Definition: pubkey_value.hpp:83
Definition: pubkey_value.hpp:49
SchemeAccumulator accumulator_set_type
Definition: pubkey_value.hpp:50
ref_pubkey_impl(accumulator_set_type &&acc)
Definition: pubkey_value.hpp:54
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: pubkey_value.hpp:52
accumulator_set_type & accumulator_set
Definition: pubkey_value.hpp:57
accumulator_set_type accumulator_set
Definition: pubkey_value.hpp:70
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: pubkey_value.hpp:64
value_pubkey_impl(accumulator_set_type &&acc)
Definition: pubkey_value.hpp:66
SchemeAccumulator accumulator_set_type
Definition: pubkey_value.hpp:62
Public key - a key that can be published and used to verify the authenticity of the signed document,...
Definition: public_key.hpp:43