From 855cdd4db8ee32bc10a4a2da32345127d712b2d8 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Thu, 1 Feb 2024 17:48:01 -0700 Subject: [PATCH 1/2] Add `CtOption::into_option` A small wrapper for `impl CtOption for Option` which is friendlier for type inference purposes. Using `Option::from(ct_option)` often doesn't work because the compiler is unable to infer `T` for `Option`, so you have to manually annotate `Option::::from`. In practice this often winds up looking like: Option::::from(T::constructor(x))` which feels quite redundant. Using an inherent method instead fixes type inference so the above can be: T::constructor(x).into_option() --- src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 795eade..6b0042d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -801,6 +801,22 @@ impl CtOption { Self::conditional_select(&self, &f, is_none) } + + /// Convert the `CtOption` wrapper into an `Option`, depending on whether + /// the underlying `is_some` `Choice` was a `0` or a `1` once unwrapped. + /// + /// # Note + /// + /// This function exists to avoid ending up with ugly, verbose and/or bad handled + /// conversions from the `CtOption` wraps to an `Option` or `Result`. + /// This implementation doesn't intend to be constant-time nor try to protect the + /// leakage of the `T` since the `Option` will do it anyways. + /// + /// It's equivalent to the corresponding `From` impl, however this version is is + /// friendlier for type inference. + pub fn into_option(self) -> Option { + self.into() + } } impl ConditionallySelectable for CtOption { From 4ffd51d4e56a698bfa16158ad2340c79cfbf348b Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Thu, 8 Feb 2024 09:28:15 -0700 Subject: [PATCH 2/2] Update src/lib.rs Co-authored-by: Aaron Feickert <66188213+AaronFeickert@users.noreply.github.com> --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6b0042d..1aba339 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -812,7 +812,7 @@ impl CtOption { /// This implementation doesn't intend to be constant-time nor try to protect the /// leakage of the `T` since the `Option` will do it anyways. /// - /// It's equivalent to the corresponding `From` impl, however this version is is + /// It's equivalent to the corresponding `From` impl, however this version is /// friendlier for type inference. pub fn into_option(self) -> Option { self.into()