Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pow.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
9#include "./get_msb.hpp"
10#include <cstdint>
11
12namespace bb::numeric {
13constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent)
14{
15 if (input == 0) {
16 return 0;
17 }
18 if (exponent == 0) {
19 return 1;
20 }
21
22 uint64_t accumulator = input;
23 uint64_t to_mul = input;
24 const uint64_t maximum_set_bit = get_msb64(exponent);
25
26 for (int i = static_cast<int>(maximum_set_bit) - 1; i >= 0; --i) {
27 accumulator *= accumulator;
28 if (((exponent >> i) & 1) != 0U) {
29 accumulator *= to_mul;
30 }
31 }
32 return accumulator;
33}
34
35constexpr bool is_power_of_two(uint64_t x)
36{
37 return (x != 0U) && ((x & (x - 1)) == 0U);
38}
39
40} // namespace bb::numeric
constexpr uint64_t get_msb64(const uint64_t in)
Definition get_msb.hpp:30
constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent)
Definition pow.hpp:13
constexpr bool is_power_of_two(uint64_t x)
Definition pow.hpp:35