2022-09-21 15:40:11 -06:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2022-09-19 11:05:48 -06:00
|
|
|
use eth_trie_utils::partial_trie::PartialTrie;
|
2022-09-29 13:45:46 -06:00
|
|
|
use ethereum_types::{Address, BigEndianHash, H256};
|
2022-06-15 09:33:52 -07:00
|
|
|
use plonky2::field::extension::Extendable;
|
|
|
|
|
use plonky2::field::polynomial::PolynomialValues;
|
|
|
|
|
use plonky2::hash::hash_types::RichField;
|
2022-09-23 10:54:17 -07:00
|
|
|
use plonky2::util::timing::TimingTree;
|
2022-09-19 11:05:48 -06:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-06-15 09:33:52 -07:00
|
|
|
|
2022-08-26 10:12:45 +02:00
|
|
|
use crate::all_stark::{AllStark, NUM_TABLES};
|
2022-08-26 18:30:26 +02:00
|
|
|
use crate::config::StarkConfig;
|
2022-06-15 09:33:52 -07:00
|
|
|
use crate::cpu::bootstrap_kernel::generate_bootstrap_kernel;
|
2022-11-30 12:55:41 -08:00
|
|
|
use crate::cpu::kernel::aggregator::KERNEL;
|
2022-10-03 12:08:29 -07:00
|
|
|
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
|
2022-08-25 12:24:22 -07:00
|
|
|
use crate::memory::segments::Segment;
|
|
|
|
|
use crate::proof::{BlockMetadata, PublicValues, TrieRoots};
|
2022-11-30 12:55:41 -08:00
|
|
|
use crate::witness::memory::{MemoryAddress, MemoryState};
|
|
|
|
|
use crate::witness::state::RegistersState;
|
|
|
|
|
use crate::witness::traces::Traces;
|
|
|
|
|
use crate::witness::transition::transition;
|
2022-06-15 09:33:52 -07:00
|
|
|
|
2022-07-23 15:35:48 +02:00
|
|
|
pub(crate) mod memory;
|
2022-09-18 09:45:31 -07:00
|
|
|
pub(crate) mod mpt;
|
|
|
|
|
pub(crate) mod prover_input;
|
2022-09-29 23:09:32 -07:00
|
|
|
pub(crate) mod rlp;
|
2022-06-15 09:33:52 -07:00
|
|
|
pub(crate) mod state;
|
|
|
|
|
|
2022-09-18 09:45:31 -07:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
|
2022-08-25 22:11:25 -07:00
|
|
|
/// Inputs needed for trace generation.
|
|
|
|
|
pub struct GenerationInputs {
|
2022-08-25 12:24:22 -07:00
|
|
|
pub signed_txns: Vec<Vec<u8>>,
|
2022-07-04 18:10:03 -07:00
|
|
|
|
2022-09-22 20:09:48 -07:00
|
|
|
pub tries: TrieInputs,
|
|
|
|
|
|
|
|
|
|
/// Mapping between smart contract code hashes and the contract byte code.
|
|
|
|
|
/// All account smart contracts that are invoked will have an entry present.
|
|
|
|
|
pub contract_code: HashMap<H256, Vec<u8>>,
|
|
|
|
|
|
|
|
|
|
pub block_metadata: BlockMetadata,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
|
|
|
|
|
pub struct TrieInputs {
|
2022-08-25 23:35:38 -07:00
|
|
|
/// A partial version of the state trie prior to these transactions. It should include all nodes
|
|
|
|
|
/// that will be accessed by these transactions.
|
2022-08-24 20:41:32 -07:00
|
|
|
pub state_trie: PartialTrie,
|
|
|
|
|
|
2022-08-25 23:35:38 -07:00
|
|
|
/// A partial version of the transaction trie prior to these transactions. It should include all
|
|
|
|
|
/// nodes that will be accessed by these transactions.
|
2022-08-25 12:24:22 -07:00
|
|
|
pub transactions_trie: PartialTrie,
|
2022-08-24 20:41:32 -07:00
|
|
|
|
2022-08-25 23:35:38 -07:00
|
|
|
/// A partial version of the receipt trie prior to these transactions. It should include all nodes
|
|
|
|
|
/// that will be accessed by these transactions.
|
2022-08-25 12:24:22 -07:00
|
|
|
pub receipts_trie: PartialTrie,
|
2022-08-24 20:41:32 -07:00
|
|
|
|
2022-08-25 23:35:38 -07:00
|
|
|
/// A partial version of each storage trie prior to these transactions. It should include all
|
|
|
|
|
/// storage tries, and nodes therein, that will be accessed by these transactions.
|
2022-08-24 20:41:32 -07:00
|
|
|
pub storage_tries: Vec<(Address, PartialTrie)>,
|
2022-06-15 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-25 12:24:22 -07:00
|
|
|
pub(crate) fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
|
2022-06-15 09:33:52 -07:00
|
|
|
all_stark: &AllStark<F, D>,
|
2022-08-25 22:11:25 -07:00
|
|
|
inputs: GenerationInputs,
|
2022-08-26 18:30:26 +02:00
|
|
|
config: &StarkConfig,
|
2022-09-23 10:54:17 -07:00
|
|
|
timing: &mut TimingTree,
|
2022-08-26 10:12:45 +02:00
|
|
|
) -> ([Vec<PolynomialValues<F>>; NUM_TABLES], PublicValues) {
|
2022-11-30 12:55:41 -08:00
|
|
|
// let mut state = GenerationState::<F>::new(inputs.clone());
|
2022-06-15 09:33:52 -07:00
|
|
|
|
2022-11-30 12:55:41 -08:00
|
|
|
let mut memory_state = MemoryState::default();
|
|
|
|
|
let mut traces = Traces::<F>::default();
|
|
|
|
|
generate_bootstrap_kernel::<F>(&mut memory_state, &mut traces);
|
2022-06-15 09:33:52 -07:00
|
|
|
|
2022-11-30 12:55:41 -08:00
|
|
|
let mut registers_state = RegistersState::default();
|
|
|
|
|
let halt_pc0 = KERNEL.global_labels["halt_pc0"];
|
|
|
|
|
let halt_pc1 = KERNEL.global_labels["halt_pc1"];
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
// If we've reached the kernel's halt routine, and our trace length is a power of 2, stop.
|
|
|
|
|
let pc = registers_state.program_counter as usize;
|
|
|
|
|
let in_halt_loop = pc == halt_pc0 || pc == halt_pc1;
|
|
|
|
|
if in_halt_loop && traces.cpu.len().is_power_of_two() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-06-15 09:33:52 -07:00
|
|
|
|
2022-11-30 12:55:41 -08:00
|
|
|
registers_state = transition(registers_state, &memory_state, &mut traces);
|
|
|
|
|
}
|
2022-08-25 12:24:22 -07:00
|
|
|
|
2022-11-30 12:55:41 -08:00
|
|
|
let read_metadata = |field| {
|
|
|
|
|
memory_state.get(MemoryAddress::new(
|
2022-08-25 12:24:22 -07:00
|
|
|
0,
|
2022-11-30 12:55:41 -08:00
|
|
|
Segment::GlobalMetadata as usize,
|
2022-08-25 12:24:22 -07:00
|
|
|
field as usize,
|
2022-11-30 12:55:41 -08:00
|
|
|
))
|
2022-08-25 12:24:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let trie_roots_before = TrieRoots {
|
2022-09-29 13:45:46 -06:00
|
|
|
state_root: H256::from_uint(&read_metadata(GlobalMetadata::StateTrieRootDigestBefore)),
|
|
|
|
|
transactions_root: H256::from_uint(&read_metadata(
|
2022-10-02 11:14:07 -07:00
|
|
|
GlobalMetadata::TransactionTrieRootDigestBefore,
|
2022-09-29 13:45:46 -06:00
|
|
|
)),
|
2022-10-02 11:14:07 -07:00
|
|
|
receipts_root: H256::from_uint(&read_metadata(GlobalMetadata::ReceiptTrieRootDigestBefore)),
|
2022-08-25 12:24:22 -07:00
|
|
|
};
|
|
|
|
|
let trie_roots_after = TrieRoots {
|
2022-09-29 13:45:46 -06:00
|
|
|
state_root: H256::from_uint(&read_metadata(GlobalMetadata::StateTrieRootDigestAfter)),
|
|
|
|
|
transactions_root: H256::from_uint(&read_metadata(
|
2022-10-02 11:14:07 -07:00
|
|
|
GlobalMetadata::TransactionTrieRootDigestAfter,
|
2022-09-29 13:45:46 -06:00
|
|
|
)),
|
2022-10-02 11:14:07 -07:00
|
|
|
receipts_root: H256::from_uint(&read_metadata(GlobalMetadata::ReceiptTrieRootDigestAfter)),
|
2022-08-25 12:24:22 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let public_values = PublicValues {
|
|
|
|
|
trie_roots_before,
|
|
|
|
|
trie_roots_after,
|
|
|
|
|
block_metadata: inputs.block_metadata,
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-30 12:55:41 -08:00
|
|
|
(traces.to_tables(all_stark, config, timing), public_values)
|
2022-06-15 09:33:52 -07:00
|
|
|
}
|