mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-02 13:53:07 +00:00
* Update prover logic * Add helper method for CTL data * Some cleanup * Update some methods * Fix * Some more fixes * More tweaks * Final * Leverage starky crate * Additional tweaks * Cleanup * More cleanup * Fix * Cleanup imports * Fix * Final tweaks * Cleanup and hide behind debug_assertions attribute * Clippy * Fix no-std * Make wasm compatible * Doc and remove todo * API cleanup and remove TODO * Add Debug impls * Add documentation for public items * Feature-gate alloc imports * Import method from starky instead * Add simple crate and module documentation * Apply comments * Add lib level documentation * Add test without lookups * Fix starks without logup * Cleanup * Some more cleanup * Fix get_challenges for non-lookup STARKs * Add additional config methods and tests * Apply comments * More comments
215 lines
7.4 KiB
Rust
215 lines
7.4 KiB
Rust
use std::collections::HashMap;
|
|
use std::str::FromStr;
|
|
use std::time::Duration;
|
|
|
|
use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV};
|
|
use eth_trie_utils::nibbles::Nibbles;
|
|
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
|
|
use ethereum_types::{Address, H256, U256};
|
|
use hex_literal::hex;
|
|
use keccak_hash::keccak;
|
|
use plonky2::field::goldilocks_field::GoldilocksField;
|
|
use plonky2::plonk::config::KeccakGoldilocksConfig;
|
|
use plonky2::util::timing::TimingTree;
|
|
use plonky2_evm::cpu::kernel::opcodes::{get_opcode, get_push_opcode};
|
|
use plonky2_evm::generation::mpt::{AccountRlp, LegacyReceiptRlp};
|
|
use plonky2_evm::generation::{GenerationInputs, TrieInputs};
|
|
use plonky2_evm::proof::{BlockHashes, BlockMetadata, TrieRoots};
|
|
use plonky2_evm::prover::prove;
|
|
use plonky2_evm::verifier::verify_proof;
|
|
use plonky2_evm::{AllStark, Node, StarkConfig};
|
|
|
|
type F = GoldilocksField;
|
|
const D: usize = 2;
|
|
type C = KeccakGoldilocksConfig;
|
|
|
|
/// Test a simple token transfer to a new address.
|
|
#[test]
|
|
#[ignore] // Too slow to run on CI.
|
|
fn test_basic_smart_contract() -> anyhow::Result<()> {
|
|
init_logger();
|
|
|
|
let all_stark = AllStark::<F, D>::default();
|
|
let config = StarkConfig::standard_fast_config();
|
|
|
|
let beneficiary = hex!("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
|
|
let sender = hex!("2c7536e3605d9c16a7a3d7b1898e529396a65c23");
|
|
let to = hex!("a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0");
|
|
|
|
let beneficiary_state_key = keccak(beneficiary);
|
|
let sender_state_key = keccak(sender);
|
|
let to_state_key = keccak(to);
|
|
|
|
let beneficiary_nibbles = Nibbles::from_bytes_be(beneficiary_state_key.as_bytes()).unwrap();
|
|
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();
|
|
|
|
let push1 = get_push_opcode(1);
|
|
let add = get_opcode("ADD");
|
|
let stop = get_opcode("STOP");
|
|
let code = [push1, 3, push1, 4, add, stop];
|
|
let code_gas = 3 + 3 + 3;
|
|
let code_hash = keccak(code);
|
|
|
|
let beneficiary_account_before = AccountRlp {
|
|
nonce: 1.into(),
|
|
..AccountRlp::default()
|
|
};
|
|
let sender_account_before = AccountRlp {
|
|
nonce: 5.into(),
|
|
balance: eth_to_wei(100_000.into()),
|
|
..AccountRlp::default()
|
|
};
|
|
let to_account_before = AccountRlp {
|
|
code_hash,
|
|
..AccountRlp::default()
|
|
};
|
|
|
|
let state_trie_before = {
|
|
let mut children = core::array::from_fn(|_| Node::Empty.into());
|
|
children[beneficiary_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
|
nibbles: beneficiary_nibbles.truncate_n_nibbles_front(1),
|
|
value: rlp::encode(&beneficiary_account_before).to_vec(),
|
|
}
|
|
.into();
|
|
children[sender_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
|
nibbles: sender_nibbles.truncate_n_nibbles_front(1),
|
|
value: rlp::encode(&sender_account_before).to_vec(),
|
|
}
|
|
.into();
|
|
children[to_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
|
nibbles: to_nibbles.truncate_n_nibbles_front(1),
|
|
value: rlp::encode(&to_account_before).to_vec(),
|
|
}
|
|
.into();
|
|
Node::Branch {
|
|
children,
|
|
value: vec![],
|
|
}
|
|
}
|
|
.into();
|
|
|
|
let tries_before = TrieInputs {
|
|
state_trie: state_trie_before,
|
|
transactions_trie: Node::Empty.into(),
|
|
receipts_trie: Node::Empty.into(),
|
|
storage_tries: vec![],
|
|
};
|
|
|
|
let txdata_gas = 2 * 16;
|
|
let gas_used = 21_000 + code_gas + txdata_gas;
|
|
|
|
// Generated using a little py-evm script.
|
|
let txn = hex!("f861050a8255f094a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0648242421ba02c89eb757d9deeb1f5b3859a9d4d679951ef610ac47ad4608dc142beb1b7e313a05af7e9fbab825455d36c36c7f4cfcafbeafa9a77bdff936b52afb36d4fe4bcdd");
|
|
let value = U256::from(100u32);
|
|
|
|
let block_metadata = BlockMetadata {
|
|
block_beneficiary: Address::from(beneficiary),
|
|
block_difficulty: 0x20000.into(),
|
|
block_number: 1.into(),
|
|
block_chain_id: 1.into(),
|
|
block_timestamp: 0x03e8.into(),
|
|
block_gaslimit: 0xff112233u32.into(),
|
|
block_gas_used: gas_used.into(),
|
|
block_bloom: [0.into(); 8],
|
|
block_base_fee: 0xa.into(),
|
|
block_random: Default::default(),
|
|
};
|
|
|
|
let mut contract_code = HashMap::new();
|
|
contract_code.insert(keccak(vec![]), vec![]);
|
|
contract_code.insert(code_hash, code.to_vec());
|
|
|
|
let expected_state_trie_after: HashedPartialTrie = {
|
|
let beneficiary_account_after = AccountRlp {
|
|
nonce: 1.into(),
|
|
..AccountRlp::default()
|
|
};
|
|
let sender_account_after = AccountRlp {
|
|
balance: sender_account_before.balance - value - gas_used * 10,
|
|
nonce: sender_account_before.nonce + 1,
|
|
..sender_account_before
|
|
};
|
|
let to_account_after = AccountRlp {
|
|
balance: to_account_before.balance + value,
|
|
..to_account_before
|
|
};
|
|
|
|
let mut children = core::array::from_fn(|_| Node::Empty.into());
|
|
children[beneficiary_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
|
nibbles: beneficiary_nibbles.truncate_n_nibbles_front(1),
|
|
value: rlp::encode(&beneficiary_account_after).to_vec(),
|
|
}
|
|
.into();
|
|
children[sender_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
|
nibbles: sender_nibbles.truncate_n_nibbles_front(1),
|
|
value: rlp::encode(&sender_account_after).to_vec(),
|
|
}
|
|
.into();
|
|
children[to_nibbles.get_nibble(0) as usize] = Node::Leaf {
|
|
nibbles: to_nibbles.truncate_n_nibbles_front(1),
|
|
value: rlp::encode(&to_account_after).to_vec(),
|
|
}
|
|
.into();
|
|
Node::Branch {
|
|
children,
|
|
value: vec![],
|
|
}
|
|
}
|
|
.into();
|
|
|
|
let receipt_0 = LegacyReceiptRlp {
|
|
status: true,
|
|
cum_gas_used: gas_used.into(),
|
|
bloom: vec![0; 256].into(),
|
|
logs: vec![],
|
|
};
|
|
let mut receipts_trie = HashedPartialTrie::from(Node::Empty);
|
|
receipts_trie.insert(
|
|
Nibbles::from_str("0x80").unwrap(),
|
|
rlp::encode(&receipt_0).to_vec(),
|
|
);
|
|
let transactions_trie: HashedPartialTrie = Node::Leaf {
|
|
nibbles: Nibbles::from_str("0x80").unwrap(),
|
|
value: txn.to_vec(),
|
|
}
|
|
.into();
|
|
|
|
let trie_roots_after = TrieRoots {
|
|
state_root: expected_state_trie_after.hash(),
|
|
transactions_root: transactions_trie.hash(),
|
|
receipts_root: receipts_trie.hash(),
|
|
};
|
|
let inputs = GenerationInputs {
|
|
signed_txn: Some(txn.to_vec()),
|
|
withdrawals: vec![],
|
|
tries: tries_before,
|
|
trie_roots_after,
|
|
contract_code,
|
|
checkpoint_state_trie_root: HashedPartialTrie::from(Node::Empty).hash(),
|
|
block_metadata,
|
|
txn_number_before: 0.into(),
|
|
gas_used_before: 0.into(),
|
|
gas_used_after: gas_used.into(),
|
|
block_hashes: BlockHashes {
|
|
prev_hashes: vec![H256::default(); 256],
|
|
cur_hash: H256::default(),
|
|
},
|
|
};
|
|
|
|
let mut timing = TimingTree::new("prove", log::Level::Debug);
|
|
let proof = prove::<F, C, D>(&all_stark, &config, inputs, &mut timing, None)?;
|
|
timing.filter(Duration::from_millis(100)).print();
|
|
|
|
verify_proof(&all_stark, proof, &config)
|
|
}
|
|
|
|
fn eth_to_wei(eth: U256) -> U256 {
|
|
// 1 ether = 10^18 wei.
|
|
eth * U256::from(10).pow(18.into())
|
|
}
|
|
|
|
fn init_logger() {
|
|
let _ = try_init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
|
|
}
|