-
Notifications
You must be signed in to change notification settings - Fork 5
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
Updates bech32 checksum constant to "M" #3
Conversation
…m; update all cpp tests and examples
add some new functions to c api; update all c tests and examples add missing header file for gcc
[BIP 0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki). Later, in November 2019, Pieter published | ||
some research regarding that an exponent used in the bech32 checksum algorithm (value = 1) may not be | ||
optimal for the error detecting properties of bech32. In February 2021, Pieter published | ||
[BIP 0350](http://www.example.com) reporting that "exhaustive analysis" showed the best possible exponent value is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bip350 needs a link
0x2bc830a3. This improved variant of Bech32 is called "Bech32m". | ||
|
||
When decoding a possible bech32 encoded string, libbech32 returns an enum value showing whether bech32m or bech32 | ||
was used to encode. This can be seen in the exaples above. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: examples
@@ -7,15 +7,15 @@ namespace { | |||
using namespace bech32::limits; | |||
|
|||
// exponent used in checksum generation, taken from recommendation | |||
// in: https://gist.github.com/sipa/a9845b37c1b298a7301c33a04090b2eb | |||
const unsigned int M = 0x3FFFFFFF; | |||
// in: https://github.com/sipa/bips/blob/bip-bech32m/bip-bech32m.mediawiki |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
link broken. ready to point to bip350.
https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki
sipa's working blob has moved here (but may be already abandoned):
https://github.com/sipa/bips/blob/6446f2af0a75e513a0c4bad0ddaad8d798bd5e2d/bip-0350.mediawiki
inputStr.length() - (index_of_separator+1) - bech32::limits::CHECKSUM_LENGTH; // +1 for zero-based index | ||
inputStr.length() | ||
- number_of_hrp_characters | ||
- 1 // for separator character |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could use new constant here
free(hrpdp); | ||
void bech32_free_HrpAndDp(bech32_HrpAndDp *hrpAndDp) { | ||
if(hrpAndDp == nullptr) | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this return or assert?
extern "C" | ||
void bech32_free_bstring(bech32_bstring *bstring) { | ||
if(bstring == nullptr) | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same Q here: is the program working well if it frees a null pointer?
discussion:
https://stackoverflow.com/questions/4190703/is-it-safe-to-delete-a-null-pointer#comment34158001_4190737
* @param dp pointer to the "data part" | ||
* @param dplen the length of the "data part" array | ||
* | ||
* @return E_BECH32_SUCCESS on success, others on error (hrp/dp/bstr is NULL, bstr not | ||
* @return E_BECH32_SUCCESS on success, others on error (i.e., hrp/dp/bstring is NULL, bstring not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grammar nazi: i think you mean e.g.
* | ||
* @return E_BECH32_SUCCESS on success, others on error (hrp/dp/bstr is NULL, hrp/dp not | ||
* @return E_BECH32_SUCCESS on success, others on error (i.e., output is NULL, hrp/dp not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same i.e. -> e.g. (i think)
@@ -481,11 +634,12 @@ bech32_error bech32_decode(bech32_HrpAndDp *output, char const *bstr, size_t bst | |||
if(hrpAndDp.hrp.empty() && hrpAndDp.dp.empty()) | |||
return E_BECH32_INVALID_CHECKSUM; | |||
|
|||
if(hrpAndDp.hrp.size() > output->hrplen-1) | |||
if(hrpAndDp.hrp.size() > output->hrplen) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see, so this wasn't a prior bug, but was a consequence of clarifying the semantics of hrplen.
b = splitString(data); | ||
convertToLowercase(b.hrp); | ||
mapDP(b.dp); | ||
ASSERT_TRUE(verifyChecksum(b.hrp, b.dp)); | ||
|
||
data = "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq8c42pv"; | ||
data = "11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if the many-q-string is sent in now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Please see minor comments about URLs and such.
This PR contains all changes needed to update the bech32 algorithm's exponential constant from the original value of "1", then the intermediate value of "0x3FFFFFFF", to the now final value of "0x2bc830a3". This final value, also known as "M", was decided upon following the recent research by Pieter Wuille, currently seen here: https://github.com/sipa/bips/blob/bip-bech32m/bip-bech32m.mediawiki
Also included are quite a few updates to the C API as I discovered a few memory leaks during my testing of the new constant.
Some items to consider: HrpAndDp is a crappy struct name for the decoded "human readable" and "data" parts. Can you suggest something better? I'm also not terribly fond of the function name "encodeUsingOriginalConstant()" which goes along with the default of "encode()".