Skip to content

Commit

Permalink
Add utilities for log2
Browse files Browse the repository at this point in the history
  • Loading branch information
sanket1729 committed Feb 8, 2023
1 parent 17417d4 commit 420353d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/modules/bulletproofs/bulletproofs_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ static void secp256k1_bulletproofs_le64(unsigned char *output, const uint64_t n)
output[7] = n >> 56;
}

/* Check if n is power of two*/
static int secp256k1_is_power_of_two(size_t n) {
return n > 0 && (n & (n - 1)) == 0;
}

/* Compute the log2 of n. If n is not a power of two, it returns the largest
* `k` such that 2^k <= n. Assumes n < 2^64. In Bulletproofs, this is bounded
* by len of input vectors which can be safely assumed to be less than 2^64.
*/
static size_t secp256k1_bulletproofs_pp_log2(size_t n) {
return 64 - 1 - secp256k1_clz64_var((uint64_t)n);
}

#endif
20 changes: 20 additions & 0 deletions src/modules/bulletproofs/tests_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#ifndef _SECP256K1_MODULE_BULLETPROOFS_TEST_
#define _SECP256K1_MODULE_BULLETPROOFS_TEST_

#include <stdint.h>

#include "include/secp256k1_bulletproofs.h"
#include "bulletproofs_util.h"
#include "bulletproofs_pp_transcript_impl.h"

static void test_bulletproofs_generators_api(void) {
Expand Down Expand Up @@ -130,7 +134,23 @@ static void test_bulletproofs_pp_tagged_hash(void) {
CHECK(secp256k1_memcmp_var(output, output_cached, 32) == 0);
}

void test_log_exp(void) {
CHECK(secp256k1_is_power_of_two(0) == 0);
CHECK(secp256k1_is_power_of_two(1) == 1);
CHECK(secp256k1_is_power_of_two(2) == 1);
CHECK(secp256k1_is_power_of_two(64) == 1);
CHECK(secp256k1_is_power_of_two(63) == 0);
CHECK(secp256k1_is_power_of_two(256) == 1);

CHECK(secp256k1_bulletproofs_pp_log2(1) == 0);
CHECK(secp256k1_bulletproofs_pp_log2(2) == 1);
CHECK(secp256k1_bulletproofs_pp_log2(255) == 7);
CHECK(secp256k1_bulletproofs_pp_log2(256) == 8);
CHECK(secp256k1_bulletproofs_pp_log2(257) == 8);
}

void run_bulletproofs_tests(void) {
test_log_exp();
test_bulletproofs_generators_api();
test_bulletproofs_generators_fixed();
test_bulletproofs_pp_tagged_hash();
Expand Down

0 comments on commit 420353d

Please sign in to comment.