-
Notifications
You must be signed in to change notification settings - Fork 13
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
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about gating on feature or configuration flag inside the cfg
statement?
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.
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.
@@ -14,5 +14,9 @@ categories = ["cryptography", "no-std"] | |||
readme = "README.md" | |||
|
|||
[features] | |||
asm = [] # Use optimized assembly when available (currently only ARMv8) |
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.
Should this perhaps be a cfg!
attribute rather than a crate feature?
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that works. I can update the PR.
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.
I am not saying a separate function is a better approach. Just floating an idea.
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 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
asm
support (added in #23) under a crate feature.Uses the
cpufeatures
crate to detect the presence of thesha3
extension for ARMv8 CPUs, automatically falling back to a software implementation if it isn't available.cc @recmo