Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Dec 26, 2025

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.

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`.
@tarcieri tarcieri force-pushed the ctutils/ct-option-methods-for-crypto-bigint branch from 09de9c9 to adbc47d Compare December 27, 2025 00:18
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 tarcieri force-pushed the ctutils/ct-option-methods-for-crypto-bigint branch from adbc47d to a6953c3 Compare December 27, 2025 01:23
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 tarcieri force-pushed the ctutils/ct-option-methods-for-crypto-bigint branch from a6953c3 to 6fc9a27 Compare December 27, 2025 01:37
@tarcieri tarcieri changed the title [WIP] ctutils: CtOption methods for crypto-bigint ctutils: CtOption methods for crypto-bigint Dec 27, 2025
@tarcieri tarcieri force-pushed the ctutils/ct-option-methods-for-crypto-bigint branch from 6fc9a27 to dd072fc Compare December 27, 2025 01:45
@tarcieri tarcieri marked this pull request as ready for review December 27, 2025 01:46
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.
@tarcieri tarcieri force-pushed the ctutils/ct-option-methods-for-crypto-bigint branch from dd072fc to a366a2a Compare December 27, 2025 01:55
@tarcieri tarcieri merged commit 533ac35 into master Dec 27, 2025
11 checks passed
@tarcieri tarcieri deleted the ctutils/ct-option-methods-for-crypto-bigint branch December 27, 2025 01:56
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants