Add secp256k1_ctz{32,64}_var functions

These functions count the number of trailing zeroes in non-zero integers.
This commit is contained in:
Pieter Wuille
2020-10-11 19:10:58 -07:00
parent 4c3ba88c3a
commit de0a643c3d
2 changed files with 86 additions and 0 deletions

View File

@@ -416,6 +416,25 @@ void run_scratch_tests(void) {
secp256k1_context_destroy(none);
}
void run_ctz_tests(void) {
static const uint32_t b32[] = {1, 0xffffffff, 0x5e56968f, 0xe0d63129};
static const uint64_t b64[] = {1, 0xffffffffffffffff, 0xbcd02462139b3fc3, 0x98b5f80c769693ef};
int shift;
unsigned i;
for (i = 0; i < sizeof(b32) / sizeof(b32[0]); ++i) {
for (shift = 0; shift < 32; ++shift) {
CHECK(secp256k1_ctz32_var_debruijn(b32[i] << shift) == shift);
CHECK(secp256k1_ctz32_var(b32[i] << shift) == shift);
}
}
for (i = 0; i < sizeof(b64) / sizeof(b64[0]); ++i) {
for (shift = 0; shift < 64; ++shift) {
CHECK(secp256k1_ctz64_var_debruijn(b64[i] << shift) == shift);
CHECK(secp256k1_ctz64_var(b64[i] << shift) == shift);
}
}
}
/***** HASH TESTS *****/
void run_sha256_tests(void) {
@@ -5606,6 +5625,8 @@ int main(int argc, char **argv) {
run_rand_bits();
run_rand_int();
run_ctz_tests();
run_sha256_tests();
run_hmac_sha256_tests();
run_rfc6979_hmac_sha256_tests();