Skip to content

Commit

Permalink
ConvertBits() - convert from one power-of-2 number base to another.
Browse files Browse the repository at this point in the history
Function extracted from upstream:
  PR bitcoin#11167
  Commit c091b99
  • Loading branch information
furszy committed Jun 15, 2020
1 parent 882edda commit 4efa2b9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ BITCOIN_TESTS =\
test/checkblock_tests.cpp \
test/Checkpoints_tests.cpp \
test/coins_tests.cpp \
test/convertbits_tests.cpp \
test/compress_tests.cpp \
test/crypto_tests.cpp \
test/DoS_tests.cpp \
Expand Down
52 changes: 52 additions & 0 deletions src/test/convertbits_tests.cpp
Original file line number Diff line number Diff line change
@@ -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_pivx.h>
#include "sapling/util.h"

#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 = 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()
25 changes: 25 additions & 0 deletions src/utilstrencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,29 @@ bool TimingResistantEqual(const T& a, const T& b)
return accumulator == 0;
}

/** 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

0 comments on commit 4efa2b9

Please sign in to comment.