h2c_m2c.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_ALGEBRA_CURVES_HASH_TO_CURVE_MAP_TO_CURVE_HPP
27 #define CRYPTO3_ALGEBRA_CURVES_HASH_TO_CURVE_MAP_TO_CURVE_HPP
28 
32 
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace algebra {
38  namespace curves {
39  namespace detail {
40  template<typename GroupType>
41  struct m2c_simple_swu {
43 
44  typedef typename suite_type::group_value_type group_value_type;
45  typedef typename suite_type::field_value_type field_value_type;
46 
47  static inline group_value_type process(const field_value_type &u) {
48  // TODO: We assume that Z meets the following criteria -- correct for predefined suites,
49  // but wrong in general case
50  // https://tools.ietf.org/html/draft-irtf-cfrg-hash-to-curve-10#section-6.6.2
51  // Preconditions:
52  // 1. Z is non-square in F,
53  // 2. Z != -1 in F,
54  // 3. the polynomial g(x) - Z is irreducible over F, and
55  // 4. g(B / (Z * A)) is square in F.
56  static const field_value_type one = field_value_type::one();
57 
58  field_value_type tv1 =
59  (suite_type::Z.pow(2) * u.pow(4) + suite_type::Z * u.pow(2)).inversed();
60  field_value_type x1 = (-suite_type::Bi / suite_type::Ai) * (one + tv1);
61  if (tv1.is_zero()) {
62  x1 = suite_type::Bi / (suite_type::Z * suite_type::Ai);
63  }
64  field_value_type gx1 = x1.pow(3) + suite_type::Ai * x1 + suite_type::Bi;
65  field_value_type x2 = suite_type::Z * u.pow(2) * x1;
66  field_value_type gx2 = x2.pow(3) + suite_type::Ai * x2 + suite_type::Bi;
67  field_value_type x, y;
68  if (gx1.is_square()) {
69  x = x1;
70  y = gx1.sqrt();
71  } else {
72  x = x2;
73  y = gx2.sqrt();
74  }
75  if (sgn0(u) != sgn0(y)) {
76  y = -y;
77  }
78  return group_value_type(x, y, one);
79  }
80  };
81 
82  template<typename GroupType>
85 
86  typedef typename suite_type::group_value_type group_value_type;
87  typedef typename suite_type::field_value_type field_value_type;
88 
89  static inline group_value_type process(const field_value_type &u) {
91  return iso_map<GroupType>::process(ci);
92  }
93  };
94 
95  template<typename GroupType>
96  struct map_to_curve;
97 
98  template<>
99  struct map_to_curve<typename bls12_381::g1_type<>>
100  : m2c_simple_swu_zeroAB<typename bls12_381::g1_type<>> { };
101 
102  template<>
103  struct map_to_curve<typename bls12_381::g2_type<>>
104  : m2c_simple_swu_zeroAB<typename bls12_381::g2_type<>> { };
105  } // namespace detail
106  } // namespace curves
107  } // namespace algebra
108  } // namespace crypto3
109 } // namespace nil
110 
111 #endif // CRYPTO3_ALGEBRA_CURVES_HASH_TO_CURVE_MAP_TO_CURVE_HPP
A struct representing a BLS12-381 and BLS12-377 curve.
Definition: curves/bls12.hpp:49
bool sgn0(const element_fp< FieldParams > &e)
Definition: h2c_sgn0.hpp:40
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/algebra/curves/detail/h2c/h2c_suites.hpp:48
suite_type::field_value_type field_value_type
Definition: h2c_m2c.hpp:87
static group_value_type process(const field_value_type &u)
Definition: h2c_m2c.hpp:89
suite_type::group_value_type group_value_type
Definition: h2c_m2c.hpp:86
h2c_suite< GroupType > suite_type
Definition: h2c_m2c.hpp:84
h2c_suite< GroupType > suite_type
Definition: h2c_m2c.hpp:42
suite_type::group_value_type group_value_type
Definition: h2c_m2c.hpp:44
static group_value_type process(const field_value_type &u)
Definition: h2c_m2c.hpp:47
suite_type::field_value_type field_value_type
Definition: h2c_m2c.hpp:45