Skip to content

Commit

Permalink
feat: add TryFrom<u64> and TryFrom<u128> bounds to the FieldElement
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Feb 6, 2024
1 parent aed1d72 commit 450da89
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 5 deletions.
2 changes: 1 addition & 1 deletion air/src/proof/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn bytes_to_element<B: StarkField>(bytes: &[u8]) -> B {

let mut buf = bytes.to_vec();
buf.resize(B::ELEMENT_BYTES, 0);
let element = match B::try_from(&buf) {
let element = match B::try_from(buf.as_slice()) {
Ok(element) => element,
Err(_) => panic!("element deserialization failed"),
};
Expand Down
34 changes: 32 additions & 2 deletions math/src/field/extensions/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use core::{
slice,
};
use utils::{
collections::Vec, string::ToString, AsBytes, ByteReader, ByteWriter, Deserializable,
DeserializationError, Randomizable, Serializable, SliceReader,
collections::Vec,
string::{String, ToString},
AsBytes, ByteReader, ByteWriter, Deserializable, DeserializationError, Randomizable,
Serializable, SliceReader,
};

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -319,6 +321,34 @@ impl<B: ExtensibleField<3>> From<u8> for CubeExtension<B> {
}
}

impl<B: ExtensibleField<3>> TryFrom<u64> for CubeExtension<B> {
type Error = String;

fn try_from(value: u64) -> Result<Self, Self::Error> {
let felt = B::try_from(value);
match felt {
Ok(elem) => Ok(Self(elem, B::ZERO, B::ZERO)),
Err(_) => Err(format!(
"invalid field element: value {value} is greater than or equal to the field modulus"
)),
}
}
}

impl<B: ExtensibleField<3>> TryFrom<u128> for CubeExtension<B> {
type Error = String;

fn try_from(value: u128) -> Result<Self, Self::Error> {
let felt = B::try_from(value);
match felt {
Ok(elem) => Ok(Self(elem, B::ZERO, B::ZERO)),
Err(_) => Err(format!(
"invalid field element: value {value} is greater than or equal to the field modulus"
)),
}
}
}

impl<'a, B: ExtensibleField<3>> TryFrom<&'a [u8]> for CubeExtension<B> {
type Error = DeserializationError;

Expand Down
34 changes: 32 additions & 2 deletions math/src/field/extensions/quadratic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use core::{
slice,
};
use utils::{
collections::Vec, string::ToString, AsBytes, ByteReader, ByteWriter, Deserializable,
DeserializationError, Randomizable, Serializable, SliceReader,
collections::Vec,
string::{String, ToString},
AsBytes, ByteReader, ByteWriter, Deserializable, DeserializationError, Randomizable,
Serializable, SliceReader,
};

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -313,6 +315,34 @@ impl<B: ExtensibleField<2>> From<u8> for QuadExtension<B> {
}
}

impl<B: ExtensibleField<2>> TryFrom<u64> for QuadExtension<B> {
type Error = String;

fn try_from(value: u64) -> Result<Self, Self::Error> {
let felt = B::try_from(value);
match felt {
Ok(elem) => Ok(Self(elem, B::ZERO)),
Err(_) => Err(format!(
"invalid field element: value {value} is greater than or equal to the field modulus"
)),
}
}
}

impl<B: ExtensibleField<2>> TryFrom<u128> for QuadExtension<B> {
type Error = String;

fn try_from(value: u128) -> Result<Self, Self::Error> {
let felt = B::try_from(value);
match felt {
Ok(elem) => Ok(Self(elem, B::ZERO)),
Err(_) => Err(format!(
"invalid field element: value {value} is greater than or equal to the field modulus"
)),
}
}
}

impl<'a, B: ExtensibleField<2>> TryFrom<&'a [u8]> for QuadExtension<B> {
type Error = DeserializationError;

Expand Down
14 changes: 14 additions & 0 deletions math/src/field/f128/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,20 @@ impl From<u8> for BaseElement {
}
}

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

fn try_from(value: u128) -> Result<Self, Self::Error> {
if value >= M {
Err(format!(
"invalid field element: value {value} is greater than or equal to the field modulus"
))
} else {
Ok(Self::new(value))
}
}
}

impl<'a> TryFrom<&'a [u8]> for BaseElement {
type Error = String;

Expand Down
14 changes: 14 additions & 0 deletions math/src/field/f62/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,20 @@ impl TryFrom<u64> for BaseElement {
}
}

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

fn try_from(value: u128) -> Result<Self, Self::Error> {
if value >= M as u128 {
Err(format!(
"invalid field element: value {value} is greater than or equal to the field modulus"
))
} else {
Ok(Self::new(value as u64))
}
}
}

impl TryFrom<[u8; 8]> for BaseElement {
type Error = String;

Expand Down
14 changes: 14 additions & 0 deletions math/src/field/f64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,20 @@ impl TryFrom<u64> for BaseElement {
}
}

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

fn try_from(value: u128) -> Result<Self, Self::Error> {
if value >= M as u128 {
Err(format!(
"invalid field element: value {value} is greater than or equal to the field modulus"
))
} else {
Ok(Self::new(value as u64))
}
}
}

impl TryFrom<[u8; 8]> for BaseElement {
type Error = String;

Expand Down
2 changes: 2 additions & 0 deletions math/src/field/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub trait FieldElement:
+ From<u16>
+ From<u8>
+ for<'a> TryFrom<&'a [u8]>
+ TryFrom<u64>
+ TryFrom<u128>
+ ExtensionOf<<Self as FieldElement>::BaseField>
+ AsBytes
+ Randomizable
Expand Down

0 comments on commit 450da89

Please sign in to comment.