From 433ce13a14fb2c4732d5c0cbda176b25613b1c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Fri, 16 Apr 2021 17:56:49 -0400 Subject: [PATCH] Add implementations of `From` for prime fields as well --- ff/Cargo.toml | 1 + ff/src/fields/arithmetic.rs | 45 +++++++++++++++++++++++++++++++++---- ff/src/fields/macros.rs | 10 ++++----- 3 files changed, 47 insertions(+), 9 deletions(-) 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..ec5963b8b 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,22 @@ macro_rules! impl_prime_field_from_int { Self::from_repr(default_int).unwrap() } } + + impl From for $field

{ + 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 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 +266,31 @@ 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 positive = other.is_positive(); + let abs = Self::from(other.unsigned_abs()); + if 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);