-
Notifications
You must be signed in to change notification settings - Fork 70
Expose const_choice.rs functions #994
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
Open
ycscaly
wants to merge
1
commit into
RustCrypto:master
Choose a base branch
from
ycscaly:expose-const-choice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ impl ConstChoice { | |
| /// Returns the truthy value if `value == Word::MAX`, and the falsy value if `value == 0`. | ||
| /// Panics for other values. | ||
| #[inline] | ||
| pub(crate) const fn from_word_mask(value: Word) -> Self { | ||
| pub const fn from_word_mask(value: Word) -> Self { | ||
| debug_assert!(value == Self::FALSE.0 || value == Self::TRUE.0); | ||
| Self(value) | ||
| } | ||
|
|
@@ -88,7 +88,7 @@ impl ConstChoice { | |
|
|
||
| /// Returns the truthy value if `value != 0`, and the falsy value otherwise. | ||
| #[inline] | ||
| pub(crate) const fn from_u32_nonzero(value: u32) -> Self { | ||
| pub const fn from_u32_nonzero(value: u32) -> Self { | ||
| Self::from_u32_lsb((value | value.wrapping_neg()) >> (u32::BITS - 1)) | ||
| } | ||
|
|
||
|
|
@@ -132,7 +132,7 @@ impl ConstChoice { | |
|
|
||
| /// Returns the truthy value if `x < y`, and the falsy value otherwise. | ||
| #[inline] | ||
| pub(crate) const fn from_u32_lt(x: u32, y: u32) -> Self { | ||
| pub const fn from_u32_lt(x: u32, y: u32) -> Self { | ||
| // See "Hacker's Delight" 2nd ed, section 2-12 (Comparison predicates) | ||
| let bit = (((!x) & y) | (((!x) | y) & (x.wrapping_sub(y)))) >> (u32::BITS - 1); | ||
| Self::from_u32_lsb(bit) | ||
|
|
@@ -156,7 +156,7 @@ impl ConstChoice { | |
|
|
||
| /// Returns the truthy value if `x <= y` and the falsy value otherwise. | ||
| #[inline] | ||
| pub(crate) const fn from_u32_le(x: u32, y: u32) -> Self { | ||
| pub const fn from_u32_le(x: u32, y: u32) -> Self { | ||
| // See "Hacker's Delight" 2nd ed, section 2-12 (Comparison predicates) | ||
| let bit = (((!x) | y) & ((x ^ y) | !(y.wrapping_sub(x)))) >> (u32::BITS - 1); | ||
| Self::from_u32_lsb(bit) | ||
|
|
@@ -169,38 +169,38 @@ impl ConstChoice { | |
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn not(&self) -> Self { | ||
| pub const fn not(&self) -> Self { | ||
| Self(!self.0) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn or(&self, other: Self) -> Self { | ||
| pub const fn or(&self, other: Self) -> Self { | ||
| Self(self.0 | other.0) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn and(&self, other: Self) -> Self { | ||
| pub const fn and(&self, other: Self) -> Self { | ||
| Self(self.0 & other.0) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn xor(&self, other: Self) -> Self { | ||
| pub const fn xor(&self, other: Self) -> Self { | ||
| Self(self.0 ^ other.0) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn ne(&self, other: Self) -> Self { | ||
| pub const fn ne(&self, other: Self) -> Self { | ||
| Self::xor(self, other) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn eq(&self, other: Self) -> Self { | ||
| pub const fn eq(&self, other: Self) -> Self { | ||
| Self::ne(self, other).not() | ||
| } | ||
|
|
||
| /// Return `b` if `self` is truthy, otherwise return `a`. | ||
| #[inline] | ||
| pub(crate) const fn select_word(&self, a: Word, b: Word) -> Word { | ||
| pub const fn select_word(&self, a: Word, b: Word) -> Word { | ||
| a ^ (self.0 & (a ^ b)) | ||
| } | ||
|
|
||
|
|
@@ -213,7 +213,7 @@ impl ConstChoice { | |
|
|
||
| /// Return `b` if `self` is truthy, otherwise return `a`. | ||
| #[inline] | ||
| pub(crate) const fn select_u32(&self, a: u32, b: u32) -> u32 { | ||
| pub const fn select_u32(&self, a: u32, b: u32) -> u32 { | ||
| a ^ (self.as_u32_mask() & (a ^ b)) | ||
| } | ||
|
|
||
|
|
@@ -229,6 +229,12 @@ impl ConstChoice { | |
| a ^ (self.as_u64_mask() & (a ^ b)) | ||
| } | ||
|
|
||
| /// Swap `a` and `b` if `self` is truthy. Otherwise do nothing. | ||
| #[inline] | ||
| pub const fn conditional_swap_u32(&self, a: &mut u32, b: &mut u32) { | ||
| (*a, *b) = (self.select_u32(*a, *b), self.select_u32(*b, *a)) | ||
| } | ||
|
|
||
| /// Return `x` if `self` is truthy, otherwise return 0. | ||
| #[inline] | ||
| pub(crate) const fn if_true_word(&self, x: Word) -> Word { | ||
|
|
@@ -253,7 +259,7 @@ impl ConstChoice { | |
|
|
||
| /// WARNING: this method should only be used in contexts that aren't constant-time critical! | ||
| #[inline] | ||
| pub(crate) const fn to_bool_vartime(self) -> bool { | ||
| pub const fn to_bool_vartime(self) -> bool { | ||
| self.to_u8() != 0 | ||
| } | ||
| } | ||
|
|
@@ -303,20 +309,20 @@ pub struct ConstCtOption<T> { | |
|
|
||
| impl<T> ConstCtOption<T> { | ||
| #[inline] | ||
| pub(crate) const fn new(value: T, is_some: ConstChoice) -> Self { | ||
| pub const fn new(value: T, is_some: ConstChoice) -> Self { | ||
| Self { value, is_some } | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn some(value: T) -> Self { | ||
| pub const fn some(value: T) -> Self { | ||
| Self { | ||
| value, | ||
| is_some: ConstChoice::TRUE, | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| pub(crate) const fn none(dummy_value: T) -> Self { | ||
| pub const fn none(dummy_value: T) -> Self { | ||
| Self { | ||
| value: dummy_value, | ||
| is_some: ConstChoice::FALSE, | ||
|
|
@@ -327,7 +333,7 @@ impl<T> ConstCtOption<T> { | |
| /// | ||
| /// **Note:** if the second element is `None`, the first value may take any value. | ||
| #[inline] | ||
| pub(crate) const fn components_ref(&self) -> (&T, ConstChoice) { | ||
| pub const fn components_ref(&self) -> (&T, ConstChoice) { | ||
| // Since Rust is not smart enough to tell that we would be moving the value, | ||
| // and hence no destructors will be called, we have to return a reference instead. | ||
| // See https://github.com/rust-lang/rust/issues/66753 | ||
|
|
@@ -359,7 +365,7 @@ impl<T> ConstCtOption<T> { | |
|
|
||
| /// Apply an additional [`ConstChoice`] requirement to `is_some`. | ||
| #[inline] | ||
| pub(crate) const fn and_choice(mut self, is_some: ConstChoice) -> Self { | ||
| pub const fn and_choice(mut self, is_some: ConstChoice) -> Self { | ||
| self.is_some = self.is_some.and(is_some); | ||
| self | ||
| } | ||
|
|
@@ -429,6 +435,20 @@ impl<const LIMBS: usize> ConstCtOption<(Uint<LIMBS>, Uint<LIMBS>)> { | |
| } | ||
| } | ||
|
|
||
| impl<const LIMBS: usize> ConstCtOption<(Uint<LIMBS>, ConstChoice)> { | ||
| /// Returns the contained value, consuming the `self` value. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if the value is none with a custom panic message provided by | ||
| /// `msg`. | ||
| #[inline] | ||
| pub const fn expect(self, msg: &str) -> (Uint<LIMBS>, ConstChoice) { | ||
| assert!(self.is_some.is_true_vartime(), "{}", msg); | ||
| self.value | ||
| } | ||
| } | ||
|
Comment on lines
+438
to
+450
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this is related to |
||
|
|
||
| impl<const LIMBS: usize> ConstCtOption<NonZero<Uint<LIMBS>>> { | ||
| /// Returns the contained value, consuming the `self` value. | ||
| /// | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only one I'm a bit wary of exposing. Notably it exposes the inner
Twhich may be in an invalid state.