Skip to content

Commit

Permalink
Add implementations of From<iXXX> for prime fields as well
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker committed Apr 16, 2021
1 parent fc60ad5 commit 133b54a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions ff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ark-std = { version = "0.2.0", default-features = false }
ark-serialize = { version = "^0.2.0", path = "../serialize", default-features = false }
derivative = { version = "2", features = ["use_core"] }
num-traits = { version = "0.2", default-features = false }
paste = "1.0"
rayon = { version = "1", optional = true }
zeroize = { version = "1", default-features = false, features = ["zeroize_derive"] }

Expand Down
45 changes: 41 additions & 4 deletions ff/src/fields/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ macro_rules! impl_prime_field_standard_sample {
}

macro_rules! impl_prime_field_from_int {
($field: ident, u128, $params: ident, $limbs:expr) => {
($field: ident, 128, $params: ident, $limbs:expr) => {
impl<P: $params> From<u128> for $field<P> {
fn from(other: u128) -> Self {
let mut default_int = P::BigInt::default();
Expand All @@ -242,10 +242,22 @@ macro_rules! impl_prime_field_from_int {
Self::from_repr(default_int).unwrap()
}
}

impl <P: $params> From<i128> for $field<P> {
fn from(other: i128) -> Self {
let positive = other.is_positive();
let abs = Self::from(other.unsigned_abs());
if positive {
abs
} else {
-abs
}
}
}
};
($field: ident, $int: ident, $params: ident, $limbs:expr) => {
impl<P: $params> From<$int> for $field<P> {
fn from(other: $int) -> Self {
($field: ident, bool, $params: ident, $limbs:expr) => {
impl<P: $params> From<bool> for $field<P> {
fn from(other: bool) -> Self {
if $limbs == 1 {
Self::from_repr(P::BigInt::from(u64::from(other) % P::MODULUS.0[0])).unwrap()
} else {
Expand All @@ -254,6 +266,31 @@ macro_rules! impl_prime_field_from_int {
}
}
};
($field: ident, $int: expr, $params: ident, $limbs:expr) => {
paste::paste!{
impl<P: $params> From<[<u $int>]> for $field<P> {
fn from(other: [<u $int>]) -> Self {
if $limbs == 1 {
Self::from_repr(P::BigInt::from(u64::from(other) % P::MODULUS.0[0])).unwrap()
} else {
Self::from_repr(P::BigInt::from(u64::from(other))).unwrap()
}
}
}

impl<P: $params> From<[<i $int>]> for $field<P> {
fn from(other: [<i $int>]) -> Self {
let positive = other.is_positive();
let abs = Self::from(other.unsigned_abs());
if positive {
abs
} else {
-abs
}
}
}
}
};
}

macro_rules! sqrt_impl {
Expand Down
10 changes: 5 additions & 5 deletions ff/src/fields/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,11 @@ macro_rules! impl_Fp {
}
}

impl_prime_field_from_int!($Fp, u128, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, u64, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, u32, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, u16, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, u8, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, 128, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, 64, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, 32, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, 16, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, 8, $FpParameters, $limbs);
impl_prime_field_from_int!($Fp, bool, $FpParameters, $limbs);

impl_prime_field_standard_sample!($Fp, $FpParameters);
Expand Down

0 comments on commit 133b54a

Please sign in to comment.