Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for the PolyMod method in b(l)ech32 #595

Merged
merged 4 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ BITCOIN_TESTS =\
test/base58_tests.cpp \
test/base64_tests.cpp \
test/bech32_tests.cpp \
test/blech32_tests.cpp \
test/bip32_tests.cpp \
test/blockchain_tests.cpp \
test/blockencodings_tests.cpp \
Expand Down
4 changes: 2 additions & 2 deletions src/blech32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ data Cat(data x, const data& y)
/** This function will compute what 6 5-bit values to XOR into the last 6 input values, in order to
* make the checksum 0. These 6 values are packed together in a single 30-bit integer. The higher
* bits correspond to earlier values. */
uint32_t PolyMod(const data& v)
uint64_t PolyMod(const data& v)
{
// The input is interpreted as a list of coefficients of a polynomial over F = GF(32), with an
// implicit 1 in front. If the input is [v0,v1,v2,v3,v4], that polynomial is v(x) =
Expand Down Expand Up @@ -136,7 +136,7 @@ data CreateChecksum(const std::string& hrp, const data& values)
{
data enc = Cat(ExpandHRP(hrp), values);
enc.resize(enc.size() + 12); // ELEMENTS: Append 6->12 zeroes
uint32_t mod = PolyMod(enc) ^ 1; // Determine what to XOR into those 6 zeroes.
uint64_t mod = PolyMod(enc) ^ 1; // Determine what to XOR into those 6 zeroes.
data ret(12); // ELEMENTS: 6->12
for (size_t i = 0; i < 12; ++i) { // ELEMENTS: 6->12
// Convert the 5-bit groups in mod to checksum values.
Expand Down
20 changes: 19 additions & 1 deletion src/test/bech32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <bech32.h>
#include <bech32.cpp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yuck (including a .cpp file).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chants export export export

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter doesn't like it either :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah was to test the internal methods. Changed that meanwhile.

#include <test/test_bitcoin.h>

#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -66,4 +66,22 @@ BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
}
}

BOOST_AUTO_TEST_CASE(bech32_polymod_sanity)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this a suitable upstream test?

{
std::vector<unsigned char> data(40);
GetRandBytes(data.data(), data.size());

std::vector<unsigned char> base32;
ConvertBits<8, 5, true>([&](unsigned char c) { base32.push_back(c); }, data.begin(), data.end());
uint64_t plm1 = PolyMod(base32);

// Now add 1023 zeros.
for (auto i = 0; i < 1023; i++) {
base32.push_back(0);
}
uint64_t plm2 = PolyMod(base32);

BOOST_CHECK_EQUAL(plm1, plm2);
}

BOOST_AUTO_TEST_SUITE_END()
46 changes: 46 additions & 0 deletions src/test/blech32_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2017 Pieter Wuille
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <blech32.cpp>
#include <random.h>
#include <utilstrencodings.h>
#include <test/test_bitcoin.h>

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(blech32_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(blech32_polymod_sanity)
{
std::vector<unsigned char> data(40);
GetRandBytes(data.data(), data.size());

std::vector<unsigned char> base32;
ConvertBits<8, 5, true>([&](unsigned char c) { base32.push_back(c); }, data.begin(), data.end());
uint64_t plm1 = PolyMod(base32);

// Now add 1023 zeros.
for (auto i = 0; i < 1023; i++) {
base32.push_back(0);
}
uint64_t plm2 = PolyMod(base32);

BOOST_CHECK_EQUAL(plm1, plm2);
}

BOOST_AUTO_TEST_CASE(blech32_checksum)
{
std::vector<unsigned char> vector{7,2,3,4,5,6,7,8,9,234,123,213,16};
std::vector<unsigned char> b32;
ConvertBits<8, 5, true>([&](unsigned char c) { b32.push_back(c); }, vector.begin(), vector.end());
std::vector<unsigned char> cs = CreateChecksum("lq", b32);

std::vector<unsigned char> expected_cs{22,13,13,5,4,4,23,7,28,21,30,12};
for (size_t i = 0; i < expected_cs.size(); i++) {
BOOST_CHECK_EQUAL(expected_cs[i], cs[i]);
}
}

BOOST_AUTO_TEST_SUITE_END()