Skip to content

Commit

Permalink
dw_hack_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Oct 22, 2024
1 parent 507e6d4 commit 422e668
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 12 deletions.
14 changes: 14 additions & 0 deletions cairo_vm_hints/src/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ fn run_hint(
lib::bit_length::HINT_BIT_LENGTH => {
lib::bit_length::hint_bit_length(vm, exec_scope, hint_data, constants)
}
tests::dw_hack::HINT_BIT_LENGTH_ASSIGN_140 => {
tests::dw_hack::hint_bit_length_assign_140(vm, exec_scope, hint_data, constants)
}
tests::dw_hack::HINT_BIT_LENGTH_ASSIGN_2500 => {
tests::dw_hack::hint_bit_length_assign_2500(vm, exec_scope, hint_data, constants)
}
tests::dw_hack::HINT_BIT_LENGTH_ASSIGN_NEGATIVE_ONE => {
tests::dw_hack::hint_bit_length_assign_negative_one(
vm, exec_scope, hint_data, constants,
)
}
tests::dw_hack::HINT_PRINT_NS => {
tests::dw_hack::hint_print_ns(vm, exec_scope, hint_data, constants)
}
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
Expand Down
82 changes: 82 additions & 0 deletions cairo_vm_hints/src/hints/tests/dw_hack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
};
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use std::collections::HashMap;

pub const HINT_BIT_LENGTH_ASSIGN_140: &str = "ids.bit_length = 140";

pub fn hint_bit_length_assign_140(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
insert_value_from_var_name(
"bit_length",
MaybeRelocatable::Int(Felt252::from_hex_unchecked("0x8C")),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

Ok(())
}

pub const HINT_BIT_LENGTH_ASSIGN_NEGATIVE_ONE: &str = "ids.bit_length = -1";

pub fn hint_bit_length_assign_negative_one(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
insert_value_from_var_name(
"bit_length",
MaybeRelocatable::Int(Felt252::ZERO - Felt252::ONE),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

Ok(())
}

pub const HINT_BIT_LENGTH_ASSIGN_2500: &str = "ids.bit_length = 2500";

pub fn hint_bit_length_assign_2500(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
insert_value_from_var_name(
"bit_length",
MaybeRelocatable::Int(Felt252::from_hex_unchecked("0x9C4")),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

Ok(())
}

pub const HINT_PRINT_NS: &str = "print(\"N\", ids.N, \"n\", ids.n)";

pub fn hint_print_ns(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
println!(
"N: {}, n: {}",
get_integer_from_var_name("N", vm, &hint_data.ids_data, &hint_data.ap_tracking)?,
get_integer_from_var_name("n", vm, &hint_data.ids_data, &hint_data.ap_tracking)?
);
Ok(())
}
1 change: 1 addition & 0 deletions cairo_vm_hints/src/hints/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod dw_hack;
pub mod mmr_size_generate;
pub mod print;
12 changes: 12 additions & 0 deletions cairo_vm_hints/src/tests/dw_hack_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use super::run_cairo_program;

#[test]
fn test() {
let cairo_runner = run_cairo_program(include_bytes!(
"../../../build/compiled_cairo_files/dw_hack_test.json"
))
.unwrap();

let execution_resources = cairo_runner.get_execution_resources().unwrap();
println!("n_steps: {}", execution_resources.n_steps)
}
1 change: 1 addition & 0 deletions cairo_vm_hints/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod dw_hack_test;
pub mod is_valid_mmr_size;

use crate::ExtendedHintProcessor;
Expand Down
15 changes: 3 additions & 12 deletions tests/cairo_programs/dw_hack_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ func main{range_check_ptr}() {
func compute_height_pre_alloc_pow2_hack0{range_check_ptr, pow2_array: felt*}(x: felt) -> felt {
alloc_locals;
local bit_length;
%{
x = ids.x
ids.bit_length = 140
%}
%{ ids.bit_length = 140 %}
// Computes N=2^bit_length and n=2^(bit_length-1)
// x is supposed to verify n = 2^(b-1) <= x < N = 2^bit_length <=> x has bit_length bits

Expand Down Expand Up @@ -55,10 +52,7 @@ func compute_height_pre_alloc_pow2_hack0{range_check_ptr, pow2_array: felt*}(x:
func compute_height_pre_alloc_pow2_hack1{range_check_ptr, pow2_array: felt*}(x: felt) -> felt {
alloc_locals;
local bit_length;
%{
x = ids.x
ids.bit_length = -1
%}
%{ ids.bit_length = -1 %}
// Computes N=2^bit_length and n=2^(bit_length-1)
// x is supposed to verify n = 2^(b-1) <= x < N = 2^bit_length <=> x has bit_length bits

Expand Down Expand Up @@ -86,10 +80,7 @@ func compute_height_pre_alloc_pow2_hack1{range_check_ptr, pow2_array: felt*}(x:
func compute_height_pre_alloc_pow2_hack2{range_check_ptr, pow2_array: felt*}(x: felt) -> felt {
alloc_locals;
local bit_length;
%{
x = ids.x
ids.bit_length = 2500
%}
%{ ids.bit_length = 2500 %}
// Computes N=2^bit_length and n=2^(bit_length-1)
// x is supposed to verify n = 2^(b-1) <= x < N = 2^bit_length <=> x has bit_length bits

Expand Down

0 comments on commit 422e668

Please sign in to comment.