Clearmatics Libff  0.1
C++ library for Finite Fields and Elliptic Curves
exponentiation.tcc
Go to the documentation of this file.
1 /** @file
2  *****************************************************************************
3 
4  Implementation of interfaces for (square-and-multiply) exponentiation.
5 
6  See exponentiation.hpp .
7 
8  *****************************************************************************
9  * @author This file is part of libff, developed by SCIPR Lab
10  * and contributors (see AUTHORS).
11  * @copyright MIT license (see LICENSE file)
12  *****************************************************************************/
13 
14 #ifndef EXPONENTIATION_TCC_
15 #define EXPONENTIATION_TCC_
16 
17 #include <libff/common/utils.hpp>
18 
19 namespace libff
20 {
21 
22 template<typename FieldT, mp_size_t m>
23 FieldT power(const FieldT &base, const bigint<m> &exponent)
24 {
25  FieldT result = FieldT::one();
26 
27  bool found_one = false;
28 
29  for (long i = exponent.max_bits() - 1; i >= 0; --i) {
30  if (found_one) {
31  result = result * result;
32  }
33 
34  if (exponent.test_bit(i)) {
35  found_one = true;
36  result = result * base;
37  }
38  }
39 
40  return result;
41 }
42 
43 template<typename FieldT>
44 FieldT power(const FieldT &base, const unsigned long exponent)
45 {
46  return power<FieldT>(base, bigint<1>(exponent));
47 }
48 
49 } // namespace libff
50 
51 #endif // EXPONENTIATION_TCC_