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