Skip to content

Commit

Permalink
Catch possible subtraction error in QUAD_BIT & DI_BIT hints (#1185)
Browse files Browse the repository at this point in the history
* Catch possible sustraction error in QUAD_BIT & DI_BIT hints

* Fix changelog entry

* Typo

* Fix changelog entry
  • Loading branch information
fmoletta authored May 29, 2023
1 parent 45bf5c8 commit 0a14a99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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 @@ pub fn n_pair_bits(
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 @@ mod tests {
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!(
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,
}

1 comment on commit 0a14a99

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 0a14a99 Previous: 45bf5c8 Ratio
add_u64_with_felt/7 4 ns/iter (± 0) 3 ns/iter (± 0) 1.33

This comment was automatically generated by workflow using github-action-benchmark.

CC: @unbalancedparentheses

Please sign in to comment.