keccak: add asm feature; use cpufeatures on aarch64#24
Conversation
1fb975f to
9f2fc58
Compare
Gates `asm` support under a crate feature. Uses the `cpufeatures` crate to detect the presence of the `sha3` extension for ARMv8 CPUs, automatically falling back to a software implementation if it isn't available.
9f2fc58 to
2af43c0
Compare
| [target.'cfg(target_arch = "aarch64")'.dependencies] | ||
| cpufeatures = "0.2" |
There was a problem hiding this comment.
It's a bit unfortunate this is a hard dependency on aarch64, although I couldn't figure out how to gate it better
There was a problem hiding this comment.
What about gating on feature or configuration flag inside the cfg statement?
There was a problem hiding this comment.
This doesn't work:
[target.'cfg(all(target_arch = "aarch64", feature = "asm"))'.dependencies]
cpufeatures = "0.2"It prints this warning:
warning: Found
feature = ...intarget.'cfg(...)'.dependencies. This key is not supported for selecting dependencies and will not work as expected. Use the [features] section instead: https://doc.rust-lang.org/cargo/reference/features.html
This however, seems to work:
[target.'cfg(all(keccak_asm, target_arch = "aarch64"))'.dependencies]
cpufeatures = "0.2"So that's a possible argument for using a cfg attribute for gating rather than a Cargo feature.
| readme = "README.md" | ||
|
|
||
| [features] | ||
| asm = [] # Use optimized assembly when available (currently only ARMv8) |
There was a problem hiding this comment.
Should this perhaps be a cfg! attribute rather than a crate feature?
There was a problem hiding this comment.
Hm, I am not sure. Application developers would probably want to have it enabled by default without any additional steps from users. It's a slightly different situation from the aes crate where configuration flags are used mostly for testing backends.
| pub use aarch64_sha3::keccak_f1600 as f1600; | ||
| #[cfg(all(target_arch = "aarch64", feature = "asm"))] | ||
| pub fn f1600(state: &mut [u64; PLEN]) { | ||
| if armv8_sha3_intrinsics::get() { |
There was a problem hiding this comment.
@newpavlov there's not really a way to make this interface work with an init token. Does it seem ok to you?
There was a problem hiding this comment.
Yeah, it's not ideal, but should be fine. One potential alternative is to expose an unsafe gated f1600_aarch64_asm function and do the switch inside sha3.
There was a problem hiding this comment.
Sure, that works. I can update the PR.
There was a problem hiding this comment.
I am not saying a separate function is a better approach. Just floating an idea.
There was a problem hiding this comment.
It seems nice to have to me, especially for the sha3 use case.
It's unsafe and #[target_feature(enable = "sha3")]-gated, which should prevent casual misuse.
|
After the discussion about crate features vs I guess I'm weakly in favor of a feature just for consistency with how |
When the `asm` feature is enabled on `aarch64` targets, exposes `f1600_asm` that relies on ARMv8 `sha3` hardware intrinsics.
|
8e40212 makes |
|
Gonna merge this. @newpavlov will wait for your approval for a release |
|
I think |
...but this is the XKCP ASM implementation. I can add target info to the name though. Edit: opened #26.
But if you're thinking of having an intrinsics implementation, it would be ambiguous? Hence why to include
|
I think it's nice to have the default API optionally accelerated for non-SHA-3 applications of Keccak.
|
I don't think so. Ideally, we would use intrinsics here, but since they are currently unstable we rely on |
The problem is that it makes |
|
That's going to be a problem with |
|
Yes, but at least |
|
...at which point the concern about a hard dependency on I'm confused what you're concerned about. IMO the main problem now is there's a dependency on The only alternative to using The reason for doing it in |
|
Ah... I thought you've removed runtime detection in My idea was to completely remove runtime detection and let user crates (such as In other words, I am fine with either one of the following options:
But not with a mix of them. |
|
The current configuration permits the following:
I don't see a disadvantage of this approach, especially with |
|
Saying that you should use |
|
But the point of having an The point of using Perhaps we should just punt on exposing it at all and leave all of the feature detection in the |
Yes, it's the first option listed earlier. |
Transitively enables the `asm` feture in the `keccak` crate which was added in RustCrypto/sponges#24.
Gates
asmsupport (added in #23) under a crate feature.Uses the
cpufeaturescrate to detect the presence of thesha3extension for ARMv8 CPUs, automatically falling back to a software implementation if it isn't available.cc @recmo