-
Notifications
You must be signed in to change notification settings - Fork 50
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
Rewrite bech32 crate #102
Rewrite bech32 crate #102
Conversation
This requires dropping `Ord` impls from the error types, but IMHO these didn't really make sense anyway. This also keeps the `u5` name. We will rename this in a later PR which replaces the *Base32 traits with iterator adaptors, since at that point there will be a much smaller API surface to change.
b77ef94
to
13827d3
Compare
src/lib.rs
Outdated
/// The codex32 checksum algorithm, defined in BIP-93. | ||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
enum Codex32 {} |
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.
Do we want to keep this in?
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.
No, we'll drop it from the final PR.
13827d3
to
61437ed
Compare
Notes:
|
src/primitives/hrpstring.rs
Outdated
} | ||
|
||
#[test] | ||
#[allow(unused_variables)] // Triggered by matches macro. |
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.
This is triggered because the matches
macro is used incorrectly.
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.
Removed, and fixed tests to fully implement the test vectors from both bips.
src/primitives/gf1024.rs
Outdated
// Written by Clark Moody and the rust-bitcoin developers. | ||
// SPDX-License-Identifier: MIT | ||
|
||
//! GF1024 - Galois Field over 1024 elements. |
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'm unclear on the purpose of including this in this PR.
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.
Will drop, thanks.
src/lib.rs
Outdated
|
||
/// True if program length is either 20 bytes (p2wpkh) or 32 bytes (p2wsh). | ||
/// ref: [BIP-141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#user-content-Witness_program) | ||
#[cfg(feature = "alloc")] |
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 part of this function requires an allocator?
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.
This is to prevent clippy warning when "alloc" is not enabled, I added a comment to it to say as such.
a07a1b1
to
035f026
Compare
Is it even worth fuzzing this crate? We accept random data and convert it to a string (filters out invalid utf-8) and then decode (filters out invalid bech32 characters). Won't the number of times this actually succeeds be so small that running the fuzzing is essentially doing nothing? Or is the fuzzing meaningfully helping to prove Am I correct in thinking we want to test that:
|
You can test whether it's able to produce valid checksums. It would surprise me if it can't, given how simple the algorithm is and the fact that it only requires modiying the last 6 characters of the string (and modifying them independently, by simply xoring them with the correct value). But regardless, it is valuable to fuzz that we don't panic. |
src/network.rs
Outdated
/// ``` | ||
/// | ||
/// [segwit address format]: <https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#user-content-Segwit_address_format> | ||
pub const fn to_hrp(&self) -> &'static str { |
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.
Why not return an instance of the human-readable part type instead of a string slice?
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'm having a play with introducing the Hrp
type as you suggested previously, cheers.
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.
Aight, I hacked up a new version of this PR that adds an Hrp
type, and also a KnownHrp
type for segwit specific code. Could be mistakes because it was a fair bit of work and I should really go over it on another day before pushing but I'm pretty psyched for this so if you'll excuse me I'll push right away.
/// | ||
/// Parsing as an HRP string does not validate the checksum in any way. | ||
#[derive(Debug)] | ||
pub struct Parsed<'s> { |
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.
The names here don't make a ton of sense. The module is called hrpstring
, but the primary data structure is Parsed
and contains not only HRP data but also witness version and the data part.
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.
The data structure is an HRP string which has been parsed into an HRP, witness version, and data part.
We should change this if it's not understandable but it seemed pretty straightforward to me.
67c92ec
to
4b841ad
Compare
The major change here is that the PR no longer removes the current API. Instead it adds a
Thanks for your patience @clarkmoody and keeping on prodding me about the |
c518a2c
to
33cd60a
Compare
33cd60a
to
1f9f8b5
Compare
Closing since this is stale. |
Draft because done on top of #101, also because @apoelstra suggested doing this in smaller parts but I think it might be easier to just pull the trigger and do it all at once. @clarkmoody please say if you find this approach too much and would prefer to see the PR broken up into multiple PRs.
Why the rewrite?
This is a bitcoin crate but currently it supports arbitrary HRP prefixes, by making the top level API more bitcoin specific we can simplify the API.Aims
segwit
API that is ergonomic to use for Bitcoin users and that implements the specification laid out in BIP-173 And BIP-350For example usage in
rust-bitcoin
see: rust-bitcoin/rust-bitcoin#1809