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 86f2e26
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 1 deletion.
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
28 changes: 28 additions & 0 deletions math/src/field/extensions/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,34 @@ impl<B: ExtensibleField<3>> From<u8> for CubeExtension<B> {
}
}

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

Check failure on line 323 in math/src/field/extensions/cubic.rs

View workflow job for this annotation

GitHub Actions / Check with --no-default-features

cannot find type `String` in this scope

Check failure on line 323 in math/src/field/extensions/cubic.rs

View workflow job for this annotation

GitHub Actions / no-std (stable, wasm32-unknown-unknown)

cannot find type `String` in this scope

Check failure on line 323 in math/src/field/extensions/cubic.rs

View workflow job for this annotation

GitHub Actions / no-std (nightly, wasm32-unknown-unknown)

cannot find type `String` in this scope

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;

Check failure on line 337 in math/src/field/extensions/cubic.rs

View workflow job for this annotation

GitHub Actions / Check with --no-default-features

cannot find type `String` in this scope

Check failure on line 337 in math/src/field/extensions/cubic.rs

View workflow job for this annotation

GitHub Actions / no-std (stable, wasm32-unknown-unknown)

cannot find type `String` in this scope

Check failure on line 337 in math/src/field/extensions/cubic.rs

View workflow job for this annotation

GitHub Actions / no-std (nightly, wasm32-unknown-unknown)

cannot find type `String` in this scope

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
28 changes: 28 additions & 0 deletions math/src/field/extensions/quadratic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,34 @@ impl<B: ExtensibleField<2>> From<u8> for QuadExtension<B> {
}
}

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

Check failure on line 317 in math/src/field/extensions/quadratic.rs

View workflow job for this annotation

GitHub Actions / Check with --no-default-features

cannot find type `String` in this scope

Check failure on line 317 in math/src/field/extensions/quadratic.rs

View workflow job for this annotation

GitHub Actions / no-std (stable, wasm32-unknown-unknown)

cannot find type `String` in this scope

Check failure on line 317 in math/src/field/extensions/quadratic.rs

View workflow job for this annotation

GitHub Actions / no-std (nightly, wasm32-unknown-unknown)

cannot find type `String` in this scope

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;

Check failure on line 331 in math/src/field/extensions/quadratic.rs

View workflow job for this annotation

GitHub Actions / Check with --no-default-features

cannot find type `String` in this scope

Check failure on line 331 in math/src/field/extensions/quadratic.rs

View workflow job for this annotation

GitHub Actions / no-std (stable, wasm32-unknown-unknown)

cannot find type `String` in this scope

Check failure on line 331 in math/src/field/extensions/quadratic.rs

View workflow job for this annotation

GitHub Actions / no-std (nightly, wasm32-unknown-unknown)

cannot find type `String` in this scope

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 86f2e26

Please sign in to comment.