-
Notifications
You must be signed in to change notification settings - Fork 150
ctutils: constant-time selection and equality testing #1243
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
Conversation
|
So why not just bump the MSRV to 1.86? |
|
@fjarri we're trying to ship everything as 1.85 so it can be packaged on Debian stable, then bumping MSRV after that (now that there's an MSRV-aware resolver) That said, there are several places post-1.85 features would be nice in |
ab9d02c to
9511556
Compare
|
Ugh, |
Inspired by the `subtle` crate, this is a next-generation constant time utility crate built on the `cmov` crate's constant-time selection/predication and equality comparisons, which are exposed as `CtSelect` and `CtEq` traits (equivalent to `ConditionallySelectable` and `ConstantTimeEq` in `subtle`). Additionally, it uses `black_box` as a "best effort" optimization barrier whenever accessing its constant time conditional type (called `Choice` as in `subtle`), providing an additional line of defense against possible future compiler optimizations. It also has a `CtOption` type like `subtle` which provides a constant-time equivalent to `Option` but with combinators that are evaluated eagerly rather than lazily so they behave the same regardless of the effective absence or presence of the underlying value. It proactively makes functions `const fn` wherever possible, making it possible to construct and use `Choice` and `CtOption` in these contexts. The `Copy` bounds have been removed, making it possible for everything to be used on heap-allocated types, such as `BoxedUint` in `crypto-bigint`. The above two issues taken together are the main reasons why `crypto-bigint` currently embeds its own mini-`subtle` alike, which it would be nice to use this library to replace.
|
I'd like to finally push this one past the draft PR it's been sitting in for ages and land it and iterate on new features/functionality. I'm going to go ahead and merge but I'd appreciate any retroactive review. |
(for lack of a better name)
This is woefully incomplete but I'm pushing it up anyway since several people have asked aboutconst fnsupport forsubtleThis 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.85This PR is MSRV 1.87 but we'll have a revertable downgrade to 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 ofAdded!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