wnaf.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_ALGEBRA_WNAF_HPP
26 #define CRYPTO3_ALGEBRA_WNAF_HPP
27 
28 #include <nil/crypto3/multiprecision/wnaf.hpp>
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace algebra {
35  template<typename BaseValueType, typename Backend,
36  multiprecision::expression_template_option ExpressionTemplates>
37  BaseValueType fixed_window_wnaf_exp(const std::size_t window_size, const BaseValueType &base,
38  const multiprecision::number<Backend, ExpressionTemplates> &scalar) {
39  std::vector<long> naf = multiprecision::find_wnaf(window_size, scalar);
40  std::vector<BaseValueType> table(1ul << (window_size - 1));
41  BaseValueType tmp = base;
42  BaseValueType dbl = base.doubled();
43  for (size_t i = 0; i < 1ul << (window_size - 1); ++i) {
44  table[i] = tmp;
45  tmp = tmp + dbl;
46  }
47 
48  BaseValueType res = BaseValueType::zero();
49  bool found_nonzero = false;
50  for (long i = naf.size() - 1; i >= 0; --i) {
51  if (found_nonzero) {
52  res = res.doubled();
53  }
54 
55  if (naf[i] != 0) {
56  found_nonzero = true;
57  if (naf[i] > 0) {
58  res = res + table[naf[i] / 2];
59  } else {
60  res = res - table[(-naf[i]) / 2];
61  }
62  }
63  }
64 
65  return res;
66  }
67 
68  // TODO: check, that CurveGroupValueType is a curve group element. Otherwise it has no wnaf_window_table
69  template<typename CurveGroupValueType, typename Backend,
70  multiprecision::expression_template_option ExpressionTemplates>
71  CurveGroupValueType opt_window_wnaf_exp(const CurveGroupValueType &base,
72  const multiprecision::number<Backend, ExpressionTemplates> &scalar,
73  const std::size_t scalar_bits) {
74  std::size_t best = 0;
75  for (long i =
77  i >= 0;
78  --i) {
79  if (scalar_bits >=
81  best = i + 1;
82  break;
83  }
84  }
85 
86  if (best > 0) {
87  return fixed_window_wnaf_exp(best, base, scalar);
88  } else {
89  return scalar * base;
90  }
91  }
92  } // namespace algebra
93  } // namespace crypto3
94 } // namespace nil
95 #endif // CRYPTO3_ALGEBRA_RANDOM_ELEMENT_HPP
CurveGroupValueType opt_window_wnaf_exp(const CurveGroupValueType &base, const multiprecision::number< Backend, ExpressionTemplates > &scalar, const std::size_t scalar_bits)
Definition: wnaf.hpp:71
BaseValueType fixed_window_wnaf_exp(const std::size_t window_size, const BaseValueType &base, const multiprecision::number< Backend, ExpressionTemplates > &scalar)
Definition: wnaf.hpp:37
Definition: pair.hpp:31
Definition: curves/params/wnaf/alt_bn128.hpp:39