Skip to content

Commit 0de2b05

Browse files
committed
Add unit test for the PolyMod method in b(l)ech32
1 parent 2412d38 commit 0de2b05

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ BITCOIN_TESTS =\
3939
test/base58_tests.cpp \
4040
test/base64_tests.cpp \
4141
test/bech32_tests.cpp \
42+
test/blech32_tests.cpp \
4243
test/bip32_tests.cpp \
4344
test/blockchain_tests.cpp \
4445
test/blockencodings_tests.cpp \

src/test/bech32_tests.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <bech32.h>
5+
#include <bech32.cpp>
66
#include <test/test_bitcoin.h>
77

88
#include <boost/test/unit_test.hpp>
@@ -66,4 +66,22 @@ BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
6666
}
6767
}
6868

69+
BOOST_AUTO_TEST_CASE(bech32_polymod_sanity)
70+
{
71+
std::vector<unsigned char> data(40);
72+
GetRandBytes(data.data(), data.size());
73+
74+
std::vector<unsigned char> base32;
75+
ConvertBits<8, 5, true>([&](unsigned char c) { base32.push_back(c); }, data.begin(), data.end());
76+
uint64_t plm1 = PolyMod(base32);
77+
78+
// Now add 1023 zeros.
79+
for (auto i = 0; i < 1023; i++) {
80+
base32.push_back(0);
81+
}
82+
uint64_t plm2 = PolyMod(base32);
83+
84+
BOOST_CHECK_EQUAL(plm1, plm2);
85+
}
86+
6987
BOOST_AUTO_TEST_SUITE_END()

src/test/blech32_tests.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2017 Pieter Wuille
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <blech32.cpp>
6+
#include <random.h>
7+
#include <utilstrencodings.h>
8+
#include <test/test_bitcoin.h>
9+
10+
#include <boost/test/unit_test.hpp>
11+
12+
BOOST_FIXTURE_TEST_SUITE(blech32_tests, BasicTestingSetup)
13+
14+
BOOST_AUTO_TEST_CASE(blech32_polymod_sanity)
15+
{
16+
std::vector<unsigned char> data(40);
17+
GetRandBytes(data.data(), data.size());
18+
19+
std::vector<unsigned char> base32;
20+
ConvertBits<8, 5, true>([&](unsigned char c) { base32.push_back(c); }, data.begin(), data.end());
21+
uint64_t plm1 = PolyMod(base32);
22+
23+
// Now add 1023 zeros.
24+
for (auto i = 0; i < 1023; i++) {
25+
base32.push_back(0);
26+
}
27+
uint64_t plm2 = PolyMod(base32);
28+
29+
BOOST_CHECK_EQUAL(plm1, plm2);
30+
}
31+
32+
BOOST_AUTO_TEST_SUITE_END()
33+

0 commit comments

Comments
 (0)