Added varint unit tests
This commit is contained in:
@@ -109,9 +109,9 @@ bool check_keys(const hash& pub, const hash& sec)
|
||||
return pub == pub_check;
|
||||
}
|
||||
|
||||
static FORCEINLINE void hash_to_scalar(const uint8_t* data, size_t length, uint8_t(&res)[HASH_SIZE])
|
||||
static FORCEINLINE void hash_to_scalar(const uint8_t* data, int length, uint8_t (&res)[HASH_SIZE])
|
||||
{
|
||||
keccak(data, static_cast<int>(length), res, HASH_SIZE);
|
||||
keccak(data, length, res, HASH_SIZE);
|
||||
sc_reduce32(res);
|
||||
}
|
||||
|
||||
@@ -122,18 +122,13 @@ static FORCEINLINE void derivation_to_scalar(const hash& derivation, size_t outp
|
||||
uint8_t output_index[(sizeof(size_t) * 8 + 6) / 7];
|
||||
} buf;
|
||||
|
||||
uint8_t* begin = buf.derivation;
|
||||
uint8_t* end = buf.output_index;
|
||||
memcpy(buf.derivation, derivation.h, sizeof(buf.derivation));
|
||||
|
||||
size_t k = output_index;
|
||||
while (k >= 0x80) {
|
||||
*(end++) = (static_cast<uint8_t>(k) & 0x7F) | 0x80;
|
||||
k >>= 7;
|
||||
}
|
||||
*(end++) = static_cast<uint8_t>(k);
|
||||
uint8_t* p = buf.output_index;
|
||||
writeVarint(output_index, [&p](uint8_t b) { *(p++) = b; });
|
||||
|
||||
hash_to_scalar(begin, end - begin, res);
|
||||
const uint8_t* data = buf.derivation;
|
||||
hash_to_scalar(data, static_cast<int>(p - data), res);
|
||||
}
|
||||
|
||||
class Cache
|
||||
|
||||
@@ -46,29 +46,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, SideChain& sidechai
|
||||
#define READ_BYTE(x) do { if (!read_byte(x)) return __LINE__; } while (0)
|
||||
#define EXPECT_BYTE(value) do { uint8_t tmp; READ_BYTE(tmp); if (tmp != (value)) return __LINE__; } while (0)
|
||||
|
||||
auto read_varint = [&data, data_end](auto& b) -> bool
|
||||
{
|
||||
uint64_t result = 0;
|
||||
int k = 0;
|
||||
|
||||
while (data < data_end) {
|
||||
if (k >= static_cast<int>(sizeof(b)) * 8) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint64_t cur_byte = *(data++);
|
||||
result |= (cur_byte & 0x7F) << k;
|
||||
k += 7;
|
||||
|
||||
if ((cur_byte & 0x80) == 0) {
|
||||
b = result;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
#define READ_VARINT(x) do { if (!read_varint(x)) return __LINE__; } while(0)
|
||||
#define READ_VARINT(x) do { data = readVarint(data, data_end, x); if (!data) return __LINE__; } while(0)
|
||||
|
||||
auto read_buf = [&data, data_end](void* buf, size_t size) -> bool
|
||||
{
|
||||
|
||||
24
src/util.h
24
src/util.h
@@ -109,6 +109,30 @@ FORCEINLINE void writeVarint(T value, std::vector<uint8_t>& out)
|
||||
writeVarint(value, [&out](uint8_t b) { out.emplace_back(b); });
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
const uint8_t* readVarint(const uint8_t* data, const uint8_t* data_end, T& b)
|
||||
{
|
||||
uint64_t result = 0;
|
||||
int k = 0;
|
||||
|
||||
while (data < data_end) {
|
||||
if (k >= static_cast<int>(sizeof(T)) * 8) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const uint64_t cur_byte = *(data++);
|
||||
result |= (cur_byte & 0x7F) << k;
|
||||
k += 7;
|
||||
|
||||
if ((cur_byte & 0x80) == 0) {
|
||||
b = result;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename T, size_t N> FORCEINLINE constexpr size_t array_size(T(&)[N]) { return N; }
|
||||
template<typename T, typename U, size_t N> FORCEINLINE constexpr size_t array_size(T(U::*)[N]) { return N; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user