mnt4/298/ate_miller_loop.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_MNT4_298_ATE_MILLER_LOOP_HPP
27 #define CRYPTO3_ALGEBRA_PAIRING_MNT4_298_ATE_MILLER_LOOP_HPP
28 
29 #include <nil/crypto3/multiprecision/number.hpp>
30 #include <nil/crypto3/multiprecision/cpp_int.hpp>
31 
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace algebra {
39  namespace pairing {
40 
41  template<std::size_t Version = 298>
43 
44  template<>
45  class mnt4_ate_miller_loop<298> {
47 
50  using gt_type = typename curve_type::gt_type;
51 
52  using base_field_type = typename curve_type::base_field_type;
53  using g1_type = typename curve_type::template g1_type<>;
54  using g2_type = typename curve_type::template g2_type<>;
55 
56  using g1_field_type_value = typename g1_type::field_type::value_type;
57  using g2_field_type_value = typename g2_type::field_type::value_type;
58 
59  public:
60  static typename gt_type::value_type process(const policy_type::ate_g1_precomputed_type &prec_P,
62 
63  g2_field_type_value L1_coeff =
64  g2_field_type_value(prec_P.PX, g1_field_type_value::zero()) - prec_Q.QX_over_twist;
65 
66  typename gt_type::value_type f = gt_type::value_type::one();
67 
68  bool found_one = false;
69  std::size_t dbl_idx = 0;
70  std::size_t add_idx = 0;
71 
72  for (long i = params_type::integral_type_max_bits - 1; i >= 0; --i) {
73  const bool bit = multiprecision::bit_test(params_type::ate_loop_count, i);
74 
75  if (!found_one) {
76  /* this skips the MSB itself */
77  found_one |= bit;
78  continue;
79  }
80 
81  /* code below gets executed for all bits (EXCEPT the MSB itself) of
82  param_p (skipping leading zeros) in MSB to LSB
83  order */
84  typename policy_type::ate_dbl_coeffs dc = prec_Q.dbl_coeffs[dbl_idx++];
85 
86  typename gt_type::value_type g_RR_at_P = typename gt_type::value_type(
87  -dc.c_4C - dc.c_J * prec_P.PX_twist + dc.c_L, dc.c_H * prec_P.PY_twist);
88  f = f.squared() * g_RR_at_P;
89  if (bit) {
90  typename policy_type::ate_add_coeffs ac = prec_Q.add_coeffs[add_idx++];
91 
92  typename gt_type::value_type g_RQ_at_P = typename gt_type::value_type(
93  ac.c_RZ * prec_P.PY_twist, -(prec_Q.QY_over_twist * ac.c_RZ + L1_coeff * ac.c_L1));
94  f = f * g_RQ_at_P;
95  }
96  }
97 
98  if (params_type::ate_is_loop_count_neg) {
99  typename policy_type::ate_add_coeffs ac = prec_Q.add_coeffs[add_idx++];
100  typename gt_type::value_type g_RnegR_at_P = typename gt_type::value_type(
101  ac.c_RZ * prec_P.PY_twist, -(prec_Q.QY_over_twist * ac.c_RZ + L1_coeff * ac.c_L1));
102  f = (f * g_RnegR_at_P).inversed();
103  }
104 
105  return f;
106  }
107  };
108  } // namespace pairing
109  } // namespace algebra
110  } // namespace crypto3
111 } // namespace nil
112 #endif // CRYPTO3_ALGEBRA_PAIRING_MNT4_298_ATE_MILLER_LOOP_HPP
Definition: pairing/detail/alt_bn128/params.hpp:38
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:36
static gt_type::value_type process(const policy_type::ate_g1_precomputed_type &prec_P, const policy_type::ate_g2_precomputed_type &prec_Q)
Definition: mnt4/298/ate_miller_loop.hpp:60
Definition: mnt4/298/ate_miller_loop.hpp:42
Definition: pair.hpp:31
A struct representing a mnt4 curve.
Definition: algebra/include/nil/crypto3/algebra/curves/mnt4.hpp:46
policy_type::gt_field_type gt_type
Definition: algebra/include/nil/crypto3/algebra/curves/mnt4.hpp:66
IETF IPsec groups.
Definition: mnt4/base_field.hpp:46
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:69
g2_field_value_type PY_twist
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:74
g2_field_value_type PX_twist
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:73
g1_field_value_type PX
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:71
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:107
std::vector< dbl_coeffs_type > dbl_coeffs
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:116
g2_field_value_type QY_over_twist
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:115
std::vector< add_coeffs_type > add_coeffs
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:117
g2_field_value_type QX_over_twist
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:114
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:97
g2_field_value_type c_L1
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:99
g2_field_value_type c_RZ
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:100
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:84
g2_field_value_type c_4C
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:87
g2_field_value_type c_L
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:89
g2_field_value_type c_J
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:88
g2_field_value_type c_H
Definition: pairing/detail/forms/short_weierstrass/projective/types.hpp:86