-
Notifications
You must be signed in to change notification settings - Fork 150
ctutils: CtOption methods for crypto-bigint
#1274
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
Merged
Merged
+166
−2
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tarcieri
added a commit
to RustCrypto/crypto-bigint
that referenced
this pull request
Dec 26, 2025
Analogous to what #1035 did for `ConstChoice`, this extracts the necessary methods onto `ctutils::CtOption` then replaces the type wholesale using the implementation in `ctutils`. There were a few tricky parts, namely we had type-specific implementations of `expect` and `unwrap_or`. It is actually possible to write a generic `const fn` implementation of `CtOption::expect`, avoiding the problems with destructors and the unstable `Destruct` trait, by bounding on `T: Copy` and returning a copy of the inner value. This is implemented as `CtOption::expect_copied` so we can still support `expect` for non-`Copy` types. Unfortunately there's no analogous generic `const fn` solution for `CtOption::unwrap_or`, as this needs a constant-time selector function and even function pointers can't be used from a `const fn`. So this does the only thing we can do to replace it with some level of abstraction: a `ctutils::unwrap_or!` macro. See also: RustCrypto/utils#1274 which extracts some needed methods onto `ctutils::CtOption`.
09de9c9 to
adbc47d
Compare
tarcieri
commented
Dec 27, 2025
tarcieri
commented
Dec 27, 2025
tarcieri
added a commit
to RustCrypto/crypto-bigint
that referenced
this pull request
Dec 27, 2025
Analogous to what #1035 did for `ConstChoice`, this extracts the necessary methods onto `ctutils::CtOption` then replaces the type wholesale using the implementation in `ctutils`. There were a few tricky parts, namely we had type-specific implementations of `expect` and `unwrap_or`. It is actually possible to write a generic `const fn` implementation of `CtOption::expect`, avoiding the problems with destructors and the unstable `Destruct` trait, by bounding on `T: Copy` and returning a copy of the inner value. This is implemented as `CtOption::expect_copied` so we can still support `expect` for non-`Copy` types. Unfortunately there's no analogous generic `const fn` solution for `CtOption::unwrap_or`, as this needs a constant-time selector function and even function pointers can't be used from a `const fn`. So this does the only thing we can do to replace it with some level of abstraction: a `ctutils::unwrap_or!` macro. See also: RustCrypto/utils#1274 which extracts some needed methods onto `ctutils::CtOption`.
adbc47d to
a6953c3
Compare
tarcieri
added a commit
to RustCrypto/crypto-bigint
that referenced
this pull request
Dec 27, 2025
Analogous to what #1035 did for `ConstChoice`, this extracts the necessary methods onto `ctutils::CtOption` then replaces the type wholesale using the implementation in `ctutils`. There were a few tricky parts, namely we had type-specific implementations of `expect` and `unwrap_or`. It is actually possible to write a generic `const fn` implementation of `CtOption::expect`, avoiding the problems with destructors and the unstable `Destruct` trait, by bounding on `T: Copy` and returning a copy of the inner value. This is implemented as `CtOption::expect_copied` so we can still support `expect` for non-`Copy` types. Unfortunately there's no analogous generic `const fn` solution for `CtOption::unwrap_or`, as this needs a constant-time selector function and even function pointers can't be used from a `const fn`. So this does the only thing we can do to replace it with some level of abstraction: a `ctutils::unwrap_or!` macro. See also: RustCrypto/utils#1274 which extracts some needed methods onto `ctutils::CtOption`.
a6953c3 to
6fc9a27
Compare
CtOption methods for crypto-bigintCtOption methods for crypto-bigint
6fc9a27 to
dd072fc
Compare
Adds the methods needed to replace `ConstCtOption` in `crypto-bigint` with `ctutils::CtOption`: - `some` - `none` - `into_option_copied` - `and_choice` - `as_inner_unchecked` - `to_inner_unchecked` Also adds a `Default` impl for `CtOption`. Also adds `map!` and `unwrap_or!` macros to provide `const fn`-friendly combinator-like functionality.
dd072fc to
a366a2a
Compare
tarcieri
added a commit
to RustCrypto/crypto-bigint
that referenced
this pull request
Dec 27, 2025
Analogous to what #1035 did for `ConstChoice`, this extracts the necessary methods onto `ctutils::CtOption` then replaces the type wholesale using the implementation in `ctutils`. There were a few tricky parts, namely we had type-specific implementations of `expect` and `unwrap_or`. It is actually possible to write a generic `const fn` implementation of `CtOption::expect`, avoiding the problems with destructors and the unstable `Destruct` trait, by bounding on `T: Copy` and returning a copy of the inner value. This is implemented as `CtOption::expect_copied` so we can still support `expect` for non-`Copy` types. Unfortunately there's no analogous generic `const fn` solution for `CtOption::unwrap_or`, as this needs a constant-time selector function and even function pointers can't be used from a `const fn`. So this does the only thing we can do to replace it with some level of abstraction: a `ctutils::unwrap_or!` macro. See also: RustCrypto/utils#1274 which extracts some needed methods onto `ctutils::CtOption`.
tarcieri
added a commit
to RustCrypto/crypto-bigint
that referenced
this pull request
Dec 27, 2025
Analogous to what #1035 did for `ConstChoice`, this extracts the necessary methods onto `ctutils::CtOption` then replaces the type wholesale using the implementation in `ctutils`. There were a few tricky parts, namely we had type-specific implementations of `expect` and `unwrap_or`. It is actually possible to write a generic `const fn` implementation of `CtOption::expect`, avoiding the problems with destructors and the unstable `Destruct` trait, by bounding on `T: Copy` and returning a copy of the inner value. This is implemented as `CtOption::expect_copied` so we can still support `expect` for non-`Copy` types. Unfortunately there's no analogous generic `const fn` solution for `CtOption::unwrap_or`, as this needs a constant-time selector function and even function pointers can't be used from a `const fn`. So this does the only thing we can do to replace it with some level of abstraction: a `ctutils::unwrap_or!` macro. See also: RustCrypto/utils#1274 which extracts some needed methods onto `ctutils::CtOption`.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds the methods needed to replace
ConstCtOptionincrypto-bigintwithctutils::CtOption:somenoneinto_option_copiedand_choiceas_inner_uncheckedto_inner_uncheckedAlso adds a
Defaultimpl forCtOption.Also adds
map!andunwrap_or!macros to provideconst fn-friendly combinator-like functionality.