-
Notifications
You must be signed in to change notification settings - Fork 151
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
feat: native keccak feature flag #185
Conversation
457a361
to
2b92471
Compare
This is really nice, thank you! pub fn keccak256<T: AsRef<[u8]>>(bytes: T) -> FixedBytes<32> {
cfg_if::cfg_if! {
if #[cfg(feature = "tiny-keccak")] {
fn keccak256 ...
} else if #[cfg(feature = "native-keccak")] {
extern "C" {
...
}
fn keccak256 ...
} else {
compile_error!("At least one Keccak-256 feature must be enabled")
}
}
keccak256(bytes.as_ref())
} This would also allow us to also expand this in the future with more Keccak implementations. Would you be willing to implement this? |
@DaniPopes is this something that could/should be addressed with a "global keccak backend" abstraction? seems like overkill for now 🤔 |
I'm happy to go any route y'all think. Other options include
|
We want to gate the tiny_keccak dep in case of this new feature, so you end up with 2 backends if you enable both features with |
@DaniPopes edited my last comment for clarity: what I meant to say was that there's an in-between option where with no features enabled you use the native keccak hook, but replace it with I have no strong feelings on the mechanism and am happy to implement whichever solution y'all think is best :) EDIT I'm seeing that there's also a |
On further reflection we have a few issues
|
Thanks everyone for your help with this. I've included a commit to add a Importantly, neither But because neither are enabled by default, there's no worry that smart contract devs might accidentally stop using I think it's a win-win :) |
SGTM. I think it's still better we use |
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.
LGTM
Background
As outlined in Paradigm's introduction of Alloy, avoiding fragmentation of the Rust ecosystem on Ethereum means that everyone should use a consistent set of primitive types. As Rust expands into new Ethereum domains like smart contract development, generality will be key for this shared dependency.
The
alloy_primatives
crate included here does this very well, but could be made even better for upcoming Rust-enabled Ethereum smart contract environments like Arbitrum Stylus.In addition to providing types, the crate currently pulls in
tiny_keccak
. This makes sense for most existing platforms, but in code running on top of VMs it's reasonable to accelerate common operations through so-called "host I/Os" or "traps". For example, the Stylus model includes a trap forkeccak256
.Because host I/O operations run the function natively, we can get the native performance of
tiny_keccak
in a VM setting without any of the contribution to binary size. Though the example here is from Stylus, this reasoning applies to other VMs that might want to add hooks for operations like keccak. Anyone can expose a hook in their VM fulfillingnative_keccak256
above and accelerate hashing.Solution
This small PR introduces a new feature flag,
native-keccak
, that when enabled substitutestiny_keccak
for anextern
import. Since the feature is not enabled by default, this change is 100% backward compatible.Though the changes are to private functions, I've tried to include exhaustive documentation. Please let me know if there's anything I've missed in opening this PR and I'll be happy to make changes.