-
Notifications
You must be signed in to change notification settings - Fork 1.7k
replace trait Hashable with fn keccak #6423
Conversation
This is just a renaming pass and still uses the C implementation, right? There is unsafe code that relies on the precise semantics of the C SHA3 (specifically, that it uses an internal buffer and so the input and output arrays can be the same/overlapping). I think that it might be better if we don't do that (and use the output buffer directly to reduce |
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.
There's some suspicious stuff here but I don't know enough about it to explicitly reject it.
util/hash/src/lib.rs
Outdated
pub fn keccak_into<T: AsRef<[u8]>>(s: T, dest: &mut [u8]) { | ||
let input = s.as_ref(); | ||
unsafe { | ||
keccak_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len()); |
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 do we ignore the return value of keccak_256
? Probably at least worth putting a comment explaining that.
util/network/src/connection.rs
Outdated
nonce_material.sha3_into(&mut key_material[32..64]); | ||
key_material.sha3().copy_to(&mut key_material[32..64]); | ||
key_material.sha3().copy_to(&mut key_material[32..64]); | ||
keccak_into(&nonce_material, &mut key_material[32..64]); |
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.
Is this really more ergonomic? What's wrong with using a trait here? I wouldn't have an opinion either way if this was original code but since it's the stated purpose of the PR it deserves analysis.
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.
It's one of the purposes :) imo, there is no point in using the trait if simple function is enough
util/hash/src/lib.rs
Outdated
H256(result) | ||
} | ||
|
||
pub fn keccak_into<T: AsRef<[u8]>>(s: T, dest: &mut [u8]) { |
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.
Bikeshed: _into
implies taking ownership of T
's data and transforming it into something rather than writing it to a buffer, this confused me when first reading this code. I recommend write_keccak
, but it's a matter of taste. I think it's good to avoid names that already have a common, conflicting use in Rust code.
Yes. Cause the C implementation is still ~10% faster than we achieved with rust.
I'm not sure I understand your point. Do you want to return internal buffer from keccak function? It's not possible with this type of hash function. |
Essentially all I'm saying is that some unsafe code relies on the hash function allowing input and output to overlap, so as long as that still holds we're all good. It looks like it does, I just wanted to make certain. |
Yes, nothing has changed internally |
ethash/src/compute.rs
Outdated
@@ -336,7 +336,7 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) | |||
); | |||
|
|||
// compute sha3-512 hash and replicate across mix |
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.
s/sha3/keccak ?
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 util/rlp/src/common.rs
there's some references to SHA3_NULL_RLP
and SHA3_EMPTY
, I'm guessing they should be changed.
ethash/src/compute.rs
Outdated
} | ||
|
||
fn sha3_512_inplace(input: &mut [u8]) { | ||
fn keccak_512_inplace(input: &mut [u8]) { | ||
// This is safe since `sha3_*` uses an internal buffer and copies the result to the output. This |
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.
s/sha3/keccak
LGTM now that grumbles have been addressed |
part of #6418
changes:
SHA3_XXX
toKECCAK_XXX
TransactionView
,BlockView
andHeaderView
has nowpub fn hash()
which returns their hash. Usage of it does not require import ofHashable
traittrait Hashable
fn keccak
. Using function is more idiomatic. Also it's more flexible and can be easily used with options and iterators. We can now write:fn keccak
is no longer a part ofutil
. This is a critical step required to make modules independent@paritytech/core-devs can you please review the code? The sooner we agree on those changes and merge them, the less hassle it will be to keep it up-to-date