Skip to content

Commit

Permalink
Add header verifier hints
Browse files Browse the repository at this point in the history
  • Loading branch information
codyx committed Dec 13, 2024
1 parent 3deb96c commit 02e6029
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cairo_vm_hints/src/hint_processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ 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);
hints.insert(lib::verifiers::evm::account_verifier::HINT_ACCOUNT_PROOF_BLOCK_NUMBER.into(), lib::verifiers::evm::account_verifier::hint_account_proof_block_number);
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);
Expand All @@ -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);
Expand Down
106 changes: 106 additions & 0 deletions cairo_vm_hints/src/hints/lib/verifiers/evm/header_verifier.rs
Original file line number Diff line number Diff line change
@@ -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<String, Felt252>,
) -> Result<(), HintError> {
let batch = exec_scopes.get::<Proofs>("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<String, Felt252>,
) -> Result<(), HintError> {
let batch = exec_scopes.get::<Proofs>("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", 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<String, Felt252>,
) -> Result<(), HintError> {
let header = exec_scopes.get::<Header>("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<String, Felt252>,
) -> Result<(), HintError> {
let header = exec_scopes.get::<Header>("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<String, Felt252>,
) -> Result<(), HintError> {
let header = exec_scopes.get::<Header>("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<String, Felt252>,
) -> Result<(), HintError> {
let header = exec_scopes.get::<Header>("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(())
}
1 change: 1 addition & 0 deletions cairo_vm_hints/src/hints/lib/verifiers/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod account_verifier;
pub mod block_tx_verifier;
pub mod header_verifier;
1 change: 1 addition & 0 deletions cairo_vm_hints/src/hints/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 02e6029

Please sign in to comment.