1
0
Fork 0
forked from lthn/blockchain

crypto: scalar_t::git_bit + test crypto_sc_get_bit; hash_helper_t extended with hp variants

This commit is contained in:
sowle 2022-04-09 21:12:44 +02:00
parent 2478dbb677
commit b9ccb10287
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
2 changed files with 62 additions and 0 deletions

View file

@ -431,6 +431,13 @@ namespace crypto
return result;
}
bool get_bit(size_t bit_index) const
{
if (bit_index > 255)
return false; // TODO: consider performace implications
return (m_u64[bit_index >> 6] & (1ull << (bit_index & 63))) != 0;
}
}; // struct scalar_t
//
@ -1063,6 +1070,21 @@ namespace crypto
ge_bytes_hash_to_ec_32(&result.m_p3, (const unsigned char*)&p);
return result;
}
static point_t hp(const scalar_t& s)
{
point_t result;
ge_bytes_hash_to_ec_32(&result.m_p3, s.data());
return result;
}
static point_t hp(const void* data, size_t size)
{
point_t result;
ge_bytes_hash_to_ec(&result.m_p3, data, size);
return result;
}
}; // hash_helper_t struct

View file

@ -1803,6 +1803,46 @@ TEST(crypto, point_is_zero)
}
TEST(crypto, sc_get_bit)
{
static_assert(sizeof(scalar_t) * 8 == 256, "size missmatch");
scalar_t v = 0; // all bits are 0
for (size_t n = 0; n < 256; ++n)
{
ASSERT_EQ(v.get_bit(n), false);
}
v = c_scalar_256m1; // all bits are 1
for (size_t n = 0; n < 256; ++n)
{
ASSERT_EQ(v.get_bit(n), true);
}
// bits out of the [0; 255] range supposed to be always 0
for (size_t n = 256; n < 2048; ++n)
{
ASSERT_EQ(v.get_bit(n), false);
}
// check random value
const scalar_t x = scalar_t::random();
for (size_t n = 0; n < 64; ++n)
ASSERT_EQ(x.get_bit(n), ((x.m_u64[0] & (1ull << (n - 0))) != 0));
for (size_t n = 64; n < 128; ++n)
ASSERT_EQ(x.get_bit(n), ((x.m_u64[1] & (1ull << (n - 64))) != 0));
for (size_t n = 128; n < 192; ++n)
ASSERT_EQ(x.get_bit(n), ((x.m_u64[2] & (1ull << (n - 128))) != 0));
for (size_t n = 192; n < 256; ++n)
ASSERT_EQ(x.get_bit(n), ((x.m_u64[3] & (1ull << (n - 192))) != 0));
// bits out of the [0; 255] range supposed to be always 0
for (size_t n = 256; n < 2048; ++n)
ASSERT_EQ(x.get_bit(n), false);
return true;
}
//
// test's runner
//