Skip to content

Commit 1ebb0b8

Browse files
enitratgabrielbosioJulianGCalderon
committed
fix: handle unfilled values in mul mod runner (#1976)
* fix: handle unfilled values in mul mod runner * update changelog * Move changelog entry * Remove whiteline * Add test --------- Co-authored-by: Gabriel Bosio <[email protected]> Co-authored-by: Julián González Calderón <[email protected]>
1 parent 17a8880 commit 1ebb0b8

File tree

5 files changed

+110
-38
lines changed

5 files changed

+110
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* fix: `mod_builtin_fill_memory` could be stuck in an infinite loop [#1975](https://github.com/lambdaclass/cairo-vm/issues/1975)
6+
57
* feat: replace `thiserror-no-std` with `thiserror 2` [#1919](https://github.com/lambdaclass/cairo-vm/pull/1919)
68

79
#### [2.0.1] - 2025-03-17

Cargo.lock

Lines changed: 45 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
%builtins range_check add_mod mul_mod
2+
3+
from cairo_programs.mod_builtin_feature.common.modulo import ModBuiltin, UInt384, run_mod_p_circuit
4+
from starkware.cairo.common.registers import get_label_location
5+
from starkware.cairo.common.alloc import alloc
6+
7+
func main{range_check_ptr, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*}() {
8+
alloc_locals;
9+
10+
11+
// The circuit definition can be described as:
12+
//
13+
// x1 (input)
14+
// x2 (input)
15+
// a1 = x1 + x2
16+
// a2 = ??
17+
// a3 = a1 * a2
18+
//
19+
// As the first mul gate tries to compute a3 when a2 is unkown, it will fail.
20+
21+
let p = UInt384(d0=1, d1=1, d2=0, d3=0);
22+
let x1 = UInt384(d0=1, d1=0, d2=0, d3=0);
23+
let x2 = UInt384(d0=2, d1=1, d2=0, d3=0);
24+
25+
let (local values_arr: UInt384*) = alloc();
26+
assert values_arr[0] = x1;
27+
assert values_arr[1] = x2;
28+
29+
let (local add_mod_offsets_arr: felt*) = alloc();
30+
assert add_mod_offsets_arr[0] = 0; // x1
31+
assert add_mod_offsets_arr[1] = 4; // x2
32+
assert add_mod_offsets_arr[2] = 8; // a1 = x1 + x2
33+
34+
let (local mul_mod_offsets_arr: felt*) = alloc();
35+
assert mul_mod_offsets_arr[0] = 8; // a1
36+
assert mul_mod_offsets_arr[1] = 12; // a2 (unknown)
37+
assert mul_mod_offsets_arr[2] = 16; // a1 * a2 (impossible)
38+
39+
run_mod_p_circuit(
40+
p=p,
41+
values_ptr=values_arr,
42+
add_mod_offsets_ptr=add_mod_offsets_arr,
43+
add_mod_n=1,
44+
mul_mod_offsets_ptr=mul_mod_offsets_arr,
45+
mul_mod_n=1,
46+
);
47+
48+
return ();
49+
}

vm/src/tests/cairo_run_test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,14 @@ fn cairo_run_mod_builtin_failure() {
11531153
run_program_with_custom_mod_builtin_params(program_data, false, 1, 3, Some(error_msg));
11541154
}
11551155

1156+
#[test]
1157+
#[cfg(feature = "mod_builtin")]
1158+
fn cairo_run_mod_builtin_no_solution() {
1159+
let program_data =
1160+
include_bytes!("../../../cairo_programs/mod_builtin_feature/mod_builtin_no_solution.json");
1161+
run_program_with_error(program_data, "Could not fill the values table");
1162+
}
1163+
11561164
#[test]
11571165
#[cfg(feature = "mod_builtin")]
11581166
fn cairo_run_mod_builtin_large_batch_size() {

vm/src/vm/runners/builtin_runner/modulo.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,13 @@ impl ModBuiltinRunner {
566566
if let Some((_, mul_mod_runner, _)) = mul_mod {
567567
if mul_mod_runner.fill_value(memory, &mul_mod_inputs, mul_mod_index)? {
568568
mul_mod_index += 1;
569+
continue;
570+
} else {
571+
return Err(RunnerError::FillMemoryCoudNotFillTable(
572+
add_mod_index,
573+
mul_mod_index,
574+
));
569575
}
570-
continue;
571576
}
572577
}
573578

0 commit comments

Comments
 (0)