accumulators/to_curve.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_HASH_TO_CURVE_HPP
27 #define CRYPTO3_ACCUMULATORS_HASH_TO_CURVE_HPP
28 
29 #include <iterator>
30 #include <type_traits>
31 
32 #include <boost/parameter/value_type.hpp>
33 
34 #include <boost/accumulators/framework/accumulator_base.hpp>
35 #include <boost/accumulators/framework/extractor.hpp>
36 #include <boost/accumulators/framework/depends_on.hpp>
37 #include <boost/accumulators/framework/parameters/sample.hpp>
38 
40 
41 namespace nil {
42  namespace crypto3 {
43  namespace hashes {
44  namespace accumulators {
45  namespace impl {
46  template<typename HashType, typename = void>
47  struct to_curve_impl;
48 
49  // TODO: maybe add dependant hash accumulator instead manually work with it inside
50  template<typename HashType>
51  struct to_curve_impl<HashType> : boost::accumulators::accumulator_base {
52  protected:
53  typedef HashType hash_type;
54  typedef typename hash_type::internal_accumulator_type internal_accumulator_type;
55 
56  public:
57  typedef typename hash_type::result_type result_type;
58 
59  template<typename Args>
60  to_curve_impl(const Args &args) {
61  hash_type::init_accumulator(acc);
62  }
63 
64  template<typename Args>
65  inline void operator()(const Args &args) {
66  resolve_type(args[boost::accumulators::sample],
67  args[::nil::crypto3::accumulators::iterator_last | nullptr]);
68  }
69 
70  inline result_type result(boost::accumulators::dont_care) const {
71  return hash_type::process(acc);
72  }
73 
74  protected:
75  template<typename InputRange, typename InputIterator>
76  inline void resolve_type(const InputRange &range, InputIterator) {
77  hash_type::update(acc, range);
78  }
79 
80  template<typename InputIterator>
81  inline void resolve_type(InputIterator first, InputIterator last) {
82  hash_type::update(acc, first, last);
83  }
84 
86  };
87  } // namespace impl
88 
89  namespace tag {
90  template<typename HashType>
91  struct to_curve : boost::accumulators::depends_on<> {
92  typedef HashType hash_type;
93 
96 
97  typedef boost::mpl::always<accumulators::impl::to_curve_impl<hash_type>> impl;
98  };
99  } // namespace tag
100 
101  namespace extract {
102  template<typename HashType, typename AccumulatorSet>
103  typename boost::mpl::apply<AccumulatorSet, tag::to_curve<HashType>>::type::result_type
104  to_curve(const AccumulatorSet &acc) {
105  return boost::accumulators::extract_result<tag::to_curve<HashType>>(acc);
106  }
107  } // namespace extract
108  } // namespace accumulators
109  } // namespace hashes
110  } // namespace crypto3
111 } // namespace nil
112 
113 #endif // CRYPTO3_ACCUMULATORS_HASH_TO_CURVE_HPP
boost::mpl::apply< AccumulatorSet, tag::to_curve< HashType > >::type::result_type to_curve(const AccumulatorSet &acc)
Definition: accumulators/to_curve.hpp:104
Definition: pair.hpp:31
void operator()(const Args &args)
Definition: accumulators/to_curve.hpp:65
void resolve_type(InputIterator first, InputIterator last)
Definition: accumulators/to_curve.hpp:81
HashType hash_type
Definition: accumulators/to_curve.hpp:53
hash_type::internal_accumulator_type internal_accumulator_type
Definition: accumulators/to_curve.hpp:54
internal_accumulator_type acc
Definition: accumulators/to_curve.hpp:85
hash_type::result_type result_type
Definition: accumulators/to_curve.hpp:57
result_type result(boost::accumulators::dont_care) const
Definition: accumulators/to_curve.hpp:70
to_curve_impl(const Args &args)
Definition: accumulators/to_curve.hpp:60
void resolve_type(const InputRange &range, InputIterator)
Definition: accumulators/to_curve.hpp:76
Definition: accumulators/to_curve.hpp:47
Definition: accumulators/to_curve.hpp:91
boost::mpl::always< accumulators::impl::to_curve_impl< hash_type > > impl
Definition: accumulators/to_curve.hpp:97
HashType hash_type
Definition: accumulators/to_curve.hpp:92