Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

felt u64: add conversion to/from usize #272

Merged
merged 1 commit into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 42 additions & 35 deletions math/src/field/f64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,31 +516,27 @@ impl ExtensibleField<3> for BaseElement {
// TYPE CONVERSIONS
// ================================================================================================

impl From<u32> for BaseElement {
/// Converts a 32-bit value into a field element.
fn from(value: u32) -> Self {
Self::new(value as u64)
impl From<bool> for BaseElement {
fn from(value: bool) -> Self {
Self::new(value.into())
}
}

impl From<u16> for BaseElement {
/// Converts a 16-bit value into a field element.
fn from(value: u16) -> Self {
Self::new(value as u64)
impl From<u8> for BaseElement {
fn from(value: u8) -> Self {
Self::new(value.into())
}
}

impl From<u8> for BaseElement {
/// Converts an 8-bit value into a field element.
fn from(value: u8) -> Self {
Self::new(value as u64)
impl From<u16> for BaseElement {
fn from(value: u16) -> Self {
Self::new(value.into())
}
}

impl From<bool> for BaseElement {
/// Converts an bool value into a field element.
fn from(value: bool) -> Self {
Self::new(value as u64)
impl From<u32> for BaseElement {
fn from(value: u32) -> Self {
Self::new(value.into())
}
}

Expand All @@ -562,7 +558,7 @@ impl TryFrom<u128> for BaseElement {
type Error = String;

fn try_from(value: u128) -> Result<Self, Self::Error> {
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"
))
Expand All @@ -572,6 +568,17 @@ impl TryFrom<u128> for BaseElement {
}
}

impl TryFrom<usize> for BaseElement {
type Error = String;

fn try_from(value: usize) -> Result<Self, Self::Error> {
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;

Expand Down Expand Up @@ -607,19 +614,19 @@ impl<'a> TryFrom<&'a [u8]> for BaseElement {
}
}

impl From<BaseElement> for u128 {
fn from(value: BaseElement) -> Self {
value.as_int() as u128
}
}
impl TryFrom<BaseElement> for bool {
type Error = String;

impl From<BaseElement> for u64 {
fn from(value: BaseElement) -> Self {
value.as_int()
fn try_from(value: BaseElement) -> Result<Self, Self::Error> {
match value.as_int() {
0 => Ok(false),
1 => Ok(true),
v => Err(format!("Field element does not represent a boolean, got {}", v)),
}
}
}

impl TryFrom<BaseElement> for u32 {
impl TryFrom<BaseElement> for u8 {
type Error = String;

fn try_from(value: BaseElement) -> Result<Self, Self::Error> {
Expand All @@ -635,23 +642,23 @@ impl TryFrom<BaseElement> for u16 {
}
}

impl TryFrom<BaseElement> for u8 {
impl TryFrom<BaseElement> for u32 {
type Error = String;

fn try_from(value: BaseElement) -> Result<Self, Self::Error> {
value.as_int().try_into().map_err(|e| format!("{}", e))
}
}

impl TryFrom<BaseElement> for bool {
type Error = String;
impl From<BaseElement> for u64 {
fn from(value: BaseElement) -> Self {
value.as_int()
}
}

fn try_from(value: BaseElement) -> Result<Self, Self::Error> {
match value.as_int() {
0 => Ok(false),
1 => Ok(true),
v => Err(format!("Field element does not represent a boolean, got {}", v)),
}
impl From<BaseElement> for u128 {
fn from(value: BaseElement) -> Self {
value.as_int().into()
}
}

Expand Down
Loading