Skip to content

Commit bb278c5

Browse files
Merge branch 'main' into feat/pin_starknet_dep
2 parents bb85832 + 712d894 commit bb278c5

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

math/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum ByteConversionError {
1111
pub enum CreationError {
1212
InvalidHexString,
1313
InvalidDecString,
14+
HexStringIsTooBig,
1415
EmptyString,
1516
}
1617

math/src/field/element.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,14 @@ impl<F: IsPrimeField> FieldElement<F> {
509509
/// Creates a `FieldElement` from a hexstring. It can contain `0x` or not.
510510
/// Returns an `CreationError::InvalidHexString`if the value is not a hexstring.
511511
/// Returns a `CreationError::EmptyString` if the input string is empty.
512+
/// Returns a `CreationError::HexStringIsTooBig` if the the input hex string is bigger
513+
/// than the maximum amount of characters for this element.
512514
pub fn from_hex(hex_string: &str) -> Result<Self, CreationError> {
513515
if hex_string.is_empty() {
514-
return Err(CreationError::EmptyString)?;
516+
return Err(CreationError::EmptyString);
515517
}
516-
517-
Ok(Self {
518-
value: F::from_hex(hex_string)?,
519-
})
518+
let value = F::from_hex(hex_string)?;
519+
Ok(Self { value })
520520
}
521521

522522
#[cfg(feature = "std")]

math/src/field/fields/montgomery_backed_prime_fields.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,22 @@ mod tests_u256_prime_fields {
11491149
assert_eq!(a, b);
11501150
}
11511151

1152+
#[test]
1153+
fn creating_a_field_element_from_hex_too_big_errors() {
1154+
let a = U256FP1Element::from_hex(&"f".repeat(65));
1155+
assert!(a.is_err());
1156+
assert_eq!(
1157+
a.unwrap_err(),
1158+
crate::errors::CreationError::HexStringIsTooBig
1159+
)
1160+
}
1161+
1162+
#[test]
1163+
fn creating_a_field_element_from_hex_works_on_the_size_limit() {
1164+
let a = U256FP1Element::from_hex(&"f".repeat(64));
1165+
assert!(a.is_ok());
1166+
}
1167+
11521168
#[test]
11531169
fn creating_a_field_element_from_hex_works_2() {
11541170
let a = U256F29Element::from_hex_unchecked("aa");

math/src/unsigned_integer/element.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
425425
/// Creates an `UnsignedInteger` from a hexstring. It can contain `0x` or not.
426426
/// Returns an `CreationError::InvalidHexString`if the value is not a hexstring.
427427
/// Returns a `CreationError::EmptyString` if the input string is empty.
428+
/// Returns a `CreationError::HexStringIsTooBig` if the the input hex string is bigger
429+
/// than the maximum amount of characters for this element.
428430
pub fn from_hex(value: &str) -> Result<Self, CreationError> {
429431
let mut string = value;
430432
let mut char_iterator = value.chars();
@@ -440,6 +442,14 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
440442
if !Self::is_hex_string(string) {
441443
return Err(CreationError::InvalidHexString);
442444
}
445+
446+
// Limbs are of 64 bits - 8 bytes
447+
// We have 16 nibbles per bytes
448+
let max_amount_of_hex_chars = NUM_LIMBS * 16;
449+
if string.len() > max_amount_of_hex_chars {
450+
return Err(CreationError::HexStringIsTooBig);
451+
}
452+
443453
Ok(Self::from_hex_unchecked(string))
444454
}
445455

@@ -1001,7 +1011,7 @@ impl<const NUM_LIMBS: usize> Arbitrary for UnsignedInteger<NUM_LIMBS> {
10011011
#[cfg(test)]
10021012
mod tests_u384 {
10031013
use crate::traits::ByteConversion;
1004-
use crate::unsigned_integer::element::{UnsignedInteger, U384};
1014+
use crate::unsigned_integer::element::{UnsignedInteger, U256, U384};
10051015
#[cfg(feature = "proptest")]
10061016
use proptest::prelude::*;
10071017
#[cfg(feature = "proptest")]
@@ -1246,6 +1256,21 @@ mod tests_u384 {
12461256
);
12471257
}
12481258

1259+
#[test]
1260+
fn from_hex_with_overflowing_hexstring_should_error() {
1261+
let u256_from_big_string = U256::from_hex(&"f".repeat(65));
1262+
assert!(u256_from_big_string.is_err());
1263+
assert!(
1264+
u256_from_big_string
1265+
== Err(crate::unsigned_integer::element::CreationError::HexStringIsTooBig)
1266+
);
1267+
}
1268+
1269+
#[test]
1270+
fn from_hex_with_non_overflowing_hexstring_should_work() {
1271+
assert_eq!(U256::from_hex(&"0".repeat(64)).unwrap().limbs, [0, 0, 0, 0])
1272+
}
1273+
12491274
#[test]
12501275
fn construct_new_integer_from_dec_1() {
12511276
let a = U384::from_dec_str("1").unwrap();

0 commit comments

Comments
 (0)