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

Add TryFrom<u64> and TryFrom<u128> bounds to the FieldElement trait #244

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
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
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
32 changes: 30 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,32 @@ 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> {
match B::try_from(value) {
Ok(elem) => Ok(Self::from(elem)),
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> {
match B::try_from(value) {
Ok(elem) => Ok(Self::from(elem)),
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
32 changes: 30 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,32 @@ 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> {
match B::try_from(value) {
Ok(elem) => Ok(Self::from(elem)),
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> {
match B::try_from(value) {
Ok(elem) => Ok(Self::from(elem)),
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 @@ -49,6 +49,8 @@ pub trait FieldElement:
+ From<u32>
+ From<u16>
+ From<u8>
+ TryFrom<u64>
+ TryFrom<u128>
+ for<'a> TryFrom<&'a [u8]>
+ ExtensionOf<<Self as FieldElement>::BaseField>
+ AsBytes
Expand Down
Loading