algebra/include/nil/crypto3/algebra/curves/detail/scalar_mul.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 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_ALGEBRA_CURVES_SCALAR_MUL_HPP
28 #define CRYPTO3_ALGEBRA_CURVES_SCALAR_MUL_HPP
29 
31 
32 #include <nil/crypto3/multiprecision/number.hpp>
33 #include <nil/crypto3/multiprecision/modular/modular_adaptor.hpp>
34 
35 #include <cstdint>
36 
37 namespace nil {
38  namespace crypto3 {
39  namespace algebra {
40  namespace curves {
41  namespace detail {
42  template<typename GroupValueType,
43  typename Backend,
44  multiprecision::expression_template_option ExpressionTemplates>
45  constexpr GroupValueType
46  scalar_mul(const GroupValueType &base,
47  const multiprecision::number<Backend, ExpressionTemplates> &scalar) {
48  if (scalar.is_zero()) {
49  return GroupValueType::zero();
50  }
51  GroupValueType result;
52 
53  bool found_one = false;
54  for (auto i = static_cast<std::int64_t>(multiprecision::msb(scalar)); i >= 0; --i) {
55  if (found_one) {
56  result = result.doubled();
57  }
58 
59  if (multiprecision::bit_test(scalar, i)) {
60  found_one = true;
61  result = result + base;
62  }
63  }
64 
65  return result;
66  }
67 
68  template<typename GroupValueType,
69  typename Backend, typename SafeType,
70  multiprecision::expression_template_option ExpressionTemplates>
71  constexpr GroupValueType
72  operator*(const GroupValueType &left,
73  const multiprecision::number<nil::crypto3::multiprecision::backends::modular_adaptor<Backend, SafeType>, ExpressionTemplates> &right) {
74  multiprecision::number<Backend, ExpressionTemplates> tmp = right.template convert_to<multiprecision::number<Backend, ExpressionTemplates>>();
75  return scalar_mul(left, right);
76  }
77 
78  template<typename GroupValueType,
79  typename Backend,
80  multiprecision::expression_template_option ExpressionTemplates>
81  constexpr GroupValueType
82  operator*(const GroupValueType &left,
83  const multiprecision::number<Backend, ExpressionTemplates> &right) {
84 
85  return scalar_mul(left, right);
86  }
87 
88  template<typename GroupValueType,
89  typename Backend,
90  multiprecision::expression_template_option ExpressionTemplates>
91  constexpr GroupValueType operator*(const multiprecision::number<Backend, ExpressionTemplates> &left,
92  const GroupValueType &right) {
93 
94  return right * left;
95  }
96 
97  /*template<typename GroupValueType, typename =
98  typename std::enable_if<is_curve_group<typename
99  GroupValueType::group_type>::value &&
100  !is_field<typename
101  GroupValueType::group_type>::value>::type> GroupValueType operator*( const GroupValueType &left,
102  const typename GroupValueType::underlying_field_type::integral_type &right) {
103 
104  return scalar_mul(left, right);
105  }
106 
107  template<typename GroupValueType, typename =
108  typename std::enable_if<is_curve_group<typename
109  GroupValueType::group_type>::value &&
110  !is_field<typename
111  GroupValueType::group_type>::value>::type> GroupValueType operator*( const typename
112  GroupValueType::underlying_field_type::integral_type &left, const GroupValueType &right) {
113 
114  return right * left;
115  }*/
116 
117  template<typename GroupValueType, typename FieldValueType>
118  typename std::enable_if<is_curve_group<typename GroupValueType::group_type>::value &&
122  GroupValueType>::type
123  operator*(const GroupValueType &left, const FieldValueType &right) {
124 
125  return left * right.data;
126  }
127 
128  template<typename GroupValueType, typename FieldValueType>
129  typename std::enable_if<is_curve_group<typename GroupValueType::group_type>::value &&
133  GroupValueType>::type
134  operator*(const FieldValueType &left, const GroupValueType &right) {
135 
136  return right * left;
137  }
138 
139  template<typename GroupValueType>
140  constexpr GroupValueType operator*(const GroupValueType &left, const std::size_t &right) {
141 
142  return scalar_mul(left, typename GroupValueType::field_type::integral_type::value_type(right));
143  }
144 
145  template<typename GroupValueType>
146  constexpr GroupValueType operator*(const std::size_t &left, const GroupValueType &right) {
147 
148  return right * left;
149  }
150  } // namespace detail
151  } // namespace curves
152  } // namespace algebra
153  } // namespace crypto3
154 } // namespace nil
155 #endif // CRYPTO3_ALGEBRA_CURVES_SCALAR_MUL_HPP
constexpr GroupValueType scalar_mul(const GroupValueType &base, const multiprecision::number< Backend, ExpressionTemplates > &scalar)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/scalar_mul.hpp:46
constexpr GroupValueType operator*(const GroupValueType &left, const multiprecision::number< nil::crypto3::multiprecision::backends::modular_adaptor< Backend, SafeType >, ExpressionTemplates > &right)
Definition: algebra/include/nil/crypto3/algebra/curves/detail/scalar_mul.hpp:72
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:105
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:95