Skip to content

Commit

Permalink
multi support sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Dec 26, 2024
1 parent 905999a commit 1c3d7f4
Show file tree
Hide file tree
Showing 38 changed files with 1,357 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2023_11"

members = [
"cairo1"
"hdp_cairo"
]

[workspace.dependencies]
Expand Down
10 changes: 8 additions & 2 deletions crates/fetcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use proof_keys::ProofKeys;
use std::{collections::HashSet, fs, num::ParseIntError, path::PathBuf};
use thiserror::Error;
use types::proofs::{account::Account, storage::Storage, HeaderMmrMeta, Proofs};
use types::{
proofs::{account::Account, storage::Storage, HeaderMmrMeta, Proofs},
ChainProofs,
};

pub mod proof_keys;

Expand Down Expand Up @@ -116,7 +119,10 @@ async fn main() -> Result<(), FetcherError> {
..Default::default()
};

fs::write(args.program_output, serde_json::to_vec(&proofs).map_err(|e| FetcherError::IO(e.into()))?)?;
fs::write(
args.program_output,
serde_json::to_vec::<Vec<ChainProofs>>(&vec![ChainProofs::EthereumSepolia(proofs)]).map_err(|e| FetcherError::IO(e.into()))?,
)?;

Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions crates/hints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ pub fn hints() -> HashMap<String, HintImpl> {
hints.insert(decoder::evm::v_is_encoded::HINT_V_IS_ENCODED.into(), decoder::evm::v_is_encoded::hint_v_is_encoded);
hints.insert(merkle::HINT_IS_LEFT_SMALLER.into(), merkle::hint_is_left_smaller);
hints.insert(merkle::HINT_TARGET_TASK_HASH.into(), merkle::hint_target_task_hash);
hints.insert(print::PROGRAM_HASH.into(), print::program_hash);
hints.insert(print::HINT_PRINT_TASK_RESULT.into(), print::hint_print_task_result);
hints.insert(print::PROGRAM_HASH.into(), print::program_hash);
hints.insert(rlp::divmod::HINT_DIVMOD_RLP.into(), rlp::divmod::hint_divmod_rlp);
hints.insert(rlp::divmod::HINT_DIVMOD_VALUE.into(), rlp::divmod::hint_divmod_value);
hints.insert(rlp::item_type::HINT_IS_LONG.into(), rlp::item_type::hint_is_long);
Expand Down Expand Up @@ -71,6 +71,7 @@ pub fn hints() -> HashMap<String, HintImpl> {
hints.insert(verifiers::evm::header_verifier::HINT_RLP_LEN.into(), verifiers::evm::header_verifier::hint_rlp_len);
hints.insert(verifiers::evm::header_verifier::HINT_SET_HEADER.into(), verifiers::evm::header_verifier::hint_set_header);
hints.insert(verifiers::evm::header_verifier::HINT_VM_ENTER_SCOPE.into(), verifiers::evm::header_verifier::hint_vm_enter_scope);
hints.insert(verifiers::evm::HINT_HEADERS_WITH_MMR_LEN.into(), verifiers::evm::hint_headers_with_mmr_headers_len);
hints.insert(verifiers::evm::receipt_verifier::HINT_BATCH_RECEIPTS_LEN.into(), verifiers::evm::receipt_verifier::hint_batch_receipts_len);
hints.insert(verifiers::evm::receipt_verifier::HINT_RECEIPT_BLOCK_NUMBER.into(), verifiers::evm::receipt_verifier::hint_receipt_block_number);
hints.insert(verifiers::evm::receipt_verifier::HINT_RECEIPT_KEY_LEADING_ZEROS.into(), verifiers::evm::receipt_verifier::hint_receipt_key_leading_zeros);
Expand Down Expand Up @@ -99,7 +100,8 @@ pub fn hints() -> HashMap<String, HintImpl> {
hints.insert(verifiers::mmr_verifier::HINT_HEADERS_WITH_MMR_META_ROOT.into(), verifiers::mmr_verifier::hint_headers_with_mmr_meta_root);
hints.insert(verifiers::mmr_verifier::HINT_HEADERS_WITH_MMR_META_SIZE.into(), verifiers::mmr_verifier::hint_headers_with_mmr_meta_size);
hints.insert(verifiers::mmr_verifier::HINT_HEADERS_WITH_MMR_PEAKS.into(), verifiers::mmr_verifier::hint_headers_with_mmr_peaks);
hints.insert(verifiers::verify::HINT_HEADERS_WITH_MMR_LEN.into(), verifiers::verify::hint_headers_with_mmr_headers_len);
hints.insert(verifiers::verify::HINT_CHAIN_PROOFS_LEN.into(), verifiers::verify::hint_chain_proofs_len);
hints.insert(verifiers::verify::HINT_CHAIN_PROOFS_CHAIN_ID.into(), verifiers::verify::hint_chain_proofs_chain_id);
hints.insert(verifiers::verify::HINT_VM_ENTER_SCOPE.into(), verifiers::verify::hint_vm_enter_scope);

hints.insert(eth_essentials_cairo_vm_hints::hints::lib::bit_length::HINT_BIT_LENGTH.into(), eth_essentials_cairo_vm_hints::hints::lib::bit_length::hint_bit_length);
Expand Down
2 changes: 2 additions & 0 deletions crates/hints/src/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod scopes {
pub const PARAMS: &str = "params";
pub const PROOF: &str = "proof";
pub const PROOFS: &str = "proofs";
pub const CHAIN_PROOFS: &str = "chain_proofs";
pub const RECEIPT: &str = "receipt";
pub const STORAGE: &str = "storage";
pub const SYSCALL_HANDLER: &str = "syscall_handler";
Expand All @@ -17,6 +18,7 @@ pub mod scopes {

pub mod ids {
pub const PEAKS: &str = "peaks";
pub const CHAIN_PROOFS_LEN: &str = "chain_proofs_len";
pub const RLP_LEN: &str = "rlp_len";
pub const ADDRESS: &str = "address";
pub const ALL_ENCODINGS: &str = "all_encodings";
Expand Down
23 changes: 23 additions & 0 deletions crates/hints/src/verifiers/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
use crate::vars;
use cairo_vm::{
hint_processor::builtin_hint_processor::{builtin_hint_processor_definition::HintProcessorData, hint_utils::insert_value_into_ap},
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
Felt252,
};
use std::collections::HashMap;
use types::proofs::Proofs;

pub mod account_verifier;
pub mod header_verifier;
pub mod receipt_verifier;
pub mod storage_item_verifier;
pub mod transaction_verifier;

pub const HINT_HEADERS_WITH_MMR_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(batch.headers_with_mmr))";

pub fn hint_headers_with_mmr_headers_len(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
_hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let proofs = exec_scopes.get::<Proofs>(vars::scopes::BATCH)?;

insert_value_into_ap(vm, Felt252::from(proofs.headers_with_mmr.len()))
}
57 changes: 40 additions & 17 deletions crates/hints/src/verifiers/verify.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,64 @@
use crate::vars;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::insert_value_into_ap;
use cairo_vm::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::{any::Any, collections::HashMap};
use types::proofs::Proofs;
use types::ChainProofs;

pub const HINT_VM_ENTER_SCOPE: &str = "vm_enter_scope({'batch': proofs, '__dict_manager': __dict_manager})";
pub const HINT_CHAIN_PROOFS_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(chain_proofs))";

pub fn hint_vm_enter_scope(
_vm: &mut VirtualMachine,
pub fn hint_chain_proofs_len(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
_hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let batch: Box<dyn Any> = Box::new(exec_scopes.get::<Proofs>(vars::scopes::PROOFS)?);
let dict_manager: Box<dyn Any> = Box::new(exec_scopes.get_dict_manager()?);
exec_scopes.enter_scope(HashMap::from([
(String::from(vars::scopes::BATCH), batch),
(String::from(vars::scopes::DICT_MANAGER), dict_manager),
]));
let chain_proofs = exec_scopes.get::<Vec<ChainProofs>>(vars::scopes::CHAIN_PROOFS)?;
insert_value_into_ap(vm, Felt252::from(chain_proofs.len()))
}

Ok(())
pub const HINT_CHAIN_PROOFS_CHAIN_ID: &str = "memory[ap] = to_felt_or_relocatable(chain_proofs[ids.idx - 1].chain_id)";

pub fn hint_chain_proofs_chain_id(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let chain_proofs = exec_scopes.get::<Vec<ChainProofs>>(vars::scopes::CHAIN_PROOFS)?;
let idx: usize = get_integer_from_var_name(vars::ids::IDX, vm, &hint_data.ids_data, &hint_data.ap_tracking)?
.try_into()
.unwrap();
insert_value_into_ap(vm, Felt252::from(chain_proofs[idx - 1].chain_id()))
}

pub const HINT_HEADERS_WITH_MMR_LEN: &str = "memory[ap] = to_felt_or_relocatable(len(batch.headers_with_mmr))";
pub const HINT_VM_ENTER_SCOPE: &str = "vm_enter_scope({'batch': chain_proofs[ids.idx - 1].value, '__dict_manager': __dict_manager})";

pub fn hint_headers_with_mmr_headers_len(
pub fn hint_vm_enter_scope(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
_hint_data: &HintProcessorData,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let proofs = exec_scopes.get::<Proofs>(vars::scopes::BATCH)?;
let chain_proofs = exec_scopes.get::<Vec<ChainProofs>>(vars::scopes::CHAIN_PROOFS)?;
let idx: usize = get_integer_from_var_name(vars::ids::IDX, vm, &hint_data.ids_data, &hint_data.ap_tracking)?
.try_into()
.unwrap();

let batch: Box<dyn Any> = match chain_proofs[idx - 1].clone() {
ChainProofs::EthereumMainnet(proofs) => Box::new(proofs),
ChainProofs::EthereumSepolia(proofs) => Box::new(proofs),
};
let dict_manager: Box<dyn Any> = Box::new(exec_scopes.get_dict_manager()?);

insert_value_into_ap(vm, Felt252::from(proofs.headers_with_mmr.len()))
exec_scopes.enter_scope(HashMap::from([
(String::from(vars::scopes::BATCH), batch),
(String::from(vars::scopes::DICT_MANAGER), dict_manager),
]));

Ok(())
}
6 changes: 3 additions & 3 deletions crates/sound_hint_processor/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use cairo_vm::{
};
use hints::vars;
use std::collections::HashMap;
use types::{param::Param, proofs::Proofs};
use types::{param::Param, ChainProofs};

pub const HINT_INPUT: &str = "from tools.py.schema import HDPInput\nrun_input = HDPInput.Schema().load(program_input)\nproofs = run_input.proofs\nparams = run_input.params\ncompiled_class = run_input.compiled_class";
pub const HINT_INPUT: &str = "from tools.py.schema import HDPInput\nrun_input = HDPInput.Schema().load(program_input)\nchain_proofs = run_input.proofs\nparams = run_input.params\ncompiled_class = run_input.compiled_class";

impl CustomHintProcessor {
pub fn hint_input(
Expand All @@ -20,7 +20,7 @@ impl CustomHintProcessor {
_hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
exec_scopes.insert_value::<Proofs>(vars::scopes::PROOFS, self.private_inputs.proofs.to_owned());
exec_scopes.insert_value::<Vec<ChainProofs>>(vars::scopes::CHAIN_PROOFS, self.private_inputs.chain_proofs.to_owned());
exec_scopes.insert_value::<Vec<Param>>(vars::scopes::PARAMS, self.private_inputs.params.to_owned());
exec_scopes.insert_value::<CasmContractClass>(vars::scopes::COMPILED_CLASS, self.private_inputs.compiled_class.to_owned());
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/sound_run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use clap::Parser;
use sound_hint_processor::CustomHintProcessor;
use std::{env, path::PathBuf};
use tracing::debug;
use types::{proofs::Proofs, HDPDryRunInput, HDPInput};
use types::{ChainProofs, HDPDryRunInput, HDPInput};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -53,7 +53,7 @@ fn main() -> Result<(), HdpOsError> {

let program_file = std::fs::read(program_file_path.as_path()).map_err(HdpOsError::IO)?;
let program_inputs: HDPDryRunInput = serde_json::from_slice(&std::fs::read(args.program_input).map_err(HdpOsError::IO)?)?;
let program_proofs: Proofs = serde_json::from_slice(&std::fs::read(args.program_proofs).map_err(HdpOsError::IO)?)?;
let program_proofs: Vec<ChainProofs> = serde_json::from_slice(&std::fs::read(args.program_proofs).map_err(HdpOsError::IO)?)?;
// Load the Program
let program = Program::from_bytes(&program_file, Some(cairo_run_config.entrypoint)).map_err(|e| HdpOsError::Runner(e.into()))?;

Expand All @@ -74,7 +74,7 @@ fn main() -> Result<(), HdpOsError> {

// Run the Cairo VM
let mut hint_processor = CustomHintProcessor::new(HDPInput {
proofs: program_proofs,
chain_proofs: program_proofs,
params: program_inputs.params,
compiled_class: program_inputs.compiled_class,
});
Expand Down
18 changes: 17 additions & 1 deletion crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod keys;
pub mod param;
pub mod proofs;

use alloy::primitives::ChainId;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use param::Param;
use proofs::Proofs;
Expand All @@ -23,7 +24,22 @@ pub struct HDPDryRunInput {

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct HDPInput {
pub proofs: Proofs,
pub chain_proofs: Vec<ChainProofs>,
pub params: Vec<Param>,
pub compiled_class: CasmContractClass,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum ChainProofs {
EthereumMainnet(Proofs),
EthereumSepolia(Proofs),
}

impl ChainProofs {
pub fn chain_id(&self) -> ChainId {
match self {
ChainProofs::EthereumMainnet(_) => 1,
ChainProofs::EthereumSepolia(_) => 11155111,
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/hdp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func run{
%{
from tools.py.schema import HDPInput
run_input = HDPInput.Schema().load(program_input)
proofs = run_input.proofs
chain_proofs = run_input.proofs
params = run_input.params
compiled_class = run_input.compiled_class
%}
Expand Down
30 changes: 12 additions & 18 deletions src/verifiers/evm/verify.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@ func run_state_verification{
pow2_array: felt*,
evm_memorizer: DictAccess*,
mmr_metas: MMRMeta*,
chain_info: ChainInfo,
}(mmr_meta_idx: felt) -> (mmr_meta_idx: felt) {
alloc_locals;

let (chain_info) = fetch_chain_info(11155111);
with chain_info {
// Step 1: Verify MMR and headers inclusion
tempvar n_proofs: felt = nondet %{ len(batch.headers_with_mmr) %};
let (mmr_meta_idx) = verify_mmr_batches(n_proofs, mmr_meta_idx);

// Step 2: Verify the accounts
verify_accounts();

// Step 3: Verify the storage items
verify_storage_items();

// Step 4: Verify the block tx proofs
verify_block_tx_proofs();

// Step 5: Verify the block receipt proofs
verify_block_receipt_proofs();
}
// Step 1: Verify MMR and headers inclusion
tempvar n_proofs: felt = nondet %{ len(batch.headers_with_mmr) %};
let (mmr_meta_idx) = verify_mmr_batches(n_proofs, mmr_meta_idx);
// Step 2: Verify the accounts
verify_accounts();
// Step 3: Verify the storage items
verify_storage_items();
// Step 4: Verify the block tx proofs
verify_block_tx_proofs();
// Step 5: Verify the block receipt proofs
verify_block_receipt_proofs();

return (mmr_meta_idx=mmr_meta_idx);
}
30 changes: 25 additions & 5 deletions src/verifiers/verify.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,34 @@ func run_state_verification{
starknet_memorizer: DictAccess*,
mmr_metas: MMRMeta*,
}() -> (mmr_metas_len: felt) {
alloc_locals;
tempvar chain_proofs_len: felt = nondet %{ len(chain_proofs) %};
let (mmr_meta_idx, _) = run_state_verification_inner(mmr_meta_idx=0, idx=chain_proofs_len);
return (mmr_metas_len=mmr_meta_idx);
}

// batch abstraction will be usefull with multiple chains
%{ vm_enter_scope({'batch': proofs, '__dict_manager': __dict_manager}) %}
func run_state_verification_inner{
range_check_ptr,
pedersen_ptr: HashBuiltin*,
poseidon_ptr: PoseidonBuiltin*,
keccak_ptr: KeccakBuiltin*,
bitwise_ptr: BitwiseBuiltin*,
pow2_array: felt*,
evm_memorizer: DictAccess*,
starknet_memorizer: DictAccess*,
mmr_metas: MMRMeta*,
}(mmr_meta_idx: felt, idx: felt) -> (mmr_meta_idx: felt, idx: felt) {
if (idx == 0) {
return (mmr_meta_idx=mmr_meta_idx, idx=idx);
}

let (mmr_meta_idx) = evm_run_state_verification(0);
tempvar chain_id: felt = nondet %{ chain_proofs[ids.idx - 1].chain_id %};
let (chain_info) = fetch_chain_info(chain_id);

%{ vm_enter_scope({'batch': chain_proofs[ids.idx - 1].value, '__dict_manager': __dict_manager}) %}
with chain_info {
let (mmr_meta_idx) = evm_run_state_verification(mmr_meta_idx);
}
%{ vm_exit_scope() %}

return (mmr_metas_len=mmr_meta_idx);
return run_state_verification_inner(mmr_meta_idx=mmr_meta_idx, idx=idx - 1);
}
Loading

0 comments on commit 1c3d7f4

Please sign in to comment.