Skip to content

Commit

Permalink
Adds From<iXXX> implementations to fields (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker authored Apr 22, 2021
1 parent bfa7043 commit 68d4347
Show file tree
Hide file tree
Showing 5 changed files with 165 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
43 changes: 39 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,21 @@ 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 abs = Self::from(other.unsigned_abs());
if other.is_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 +265,30 @@ 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 abs = Self::from(other.unsigned_abs());
if other.is_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 @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions ff/src/fields/models/cubic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,34 +312,94 @@ impl<P: CubicExtParameters> From<u128> for CubicExtField<P> {
}
}

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

impl<P: CubicExtParameters> From<u64> for CubicExtField<P> {
fn from(other: u64) -> Self {
let fe: P::BaseField = other.into();
Self::new(fe, P::BaseField::zero(), P::BaseField::zero())
}
}

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

impl<P: CubicExtParameters> From<u32> for CubicExtField<P> {
fn from(other: u32) -> Self {
let fe: P::BaseField = other.into();
Self::new(fe, P::BaseField::zero(), P::BaseField::zero())
}
}

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

impl<P: CubicExtParameters> From<u16> for CubicExtField<P> {
fn from(other: u16) -> Self {
let fe: P::BaseField = other.into();
Self::new(fe, P::BaseField::zero(), P::BaseField::zero())
}
}

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

impl<P: CubicExtParameters> From<u8> for CubicExtField<P> {
fn from(other: u8) -> Self {
let fe: P::BaseField = other.into();
Self::new(fe, P::BaseField::zero(), P::BaseField::zero())
}
}

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

impl<P: CubicExtParameters> From<bool> for CubicExtField<P> {
fn from(other: bool) -> Self {
Self::new(
Expand Down
60 changes: 60 additions & 0 deletions ff/src/fields/models/quadratic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,30 +435,90 @@ impl<P: QuadExtParameters> From<u128> for QuadExtField<P> {
}
}

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

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

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

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

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

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

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

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

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

impl<P: QuadExtParameters> From<bool> for QuadExtField<P> {
fn from(other: bool) -> Self {
Self::new(u8::from(other).into(), P::BaseField::zero())
Expand Down

0 comments on commit 68d4347

Please sign in to comment.