Skip to content

Commit

Permalink
Add EXCESS_BALANCE hint (#1777)
Browse files Browse the repository at this point in the history
* WIP

* Add MarginParams

* Impl imf

* Start excess_balance fn

* Add TODO comment

* refactor + handle errors

* Remove unused error

* Finish implementing hint

* Handle error

* Fix var names

* Setup hint test

* Fix test

* Fix test

* Fix test

* Fix test values

* Fix

* Update

* Fix

* Use constants

* Remove unwrap

* Remove allow

* Remove unused feature

* Add failure test case

* Fix test values

* Add no-std imports

* Add no-std imports

* Add hint code

* Add changelog entry

* Use checked arithmetic operations
  • Loading branch information
fmoletta committed May 30, 2024
1 parent c745ece commit c65591e
Show file tree
Hide file tree
Showing 9 changed files with 1,268 additions and 0 deletions.
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

* feat: Add `EXCESS_BALANCE` hint [#1777](https://github.com/lambdaclass/cairo-vm/pull/1777)

* feat(BREAKING): Use a cheatcode to relocate all dicts + Make temporary segment usage configurable [#1776](https://github.com/lambdaclass/cairo-vm/pull/1776)
* Add the flags `segment_arena_validation` & `use_temporary_segments` to the `Cairo1HintProcessor` & `DictManagerExecScope` respectively. These flags will determine if real segments or temporary segments will be used when creating dictionaries.
* `DictManagerExecScope::finalize_segment` no longer performs relocation and is ignored if `use_temporary_segments` is set to false.
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ hashbrown = { workspace = true }
anyhow = { workspace = true }
thiserror-no-std = { workspace = true }
starknet-types-core = { version = "0.1.2", default-features = false, features = ["serde", "curve", "num-traits", "hash"] }
rust_decimal = { version = "1.35.0", default-features = false }

# only for std
num-prime = { version = "0.4.3", features = ["big-int"], optional = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{
ec_recover_divmod_n_packed, ec_recover_product_div_m, ec_recover_product_mod,
ec_recover_sub_a_b,
},
excess_balance::excess_balance_hint,
field_arithmetic::{u256_get_square_root, u384_get_square_root, uint384_div},
mod_circuit::{run_p_mod_circuit, run_p_mod_circuit_with_large_batch_size},
secp::{
Expand Down Expand Up @@ -866,6 +867,13 @@ impl HintProcessorLogic for BuiltinHintProcessor {
hint_code::PRINT_DICT => {
print_dict(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::EXCESS_BALANCE => excess_balance_hint(
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
constants,
exec_scopes,
),
code => Err(HintError::UnknownHint(code.to_string().into_boxed_str())),
}
}
Expand Down
11 changes: 11 additions & 0 deletions vm/src/hint_processor/builtin_hint_processor/dict_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ impl DictTracker {
}
}

//Returns a reference to the contained dictionary, losing the dictionary type in the process
pub fn get_dictionary_ref(&self) -> &HashMap<MaybeRelocatable, MaybeRelocatable> {
match &self.data {
Dictionary::SimpleDictionary(dict) => dict,
Dictionary::DefaultDictionary {
dict,
default_value: _,
} => dict,
}
}

pub fn get_value(&mut self, key: &MaybeRelocatable) -> Result<&MaybeRelocatable, HintError> {
self.data
.get(key)
Expand Down
Loading

0 comments on commit c65591e

Please sign in to comment.