github.com/ledgerwatch/erigon-lib@v1.0.0/pedersen_hash/math.h (about) 1 #ifndef STARKWARE_UTILS_MATH_H_ 2 #define STARKWARE_UTILS_MATH_H_ 3 4 #include <cstdint> 5 #include <vector> 6 7 #include "error_handling.h" 8 9 namespace starkware { 10 11 using std::size_t; 12 13 constexpr uint64_t inline Pow2(uint64_t n) { 14 ASSERT(n < 64, "n must be smaller than 64."); 15 return UINT64_C(1) << n; 16 } 17 18 /* 19 Returns floor(Log_2(n)), n must be > 0. 20 */ 21 constexpr size_t inline Log2Floor(const uint64_t n) { 22 ASSERT(n != 0, "log2 of 0 is undefined"); 23 static_assert(sizeof(long long) == 8, "It is assumed that long long is 64bits"); // NOLINT 24 return 63 - __builtin_clzll(n); 25 } 26 27 /* 28 Computes base to the power of the number given by exponent_bits in a generic group, given the 29 element one in the group and a function mult(const GroupElementT& multiplier, GroupElementT* dst) 30 that performs: 31 *dst *= multiplier 32 in the group. 33 Note that it is possible that the address of multiplier is the same as dst. 34 */ 35 template <typename GroupElementT, typename MultFunc> 36 GroupElementT GenericPow( 37 const GroupElementT& base, const std::vector<bool>& exponent_bits, const GroupElementT& one, 38 const MultFunc& mult) { 39 GroupElementT power = base; 40 GroupElementT res = one; 41 for (const auto&& b : exponent_bits) { 42 if (b) { 43 mult(power, &res); 44 } 45 46 mult(power, &power); 47 } 48 49 return res; 50 } 51 52 } // namespace starkware 53 54 #endif // STARKWARE_UTILS_MATH_H_