-
Notifications
You must be signed in to change notification settings - Fork 138
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
Use bech32
to calculate descriptor checksums
#608
Use bech32
to calculate descriptor checksums
#608
Conversation
7865784
to
9ca3380
Compare
Up to 5018673 is great and could be PR'd separately if you want to move more quickly. |
As for 9ca3380:
|
Sick! Thanks man. |
I learned a new expression 'futzing with', not the first one I've learned from you @apoelstra, I love it. |
9ca3380
to
22ee854
Compare
src/descriptor/bare.rs
Outdated
for ch in Checksummed::new_unchecked(&s) { | ||
f.write_char(ch)?; | ||
} | ||
} |
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.
In 22ee854:
We definitely want to keep the wrapped_f
stuff. This new code is both longer and now allocates.
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.
Yeah I balked at this before I pushed it. Will add it back in.
src/descriptor/checksum.rs
Outdated
/// | ||
/// [BIP-380]: <https://github.com/bitcoin/bips/blob/master/bip-0380.mediawiki> | ||
#[inline] | ||
pub fn new(descriptor: &'a str) -> Result<Checksummed<'a>, Error> { |
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.
In 22ee854:
Rather than taking a string this should take a D: Display
and a fmt::Formatter
.
src/descriptor/checksum.rs
Outdated
let checksummed = Checksummed::new(desc_str)?; | ||
if !checksummed.eq(s.chars()) { | ||
return Err(Error::BadDescriptor("invalid checksum".to_owned())); | ||
} |
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.
In 22ee854:
It would be faster to feed the checksum into the checksum iterator and compare the residue against the target. Then in the happy case we only need to iterate once. Actually, the speedup here would be minimal, since you are only checksumming once, and s.chars()
is extremely fast to iterate on.
In the unhappy case, we should probably return a better error variant that has the expected and received checksum in it. It's fine if this is slow and allocating.
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.
Sure thing.
22ee854
to
7335471
Compare
Alright, I think I got a little trigger happy before to use my new found iterator skills from the recent bech32 work. Instead this PR now just does the minimum required to remove the |
I was futzing with the whole |
045d645
to
ad7d5dc
Compare
Ok, ACK this. It's a good start. |
5018673 Remove magic number (Tobin C. Harding) 535cd17 Improve docs in the checksum module (Tobin C. Harding) fec700a Add bip 380 checksum test vectors (Tobin C. Harding) 73f4892 Add output descriptor bip referenece (Tobin C. Harding) Pull request description: The first 4 patches from #608, no changes. Clean up and add some test vector unit tests. ACKs for top commit: apoelstra: ACK 5018673 Tree-SHA512: f50f64bf9a1fc28eebca809379e02580cab96e7e41228aab6045441eb71702bef1b1979e497a6dcb1e1bce082965e5c93e78dba6e8fbd78c7a0ae2e3c8035660
ad7d5dc
to
c5c08fe
Compare
Rebased to pick up changes in #609 and use newly release |
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.
ACK c5c08fe
@sanket1729 I'll let you take a look at this before merging. It basically gets rid of some of the magic constants and uses rust-bech32 facilities to do the checksum. But this doesn't represent a "real" use of the new rust-bech32 API, which would pretty-much reduce the checksum.rs module to just encoding/decoding strings as u5s and then handing off to rust-bech32 APIs. |
I was going to attack it in steps:
The reason to do two steps is that I don't think we can have a general "input multiple strings" to the checksum engine because cls/clscount is state that has to be kept between successive input strings. But i noticed that we never do more than input a single descriptor. |
I don't like the idea of moving so much logic into a macro. If we need to retain state then we can keep the existing But either way, once we have a stream of Fe32s, we should be able to just put them into a rust-bech32 |
The logic is just the more or less duplicate lines use fmt::Write;
let mut wrapped_f = checksum::Formatter::new(f);
write!(wrapped_f, "{}", self.ms)?;
wrapped_f.write_checksum_if_not_alt() Other descriptors have a match statement when calling
Another complication is we cannot use bech32 iterators to encode because of the |
Oh, yeah, this seems fine to me.
Yeah, that sucks. But I think your solution is good. |
I had another look at this, I'm not sure we can do much better even using some sort of iterator because of how we use |
I think this could be considered for merge as is, I wrote up #610 to discuss potential improvements. |
Yeah, you may be right. |
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.
ACK c44ce81
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.
utACK c44ce81. Did not review the use of bech32 carefully. Any breakage there should have caused existing test cases to fail.
We can also safely merge this despite the red crosses as CI is fixed in master. |
The `checksum::poly_mod` function implements BCH codes to calculate a checksum (appended to descriptors). We recently released a version of BCH codes in `bech32`. We can implement the `bech32::Checksum` trait for BIP-380 and use the `primitives::checksum` module, removing the custom `poly_mod` function.
Currently we have a bunch of places in the code where we create a checksum formatter wrapper then write a descriptor to it then write the checksum. This can all be wrapped up in a macro, has to be a macro because the number of args is variable. This reduces the line count with no real loss of clarity `write_descriptor!` is pretty self explanatory.
c44ce81
to
fe1a6be
Compare
You were correct @sanket1729, I rebased it for good measure, no changes. |
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.
ACK fe1a6be
This is just the last patch, the rest are in #609 now.
The
checksum::poly_mod
function implements BCH codes to calculate a checksum (appended to descriptors). We recently released a version of BCH codes inbech32
. We can implement thebech32::Checksum
trait for BIP-380 and use theprimitives::checksum
module, removing the custompoly_mod
function.