find_group_hash.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_HASH_FIND_GROUP_HASH_HPP
27 #define CRYPTO3_HASH_FIND_GROUP_HASH_HPP
28 
29 #include <string>
30 #include <array>
31 #include <vector>
32 
34 
35 #include <nil/marshalling/status_type.hpp>
36 #include <nil/marshalling/field_type.hpp>
37 #include <nil/marshalling/endianness.hpp>
38 #include <nil/marshalling/algorithms/pack.hpp>
39 #include <nil/crypto3/marshalling/algebra/types/curve_element.hpp>
40 
43 
44 namespace nil {
45  namespace crypto3 {
46  namespace hashes {
48  static constexpr std::size_t dst_bits = 8 * 8;
49  using dst_type = std::vector<std::uint8_t>;
50  static inline dst_type dst = []() {
51  std::string default_tag_str = "Zcash_PH";
52  dst_type dst(default_tag_str.begin(), default_tag_str.end());
53  assert(dst.size() == 8);
54  return dst;
55  }();
56  };
57 
65  // TODO: use blake2s by default
66  template<typename Params = find_group_hash_default_params,
67  typename Hash = sha2<256>,
68  typename Group = algebra::curves::jubjub::template g1_type<
71  struct find_group_hash {
72  using params = Params;
73  using hash_type = Hash;
74  using group_type = Group;
75  using curve_type = typename group_type::curve_type;
76 
77  using group_value_type = typename group_type::value_type;
80 
81  static inline std::vector<std::uint8_t> urs = {
82  0x30, 0x39, 0x36, 0x62, 0x33, 0x36, 0x61, 0x35, 0x38, 0x30, 0x34, 0x62, 0x66, 0x61, 0x63, 0x65,
83  0x66, 0x31, 0x36, 0x39, 0x31, 0x65, 0x31, 0x37, 0x33, 0x63, 0x33, 0x36, 0x36, 0x61, 0x34, 0x37,
84  0x66, 0x66, 0x35, 0x62, 0x61, 0x38, 0x34, 0x61, 0x34, 0x34, 0x66, 0x32, 0x36, 0x64, 0x64, 0x64,
85  0x37, 0x65, 0x38, 0x64, 0x39, 0x66, 0x37, 0x39, 0x64, 0x35, 0x62, 0x34, 0x32, 0x64, 0x66, 0x30};
86 
87  static inline void init_accumulator(internal_accumulator_type &acc) {
88  hash<hash_type>(params::dst, acc);
89  hash<hash_type>(urs, acc);
90  }
91 
92  template<typename InputRange>
93  static inline void update(internal_accumulator_type &acc, const InputRange &range) {
94  hash<hash_type>(range, acc);
95  }
96 
97  template<typename InputIterator>
98  static inline void update(internal_accumulator_type &acc, InputIterator first, InputIterator last) {
99  hash<hash_type>(first, last, acc);
100  }
101 
103  nil::marshalling::status_type status;
104  group_value_type point;
105  std::uint8_t i = 0;
106 
107  while (true) {
108  auto acc_copy = acc;
109  hash<hash_type>(
110  {
111  i++,
112  },
113  acc_copy);
114  typename hash_type::digest_type H =
115  nil::crypto3::accumulators::extract::hash<hash_type>(acc_copy);
116  // TODO: generalize pack interface to accept arbitrary containers
117  std::vector<std::uint8_t> H_vec(std::cbegin(H), std::cend(H));
118  point = nil::marshalling::pack<nil::marshalling::option::little_endian>(H_vec, status);
119  if (status == nil::marshalling::status_type::success) {
120  break;
121  }
122  // TODO: return status
123  assert(i < 256);
124  }
125  point = typename group_type::field_type::value_type(group_type::params_type::cofactor) * point;
126  // TODO: return status
127  assert(!point.is_zero());
128  assert(point.is_well_formed());
129 
130  return point;
131  }
132  };
133  } // namespace hashes
134  } // namespace crypto3
135 } // namespace nil
136 
137 #endif // CRYPTO3_HASH_FIND_GROUP_HASH_HPP
Definition: pair.hpp:31
Definition: hash_state.hpp:43
Jacobi quatrics curve group element coordinates representation. Description: https://hyperelliptic....
Definition: jacobi_quartics/coordinates.hpp:40
static constexpr std::size_t dst_bits
Definition: find_group_hash.hpp:48
static dst_type dst
Definition: find_group_hash.hpp:50
std::vector< std::uint8_t > dst_type
Definition: find_group_hash.hpp:49
Hashing to elliptic curve Jubjub according to FindGroupHash Zcash algorithm https://zips....
Definition: find_group_hash.hpp:71
static void update(internal_accumulator_type &acc, InputIterator first, InputIterator last)
Definition: find_group_hash.hpp:98
static void update(internal_accumulator_type &acc, const InputRange &range)
Definition: find_group_hash.hpp:93
Group group_type
Definition: find_group_hash.hpp:74
Hash hash_type
Definition: find_group_hash.hpp:73
static std::vector< std::uint8_t > urs
Definition: find_group_hash.hpp:81
group_value_type result_type
Definition: find_group_hash.hpp:79
static void init_accumulator(internal_accumulator_type &acc)
Definition: find_group_hash.hpp:87
typename group_type::curve_type curve_type
Definition: find_group_hash.hpp:75
typename group_type::value_type group_value_type
Definition: find_group_hash.hpp:77
Params params
Definition: find_group_hash.hpp:72
static result_type process(internal_accumulator_type &acc)
Definition: find_group_hash.hpp:102