Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* fix: `dibit` hint no longer fails when called with an `m` of zero [#1247](https://github.com/lambdaclass/cairo-rs/pull/1247)

* fix(security): avoid denial of service on malicious input exploiting the scientific notation parser [#1239](https://github.com/lambdaclass/cairo-rs/pull/1239)

* perf: accumulate `min` and `max` instruction offsets during run to speed up range check [#1080](https://github.com/lambdaclass/cairo-rs/pull/)
Expand Down
43 changes: 33 additions & 10 deletions vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ pub fn n_pair_bits(
if m >= 253 {
return insert_value_from_var_name(result_name, 0, vm, ids_data, ap_tracking);
}
if m.is_zero() {
return Err(HintError::NPairBitsMZero);
if m + 1 < number_of_pairs {
return Err(HintError::NPairBitsTooLowM);
}

let (scalar_v, scalar_u) = (scalar_v.to_biguint(), scalar_u.to_biguint());
Expand Down Expand Up @@ -1329,7 +1329,7 @@ mod tests {
}

#[test]
fn run_quad_bit_for_m_1() {
fn run_quad_bit_for_m_1_ok() {
let hint_code = hint_code::QUAD_BIT;
let mut vm = vm_with_range_check!();

Expand All @@ -1351,6 +1351,29 @@ mod tests {
check_memory![vm.segments.memory, ((1, 3), 0)];
}

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

let scalar_u = 0b1010101;
let scalar_v = 0b1010101;
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", "quad_bit"];

// Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::NPairBitsTooLowM)
);
}

#[test]
fn run_quad_bit_with_max_m_ok() {
let hint_code = hint_code::QUAD_BIT;
Expand Down Expand Up @@ -1423,12 +1446,12 @@ mod tests {
}

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

let scalar_u = 0b10101111001110000;
let scalar_v = 0b101101000111011111100;
let scalar_u = 0b00;
let scalar_v = 0b01;
let m = 0;
// Insert ids.scalar into memory
vm.segments = segments![((1, 0), scalar_u), ((1, 1), scalar_v), ((1, 2), m)];
Expand All @@ -1439,10 +1462,10 @@ mod tests {
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)
);
assert_matches!(run_hint!(vm, ids_data, hint_code), Ok(()));

// Check hint memory inserts
check_memory![vm.segments.memory, ((1, 3), 0b10)];
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm/errors/hint_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub enum HintError {
#[error("Invalid value for {}. Got: {}. Expected: {}", (*.0).0, (*.0).1, (*.0).2)]
InvalidValue(Box<(&'static str, Felt252, Felt252)>),
#[error("Attempt to subtract with overflow: ids.m - 1")]
NPairBitsMZero,
NPairBitsTooLowM,
}

#[cfg(test)]
Expand Down