-
Notifications
You must be signed in to change notification settings - Fork 3
Use tiny-keccak instead of wrapping the one-file C-lib #1
base: master
Are you sure you want to change the base?
Conversation
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 switch it over to use tiny-keccak
all-together? Performance?
Imo, we can switch to tiny-keccak as rust becomes faster with almost every single release. Yes, it was only because of the performance.
I'm just not sure about creating this repo to separate keccak-hash
crate. It seems a bit redundant to me :p
I agree that without the C shenanigans it's mostly a collection of utilities. Atm When that work is completed and So, just to make sure, can I merge this? /cc @rphmeier |
@folsen it is indeed used in many places but most of the time it seems like it's used in vicinity to db lookup code so it's quite possible that 480ns vs 520ns might not matter much. I'm happy to do some more sleuthing to prove/disprove that ofc (do you know of a good piece of code I should start looking at? where we're |
Not unrolling the outer loop seems to speed up hashing quite significally: Original (unrolled): ``` running 3 tests test bench_keccak_256_with_empty_input ... bench: 557 ns/iter (+/- 46) test bench_keccak_256_with_large_input ... bench: 17,288 ns/iter (+/- 1,871) = 236 MB/s test bench_keccak_256_with_typical_input ... bench: 577 ns/iter (+/- 28) = 88 MB/s ``` This branch (not unrolled): ``` running 3 tests test bench_keccak_256_with_empty_input ... bench: 487 ns/iter (+/- 25) test bench_keccak_256_with_large_input ... bench: 14,645 ns/iter (+/- 675) = 279 MB/s test bench_keccak_256_with_typical_input ... bench: 495 ns/iter (+/- 32) = 103 MB/s ``` "Inspired" by https://github.com/RustCrypto/sponges/blob/master/keccak/src/lib.rs#L138 Running benchmarks from the `keccak-hash` crate so we can compare to the numbers [here](paritytech/keccak-hash#1).
@dvdplm keccak512 is used in ethash, so is called like a thousand times to validate a block, and block validation actually consumes a lot during sync. keccak256 also used in the EcRecover builtin so can potentially be used by any smart contract however many times they want. The big thing I'd say is ethash though, show me a benchmark of running ethash 5 million times with the C impl vs the Rust impl and if it only adds a few minutes then I'd say it's acceptable ^^ |
Here's what the benchmarks in Pure rust keccak (with this patch)
Original C-impl
The variability for both implementations is big, which is somewhat disturbing, but if the numbers measure anything useful then I'd say they perform very similarly. As a sanity check I ran |
Shameless plug: consider moving to Performance will be the same (after |
@dvdplm yeah, the difference in ethash really doesn't look that bad, you've convinced me :P |
…cked – mostly for compatibility with ethash
@debris the background here is that during work on extracting the
patricia_trie
crate and making it usable with different hashing algorithms, I realised that this crate was using both the in-repo one-file-C-lib (and unsafe code to call it) and your pure Rust impl.Why not switch it over to use
tiny-keccak
all-together? Performance?Benchmarks
Original code, implements
keccak()
in C:This branch, implements
keccak()
in Rust: