-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from HerodotusDev/hints/rlp
- Loading branch information
Showing
8 changed files
with
307 additions
and
14 deletions.
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
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,5 @@ | ||
pub mod contract_bootloader; | ||
pub mod decoder; | ||
pub mod print; | ||
pub mod rlp; | ||
pub mod segments; |
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,122 @@ | ||
use cairo_vm::{ | ||
hint_processor::builtin_hint_processor::{ | ||
builtin_hint_processor_definition::HintProcessorData, | ||
hint_utils::{ | ||
get_integer_from_var_name, get_ptr_from_var_name, insert_value_from_var_name, | ||
}, | ||
}, | ||
types::relocatable::MaybeRelocatable, | ||
}; | ||
use cairo_vm::{ | ||
types::exec_scope::ExecutionScopes, | ||
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, | ||
Felt252, | ||
}; | ||
use starknet_types_core::felt::NonZeroFelt; | ||
use std::collections::HashMap; | ||
|
||
use crate::hints::vars; | ||
|
||
pub const HINT_DIVMOD_RLP: &str = "ids.q, ids.r = divmod(memory[ids.rlp + ids.i], ids.devisor)"; | ||
|
||
pub fn hint_divmod_rlp( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let rlp = get_ptr_from_var_name( | ||
vars::ids::RLP, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let i: usize = get_integer_from_var_name( | ||
vars::ids::I, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)? | ||
.try_into() | ||
.unwrap(); | ||
|
||
let devisor = get_integer_from_var_name( | ||
vars::ids::DEVISOR, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let (q, r) = vm | ||
.get_integer((rlp + i)?)? | ||
.div_rem(&NonZeroFelt::try_from(devisor).unwrap()); | ||
|
||
insert_value_from_var_name( | ||
vars::ids::Q, | ||
MaybeRelocatable::Int(q), | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
insert_value_from_var_name( | ||
vars::ids::R, | ||
MaybeRelocatable::Int(r), | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
) | ||
} | ||
|
||
pub const HINT_DIVMOD_VALUE: &str = "ids.q, ids.r = divmod(memory[ids.value + ids.i], ids.devisor)"; | ||
|
||
pub fn hint_divmod_value( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let value = get_ptr_from_var_name( | ||
vars::ids::VALUE, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let i: usize = get_integer_from_var_name( | ||
vars::ids::I, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)? | ||
.try_into() | ||
.unwrap(); | ||
|
||
let devisor = get_integer_from_var_name( | ||
vars::ids::DEVISOR, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let (q, r) = vm | ||
.get_integer((value + i)?)? | ||
.div_rem(&NonZeroFelt::try_from(devisor).unwrap()); | ||
|
||
insert_value_from_var_name( | ||
vars::ids::Q, | ||
MaybeRelocatable::Int(q), | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
insert_value_from_var_name( | ||
vars::ids::R, | ||
MaybeRelocatable::Int(r), | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
) | ||
} |
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,88 @@ | ||
use crate::hints::vars; | ||
|
||
use super::*; | ||
use cairo_vm::{ | ||
hint_processor::builtin_hint_processor::{ | ||
builtin_hint_processor_definition::HintProcessorData, | ||
hint_utils::{get_integer_from_var_name, insert_value_from_var_name}, | ||
}, | ||
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, | ||
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, | ||
Felt252, | ||
}; | ||
use std::collections::HashMap; | ||
|
||
pub const HINT_IS_LONG: &str = | ||
"ids.is_long = 0 if 0xc0 <= ids.first_byte <= 0xf6 else 1 if 0xf7 <= ids.first_byte <= 0xff else assert False, 'Invalid RLP list'"; | ||
|
||
pub fn hint_is_long( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let first_byte = get_integer_from_var_name( | ||
vars::ids::FIRST_BYTE, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let insert = if FELT_C0 <= first_byte && first_byte <= FELT_F6 { | ||
Felt252::ZERO | ||
} else if FELT_F7 <= first_byte && first_byte <= FELT_FF { | ||
Felt252::ONE | ||
} else { | ||
return Err(HintError::UnknownIdentifier( | ||
"Invalid RLP list".to_string().into_boxed_str(), | ||
)); | ||
}; | ||
|
||
insert_value_from_var_name( | ||
vars::ids::IS_LONG, | ||
MaybeRelocatable::Int(insert), | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
) | ||
} | ||
|
||
pub const HINT_ITEM_TYPE: &str = "ids.item_type = 0 if ids.current_item <= 0x7f else 1 if 0x80 <= ids.current_item <= 0xb6 else 2 if 0xb7 <= ids.current_item <= 0xbf else 3 if 0xc0 <= ids.current_item <= 0xf6 else 4 if 0xf7 <= ids.current_item <= 0xff else assert False, 'Invalid RLP item'"; | ||
|
||
pub fn hint_item_type( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let current_item = get_integer_from_var_name( | ||
vars::ids::CURRENT_ITEM, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let insert = if current_item <= FELT_7F { | ||
Felt252::ZERO | ||
} else if FELT_80 <= current_item && current_item <= FELT_B6 { | ||
Felt252::ONE // short string | ||
} else if FELT_B7 <= current_item && current_item <= FELT_BF { | ||
Felt252::TWO // long string | ||
} else if FELT_C0 <= current_item && current_item <= FELT_F6 { | ||
Felt252::THREE // short list | ||
} else if FELT_F7 <= current_item && current_item <= FELT_FF { | ||
FELT_4 // long list | ||
} else { | ||
return Err(HintError::UnknownIdentifier( | ||
"Invalid RLP item".to_string().into_boxed_str(), | ||
)); | ||
}; | ||
|
||
insert_value_from_var_name( | ||
vars::ids::ITEM_TYPE, | ||
MaybeRelocatable::Int(insert), | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
) | ||
} |
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,16 @@ | ||
pub mod divmod; | ||
pub mod item_type; | ||
pub mod processed_words; | ||
|
||
use cairo_vm::Felt252; | ||
|
||
pub const FELT_4: Felt252 = Felt252::from_hex_unchecked("0x04"); | ||
pub const FELT_7F: Felt252 = Felt252::from_hex_unchecked("0x7f"); | ||
pub const FELT_80: Felt252 = Felt252::from_hex_unchecked("0x80"); | ||
pub const FELT_B6: Felt252 = Felt252::from_hex_unchecked("0xb6"); | ||
pub const FELT_B7: Felt252 = Felt252::from_hex_unchecked("0xb7"); | ||
pub const FELT_BF: Felt252 = Felt252::from_hex_unchecked("0xbf"); | ||
pub const FELT_C0: Felt252 = Felt252::from_hex_unchecked("0xc0"); | ||
pub const FELT_F6: Felt252 = Felt252::from_hex_unchecked("0xf6"); | ||
pub const FELT_F7: Felt252 = Felt252::from_hex_unchecked("0xf7"); | ||
pub const FELT_FF: Felt252 = Felt252::from_hex_unchecked("0xff"); |
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,45 @@ | ||
use cairo_vm::{ | ||
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData, | ||
hint_processor::builtin_hint_processor::hint_utils::{ | ||
get_integer_from_var_name, insert_value_into_ap, | ||
}, | ||
}; | ||
use cairo_vm::{ | ||
types::exec_scope::ExecutionScopes, | ||
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, | ||
Felt252, | ||
}; | ||
use std::{cmp::Ordering, collections::HashMap}; | ||
|
||
use crate::hints::vars; | ||
|
||
pub const HINT_PROCESSED_WORDS: &str = | ||
"memory[ap] = 1 if (ids.value_len - ids.n_processed_words == 0) else 0"; | ||
|
||
pub fn hint_processed_words( | ||
vm: &mut VirtualMachine, | ||
_exec_scope: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let value_len = get_integer_from_var_name( | ||
vars::ids::VALUE_LEN, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let n_processed_words = get_integer_from_var_name( | ||
vars::ids::N_PROCESSED_WORDS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let insert = match (value_len - n_processed_words).cmp(&Felt252::ZERO) { | ||
Ordering::Equal => Felt252::ONE, | ||
_ => Felt252::ZERO, | ||
}; | ||
|
||
insert_value_into_ap(vm, insert) | ||
} |
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