affine_ate_precompute_g2.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@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_PAIRING_SHORT_WEIERSTRASS_PROJECTIVE_AFFINE_ATE_PRECOMPUTE_G2_HPP
27 #define CRYPTO3_ALGEBRA_PAIRING_SHORT_WEIERSTRASS_PROJECTIVE_AFFINE_ATE_PRECOMPUTE_G2_HPP
28 
29 #include <nil/crypto3/multiprecision/number.hpp>
30 #include <nil/crypto3/multiprecision/cpp_int.hpp>
31 
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace algebra {
37  namespace pairing {
38 
39  template<typename CurveType>
41  using curve_type = CurveType;
42 
45 
46  using base_field_type = typename curve_type::base_field_type;
47  using g2_type = typename curve_type::template g2_type<>;
48  using g2_affine_type = typename curve_type::template g2_type<curves::coordinates::affine>;
49 
50  using g2_field_type_value = typename g2_type::field_type::value_type;
51 
52  public:
54 
55  static g2_precomputed_type process(const typename g2_type::value_type &Q) {
56 
57  typename g2_affine_type::value_type Qcopy = Q.to_affine();
58 
59  g2_precomputed_type result;
60  result.QX = Qcopy.X;
61  result.QY = Qcopy.Y;
62 
63  g2_field_type_value RX = Qcopy.X;
64  g2_field_type_value RY = Qcopy.Y;
65  bool found_nonzero = false;
66 
67  std::vector<long> NAF = multiprecision::find_wnaf(1, policy_type::ate_loop_count);
68 
69  for (long i = NAF.size() - 1; i >= 0; --i) {
70  if (!found_nonzero) {
71  /* this skips the MSB itself */
72  found_nonzero |= (NAF[i] != 0);
73  continue;
74  }
75 
77  c.old_RX = RX;
78  c.old_RY = RY;
79  g2_field_type_value old_RX_2 = c.old_RX.squared();
80  c.gamma = (old_RX_2 + old_RX_2 + old_RX_2 + params_type::twist_coeff_a) *
81  (c.old_RY + c.old_RY).inversed();
82  c.gamma_twist = c.gamma * params_type::twist;
83 
84  c.gamma_X = c.gamma * c.old_RX;
85  result.coeffs.push_back(c);
86 
87  RX = c.gamma.squared() - (c.old_RX + c.old_RX);
88  RY = c.gamma * (c.old_RX - RX) - c.old_RY;
89 
90  if (NAF[i] != 0) {
92  c.old_RX = RX;
93  c.old_RY = RY;
94  if (NAF[i] > 0) {
95  c.gamma = (c.old_RY - result.QY) * (c.old_RX - result.QX).inversed();
96  } else {
97  c.gamma = (c.old_RY + result.QY) * (c.old_RX - result.QX).inversed();
98  }
99  c.gamma_twist = c.gamma * params_type::twist;
100 
101  c.gamma_X = c.gamma * result.QX;
102  result.coeffs.push_back(c);
103 
104  RX = c.gamma.squared() - (c.old_RX + result.QX);
105  RY = c.gamma * (c.old_RX - RX) - c.old_RY;
106  }
107  }
108 
109  return result;
110  }
111  };
112  } // namespace pairing
113  } // namespace algebra
114  } // namespace crypto3
115 } // namespace nil
116 #endif // CRYPTO3_ALGEBRA_PAIRING_SHORT_WEIERSTRASS_PROJECTIVE_AFFINE_ATE_PRECOMPUTE_G2_HPP
Definition: pairing/detail/alt_bn128/params.hpp:38
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:36
static g2_precomputed_type process(const typename g2_type::value_type &Q)
Definition: affine_ate_precompute_g2.hpp:55
typename policy_type::affine_ate_g2_precomputation g2_precomputed_type
Definition: affine_ate_precompute_g2.hpp:53
Definition: pair.hpp:31
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:61
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:52
g2_field_value_type gamma_X
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:58
g2_field_value_type gamma
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:56
g2_field_value_type old_RX
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:54
g2_field_value_type old_RY
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:55
g2_field_value_type gamma_twist
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:57