diff --git a/math/src/field/f64/mod.rs b/math/src/field/f64/mod.rs index cd4b07c2f..0ac84ccc0 100644 --- a/math/src/field/f64/mod.rs +++ b/math/src/field/f64/mod.rs @@ -516,31 +516,27 @@ impl ExtensibleField<3> for BaseElement { // TYPE CONVERSIONS // ================================================================================================ -impl From for BaseElement { - /// Converts a 32-bit value into a field element. - fn from(value: u32) -> Self { - Self::new(value as u64) +impl From for BaseElement { + fn from(value: bool) -> Self { + Self::new(value.into()) } } -impl From for BaseElement { - /// Converts a 16-bit value into a field element. - fn from(value: u16) -> Self { - Self::new(value as u64) +impl From for BaseElement { + fn from(value: u8) -> Self { + Self::new(value.into()) } } -impl From for BaseElement { - /// Converts an 8-bit value into a field element. - fn from(value: u8) -> Self { - Self::new(value as u64) +impl From for BaseElement { + fn from(value: u16) -> Self { + Self::new(value.into()) } } -impl From for BaseElement { - /// Converts an bool value into a field element. - fn from(value: bool) -> Self { - Self::new(value as u64) +impl From for BaseElement { + fn from(value: u32) -> Self { + Self::new(value.into()) } } @@ -562,7 +558,7 @@ impl TryFrom for BaseElement { type Error = String; fn try_from(value: u128) -> Result { - if value >= M as u128 { + if value >= M.into() { Err(format!( "invalid field element: value {value} is greater than or equal to the field modulus" )) @@ -572,6 +568,17 @@ impl TryFrom for BaseElement { } } +impl TryFrom for BaseElement { + type Error = String; + + fn try_from(value: usize) -> Result { + match u64::try_from(value) { + Err(_) => Err(format!("invalid field element: value {value} does not fit in a u64")), + Ok(v) => v.try_into(), + } + } +} + impl TryFrom<[u8; 8]> for BaseElement { type Error = String; @@ -607,19 +614,19 @@ impl<'a> TryFrom<&'a [u8]> for BaseElement { } } -impl From for u128 { - fn from(value: BaseElement) -> Self { - value.as_int() as u128 - } -} +impl TryFrom for bool { + type Error = String; -impl From for u64 { - fn from(value: BaseElement) -> Self { - value.as_int() + fn try_from(value: BaseElement) -> Result { + match value.as_int() { + 0 => Ok(false), + 1 => Ok(true), + v => Err(format!("Field element does not represent a boolean, got {}", v)), + } } } -impl TryFrom for u32 { +impl TryFrom for u8 { type Error = String; fn try_from(value: BaseElement) -> Result { @@ -635,7 +642,7 @@ impl TryFrom for u16 { } } -impl TryFrom for u8 { +impl TryFrom for u32 { type Error = String; fn try_from(value: BaseElement) -> Result { @@ -643,15 +650,15 @@ impl TryFrom for u8 { } } -impl TryFrom for bool { - type Error = String; +impl From for u64 { + fn from(value: BaseElement) -> Self { + value.as_int() + } +} - fn try_from(value: BaseElement) -> Result { - match value.as_int() { - 0 => Ok(false), - 1 => Ok(true), - v => Err(format!("Field element does not represent a boolean, got {}", v)), - } +impl From for u128 { + fn from(value: BaseElement) -> Self { + value.as_int().into() } }