infincia / bip39-rs

A Rust implementation of BIP-0039
Apache License 2.0
54 stars 61 forks source link

Further Refactoring #15

Closed QuestofIranon closed 5 years ago

QuestofIranon commented 5 years ago

I did some minor refactoring on top of #14

Includes the API improvements, fixes, and most of the performance boosts from #14 but uses newer dependencies based on recent advances in the Rust ecosystem.

steveatinfincia commented 5 years ago

Hey this looks good, I'll take a closer look tomorrow and get it merged :)

maciejhirsz commented 5 years ago

So I just learned about hashbrown from this PR and it looks super impressive, but just running the benchmarks here it seems to be a regression:

test from_entropy ... bench:         517 ns/iter (+/- 414)
test new_mnemonic ... bench:         563 ns/iter (+/- 1)
test new_seed     ... bench:   1,706,894 ns/iter (+/- 85,852)
test validate     ... bench:         705 ns/iter (+/- 5)

Compared to results on #14:

test from_entropy ... bench:         395 ns/iter (+/- 226)
test new_mnemonic ... bench:         477 ns/iter (+/- 24)
test new_seed     ... bench:   1,173,651 ns/iter (+/- 69,909)
test validate     ... bench:         566 ns/iter (+/- 5)

new_seed is slower because the pbkdf2 crate is slower than the C (AFAIK?) implementation that ring binds to. This is probably quite fine, and having a pure Rust implementation might be beneficial for me since I want to target multiple architectures (Android and iOS specifically).

The other degradation I'm not sure about. Things I've checked thus far:

I might try to go at it from the other end - start at my branch and slowly re-introduce changes made here to see if I can figure out where the degradation happens.

Edit: it has to be sha2 crate, since all functions have to grab the first byte of the digest from it.

QuestofIranon commented 5 years ago

The sha2 crate has an asm feature. Using the asm implementation sees a better performance boost. I disabled it by default because users were having trouble building another crate that depended on my fork in Windows and anyone who wants that improvement can enable it in their project.

pbkdf2 is likely the main culprit of the performance drop, but it would probably be better to focus on improving the performance of that crate which would benefit other crates using it as well.

QuestofIranon commented 5 years ago

I should mention that one of my intentions with the changes in this PR was for better support across platforms, acknowledging the performance cost.

maciejhirsz commented 5 years ago

That makes sense. The performance degradation isn't bad at all (and it's still way faster than what it used to be prior to #14) so that seems like a reasonable trade-off.

QuestofIranon commented 5 years ago

I made another change that makes the non-English languages compile time features, though I left them all enabled by default. I thought this would be nice as an optional enhancement because it reduces the size of the compiled library by almost half if you disable the additional languages.

With default features disabled (only English) the size of the .rlib is 664 KB compared to 1390 KB with all languages.

The main downside is that currently English is always included even if you specify other languages. I needed a default language due to the current implementation and it is the standard language defined in bip. I might look into ways around this but thought it might be worth discussing first. @maciejhirsz @steveatinfincia

maciejhirsz commented 5 years ago

@steveatinfincia can we get this merged, pretty please? :heart:

steveatinfincia commented 5 years ago

@steveatinfincia can we get this merged, pretty please? ❤️

Yep, was just giving some room for the earlier discussion to play out :)

I'll merge and tag a beta release for Cargo tonight or tomorrow morning.

steveatinfincia commented 5 years ago

This is released on Cargo now as the v0.6.0-beta.1 tag