ConvertBits() - convert from one power-of-2 number base to another.
Function extracted from upstream: PR bitcoin/bitcoin#11167 Commit c091b99379b97cb314c9fa123beabdbc324cf7a4
This commit is contained in:
parent
7e45636707
commit
6b759fb092
|
@ -61,6 +61,7 @@ BITCOIN_TESTS =\
|
|||
test/Checkpoints_tests.cpp \
|
||||
test/coins_tests.cpp \
|
||||
test/compress_tests.cpp \
|
||||
test/convertbits_tests.cpp \
|
||||
test/crypto_tests.cpp \
|
||||
test/DoS_tests.cpp \
|
||||
test/equihash_tests.cpp \
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
// Copyright (c) 2018 The Zcash developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <utilstrencodings.h>
|
||||
#include <test/test_bitcoin.h>
|
||||
#include <zcash/NoteEncryption.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(convertbits_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(convertbits_deterministic)
|
||||
{
|
||||
for (size_t i = 0; i < 256; i++) {
|
||||
std::vector<unsigned char> input(32, i);
|
||||
std::vector<unsigned char> data;
|
||||
std::vector<unsigned char> output;
|
||||
ConvertBits<8, 5, true>(data, input.begin(), input.end());
|
||||
ConvertBits<5, 8, false>(output, data.begin(), data.end());
|
||||
BOOST_CHECK_EQUAL(data.size(), 52);
|
||||
BOOST_CHECK_EQUAL(output.size(), 32);
|
||||
BOOST_CHECK(input == output);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 256; i++) {
|
||||
std::vector<unsigned char> input(43, i);
|
||||
std::vector<unsigned char> data;
|
||||
std::vector<unsigned char> output;
|
||||
ConvertBits<8, 5, true>(data, input.begin(), input.end());
|
||||
ConvertBits<5, 8, false>(output, data.begin(), data.end());
|
||||
BOOST_CHECK_EQUAL(data.size(), 69);
|
||||
BOOST_CHECK_EQUAL(output.size(), 43);
|
||||
BOOST_CHECK(input == output);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(convertbits_random)
|
||||
{
|
||||
for (size_t i = 0; i < 1000; i++) {
|
||||
auto input = libzcash::random_uint256();
|
||||
std::vector<unsigned char> data;
|
||||
std::vector<unsigned char> output;
|
||||
ConvertBits<8, 5, true>(data, input.begin(), input.end());
|
||||
ConvertBits<5, 8, false>(output, data.begin(), data.end());
|
||||
BOOST_CHECK_EQUAL(data.size(), 52);
|
||||
BOOST_CHECK_EQUAL(output.size(), 32);
|
||||
BOOST_CHECK(input == uint256(output));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
|
@ -133,4 +133,28 @@ bool TimingResistantEqual(const T& a, const T& b)
|
|||
*/
|
||||
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
|
||||
|
||||
/** Convert from one power-of-2 number base to another. */
|
||||
template<int frombits, int tobits, bool pad, typename O, typename I>
|
||||
bool ConvertBits(O& out, I it, I end) {
|
||||
size_t acc = 0;
|
||||
size_t bits = 0;
|
||||
constexpr size_t maxv = (1 << tobits) - 1;
|
||||
constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
|
||||
while (it != end) {
|
||||
acc = ((acc << frombits) | *it) & max_acc;
|
||||
bits += frombits;
|
||||
while (bits >= tobits) {
|
||||
bits -= tobits;
|
||||
out.push_back((acc >> bits) & maxv);
|
||||
}
|
||||
++it;
|
||||
}
|
||||
if (pad) {
|
||||
if (bits) out.push_back((acc << (tobits - bits)) & maxv);
|
||||
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // BITCOIN_UTILSTRENCODINGS_H
|
||||
|
|
Loading…
Reference in New Issue