-
Notifications
You must be signed in to change notification settings - Fork 57
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
Int
- Bit ops
#697
Merged
Merged
Int
- Bit ops
#697
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
50f2a43
Create `Int::bit_and`
erik-3milabs 974e080
Create `Int::bit_or`
erik-3milabs f831f0d
Create `Int::bit_xor`
erik-3milabs a9a86cc
Create `Int::bit_not`
erik-3milabs 0302d9e
Implement `Int::shl`
erik-3milabs 075c179
Implement `Int::shr`
erik-3milabs 8e3b710
Remove superfluous `.into`
erik-3milabs 6dfaf06
Fix `Int::as_int`
erik-3milabs 5bf36da
Reintroduce `Int::select`
erik-3milabs d11fe05
Fix `Int::shr`
erik-3milabs 95f207d
Make `Int`'s checked bitops const
erik-3milabs dd5b917
Fix fmt
erik-3milabs 56a3d8f
Make `Int::from_i32` const
erik-3milabs 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 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//! [`Int`] bitwise AND operations. | ||
|
||
use core::ops::{BitAnd, BitAndAssign}; | ||
|
||
use crate::{ConstCtOption, Int, Limb, Uint, Wrapping}; | ||
|
||
impl<const LIMBS: usize> Int<LIMBS> { | ||
/// Computes bitwise `a & b`. | ||
#[inline(always)] | ||
pub const fn bitand(&self, rhs: &Self) -> Self { | ||
Self(Uint::bitand(&self.0, &rhs.0)) | ||
} | ||
|
||
/// Perform bitwise `AND` between `self` and the given [`Limb`], performing the `AND` operation | ||
/// on every limb of `self`. | ||
pub const fn bitand_limb(&self, rhs: Limb) -> Self { | ||
Self(Uint::bitand_limb(&self.0, rhs)) | ||
} | ||
|
||
/// Perform wrapping bitwise `AND`. | ||
/// | ||
/// There's no way wrapping could ever happen. | ||
/// This function exists so that all operations are accounted for in the wrapping operations | ||
pub const fn wrapping_and(&self, rhs: &Self) -> Self { | ||
self.bitand(rhs) | ||
} | ||
|
||
/// Perform checked bitwise `AND`, returning a [`ConstCtOption`] which `is_some` always | ||
pub const fn checked_and(&self, rhs: &Self) -> ConstCtOption<Self> { | ||
ConstCtOption::some(self.bitand(rhs)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd for Int<LIMBS> { | ||
type Output = Self; | ||
|
||
fn bitand(self, rhs: Self) -> Int<LIMBS> { | ||
self.bitand(&rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd<&Int<LIMBS>> for Int<LIMBS> { | ||
type Output = Int<LIMBS>; | ||
|
||
#[allow(clippy::needless_borrow)] | ||
fn bitand(self, rhs: &Int<LIMBS>) -> Int<LIMBS> { | ||
(&self).bitand(rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd<Int<LIMBS>> for &Int<LIMBS> { | ||
type Output = Int<LIMBS>; | ||
|
||
fn bitand(self, rhs: Int<LIMBS>) -> Int<LIMBS> { | ||
self.bitand(&rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd<&Int<LIMBS>> for &Int<LIMBS> { | ||
type Output = Int<LIMBS>; | ||
|
||
fn bitand(self, rhs: &Int<LIMBS>) -> Int<LIMBS> { | ||
self.bitand(rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAndAssign for Int<LIMBS> { | ||
#[allow(clippy::assign_op_pattern)] | ||
fn bitand_assign(&mut self, other: Self) { | ||
*self = *self & other; | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAndAssign<&Int<LIMBS>> for Int<LIMBS> { | ||
#[allow(clippy::assign_op_pattern)] | ||
fn bitand_assign(&mut self, other: &Self) { | ||
*self = *self & other; | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd for Wrapping<Int<LIMBS>> { | ||
type Output = Self; | ||
|
||
fn bitand(self, rhs: Self) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitand(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> { | ||
type Output = Wrapping<Int<LIMBS>>; | ||
|
||
fn bitand(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitand(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd<Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> { | ||
type Output = Wrapping<Int<LIMBS>>; | ||
|
||
fn bitand(self, rhs: Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitand(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAnd<&Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> { | ||
type Output = Wrapping<Int<LIMBS>>; | ||
|
||
fn bitand(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitand(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAndAssign for Wrapping<Int<LIMBS>> { | ||
#[allow(clippy::assign_op_pattern)] | ||
fn bitand_assign(&mut self, other: Self) { | ||
*self = *self & other; | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitAndAssign<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> { | ||
#[allow(clippy::assign_op_pattern)] | ||
fn bitand_assign(&mut self, other: &Self) { | ||
*self = *self & other; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::I128; | ||
|
||
#[test] | ||
fn checked_and_ok() { | ||
assert_eq!(I128::ZERO.checked_and(&I128::ONE).unwrap(), I128::ZERO); | ||
} | ||
|
||
#[test] | ||
fn overlapping_and_ok() { | ||
assert_eq!(I128::MAX.wrapping_and(&I128::ONE), I128::ONE); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//! [`Int`] bitwise NOT operations. | ||
|
||
use core::ops::Not; | ||
|
||
use crate::{Uint, Wrapping}; | ||
|
||
use super::Int; | ||
|
||
impl<const LIMBS: usize> Int<LIMBS> { | ||
/// Computes bitwise `!a`. | ||
#[inline(always)] | ||
pub const fn not(&self) -> Self { | ||
Self(Uint::not(&self.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> Not for Int<LIMBS> { | ||
type Output = Self; | ||
|
||
fn not(self) -> Self { | ||
Self::not(&self) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> Not for Wrapping<Int<LIMBS>> { | ||
type Output = Self; | ||
|
||
fn not(self) -> <Self as Not>::Output { | ||
Wrapping(self.0.not()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::I128; | ||
|
||
#[test] | ||
fn bitnot_ok() { | ||
assert_eq!(I128::ZERO.not(), I128::MINUS_ONE); | ||
assert_eq!(I128::MAX.not(), I128::MIN); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
//! [`Int`] bitwise OR operations. | ||
|
||
use core::ops::{BitOr, BitOrAssign}; | ||
|
||
use crate::{ConstCtOption, Uint, Wrapping}; | ||
|
||
use super::Int; | ||
|
||
impl<const LIMBS: usize> Int<LIMBS> { | ||
/// Computes bitwise `a & b`. | ||
#[inline(always)] | ||
pub const fn bitor(&self, rhs: &Self) -> Self { | ||
Self(Uint::bitor(&self.0, &rhs.0)) | ||
} | ||
|
||
/// Perform wrapping bitwise `OR`. | ||
/// | ||
/// There's no way wrapping could ever happen. | ||
/// This function exists so that all operations are accounted for in the wrapping operations | ||
pub const fn wrapping_or(&self, rhs: &Self) -> Self { | ||
self.bitor(rhs) | ||
} | ||
|
||
/// Perform checked bitwise `OR`, returning a [`ConstCtOption`] which `is_some` always | ||
pub const fn checked_or(&self, rhs: &Self) -> ConstCtOption<Self> { | ||
ConstCtOption::some(self.bitor(rhs)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr for Int<LIMBS> { | ||
type Output = Self; | ||
|
||
fn bitor(self, rhs: Self) -> Int<LIMBS> { | ||
self.bitor(&rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr<&Int<LIMBS>> for Int<LIMBS> { | ||
type Output = Int<LIMBS>; | ||
|
||
#[allow(clippy::needless_borrow)] | ||
fn bitor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> { | ||
(&self).bitor(rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr<Int<LIMBS>> for &Int<LIMBS> { | ||
type Output = Int<LIMBS>; | ||
|
||
fn bitor(self, rhs: Int<LIMBS>) -> Int<LIMBS> { | ||
self.bitor(&rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr<&Int<LIMBS>> for &Int<LIMBS> { | ||
type Output = Int<LIMBS>; | ||
|
||
fn bitor(self, rhs: &Int<LIMBS>) -> Int<LIMBS> { | ||
self.bitor(rhs) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOrAssign for Int<LIMBS> { | ||
fn bitor_assign(&mut self, other: Self) { | ||
*self = *self | other; | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOrAssign<&Int<LIMBS>> for Int<LIMBS> { | ||
fn bitor_assign(&mut self, other: &Self) { | ||
*self = *self | other; | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr for Wrapping<Int<LIMBS>> { | ||
type Output = Self; | ||
|
||
fn bitor(self, rhs: Self) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitor(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> { | ||
type Output = Wrapping<Int<LIMBS>>; | ||
|
||
fn bitor(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitor(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr<Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> { | ||
type Output = Wrapping<Int<LIMBS>>; | ||
|
||
fn bitor(self, rhs: Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitor(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOr<&Wrapping<Int<LIMBS>>> for &Wrapping<Int<LIMBS>> { | ||
type Output = Wrapping<Int<LIMBS>>; | ||
|
||
fn bitor(self, rhs: &Wrapping<Int<LIMBS>>) -> Wrapping<Int<LIMBS>> { | ||
Wrapping(self.0.bitor(&rhs.0)) | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOrAssign for Wrapping<Int<LIMBS>> { | ||
fn bitor_assign(&mut self, other: Self) { | ||
*self = *self | other; | ||
} | ||
} | ||
|
||
impl<const LIMBS: usize> BitOrAssign<&Wrapping<Int<LIMBS>>> for Wrapping<Int<LIMBS>> { | ||
fn bitor_assign(&mut self, other: &Self) { | ||
*self = *self | other; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::I128; | ||
|
||
#[test] | ||
fn checked_or_ok() { | ||
assert_eq!(I128::ZERO.checked_or(&I128::ONE).unwrap(), I128::ONE); | ||
} | ||
|
||
#[test] | ||
fn overlapping_or_ok() { | ||
assert_eq!(I128::MAX.wrapping_or(&I128::ONE), I128::MAX); | ||
} | ||
} |
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.
I copy-pasted this from
ConstCtOption<Uint>
.In doing so, I did notice that, although it uses
is_true_vartime
,ConstCtOption<Uint>::expect
was not annotated as being vartime. Moreover, it is used forUint::shr
andUint::shl
, among others, which are not vartime either. Is this OK because unwraps are vartime anyway, or do we need to do something about this @tarcieri ?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.
They're not really "vartime" in that if the condition is violated, it will crash the entire program, as opposed to completing successfully in a variable number of CPU cycles