mnt6/298/ate_double_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_MNT6_298_ATE_DOUBLE_MILLER_LOOP_HPP
27 #define CRYPTO3_ALGEBRA_PAIRING_MNT6_298_ATE_DOUBLE_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<>
47 
50 
51  using gt_type = typename curve_type::gt_type;
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
62  const typename policy_type::ate_g2_precomputed_type &prec_Q1,
63  const typename policy_type::ate_g1_precomputed_type &prec_P2,
64  const typename policy_type::ate_g2_precomputed_type &prec_Q2) {
65 
66  g2_field_type_value L1_coeff1 =
67  g2_field_type_value(prec_P1.PX, g1_field_type_value::zero(), g1_field_type_value::zero()) -
68  prec_Q1.QX_over_twist;
69  g2_field_type_value L1_coeff2 =
70  g2_field_type_value(prec_P2.PX, g1_field_type_value::zero(), g1_field_type_value::zero()) -
71  prec_Q2.QX_over_twist;
72 
73  typename gt_type::value_type f = gt_type::value_type::one();
74 
75  bool found_one = false;
76  std::size_t dbl_idx = 0;
77  std::size_t add_idx = 0;
78 
79  for (long i = params_type::integral_type_max_bits - 1; i >= 0; --i) {
80  const bool bit = multiprecision::bit_test(params_type::ate_loop_count, i);
81 
82  if (!found_one) {
83  /* this skips the MSB itself */
84  found_one |= bit;
85  continue;
86  }
87 
88  /* code below gets executed for all bits (EXCEPT the MSB itself) of
89  param_p (skipping leading zeros) in MSB to LSB
90  order */
91  typename policy_type::ate_dbl_coeffs dc1 = prec_Q1.dbl_coeffs[dbl_idx];
92  typename policy_type::ate_dbl_coeffs dc2 = prec_Q2.dbl_coeffs[dbl_idx];
93  ++dbl_idx;
94 
95  typename gt_type::value_type g_RR_at_P1 = typename gt_type::value_type(
96  -dc1.c_4C - dc1.c_J * prec_P1.PX_twist + dc1.c_L, dc1.c_H * prec_P1.PY_twist);
97 
98  typename gt_type::value_type g_RR_at_P2 = typename gt_type::value_type(
99  -dc2.c_4C - dc2.c_J * prec_P2.PX_twist + dc2.c_L, dc2.c_H * prec_P2.PY_twist);
100 
101  f = f.squared() * g_RR_at_P1 * g_RR_at_P2;
102 
103  if (bit) {
104  typename policy_type::ate_add_coeffs ac1 = prec_Q1.add_coeffs[add_idx];
105  typename policy_type::ate_add_coeffs ac2 = prec_Q2.add_coeffs[add_idx];
106  ++add_idx;
107 
108  typename gt_type::value_type g_RQ_at_P1 = typename gt_type::value_type(
109  ac1.c_RZ * prec_P1.PY_twist,
110  -(prec_Q1.QY_over_twist * ac1.c_RZ + L1_coeff1 * ac1.c_L1));
111  typename gt_type::value_type g_RQ_at_P2 = typename gt_type::value_type(
112  ac2.c_RZ * prec_P2.PY_twist,
113  -(prec_Q2.QY_over_twist * ac2.c_RZ + L1_coeff2 * ac2.c_L1));
114 
115  f = f * g_RQ_at_P1 * g_RQ_at_P2;
116  }
117  }
118 
119  if (params_type::ate_is_loop_count_neg) {
120  typename policy_type::ate_add_coeffs ac1 = prec_Q1.add_coeffs[add_idx];
121  typename policy_type::ate_add_coeffs ac2 = prec_Q2.add_coeffs[add_idx];
122  ++add_idx;
123  typename gt_type::value_type g_RnegR_at_P1 = typename gt_type::value_type(
124  ac1.c_RZ * prec_P1.PY_twist,
125  -(prec_Q1.QY_over_twist * ac1.c_RZ + L1_coeff1 * ac1.c_L1));
126  typename gt_type::value_type g_RnegR_at_P2 = typename gt_type::value_type(
127  ac2.c_RZ * prec_P2.PY_twist,
128  -(prec_Q2.QY_over_twist * ac2.c_RZ + L1_coeff2 * ac2.c_L1));
129 
130  f = (f * g_RnegR_at_P1 * g_RnegR_at_P2).inversed();
131  }
132 
133  return f;
134  }
135  };
136  } // namespace pairing
137  } // namespace algebra
138  } // namespace crypto3
139 } // namespace nil
140 #endif // CRYPTO3_ALGEBRA_PAIRING_MNT6_298_ATE_DOUBLE_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 typename policy_type::ate_g1_precomputed_type &prec_P1, const typename policy_type::ate_g2_precomputed_type &prec_Q1, const typename policy_type::ate_g1_precomputed_type &prec_P2, const typename policy_type::ate_g2_precomputed_type &prec_Q2)
Definition: mnt6/298/ate_double_miller_loop.hpp:61
Definition: mnt6/298/ate_double_miller_loop.hpp:42
Definition: pair.hpp:31
A struct representing a mnt6 curve.
Definition: algebra/include/nil/crypto3/algebra/curves/mnt6.hpp:46
policy_type::gt_field_type gt_type
Definition: algebra/include/nil/crypto3/algebra/curves/mnt6.hpp:66
IETF IPsec groups.
Definition: mnt6/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