-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl a few num-traits on Felt, behind a feature flag
- Loading branch information
Showing
5 changed files
with
154 additions
and
76 deletions.
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
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,123 @@ | ||
use super::Felt; | ||
use num_traits::{FromPrimitive, Inv, One, Pow, ToPrimitive, Zero}; | ||
|
||
impl FromPrimitive for Felt { | ||
fn from_i64(value: i64) -> Option<Self> { | ||
Some(value.into()) | ||
} | ||
|
||
fn from_u64(value: u64) -> Option<Self> { | ||
Some(value.into()) | ||
} | ||
|
||
fn from_i128(value: i128) -> Option<Self> { | ||
Some(value.into()) | ||
} | ||
|
||
fn from_u128(value: u128) -> Option<Self> { | ||
Some(value.into()) | ||
} | ||
} | ||
|
||
// TODO: we need to decide whether we want conversions to signed primitives | ||
// will support converting the high end of the field to negative. | ||
impl ToPrimitive for Felt { | ||
fn to_u64(&self) -> Option<u64> { | ||
self.to_u128().and_then(|x| u64::try_from(x).ok()) | ||
} | ||
|
||
fn to_i64(&self) -> Option<i64> { | ||
self.to_u128().and_then(|x| i64::try_from(x).ok()) | ||
} | ||
|
||
fn to_u128(&self) -> Option<u128> { | ||
match self.0.representative().limbs { | ||
[0, 0, hi, lo] => Some((lo as u128) | ((hi as u128) << 64)), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn to_i128(&self) -> Option<i128> { | ||
self.to_u128().and_then(|x| i128::try_from(x).ok()) | ||
} | ||
} | ||
|
||
impl Zero for Felt { | ||
fn is_zero(&self) -> bool { | ||
*self == Felt::ZERO | ||
} | ||
|
||
fn zero() -> Felt { | ||
Felt::ZERO | ||
} | ||
} | ||
|
||
impl One for Felt { | ||
fn one() -> Self { | ||
Felt::ONE | ||
} | ||
} | ||
|
||
impl Inv for Felt { | ||
type Output = Option<Self>; | ||
|
||
fn inv(self) -> Self::Output { | ||
self.inverse() | ||
} | ||
} | ||
|
||
impl Pow<u8> for Felt { | ||
type Output = Self; | ||
|
||
fn pow(self, rhs: u8) -> Self::Output { | ||
Self(self.0.pow(rhs as u128)) | ||
} | ||
} | ||
impl Pow<u16> for Felt { | ||
type Output = Self; | ||
|
||
fn pow(self, rhs: u16) -> Self::Output { | ||
Self(self.0.pow(rhs as u128)) | ||
} | ||
} | ||
impl Pow<u32> for Felt { | ||
type Output = Self; | ||
|
||
fn pow(self, rhs: u32) -> Self::Output { | ||
Self(self.0.pow(rhs as u128)) | ||
} | ||
} | ||
impl Pow<u64> for Felt { | ||
type Output = Self; | ||
|
||
fn pow(self, rhs: u64) -> Self::Output { | ||
Self(self.0.pow(rhs as u128)) | ||
} | ||
} | ||
impl Pow<u128> for Felt { | ||
type Output = Self; | ||
|
||
fn pow(self, rhs: u128) -> Self::Output { | ||
Self(self.0.pow(rhs)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn zero_is_zero() { | ||
assert!(Felt::ZERO.is_zero()); | ||
} | ||
|
||
#[test] | ||
fn one_is_one() { | ||
assert!(Felt::ONE.is_one()); | ||
} | ||
|
||
#[test] | ||
fn default_is_zero() { | ||
assert!(Felt::default().is_zero()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,5 +5,3 @@ pub mod curve; | |
pub mod hash; | ||
|
||
pub mod felt; | ||
#[cfg(test)] | ||
mod felt_arbitrary; |