[WIP] ctutils: constant-time selection and equality testing #1243
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(for lack of a better name)
This is woefully incomplete but I'm pushing it up anyway since several people have asked about
const fnsupport forsubtle.This is effectively a rewrite of
subtleusing thecmovcrate for both constant-time selection/predication as well as equality comparisons. Thecmovcrate uses architecture-specific predication instructions on x86(_64) and ARM, with a portable "best effort" fallback.It uses
core::hint::black_boxon-access as an optimization barrier, however this is a belt-and-suspenders defense paired with the use of intrinsics where available. This is a bit different thansubtlewhich uses a similar black box optimization barrier at initialization time. There are a couple problems with this approach:Choice, which means it could potentially insert a branch to e.g. shortcut-on-zeroblack_boxis (rather annoyingly) onlyconst fnin Rust 1.86. This is targeting an initial MSRV of 1.85, as well as supportingconst fnconstructors forChoicewhich are a big missing piece insubtleright nowI'm not intending to replace our usages of
subtlewith this yet (I'd much rather ship everything), but would like to have a testbed for usingcmovfor constant-time operations which can perhaps inform a potentialsubtlev3.0 (if I can make that happen).To be useful, this still needs an equivalent of
CtOption(ideally with much moreconst fnsupport), which I was hoping to implement before pushing this up.One thing we could consider is trying to get this complete enough to use in
crypto-bigintto replaceConstChoice/ConstCtOption, though it would likely need all of the methods onChoiceto beconst fn, which would probably involve shippingChoicewithoutblack_box(i.e. whatcrypto-bigintis already doing), and then adding asubtleintegration for convertingctutil::Choice->subtle::Choiceand a prospectivectutil::CtOption->subtle::CtOption.cc @andrewwhitehead @fjarri @ycscaly