diff --git a/tooling/noirc_abi/src/input_parser/mod.rs b/tooling/noirc_abi/src/input_parser/mod.rs index e26cfecc389..0fa3537d9b3 100644 --- a/tooling/noirc_abi/src/input_parser/mod.rs +++ b/tooling/noirc_abi/src/input_parser/mod.rs @@ -194,48 +194,46 @@ mod serialization_tests { } fn parse_str_to_field(value: &str) -> Result { - if value.starts_with("0x") { - FieldElement::from_hex(value).ok_or_else(|| InputParserError::ParseHexStr(value.to_owned())) + let big_num = if let Some(hex) = value.strip_prefix("0x") { + BigUint::from_str_radix(hex, 16) } else { BigUint::from_str_radix(value, 10) - .map_err(|err_msg| InputParserError::ParseStr(err_msg.to_string())) - .and_then(|bigint| { - if bigint < FieldElement::modulus() { - Ok(field_from_big_uint(bigint)) - } else { - Err(InputParserError::ParseStr(format!( - "Input exceeds field modulus. Values must fall within [0, {})", - FieldElement::modulus(), - ))) - } - }) - } + }; + big_num.map_err(|err_msg| InputParserError::ParseStr(err_msg.to_string())).and_then(|bigint| { + if bigint < FieldElement::modulus() { + Ok(field_from_big_uint(bigint)) + } else { + Err(InputParserError::ParseStr(format!( + "Input exceeds field modulus. Values must fall within [0, {})", + FieldElement::modulus(), + ))) + } + }) } fn parse_str_to_signed(value: &str, witdh: u32) -> Result { - if value.starts_with("0x") { - FieldElement::from_hex(value).ok_or_else(|| InputParserError::ParseHexStr(value.to_owned())) + let big_num = if let Some(hex) = value.strip_prefix("0x") { + BigInt::from_str_radix(hex, 16) } else { BigInt::from_str_radix(value, 10) - .map_err(|err_msg| InputParserError::ParseStr(err_msg.to_string())) - .and_then(|bigint| { - let modulus: BigInt = FieldElement::modulus().into(); - let bigint = if bigint.sign() == num_bigint::Sign::Minus { - BigInt::from(2).pow(witdh) + bigint - } else { - bigint - }; - if bigint.is_zero() || (bigint.sign() == num_bigint::Sign::Plus && bigint < modulus) - { - Ok(field_from_big_int(bigint)) - } else { - Err(InputParserError::ParseStr(format!( - "Input exceeds field modulus. Values must fall within [0, {})", - FieldElement::modulus(), - ))) - } - }) - } + }; + + big_num.map_err(|err_msg| InputParserError::ParseStr(err_msg.to_string())).and_then(|bigint| { + let modulus: BigInt = FieldElement::modulus().into(); + let bigint = if bigint.sign() == num_bigint::Sign::Minus { + BigInt::from(2).pow(witdh) + bigint + } else { + bigint + }; + if bigint.is_zero() || (bigint.sign() == num_bigint::Sign::Plus && bigint < modulus) { + Ok(field_from_big_int(bigint)) + } else { + Err(InputParserError::ParseStr(format!( + "Input exceeds field modulus. Values must fall within [0, {})", + FieldElement::modulus(), + ))) + } + }) } fn field_from_big_uint(bigint: BigUint) -> FieldElement {