-
Notifications
You must be signed in to change notification settings - Fork 193
Add test for simulated builtins #2167
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cd8b2e0
Add get_simulated_builtin_base hint for testing purposes
JulianGCalderon 7ed8e26
Add simulated_builtins.cairo example
JulianGCalderon 3a937af
Document simulated builtins field
JulianGCalderon c6c5e3e
Add a test for simulated builtins
JulianGCalderon 9ca4500
Merge branch 'main' into simulated-builtin-test
JulianGCalderon 184b798
Add documentation
JulianGCalderon 3cb56c0
Merge branch 'main' into simulated-builtin-test
JulianGCalderon 48cb8d1
Merge branch 'main' into simulated-builtin-test
JulianGCalderon 6f0ff13
Merge branch 'main' into simulated-builtin-test
JulianGCalderon 692a730
Merge branch 'main' into simulated-builtin-test
JulianGCalderon c23fdb0
Fix typos
JulianGCalderon f2de2ef
Merge branch 'main' into simulated-builtin-test
JulianGCalderon 916f768
Merge branch 'main' into simulated-builtin-test
gabrielbosio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
%builtins ec_op | ||
|
||
from starkware.cairo.common.cairo_builtins import EcOpBuiltin | ||
from starkware.cairo.common.ec_point import EcPoint | ||
from starkware.cairo.common.ec import ec_op | ||
|
||
func main{ec_op_ptr: EcOpBuiltin*}() { | ||
alloc_locals; | ||
let (local ec_op_ptr) = init_ec_op(ec_op_ptr=ec_op_ptr); | ||
|
||
let p = EcPoint( | ||
0x6a4beaef5a93425b973179cdba0c9d42f30e01a5f1e2db73da0884b8d6756fc, | ||
0x72565ec81bc09ff53fbfad99324a92aa5b39fb58267e395e8abe36290ebf24f, | ||
); | ||
let m = 34; | ||
let q = EcPoint( | ||
0x654fd7e67a123dd13868093b3b7777f1ffef596c2e324f25ceaf9146698482c, | ||
0x4fad269cbf860980e38768fe9cb6b0b9ab03ee3fe84cfde2eccce597c874fd8, | ||
); | ||
let (r) = ec_op(p, m, q); | ||
assert r.x = 108925483682366235368969256555281508851459278989259552980345066351008608800; | ||
assert r.y = 1592365885972480102953613056006596671718206128324372995731808913669237079419; | ||
return (); | ||
} | ||
|
||
// Initializes the ec_op builtin pointer if not initialized. | ||
// | ||
// If the builtin is included in the layout, the ec_op_ptr will be valid, and | ||
// this function will do nothing. If the builtin is not included in the layout, | ||
// then it will obtain the pointer of the ec_op simulated builtin and return | ||
// it. For this to work properly, the runner must have the ec_op simulated | ||
// builtin runner at index 0. | ||
func init_ec_op(ec_op_ptr: EcOpBuiltin*) -> (ec_op_ptr: EcOpBuiltin*) { | ||
if (ec_op_ptr != 0) { | ||
return (ec_op_ptr=ec_op_ptr); | ||
} | ||
|
||
alloc_locals; | ||
local builtin_idx = 0; | ||
local new_ptr; | ||
|
||
// This hint is not defined in the original VM, | ||
// and its declared for testing purposes only. | ||
%{ ids.new_ptr = get_simulated_builtin_base(ids.builtin_idx) %} | ||
|
||
return (ec_op_ptr=cast(new_ptr, EcOpBuiltin*)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
vm/src/hint_processor/builtin_hint_processor/simulated_builtins.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::hint_processor::hint_processor_definition::HintReference; | ||
use crate::stdlib::collections::HashMap; | ||
use crate::types::relocatable::Relocatable; | ||
use crate::Felt252; | ||
use crate::{ | ||
serde::deserialize_program::ApTracking, | ||
types::exec_scope::ExecutionScopes, | ||
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, | ||
}; | ||
use num_traits::ToPrimitive; | ||
|
||
use super::hint_utils::{get_integer_from_var_name, insert_value_from_var_name}; | ||
|
||
pub const GET_SIMULATED_BUILTIN_BASE: &str = | ||
"ids.new_ptr = get_simulated_builtin_base(ids.builtin_idx)"; | ||
|
||
/// Obtains the simulated builtin runner base, at the given index. The simulated | ||
/// builtin runner must be initialized before the execution. | ||
/// | ||
/// This hint is not defined in the original VM, and its declared for testing | ||
/// purposes only. | ||
pub fn get_simulated_builtin_base( | ||
vm: &mut VirtualMachine, | ||
_exec_scopes: &mut ExecutionScopes, | ||
ids_data: &HashMap<String, HintReference>, | ||
ap_tracking: &ApTracking, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
// Obtain the simulated builtin runner from the builtin_idx variable. | ||
let builtin_idx = get_integer_from_var_name("builtin_idx", vm, ids_data, ap_tracking)? | ||
.to_usize() | ||
.ok_or(HintError::BigintToUsizeFail)?; | ||
let builtin_runner = &vm.simulated_builtin_runners[builtin_idx]; | ||
|
||
// Set new_ptr with the value of the builtin runner base. | ||
insert_value_from_var_name( | ||
"new_ptr", | ||
Relocatable { | ||
segment_index: builtin_runner.base() as isize, | ||
offset: 0, | ||
}, | ||
vm, | ||
ids_data, | ||
ap_tracking, | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.