2022-11-22 08:48:48 -05:00
|
|
|
#![allow(clippy::upper_case_acronyms)]
|
|
|
|
|
|
2022-09-21 15:40:11 -06:00
|
|
|
use std::collections::HashMap;
|
2022-12-09 12:51:42 -08:00
|
|
|
use std::time::Duration;
|
2022-09-21 15:40:11 -06:00
|
|
|
|
2022-12-06 23:05:47 -08:00
|
|
|
use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV};
|
2023-03-27 17:30:11 -06:00
|
|
|
use eth_trie_utils::nibbles::Nibbles;
|
2023-03-28 14:38:58 -06:00
|
|
|
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
|
2023-03-12 21:35:04 -07:00
|
|
|
use ethereum_types::{Address, U256};
|
2022-07-04 18:10:03 -07:00
|
|
|
use hex_literal::hex;
|
2022-12-06 23:05:47 -08:00
|
|
|
use keccak_hash::keccak;
|
2022-07-04 18:10:03 -07:00
|
|
|
use plonky2::field::goldilocks_field::GoldilocksField;
|
2023-04-01 09:34:13 -04:00
|
|
|
use plonky2::plonk::config::PoseidonGoldilocksConfig;
|
2022-07-04 18:10:03 -07:00
|
|
|
use plonky2::util::timing::TimingTree;
|
|
|
|
|
use plonky2_evm::all_stark::AllStark;
|
|
|
|
|
use plonky2_evm::config::StarkConfig;
|
2022-12-06 23:05:47 -08:00
|
|
|
use plonky2_evm::generation::mpt::AccountRlp;
|
2022-09-22 20:09:48 -07:00
|
|
|
use plonky2_evm::generation::{GenerationInputs, TrieInputs};
|
2022-08-25 12:24:22 -07:00
|
|
|
use plonky2_evm::proof::BlockMetadata;
|
2022-07-04 18:10:03 -07:00
|
|
|
use plonky2_evm::prover::prove;
|
|
|
|
|
use plonky2_evm::verifier::verify_proof;
|
2023-03-28 14:38:58 -06:00
|
|
|
use plonky2_evm::Node;
|
2022-07-04 18:10:03 -07:00
|
|
|
|
|
|
|
|
type F = GoldilocksField;
|
|
|
|
|
const D: usize = 2;
|
|
|
|
|
type C = PoseidonGoldilocksConfig;
|
|
|
|
|
|
|
|
|
|
/// Test a simple token transfer to a new address.
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple_transfer() -> anyhow::Result<()> {
|
2022-12-06 23:05:47 -08:00
|
|
|
init_logger();
|
|
|
|
|
|
2022-07-04 18:10:03 -07:00
|
|
|
let all_stark = AllStark::<F, D>::default();
|
2022-08-25 12:24:22 -07:00
|
|
|
let config = StarkConfig::standard_fast_config();
|
|
|
|
|
|
2023-03-12 21:35:04 -07:00
|
|
|
let beneficiary = hex!("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
|
2022-12-06 23:05:47 -08:00
|
|
|
let sender = hex!("2c7536e3605d9c16a7a3d7b1898e529396a65c23");
|
|
|
|
|
let to = hex!("a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0");
|
2023-03-12 21:35:04 -07:00
|
|
|
|
2022-12-06 23:05:47 -08:00
|
|
|
let sender_state_key = keccak(sender);
|
|
|
|
|
let to_state_key = keccak(to);
|
2023-03-12 21:35:04 -07:00
|
|
|
|
2022-12-15 13:56:48 -08:00
|
|
|
let sender_nibbles = Nibbles::from_bytes_be(sender_state_key.as_bytes()).unwrap();
|
|
|
|
|
let to_nibbles = Nibbles::from_bytes_be(to_state_key.as_bytes()).unwrap();
|
2022-07-04 18:10:03 -07:00
|
|
|
|
2022-12-06 23:05:47 -08:00
|
|
|
let sender_account_before = AccountRlp {
|
|
|
|
|
nonce: 5.into(),
|
|
|
|
|
balance: eth_to_wei(100_000.into()),
|
2023-03-28 14:38:58 -06:00
|
|
|
storage_root: HashedPartialTrie::from(Node::Empty).hash(),
|
2022-12-06 23:05:47 -08:00
|
|
|
code_hash: keccak([]),
|
|
|
|
|
};
|
2023-03-12 21:35:04 -07:00
|
|
|
let to_account_before = AccountRlp::default();
|
2022-12-06 23:05:47 -08:00
|
|
|
|
2023-03-27 17:30:11 -06:00
|
|
|
let state_trie_before = Node::Leaf {
|
2022-12-06 23:05:47 -08:00
|
|
|
nibbles: sender_nibbles,
|
|
|
|
|
value: rlp::encode(&sender_account_before).to_vec(),
|
2023-03-27 17:30:11 -06:00
|
|
|
}
|
|
|
|
|
.into();
|
2022-12-06 23:05:47 -08:00
|
|
|
let tries_before = TrieInputs {
|
|
|
|
|
state_trie: state_trie_before,
|
2023-03-28 14:38:58 -06:00
|
|
|
transactions_trie: HashedPartialTrie::from(Node::Empty),
|
|
|
|
|
receipts_trie: HashedPartialTrie::from(Node::Empty),
|
2022-12-06 23:05:47 -08:00
|
|
|
storage_tries: vec![],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Generated using a little py-evm script.
|
|
|
|
|
let txn = hex!("f861050a8255f094a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0648242421ba02c89eb757d9deeb1f5b3859a9d4d679951ef610ac47ad4608dc142beb1b7e313a05af7e9fbab825455d36c36c7f4cfcafbeafa9a77bdff936b52afb36d4fe4bcdd");
|
2023-03-12 21:35:04 -07:00
|
|
|
let value = U256::from(100u32);
|
2022-12-06 23:05:47 -08:00
|
|
|
|
2023-03-12 21:35:04 -07:00
|
|
|
let block_metadata = BlockMetadata {
|
|
|
|
|
block_beneficiary: Address::from(beneficiary),
|
2023-05-23 15:06:26 +02:00
|
|
|
block_timestamp: 0x03e8.into(),
|
|
|
|
|
block_number: 1.into(),
|
|
|
|
|
block_difficulty: 0x020000.into(),
|
|
|
|
|
block_gaslimit: 0xff112233445566u64.into(),
|
|
|
|
|
block_chain_id: 1.into(),
|
|
|
|
|
block_base_fee: 0xa.into(),
|
2023-03-12 21:35:04 -07:00
|
|
|
};
|
2022-08-25 12:24:22 -07:00
|
|
|
|
2023-03-16 14:11:40 -07:00
|
|
|
let mut contract_code = HashMap::new();
|
|
|
|
|
contract_code.insert(keccak(vec![]), vec![]);
|
|
|
|
|
|
2022-08-25 22:11:25 -07:00
|
|
|
let inputs = GenerationInputs {
|
2022-08-25 12:24:22 -07:00
|
|
|
signed_txns: vec![txn.to_vec()],
|
2022-12-06 23:05:47 -08:00
|
|
|
tries: tries_before,
|
2023-03-16 14:11:40 -07:00
|
|
|
contract_code,
|
2022-08-25 12:24:22 -07:00
|
|
|
block_metadata,
|
2023-03-16 14:08:31 -07:00
|
|
|
addresses: vec![],
|
2022-07-04 18:10:03 -07:00
|
|
|
};
|
|
|
|
|
|
2022-12-06 23:05:47 -08:00
|
|
|
let mut timing = TimingTree::new("prove", log::Level::Debug);
|
2023-04-01 09:34:13 -04:00
|
|
|
let proof = prove::<F, C, D>(&all_stark, &config, inputs, &mut timing)?;
|
2022-12-09 12:51:42 -08:00
|
|
|
timing.filter(Duration::from_millis(100)).print();
|
2022-12-06 23:05:47 -08:00
|
|
|
|
2023-03-28 14:38:58 -06:00
|
|
|
let expected_state_trie_after: HashedPartialTrie = {
|
2023-03-10 08:54:26 -08:00
|
|
|
let txdata_gas = 2 * 16;
|
|
|
|
|
let gas_used = 21_000 + txdata_gas;
|
2023-03-12 21:35:04 -07:00
|
|
|
|
2022-12-06 23:05:47 -08:00
|
|
|
let sender_account_after = AccountRlp {
|
2023-03-10 08:54:26 -08:00
|
|
|
balance: sender_account_before.balance - value - gas_used * 10,
|
2023-01-14 22:08:07 -08:00
|
|
|
nonce: sender_account_before.nonce + 1,
|
2022-12-06 23:05:47 -08:00
|
|
|
..sender_account_before
|
|
|
|
|
};
|
|
|
|
|
let to_account_after = AccountRlp {
|
|
|
|
|
balance: value,
|
2023-03-12 21:35:04 -07:00
|
|
|
..to_account_before
|
2022-12-06 23:05:47 -08:00
|
|
|
};
|
|
|
|
|
|
2023-03-27 17:30:11 -06:00
|
|
|
let mut children = core::array::from_fn(|_| Node::Empty.into());
|
|
|
|
|
children[sender_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
2022-12-06 23:05:47 -08:00
|
|
|
nibbles: sender_nibbles.truncate_n_nibbles_front(1),
|
|
|
|
|
value: rlp::encode(&sender_account_after).to_vec(),
|
|
|
|
|
}
|
|
|
|
|
.into();
|
2023-03-27 17:30:11 -06:00
|
|
|
children[to_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
2022-12-06 23:05:47 -08:00
|
|
|
nibbles: to_nibbles.truncate_n_nibbles_front(1),
|
|
|
|
|
value: rlp::encode(&to_account_after).to_vec(),
|
|
|
|
|
}
|
|
|
|
|
.into();
|
2023-03-27 17:30:11 -06:00
|
|
|
Node::Branch {
|
2022-12-06 23:05:47 -08:00
|
|
|
children,
|
|
|
|
|
value: vec![],
|
|
|
|
|
}
|
2023-03-27 17:30:11 -06:00
|
|
|
.into()
|
2022-12-06 23:05:47 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
proof.public_values.trie_roots_after.state_root,
|
2023-03-27 17:30:11 -06:00
|
|
|
expected_state_trie_after.hash()
|
2022-12-06 23:05:47 -08:00
|
|
|
);
|
2022-07-04 18:10:03 -07:00
|
|
|
|
Shrink STARK proofs to a constant degree
The goal here is to end up with a single "root" circuit representing any EVM proof. I.e. it must verify each STARK, but be general enough to work with any combination of STARK sizes (within some range of sizes that we chose to support). This root circuit can then be plugged into our aggregation circuit.
In particular, for each STARK, and for each initial `degree_bits` (within a range that we choose to support), this adds a "shrinking chain" of circuits. Such a chain shrinks a STARK proof from that initial `degree_bits` down to a constant, `THRESHOLD_DEGREE_BITS`.
The root circuit then combines these shrunk-to-constant proofs for each table. It's similar to `RecursiveAllProof::verify_circuit`; I adapted the code from there and I think we can remove it after. The main difference is that now instead of having one verification key per STARK, we have several possible VKs, one per initial `degree_bits`. We bake the list of possible VKs into the root circuit, and have the prover indicate the index of the VK they're actually using.
This also partially removes the default feature of CTLs. So far we've used filters instead of defaults. Until now it was easy to keep supporting defaults just in case, but here maintaining support would require some more work. E.g. we couldn't use `exp_u64` any more, since the size delta is now dynamic, it can't be hardcoded. If there are no concerns, I'll fully remove the feature after.
2022-12-27 18:15:18 -08:00
|
|
|
verify_proof(&all_stark, proof, &config)
|
2022-07-04 18:10:03 -07:00
|
|
|
}
|
2022-12-06 23:05:47 -08:00
|
|
|
|
|
|
|
|
fn eth_to_wei(eth: U256) -> U256 {
|
2022-12-09 18:53:24 -08:00
|
|
|
// 1 ether = 10^18 wei.
|
2022-12-09 19:57:02 -08:00
|
|
|
eth * U256::from(10).pow(18.into())
|
2022-12-06 23:05:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init_logger() {
|
2022-12-09 13:20:33 -08:00
|
|
|
let _ = try_init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
|
2022-12-06 23:05:47 -08:00
|
|
|
}
|