Skip to content

Commit b4feace

Browse files
committed
Add modulus range check when creating Felt from hex
1 parent ad2a380 commit b4feace

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

crates/math/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum CreationError {
1313
InvalidHexString,
1414
InvalidDecString,
1515
HexStringIsTooBig,
16+
RepresentativeOutOfRange,
1617
EmptyString,
1718
}
1819

crates/math/src/field/element.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,8 @@ impl<F: IsPrimeField> FieldElement<F> {
608608
/// Returns a `CreationError::EmptyString` if the input string is empty.
609609
/// Returns a `CreationError::HexStringIsTooBig` if the the input hex string is bigger than the
610610
/// maximum amount of characters for this element.
611+
/// Returns a `CreationError::RepresentativeOutOfRange` if the representative of the value is
612+
/// out of the range [0, p-1] where p is the modulus.
611613
pub fn from_hex(hex_string: &str) -> Result<Self, CreationError> {
612614
if hex_string.is_empty() {
613615
return Err(CreationError::EmptyString);
@@ -980,6 +982,14 @@ mod tests {
980982
assert!(FE::from_hex("").is_err());
981983
}
982984

985+
#[test]
986+
fn construct_new_field_element_from_value_bigger_than_modulus() {
987+
type F = Stark252PrimeField;
988+
type FE = FieldElement<F>;
989+
// A number that consists of 255 1s is bigger than the `Stark252PrimeField` modulus
990+
assert!(FE::from_hex(&format!("0x{}", "f".repeat(65))).is_err());
991+
}
992+
983993
prop_compose! {
984994
fn field_element()(num in any::<u64>().prop_filter("Avoid null coefficients", |x| x != &0)) -> FieldElement::<Stark252PrimeField> {
985995
FieldElement::<Stark252PrimeField>::from(num)

crates/math/src/field/fields/montgomery_backed_prime_fields.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors::CreationError;
12
use crate::field::element::FieldElement;
23
use crate::field::errors::FieldError;
34
use crate::field::traits::{HasDefaultTranscript, IsPrimeField};
@@ -305,8 +306,12 @@ where
305306
evaluated_bit + 1
306307
}
307308

308-
fn from_hex(hex_string: &str) -> Result<Self::BaseType, crate::errors::CreationError> {
309+
fn from_hex(hex_string: &str) -> Result<Self::BaseType, CreationError> {
309310
let integer = Self::BaseType::from_hex(hex_string)?;
311+
if integer > M::MODULUS {
312+
return Err(CreationError::RepresentativeOutOfRange);
313+
}
314+
310315
Ok(MontgomeryAlgorithms::cios(
311316
&integer,
312317
&MontgomeryBackendPrimeField::<M, NUM_LIMBS>::R2,

0 commit comments

Comments
 (0)