Closed sipa closed 7 years ago
@gmaxwell Added some invalid checksum tests that should cover all the cases you found in bech32.cpp, but no invalid encode tests yet.
I optimize ur new code:
index 9569e56..77680f9 100644
--- a/ref/c++/bech32.cpp
+++ b/ref/c++/bech32.cpp
@@ -127,7 +127,7 @@ std::pair<std::string, data> decode(const std::string& str) {
}
if (lower && upper) ok = false;
size_t pos = str.rfind('1');
- if (ok && str.size() <= 90 && pos != str.npos && pos >= 1 && pos + 7 <= str.size()) {
+ if (ok && str.size() <= 90 && pos != str.npos && pos >= 1 /*&& pos + 7 <= str.size()*/) {
data values;
values.resize(str.size() - 1 - pos);
for (size_t i = 0; i < str.size() - 1 - pos; ++i) {
diff --git a/ref/c++/segwit_addr.cpp b/ref/c++/segwit_addr.cpp
index a52cba1..9ca6d0a 100644
--- a/ref/c++/segwit_addr.cpp
+++ b/ref/c++/segwit_addr.cpp
@@ -36,7 +36,7 @@ bool convertbits(data& out, const data& in) {
const int max_acc = (1 << (frombits + tobits - 1)) - 1;
for (size_t i = 0; i < in.size(); ++i) {
int value = in[i];
- if (value < 0 || (value >> frombits)) return false;
+/* if (value < 0 || (value >> frombits)) return false;*/
acc = ((acc << frombits) | value) & max_acc;
bits += frombits;
while (bits >= tobits) {
@@ -60,7 +60,7 @@ namespace segwit_addr
/** Decode a SegWit address. */
std::pair<int, data> decode(const std::string& hrp, const std::string& addr) {
std::pair<std::string, data> dec = bech32::decode(addr);
- if (dec.first != hrp || dec.second.size() < 1) return std::make_pair(-1, data());
+ if (dec.first != hrp /*|| dec.second.size() < 1*/) return std::make_pair(-1, data());
data conv;
if (!convertbits<5, 8, false>(conv, data(dec.second.begin() + 1, dec.second.end())) ||
conv.size() < 2 || conv.size() > 40 || dec.second[0] > 16 || (dec.second[0] == 0 &&
@@ -76,7 +76,7 @@ std::string encode(const std::string& hrp, int witver, const data& witprog) {
enc.push_back(witver);
convertbits<8, 5, true>(enc, witprog);
std::string ret = bech32::encode(hrp, enc);
- if (decode(hrp, ret).first == -1) return "";
+/* if (decode(hrp, ret).first == -1) return "";*/
return ret;
}
@gmaxwell Updated again.
I optimize ur code for xtreme performance, tests show 100% perfection. (each one line change also works in isolation.)