From 02e6029b6801a6f0067eeb116b6e470c538ef298 Mon Sep 17 00:00:00 2001 From: codyx Date: Fri, 13 Dec 2024 17:17:26 +0400 Subject: [PATCH] Add header verifier hints --- cairo_vm_hints/src/hint_processor/mod.rs | 8 +- .../lib/verifiers/evm/header_verifier.rs | 106 ++++++++++++++++++ .../src/hints/lib/verifiers/evm/mod.rs | 1 + cairo_vm_hints/src/hints/vars.rs | 1 + 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 cairo_vm_hints/src/hints/lib/verifiers/evm/header_verifier.rs diff --git a/cairo_vm_hints/src/hint_processor/mod.rs b/cairo_vm_hints/src/hint_processor/mod.rs index 40842150..6d732e23 100644 --- a/cairo_vm_hints/src/hint_processor/mod.rs +++ b/cairo_vm_hints/src/hint_processor/mod.rs @@ -84,7 +84,6 @@ impl CustomHintProcessor { hints.insert(lib::segments::SEGMENTS_ADD_EVM_MEMORIZER_OFFSET.into(), lib::segments::segments_add_evm_memorizer_offset); hints.insert(lib::segments::SEGMENTS_ADD_EVM_STARKNET_MEMORIZER_INDEX.into(), lib::segments::segments_add_evm_starknet_memorizer_index); hints.insert(lib::segments::SEGMENTS_ADD_STARKNET_MEMORIZER_OFFSET.into(), lib::segments::segments_add_starknet_memorizer_offset); - hints.insert(lib::verifiers::evm::account_verifier::HINT_BATCH_ACCOUNTS_LEN.into(), lib::verifiers::evm::account_verifier::hint_batch_accounts_len); hints.insert(lib::verifiers::evm::account_verifier::HINT_ACCOUNT_KEY.into(), lib::verifiers::evm::account_verifier::hint_account_key); hints.insert(lib::verifiers::evm::account_verifier::HINT_ACCOUNT_KEY_LEADING_ZEROS.into(), lib::verifiers::evm::account_verifier::hint_account_key_leading_zeros); hints.insert(lib::verifiers::evm::account_verifier::HINT_ACCOUNT_PROOF_AT.into(), lib::verifiers::evm::account_verifier::hint_account_proof_at); @@ -92,6 +91,7 @@ impl CustomHintProcessor { hints.insert(lib::verifiers::evm::account_verifier::HINT_ACCOUNT_PROOF_BYTES_LEN.into(), lib::verifiers::evm::account_verifier::hint_account_proof_bytes_len); hints.insert(lib::verifiers::evm::account_verifier::HINT_ACCOUNT_PROOFS_LEN.into(), lib::verifiers::evm::account_verifier::hint_account_proofs_len); hints.insert(lib::verifiers::evm::account_verifier::HINT_ACCOUNT_PROOF_LEN.into(), lib::verifiers::evm::account_verifier::hint_account_proof_len); + hints.insert(lib::verifiers::evm::account_verifier::HINT_BATCH_ACCOUNTS_LEN.into(), lib::verifiers::evm::account_verifier::hint_batch_accounts_len); hints.insert(lib::verifiers::evm::account_verifier::HINT_GET_ACCOUNT_ADDRESS.into(), lib::verifiers::evm::account_verifier::hint_get_account_address); hints.insert(lib::verifiers::evm::account_verifier::HINT_GET_MPT_PROOF.into(), lib::verifiers::evm::account_verifier::hint_get_mpt_proof); hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_BATCH_TRANSACTIONS_LEN.into(), lib::verifiers::evm::block_tx_verifier::hint_batch_transactions_len); @@ -102,6 +102,12 @@ impl CustomHintProcessor { hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_SET_TX_BLOCK_NUMBER.into(), lib::verifiers::evm::block_tx_verifier::hint_set_tx_block_number); hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_PROOF_BYTES_LEN.into(), lib::verifiers::evm::block_tx_verifier::hint_proof_bytes_len); hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_MPT_PROOF.into(), lib::verifiers::evm::block_tx_verifier::hint_mpt_proof); + hints.insert(lib::verifiers::evm::header_verifier::HINT_BATCH_HEADERS_LEN.into(), lib::verifiers::evm::header_verifier::hint_batch_headers_len); + hints.insert(lib::verifiers::evm::header_verifier::HINT_LEAF_IDX.into(), lib::verifiers::evm::header_verifier::hint_leaf_idx); + hints.insert(lib::verifiers::evm::header_verifier::HINT_MMR_PATH_LEN.into(), lib::verifiers::evm::header_verifier::hint_mmr_path_len); + hints.insert(lib::verifiers::evm::header_verifier::HINT_MMR_PATH.into(), lib::verifiers::evm::header_verifier::hint_mmr_path); + hints.insert(lib::verifiers::evm::header_verifier::HINT_RLP_LEN.into(), lib::verifiers::evm::header_verifier::hint_rlp_len); + hints.insert(lib::verifiers::evm::header_verifier::HINT_SET_HEADER.into(), lib::verifiers::evm::header_verifier::hint_set_header); hints.insert(lib::verifiers::verify::HINT_BATCH_LEN.into(), lib::verifiers::verify::hint_batch_len); hints.insert(lib::verifiers::verify::HINT_CHAIN_ID.into(), lib::verifiers::verify::hint_chain_id); hints.insert(lib::verifiers::verify::HINT_VM_ENTER_SCOPE.into(), lib::verifiers::verify::hint_vm_enter_scope); diff --git a/cairo_vm_hints/src/hints/lib/verifiers/evm/header_verifier.rs b/cairo_vm_hints/src/hints/lib/verifiers/evm/header_verifier.rs new file mode 100644 index 00000000..da52dcc7 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/verifiers/evm/header_verifier.rs @@ -0,0 +1,106 @@ +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_into_ap}, + }, + types::exec_scope::ExecutionScopes, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, + Felt252, +}; +use std::collections::HashMap; + +use crate::{ + hint_processor::models::proofs::{header::Header, Proofs}, + hints::vars, +}; + +pub const HINT_BATCH_HEADERS_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(batch.headers))"; + +pub fn hint_batch_headers_len( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let batch = exec_scopes.get::("batch")?; + + insert_value_into_ap(vm, Felt252::from(batch.headers.len())) +} + +pub const HINT_SET_HEADER: &str = "header = batch.headers[ids.idx - 1]\nsegments.write_arg(ids.rlp, [int(x, 16) for x in header.rlp])"; + +pub fn hint_set_header( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let batch = exec_scopes.get::("batch")?; + let idx: usize = get_integer_from_var_name(vars::ids::IDX, vm, &hint_data.ids_data, &hint_data.ap_tracking)? + .try_into() + .unwrap(); + let header = batch.headers[idx - 1].clone(); + let rlp = header.rlp.clone(); + + exec_scopes.insert_value::
("header", header); + + let rlp_ptr = get_ptr_from_var_name(vars::ids::RLP, vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + vm.write_arg(rlp_ptr, &rlp)?; + + Ok(()) +} + +pub const HINT_RLP_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(header.rlp))"; + +pub fn hint_rlp_len( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let header = exec_scopes.get::
("header")?; + + insert_value_into_ap(vm, Felt252::from(header.rlp.len())) +} + +pub const HINT_LEAF_IDX: &str = "memory[ap] = to_felt_or_relocatable(len(header.proof.leaf_idx))"; + +pub fn hint_leaf_idx( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let header = exec_scopes.get::
("header")?; + + insert_value_into_ap(vm, Felt252::from(header.proof.leaf_idx)) +} + +pub const HINT_MMR_PATH_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(header.proof.mmr_path))"; + +pub fn hint_mmr_path_len( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let header = exec_scopes.get::
("header")?; + + insert_value_into_ap(vm, Felt252::from(header.proof.mmr_path.len())) +} + +pub const HINT_MMR_PATH: &str = "segments.write_arg(ids.mmr_path, [int(x, 16) for x in header.proof.mmr_path])"; + +pub fn hint_mmr_path( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let header = exec_scopes.get::
("header")?; + + let mmr_path_ptr = get_ptr_from_var_name(vars::ids::MMR_PATH, vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + vm.write_arg(mmr_path_ptr, &header.proof.mmr_path)?; + + Ok(()) +} diff --git a/cairo_vm_hints/src/hints/lib/verifiers/evm/mod.rs b/cairo_vm_hints/src/hints/lib/verifiers/evm/mod.rs index 7118d73f..ad6b2ccd 100644 --- a/cairo_vm_hints/src/hints/lib/verifiers/evm/mod.rs +++ b/cairo_vm_hints/src/hints/lib/verifiers/evm/mod.rs @@ -1,2 +1,3 @@ pub mod account_verifier; pub mod block_tx_verifier; +pub mod header_verifier; diff --git a/cairo_vm_hints/src/hints/vars.rs b/cairo_vm_hints/src/hints/vars.rs index 774f749b..d5475e25 100644 --- a/cairo_vm_hints/src/hints/vars.rs +++ b/cairo_vm_hints/src/hints/vars.rs @@ -30,6 +30,7 @@ pub mod ids { pub(crate) const KEY_LEADING_ZEROS: &str = "ids.key_leading_zeros"; pub(crate) const KEY_HIGH: &str = "ids.key_high"; pub(crate) const LEFT: &str = "ids.left"; + pub(crate) const MMR_PATH: &str = "ids.mmr_path"; pub(crate) const MPT_PROOF: &str = "ids.mpt_proof"; pub(crate) const PROOF_BYTES_LEN: &str = "ids.proof_bytes_len"; pub(crate) const N_BUILTINS: &str = "n_builtins";