From 3deb96c5aabd36bd8c24d82f083e637ec9fabf1f Mon Sep 17 00:00:00 2001 From: codyx Date: Fri, 13 Dec 2024 16:53:08 +0400 Subject: [PATCH] Add block_tx_verifier related hints --- cairo_vm_hints/src/hint_processor/mod.rs | 8 + .../lib/verifiers/evm/block_tx_verifier.rs | 159 ++++++++++++++++++ .../src/hints/lib/verifiers/evm/mod.rs | 1 + 3 files changed, 168 insertions(+) create mode 100644 cairo_vm_hints/src/hints/lib/verifiers/evm/block_tx_verifier.rs diff --git a/cairo_vm_hints/src/hint_processor/mod.rs b/cairo_vm_hints/src/hint_processor/mod.rs index fea6f492..40842150 100644 --- a/cairo_vm_hints/src/hint_processor/mod.rs +++ b/cairo_vm_hints/src/hint_processor/mod.rs @@ -94,6 +94,14 @@ impl CustomHintProcessor { 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_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); + hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_SET_TX.into(), lib::verifiers::evm::block_tx_verifier::hint_set_tx); + hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_SET_TX_KEY.into(), lib::verifiers::evm::block_tx_verifier::hint_set_tx_key); + hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_SET_TX_KEY_LEADING_ZEROS.into(), lib::verifiers::evm::block_tx_verifier::hint_set_tx_key_leading_zeros); + hints.insert(lib::verifiers::evm::block_tx_verifier::HINT_SET_TX_PROOF_LEN.into(), lib::verifiers::evm::block_tx_verifier::hint_set_tx_proof_len); + 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::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/block_tx_verifier.rs b/cairo_vm_hints/src/hints/lib/verifiers/evm/block_tx_verifier.rs new file mode 100644 index 00000000..a347b352 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/verifiers/evm/block_tx_verifier.rs @@ -0,0 +1,159 @@ +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, 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::{transaction::Transaction, Proofs}, + hints::{lib::utils::count_leading_zero_nibbles_from_hex, vars}, +}; + +pub const HINT_BATCH_TRANSACTIONS_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(batch.transactions))"; + +pub fn hint_batch_transactions_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.transactions.len())) +} + +pub const HINT_SET_TX: &str = "transaction = transactions[ids.idx]"; + +pub fn hint_set_tx( + 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 transaction = batch.transactions[idx].clone(); + + exec_scopes.insert_value::("transaction", transaction); + + Ok(()) +} + +pub const HINT_SET_TX_KEY: &str = "from tools.py.utils import split_128\n(ids.key.low, ids.key.high) = split_128(int(transaction.key, 16))"; + +pub fn hint_set_tx_key( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let transaction = exec_scopes.get::("transaction")?; + let key = transaction.key; + + let key_as_limbs = key.as_limbs(); + let key_low = key_as_limbs[0] as u128 | ((key_as_limbs[1] as u128) << 64); + let key_high = key_as_limbs[2] as u128 | ((key_as_limbs[3] as u128) << 64); + + insert_value_from_var_name( + vars::ids::KEY_LOW, + Felt252::from(key_low), + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + )?; + insert_value_from_var_name( + vars::ids::KEY_HIGH, + Felt252::from(key_high), + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + )?; + + Ok(()) +} + +pub const HINT_SET_TX_KEY_LEADING_ZEROS: &str = + "ids.key_leading_zeros = len(transaction.key.lstrip(\"0x\")) - len(transaction.key.lstrip(\"0x\").lstrip(\"0\"))"; + +pub fn hint_set_tx_key_leading_zeros( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let transaction = exec_scopes.get::("transaction")?; + let key_leading_zeros = count_leading_zero_nibbles_from_hex(&format!("{:x}", transaction.key)); + + insert_value_from_var_name( + vars::ids::KEY_LEADING_ZEROS, + Felt252::from(key_leading_zeros), + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + ) +} + +pub const HINT_SET_TX_PROOF_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(transaction.proof))"; + +pub fn hint_set_tx_proof_len( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let transaction = exec_scopes.get::("transaction")?; + + insert_value_into_ap(vm, Felt252::from(transaction.proof.proof.len())) +} + +pub const HINT_SET_TX_BLOCK_NUMBER: &str = "memory[ap] = to_felt_or_relocatable(transaction.block_number)"; + +pub fn hint_set_tx_block_number( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let transaction = exec_scopes.get::("transaction")?; + + insert_value_into_ap(vm, Felt252::from(transaction.proof.block_number)) +} + +pub const HINT_PROOF_BYTES_LEN: &str = "segments.write_arg(ids.proof_bytes_len, transaction.proof_bytes_len)"; + +pub fn hint_proof_bytes_len( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let transaction = exec_scopes.get::("transaction")?; + + let proof_bytes_len_ptr = get_ptr_from_var_name(vars::ids::PROOF_BYTES_LEN, vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + vm.insert_value(proof_bytes_len_ptr, Felt252::from(transaction.proof.proof_bytes_len))?; + + Ok(()) +} + +pub const HINT_MPT_PROOF: &str = "segments.write_arg(ids.mpt_proof, [int(x, 16) for x in transaction.proof])"; + +pub fn hint_mpt_proof( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let transaction = exec_scopes.get::("transaction")?; + + let mpt_proof_ptr = get_ptr_from_var_name(vars::ids::MPT_PROOF, vm, &hint_data.ids_data, &hint_data.ap_tracking)?; + vm.write_arg(mpt_proof_ptr, &transaction.proof.proof)?; + + 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 158bbb7c..7118d73f 100644 --- a/cairo_vm_hints/src/hints/lib/verifiers/evm/mod.rs +++ b/cairo_vm_hints/src/hints/lib/verifiers/evm/mod.rs @@ -1 +1,2 @@ pub mod account_verifier; +pub mod block_tx_verifier;