Skip to content

Commit

Permalink
Adds From<iXXX> implementations to quadratic / cubic field extensions
Browse files Browse the repository at this point in the history
Those delegate to the respective, existing `From<uXXX>` implementations for `XXX` in `[8, 16, 32, 64, 128]`
  • Loading branch information
huitseeker committed Apr 16, 2021
1 parent b1e5449 commit fc60ad5
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
65 changes: 65 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,99 @@ impl<P: CubicExtParameters> From<u128> for CubicExtField<P> {
}
}

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

impl<P: CubicExtParameters> From<bool> for CubicExtField<P> {
fn from(other: bool) -> Self {
Self::new(
Expand Down
65 changes: 65 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,95 @@ impl<P: QuadExtParameters> From<u128> for QuadExtField<P> {
}
}

impl<P: QuadExtParameters> From<i128> for QuadExtField<P> {
#[inline]
fn from(val: i128) -> Self {
let positivity = val.is_positive();
let abs = Self::from(val.unsigned_abs());
if positivity {
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 positivity = val.is_positive();
let abs = Self::from(val.unsigned_abs());
if positivity {
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 positivity = val.is_positive();
let abs = Self::from(val.unsigned_abs());
if positivity {
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 positivity = val.is_positive();
let abs = Self::from(val.unsigned_abs());
if positivity {
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 positivity = val.is_positive();
let abs = Self::from(val.unsigned_abs());
if positivity {
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 fc60ad5

Please sign in to comment.