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

fix: check for overflow with hexadecimal inputs #3004

Merged
merged 1 commit into from
Oct 6, 2023
Merged
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
68 changes: 33 additions & 35 deletions tooling/noirc_abi/src/input_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,48 +194,46 @@
}

fn parse_str_to_field(value: &str) -> Result<FieldElement, InputParserError> {
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<FieldElement, InputParserError> {

Check warning on line 214 in tooling/noirc_abi/src/input_parser/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (witdh)
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

Check warning on line 224 in tooling/noirc_abi/src/input_parser/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (witdh)
} 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 {
Expand Down Expand Up @@ -293,9 +291,9 @@
}

#[test]
fn rejects_noncanonical_fields() {

Check warning on line 294 in tooling/noirc_abi/src/input_parser/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (noncanonical)
let noncanonical_field = FieldElement::modulus().to_string();

Check warning on line 295 in tooling/noirc_abi/src/input_parser/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (noncanonical)
let parsed_field = parse_str_to_field(&noncanonical_field);

Check warning on line 296 in tooling/noirc_abi/src/input_parser/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (noncanonical)
println!("{parsed_field:?}");
}
}