-
Notifications
You must be signed in to change notification settings - Fork 32
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
log2 hint #454
Merged
+100
−4
Merged
log2 hint #454
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,97 @@ | ||
use std::collections::HashMap; | ||
|
||
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{ | ||
get_integer_from_var_name, insert_value_from_var_name, | ||
}; | ||
use cairo_vm::hint_processor::hint_processor_definition::HintReference; | ||
use cairo_vm::serde::deserialize_program::ApTracking; | ||
use cairo_vm::types::exec_scope::ExecutionScopes; | ||
use cairo_vm::vm::errors::hint_errors::HintError; | ||
use cairo_vm::vm::vm_core::VirtualMachine; | ||
use cairo_vm::Felt252; | ||
use indoc::indoc; | ||
use num_bigint::BigUint; | ||
use num_traits::{One, Zero}; | ||
|
||
use crate::hints::vars; | ||
|
||
pub const LOG2_CEIL: &str = indoc! {r#" | ||
from starkware.python.math_utils import log2_ceil | ||
ids.res = log2_ceil(ids.value)"# | ||
}; | ||
pub fn log2_ceil_hint( | ||
vm: &mut VirtualMachine, | ||
_exec_scopes: &mut ExecutionScopes, | ||
ids_data: &HashMap<String, HintReference>, | ||
ap_tracking: &ApTracking, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let value = get_integer_from_var_name(vars::ids::VALUE, vm, ids_data, ap_tracking)?; | ||
let res = log2_ceil(&value.to_biguint()); | ||
insert_value_from_var_name(vars::ids::RES, Felt252::from(res), vm, ids_data, ap_tracking)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn log2_ceil(value: &BigUint) -> u64 { | ||
assert!(!value.is_zero(), "log2_ceil is not defined for zero."); | ||
(value - &BigUint::one()).bits() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use cairo_vm::types::relocatable::Relocatable; | ||
use num_bigint::BigUint; | ||
use rstest::rstest; | ||
|
||
use super::*; | ||
|
||
#[rstest] | ||
// Powers of two | ||
#[case(1, 0)] // 1 = 2^0 | ||
#[case(2, 1)] // 2 = 2^1 | ||
#[case(4, 2)] // 4 = 2^2 | ||
#[case(8, 3)] // 8 = 2^3 | ||
#[case(1024, 10)] // 1024 = 2^10 | ||
|
||
// Non-powers of two | ||
#[case(3, 2)] // between 2 and 4, floor(log2(3))=1 => log2_ceil=2 | ||
#[case(5, 3)] // between 4 and 8, floor(log2(5))=2 => log2_ceil=3 | ||
#[case(6, 3)] // between 4 and 8, floor(log2(6))=2 => log2_ceil=3 | ||
#[case(9, 4)] // between 8 and 16, floor(log2(9))=3 => log2_ceil=4 | ||
fn test_log2_ceil_parameterized(#[case] value: u64, #[case] expected: u64) { | ||
let val = BigUint::from(value); | ||
assert_eq!(log2_ceil(&val), expected); | ||
test_log2_ceil_hint(value, expected); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "not defined for zero")] | ||
fn test_log2_ceil_zero() { | ||
let zero = BigUint::from(0u64); | ||
log2_ceil(&zero); | ||
} | ||
|
||
fn test_log2_ceil_hint(value: u64, expected: u64) { | ||
let mut vm = VirtualMachine::new(false); | ||
vm.add_memory_segment(); | ||
vm.add_memory_segment(); | ||
vm.set_fp(2); | ||
|
||
let ap_tracking = ApTracking::new(); | ||
let constants = HashMap::new(); | ||
let ids_data = HashMap::from([ | ||
(vars::ids::VALUE.to_string(), HintReference::new_simple(-2)), | ||
(vars::ids::RES.to_string(), HintReference::new_simple(-1)), | ||
]); | ||
|
||
vm.insert_value(Relocatable::from((1, 0)), Felt252::from(value)).unwrap(); | ||
|
||
let mut exec_scopes: ExecutionScopes = Default::default(); | ||
|
||
log2_ceil_hint(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking, &constants).unwrap(); | ||
|
||
let exp = get_integer_from_var_name(vars::ids::RES, &vm, &ids_data, &ap_tracking).unwrap(); | ||
|
||
assert_eq!(exp, Felt252::from(expected)) | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1 @@ | ||
use indoc::indoc; | ||
|
||
#[allow(unused)] | ||
pub const HINT_4: &str = indoc! {r#"exit_syscall(selector=ids.SHA256_PROCESS_BLOCK_SELECTOR)"#}; | ||
// This file is intended to list all the unimplemented hints during an OS upgrade |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We possibly just need to remove the hint from
unimplemented.rs