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

Catch possible subtraction error in QUAD_BIT & DI_BIT hints #1185

Merged
merged 4 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#### Upcoming Changes

* Fix possible subtraction overflow in `QUAD_BIT` & `DI_BIT` hints [#1185](https://github.com/lambdaclass/cairo-rs/pull/1185)

* These hints now return an error when ids.m equals zero

* Add `CairoRunner::run_until_pc_with_steps_limit method` [#1181](https://github.com/lambdaclass/cairo-rs/pull/1181)

* fix: felt_from_number not properly returning parse errors [#1012](https://github.com/lambdaclass/cairo-rs/pull/1012)
Expand Down
26 changes: 26 additions & 0 deletions src/hint_processor/builtin_hint_processor/secp/ec_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@
if m >= 253 {
return insert_value_from_var_name("quad_bit", 0, vm, ids_data, ap_tracking);
}
if m.is_zero() {
return Err(HintError::NPairBitsMZero);
}

let one = &Felt252::one();
let two = &Felt252::from(2);
Expand Down Expand Up @@ -1281,6 +1284,29 @@
check_memory![vm.segments.memory, ((1, 3), 2)];
}

#[test]
fn run_di_bit_m_zero() {
let hint_code = hint_code::DI_BIT;
let mut vm = vm_with_range_check!();

let scalar_u = 0b10101111001110000;
let scalar_v = 0b101101000111011111100;
let m = 0;
// Insert ids.scalar into memory
vm.segments = segments![((1, 0), scalar_u), ((1, 1), scalar_v), ((1, 2), m)];

// Initialize RunContext
run_context!(vm, 0, 4, 4);

let ids_data = ids_data!["scalar_u", "scalar_v", "m", "dibit"];

// Execute the hint
assert_matches!(

Check warning on line 1304 in src/hint_processor/builtin_hint_processor/secp/ec_utils.rs

View check run for this annotation

Codecov / codecov/patch

src/hint_processor/builtin_hint_processor/secp/ec_utils.rs#L1304

Added line #L1304 was not covered by tests
run_hint!(vm, ids_data, hint_code),
Err(HintError::NPairBitsMZero)
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_import_secp256r1_alpha() {
Expand Down
2 changes: 2 additions & 0 deletions src/vm/errors/hint_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@ pub enum HintError {
RecoverYPointNotOnCurve(Felt252),
#[error("Invalid value for {0}. Got: {1}. Expected: {2}")]
InvalidValue(&'static str, Felt252, Felt252),
#[error("Attempt to subtract with overflow: ids.m - 1")]
NPairBitsMZero,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use a more descriptive name to this new error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose this name as it is more useful when debugging, as it has the name of the function that fails and the failure case, while leaving the error string useful for users, a more descriptive name could be NPairBitsMMinusOneSubtractioOverflow or MMinusOneSubtractionOverflow but it doesn't sound that much useful

}