2022-07-18 21:01:06 -07:00
|
|
|
use ethereum_types::U256;
|
2022-06-15 09:33:52 -07:00
|
|
|
use plonky2::field::extension::Extendable;
|
|
|
|
|
use plonky2::field::polynomial::PolynomialValues;
|
|
|
|
|
use plonky2::field::types::Field;
|
|
|
|
|
use plonky2::hash::hash_types::RichField;
|
|
|
|
|
|
|
|
|
|
use crate::all_stark::AllStark;
|
|
|
|
|
use crate::cpu::bootstrap_kernel::generate_bootstrap_kernel;
|
|
|
|
|
use crate::cpu::columns::NUM_CPU_COLUMNS;
|
|
|
|
|
use crate::generation::state::GenerationState;
|
|
|
|
|
use crate::util::trace_rows_to_poly_values;
|
|
|
|
|
|
2022-07-23 15:35:48 +02:00
|
|
|
pub(crate) mod memory;
|
2022-06-15 09:33:52 -07:00
|
|
|
pub(crate) mod state;
|
|
|
|
|
|
2022-07-13 16:45:21 -07:00
|
|
|
/// A piece of data which has been encoded using Recursive Length Prefix (RLP) serialization.
|
|
|
|
|
/// See https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/
|
2022-07-12 14:33:10 -07:00
|
|
|
pub type RlpBlob = Vec<u8>;
|
|
|
|
|
|
|
|
|
|
/// Merkle proofs are encoded using an RLP blob for each node in the path.
|
|
|
|
|
pub type RlpMerkleProof = Vec<RlpBlob>;
|
2022-06-15 09:33:52 -07:00
|
|
|
|
|
|
|
|
#[allow(unused)] // TODO: Should be used soon.
|
|
|
|
|
pub struct TransactionData {
|
2022-07-04 18:10:03 -07:00
|
|
|
pub signed_txn: Vec<u8>,
|
|
|
|
|
|
2022-06-15 09:33:52 -07:00
|
|
|
/// A Merkle proof for each interaction with the state trie, ordered chronologically.
|
2022-07-04 18:10:03 -07:00
|
|
|
pub trie_proofs: Vec<RlpMerkleProof>,
|
2022-06-15 09:33:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unused)] // TODO: Should be used soon.
|
2022-07-04 18:10:03 -07:00
|
|
|
pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
|
2022-06-15 09:33:52 -07:00
|
|
|
all_stark: &AllStark<F, D>,
|
|
|
|
|
txns: &[TransactionData],
|
|
|
|
|
) -> Vec<Vec<PolynomialValues<F>>> {
|
|
|
|
|
let mut state = GenerationState::<F>::default();
|
|
|
|
|
|
|
|
|
|
generate_bootstrap_kernel::<F>(&mut state);
|
|
|
|
|
|
|
|
|
|
for txn in txns {
|
|
|
|
|
generate_txn(&mut state, txn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let GenerationState {
|
|
|
|
|
cpu_rows,
|
|
|
|
|
current_cpu_row,
|
|
|
|
|
memory,
|
|
|
|
|
keccak_inputs,
|
2022-07-18 21:01:06 -07:00
|
|
|
logic_ops,
|
|
|
|
|
prover_inputs,
|
2022-06-15 09:33:52 -07:00
|
|
|
..
|
|
|
|
|
} = state;
|
2022-07-14 11:31:47 -07:00
|
|
|
assert_eq!(current_cpu_row, [F::ZERO; NUM_CPU_COLUMNS].into());
|
2022-07-18 21:01:06 -07:00
|
|
|
assert_eq!(prover_inputs, vec![], "Not all prover inputs were consumed");
|
2022-06-15 09:33:52 -07:00
|
|
|
|
|
|
|
|
let cpu_trace = trace_rows_to_poly_values(cpu_rows);
|
|
|
|
|
let keccak_trace = all_stark.keccak_stark.generate_trace(keccak_inputs);
|
2022-07-18 21:01:06 -07:00
|
|
|
let logic_trace = all_stark.logic_stark.generate_trace(logic_ops);
|
2022-06-15 09:33:52 -07:00
|
|
|
let memory_trace = all_stark.memory_stark.generate_trace(memory.log);
|
|
|
|
|
vec![cpu_trace, keccak_trace, logic_trace, memory_trace]
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-18 21:01:06 -07:00
|
|
|
fn generate_txn<F: Field>(state: &mut GenerationState<F>, txn: &TransactionData) {
|
|
|
|
|
// TODO: Add transaction RLP to prover_input.
|
|
|
|
|
|
|
|
|
|
// Supply Merkle trie proofs as prover inputs.
|
|
|
|
|
for proof in &txn.trie_proofs {
|
|
|
|
|
let proof = proof
|
|
|
|
|
.iter()
|
|
|
|
|
.flat_map(|node_rlp| node_rlp.iter().map(|byte| U256::from(*byte)));
|
|
|
|
|
state.prover_inputs.extend(proof);
|
|
|
|
|
}
|
2022-06-15 09:33:52 -07:00
|
|
|
}
|