diff --git a/src/crypto/crypto-sugar.h b/src/crypto/crypto-sugar.h index 82b58777..fac168b8 100644 --- a/src/crypto/crypto-sugar.h +++ b/src/crypto/crypto-sugar.h @@ -20,6 +20,34 @@ namespace crypto // Helpers // + // returns greatest k, s.t. n**k <= v + // tests in crypto_tests_range_proofs.h + constexpr uint64_t constexpr_floor_log_n(uint64_t v, uint64_t n) + { + return (v < n || n <= 1) ? 0 : constexpr_floor_log_n(v / n, n) + 1; + } + + // returns smallest k, s.t. v <= n**k + // tests in crypto_tests_range_proofs.h + constexpr uint64_t constexpr_ceil_log_n(uint64_t v, uint64_t n) + { + return (v <= 1 || n <= 1) ? 0 : constexpr_floor_log_n(v - 1, n) + 1; + } + + // returns smallest k, s.t. v <= 2**k + // tests in crypto_tests_range_proofs.h + constexpr uint64_t constexpr_ceil_log2(uint64_t v) + { + return constexpr_ceil_log_n(v, 2); + } + + // returns base ** k + constexpr uint64_t constexpr_pow(uint64_t k, uint64_t base) + { + return k == 0 ? 1 : base * constexpr_pow(k - 1, base); + } + + template std::string pod_to_hex_reversed(const pod_t &h) { diff --git a/src/crypto/range_proofs.h b/src/crypto/range_proofs.h index 07fd70ca..7992d9d3 100644 --- a/src/crypto/range_proofs.h +++ b/src/crypto/range_proofs.h @@ -23,30 +23,6 @@ namespace crypto return result; } - - // returns greatest k, s.t. n**k <= v - // tests in crypto_tests_range_proofs.h - constexpr size_t constexpr_floor_log_n(size_t v, size_t n) - { - return (v < n || n <= 1) ? 0 : constexpr_floor_log_n(v / n, n) + 1; - } - - // returns smallest k, s.t. v <= n**k - // tests in crypto_tests_range_proofs.h - constexpr size_t constexpr_ceil_log_n(size_t v, size_t n) - { - return (v <= 1 || n <= 1) ? 0 : constexpr_floor_log_n(v - 1, n) + 1; - } - - // returns smallest k, s.t. v <= 2**k - // tests in crypto_tests_range_proofs.h - constexpr size_t constexpr_ceil_log2(size_t v) - { - return constexpr_ceil_log_n(v, 2); - } - - - // returns least significant bit uing de Bruijn sequence // http://graphics.stanford.edu/~seander/bithacks.html inline uint8_t calc_lsb_32(uint32_t v) diff --git a/tests/functional_tests/crypto_tests_range_proofs.h b/tests/functional_tests/crypto_tests_range_proofs.h index be2500bb..527e84d4 100644 --- a/tests/functional_tests/crypto_tests_range_proofs.h +++ b/tests/functional_tests/crypto_tests_range_proofs.h @@ -89,6 +89,19 @@ static_assert(constexpr_ceil_log2(100000000) == 27, ""); static_assert(constexpr_ceil_log2(0x7fffffffffffffff) == 63, ""); static_assert(constexpr_ceil_log2(SIZE_MAX) == 64, ""); +static_assert(constexpr_pow(0, 0) == 1, ""); +static_assert(constexpr_pow(0, 1) == 1, ""); +static_assert(constexpr_pow(1, 1) == 1, ""); + +static_assert(constexpr_pow(1, 0) == 0, ""); +static_assert(constexpr_pow(0, 2) == 1, ""); +static_assert(constexpr_pow(1, 2) == 2, ""); +static_assert(constexpr_pow(10, 2) == 1024, ""); +static_assert(constexpr_pow(63, 2) == 1ull << 63, ""); +static_assert(constexpr_pow(3, 3) == 27, ""); +static_assert(constexpr_pow(33, 3) == 5559060566555523ull, ""); + + TEST(bpp, basics) {