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

perf: verified addresses for Pedersen as Vec<bool> #1029

Merged
merged 3 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#### Upcoming Changes

* Optimizations for hash builtin [#1029](https://github.com/lambdaclass/cairo-rs/pull/1029):
* Track the verified addresses by offset in a `Vec<bool>` rather than storing the address in a `Vec<Relocatable>`

* Add missing hint on uint256_improvements lib [#1025](https://github.com/lambdaclass/cairo-rs/pull/1025):

`BuiltinHintProcessor` now supports the following hint:
Expand Down
21 changes: 16 additions & 5 deletions src/vm/runners/builtin_runner/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ pub struct HashBuiltinRunner {
pub(crate) instances_per_component: u32,
// This act as a cache to optimize calls to deduce_memory_cell
// Therefore need interior mutability
pub(self) verified_addresses: RefCell<Vec<Relocatable>>,
// 1 at position 'n' means offset 'n' relative to base pointer
// has been verified
pub(self) verified_addresses: RefCell<Vec<bool>>,
}

impl HashBuiltinRunner {
Expand Down Expand Up @@ -72,7 +74,11 @@ impl HashBuiltinRunner {
.offset
.mod_floor(&(self.cells_per_instance as usize))
!= 2
|| self.verified_addresses.borrow().contains(&address)
|| *self
.verified_addresses
.borrow()
.get(address.offset)
.unwrap_or(&false)
{
return Ok(None);
};
Expand All @@ -89,7 +95,12 @@ impl HashBuiltinRunner {
num_a.as_ref().map(|x| x.as_ref()),
num_b.as_ref().map(|x| x.as_ref()),
) {
self.verified_addresses.borrow_mut().push(address);
if self.verified_addresses.borrow().len() <= address.offset {
self.verified_addresses
.borrow_mut()
.resize(address.offset + 1, false);
}
self.verified_addresses.borrow_mut()[address.offset] = true;

//Convert MaybeRelocatable to FieldElement
let a_string = num_a.to_str_radix(10);
Expand Down Expand Up @@ -404,7 +415,7 @@ mod tests {
);
assert_eq!(
builtin.verified_addresses.into_inner(),
vec![Relocatable::from((0, 5))]
vec![false, false, false, false, false, true],
);
}

Expand All @@ -431,7 +442,7 @@ mod tests {
fn deduce_memory_cell_pedersen_for_preset_memory_already_computed() {
let memory = memory![((0, 3), 32), ((0, 4), 72), ((0, 5), 0)];
let mut builtin = HashBuiltinRunner::new(Some(8), true);
builtin.verified_addresses = RefCell::new(vec![Relocatable::from((0, 5))]);
builtin.verified_addresses = RefCell::new(vec![false, false, false, false, false, true]);
let result = builtin.deduce_memory_cell(Relocatable::from((0, 5)), &memory);
assert_eq!(result, Ok(None));
}
Expand Down