Skip to content

Commit de9cb2b

Browse files
authored
Implement PyCairoRunner::add_additional_hash_builtin(). (#144)
* Implement `PyCairoRunner::add_additional_hash_builtin()`. * Fix test. * Remove `hash_func` argument. * Remove unnecessary test. * Add test. * Empty commit to trigger CI.
1 parent 56490ea commit de9cb2b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ default = ["pyo3/num-bigint", "pyo3/auto-initialize"]
1313

1414
[dependencies]
1515
pyo3 = { version = "0.16.5" }
16-
cairo-rs = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "8c47dda53e874545895b34d675be6254878a9e7b" }
16+
cairo-rs = { git = "https://github.com/lambdaclass/cairo-rs.git", rev = "c7c3e4fca78d38da968e92c6586ec3a518697745" }
1717
num-bigint = "0.4"
1818
lazy_static = "1.4.0"
1919

src/cairo_runner.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,12 @@ impl PyCairoRunner {
534534
.collect::<Vec<_>>()
535535
.to_object(py))
536536
}
537+
538+
/// Add (or replace if already present) a custom hash builtin.
539+
pub fn add_additional_hash_builtin(&self) {
540+
let mut vm = (*self.pyvm.vm).borrow_mut();
541+
self.inner.add_additional_hash_builtin(&mut vm);
542+
}
537543
}
538544

539545
#[pyclass]
@@ -1198,6 +1204,10 @@ mod test {
11981204
segment_index: 6,
11991205
offset: 0,
12001206
})],
1207+
vec![RelocatableValue(PyRelocatable {
1208+
segment_index: 7,
1209+
offset: 0,
1210+
})],
12011211
];
12021212

12031213
Python::with_gil(|py| {
@@ -1491,4 +1501,37 @@ mod test {
14911501
);
14921502
});
14931503
}
1504+
1505+
/// Test that add_additional_hash_builtin() returns successfully.
1506+
#[test]
1507+
fn add_additional_hash_builtin() {
1508+
Python::with_gil(|_| {
1509+
let program = fs::read_to_string("cairo_programs/fibonacci.json").unwrap();
1510+
let runner = PyCairoRunner::new(
1511+
program,
1512+
Some("main".to_string()),
1513+
Some("small".to_string()),
1514+
false,
1515+
)
1516+
.unwrap();
1517+
1518+
runner.add_additional_hash_builtin();
1519+
assert_eq!(
1520+
(*runner.pyvm.vm)
1521+
.borrow()
1522+
.get_builtin_runners()
1523+
.last()
1524+
.map(|(key, _)| key.as_str()),
1525+
Some("hash_builtin"),
1526+
);
1527+
1528+
let mut vm = (*runner.pyvm.vm).borrow_mut();
1529+
// Check that the segment exists by writing to it.
1530+
vm.insert_value(
1531+
&Relocatable::from((0, 0)),
1532+
MaybeRelocatable::Int(bigint!(42)),
1533+
)
1534+
.expect("memory insert failed");
1535+
});
1536+
}
14941537
}

0 commit comments

Comments
 (0)