diff --git a/ff/Cargo.toml b/ff/Cargo.toml index 56939ca93..4a51ec433 100644 --- a/ff/Cargo.toml +++ b/ff/Cargo.toml @@ -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"] } diff --git a/ff/src/fields/arithmetic.rs b/ff/src/fields/arithmetic.rs index 34924103b..b65e60974 100644 --- a/ff/src/fields/arithmetic.rs +++ b/ff/src/fields/arithmetic.rs @@ -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 From for $field

{ fn from(other: u128) -> Self { let mut default_int = P::BigInt::default(); @@ -242,10 +242,21 @@ macro_rules! impl_prime_field_from_int { Self::from_repr(default_int).unwrap() } } + + impl From for $field

{ + fn from(other: i128) -> Self { + let abs = Self::from(other.unsigned_abs()); + if other.is_positive() { + abs + } else { + -abs + } + } + } }; - ($field: ident, $int: ident, $params: ident, $limbs:expr) => { - impl From<$int> for $field

{ - fn from(other: $int) -> Self { + ($field: ident, bool, $params: ident, $limbs:expr) => { + impl From for $field

{ + fn from(other: bool) -> Self { if $limbs == 1 { Self::from_repr(P::BigInt::from(u64::from(other) % P::MODULUS.0[0])).unwrap() } else { @@ -254,6 +265,30 @@ macro_rules! impl_prime_field_from_int { } } }; + ($field: ident, $int: expr, $params: ident, $limbs:expr) => { + paste::paste!{ + impl From<[]> for $field

{ + fn from(other: []) -> 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 From<[]> for $field

{ + fn from(other: []) -> Self { + let abs = Self::from(other.unsigned_abs()); + if other.is_positive() { + abs + } else { + -abs + } + } + } + } + }; } macro_rules! sqrt_impl { diff --git a/ff/src/fields/macros.rs b/ff/src/fields/macros.rs index 09ccf9e42..4fbc434aa 100644 --- a/ff/src/fields/macros.rs +++ b/ff/src/fields/macros.rs @@ -530,11 +530,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); diff --git a/ff/src/fields/models/cubic_extension.rs b/ff/src/fields/models/cubic_extension.rs index 48d33ca4a..88db731bf 100644 --- a/ff/src/fields/models/cubic_extension.rs +++ b/ff/src/fields/models/cubic_extension.rs @@ -312,6 +312,18 @@ impl From for CubicExtField

{ } } +impl From for CubicExtField

{ + #[inline] + fn from(val: i128) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for CubicExtField

{ fn from(other: u64) -> Self { let fe: P::BaseField = other.into(); @@ -319,6 +331,18 @@ impl From for CubicExtField

{ } } +impl From for CubicExtField

{ + #[inline] + fn from(val: i64) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for CubicExtField

{ fn from(other: u32) -> Self { let fe: P::BaseField = other.into(); @@ -326,6 +350,18 @@ impl From for CubicExtField

{ } } +impl From for CubicExtField

{ + #[inline] + fn from(val: i32) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for CubicExtField

{ fn from(other: u16) -> Self { let fe: P::BaseField = other.into(); @@ -333,6 +369,18 @@ impl From for CubicExtField

{ } } +impl From for CubicExtField

{ + #[inline] + fn from(val: i16) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for CubicExtField

{ fn from(other: u8) -> Self { let fe: P::BaseField = other.into(); @@ -340,6 +388,18 @@ impl From for CubicExtField

{ } } +impl From for CubicExtField

{ + #[inline] + fn from(val: i8) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for CubicExtField

{ fn from(other: bool) -> Self { Self::new( diff --git a/ff/src/fields/models/quadratic_extension.rs b/ff/src/fields/models/quadratic_extension.rs index 4e0d664de..a7e36dace 100644 --- a/ff/src/fields/models/quadratic_extension.rs +++ b/ff/src/fields/models/quadratic_extension.rs @@ -435,30 +435,90 @@ impl From for QuadExtField

{ } } +impl From for QuadExtField

{ + #[inline] + fn from(val: i128) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for QuadExtField

{ fn from(other: u64) -> Self { Self::new(other.into(), P::BaseField::zero()) } } +impl From for QuadExtField

{ + #[inline] + fn from(val: i64) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for QuadExtField

{ fn from(other: u32) -> Self { Self::new(other.into(), P::BaseField::zero()) } } +impl From for QuadExtField

{ + #[inline] + fn from(val: i32) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for QuadExtField

{ fn from(other: u16) -> Self { Self::new(other.into(), P::BaseField::zero()) } } +impl From for QuadExtField

{ + #[inline] + fn from(val: i16) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for QuadExtField

{ fn from(other: u8) -> Self { Self::new(other.into(), P::BaseField::zero()) } } +impl From for QuadExtField

{ + #[inline] + fn from(val: i8) -> Self { + let abs = Self::from(val.unsigned_abs()); + if val.is_positive() { + abs + } else { + -abs + } + } +} + impl From for QuadExtField

{ fn from(other: bool) -> Self { Self::new(u8::from(other).into(), P::BaseField::zero())