This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from 0xPolygonZero/plonky2
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add withdrawals (0xPolygonZero#1322)
* Withdrawals * Remove AllRecursiveCircuits in withdrawals test * Fix ERC20 test
- Loading branch information
Showing
17 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
%macro withdrawals | ||
// stack: (empty) | ||
PUSH %%after | ||
%jump(withdrawals) | ||
%%after: | ||
// stack: (empty) | ||
%endmacro | ||
|
||
global withdrawals: | ||
// stack: retdest | ||
PROVER_INPUT(withdrawal) | ||
// stack: address, retdest | ||
PROVER_INPUT(withdrawal) | ||
// stack: amount, address, retdest | ||
DUP2 %eq_const(@U256_MAX) %jumpi(withdrawals_end) | ||
SWAP1 | ||
// stack: address, amount, retdest | ||
%add_eth | ||
// stack: retdest | ||
%jump(withdrawals) | ||
|
||
withdrawals_end: | ||
// stack: amount, address, retdest | ||
%pop2 | ||
JUMP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use std::collections::HashMap; | ||
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::{H160, H256, U256}; | ||
use keccak_hash::keccak; | ||
use plonky2::field::goldilocks_field::GoldilocksField; | ||
use plonky2::plonk::config::PoseidonGoldilocksConfig; | ||
use plonky2::util::timing::TimingTree; | ||
use plonky2_evm::all_stark::AllStark; | ||
use plonky2_evm::config::StarkConfig; | ||
use plonky2_evm::generation::mpt::AccountRlp; | ||
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::Node; | ||
use rand::random; | ||
|
||
type F = GoldilocksField; | ||
const D: usize = 2; | ||
type C = PoseidonGoldilocksConfig; | ||
|
||
/// Execute 0 txns and 1 withdrawal. | ||
#[test] | ||
fn test_withdrawals() -> anyhow::Result<()> { | ||
init_logger(); | ||
|
||
let all_stark = AllStark::<F, D>::default(); | ||
let config = StarkConfig::standard_fast_config(); | ||
|
||
let block_metadata = BlockMetadata::default(); | ||
|
||
let state_trie_before = HashedPartialTrie::from(Node::Empty); | ||
let transactions_trie = HashedPartialTrie::from(Node::Empty); | ||
let receipts_trie = HashedPartialTrie::from(Node::Empty); | ||
let storage_tries = vec![]; | ||
|
||
let mut contract_code = HashMap::new(); | ||
contract_code.insert(keccak(vec![]), vec![]); | ||
|
||
// Just one withdrawal. | ||
let withdrawals = vec![(H160(random()), U256(random()))]; | ||
|
||
let state_trie_after = { | ||
let mut trie = HashedPartialTrie::from(Node::Empty); | ||
let addr_state_key = keccak(withdrawals[0].0); | ||
let addr_nibbles = Nibbles::from_bytes_be(addr_state_key.as_bytes()).unwrap(); | ||
let account = AccountRlp { | ||
balance: withdrawals[0].1, | ||
..AccountRlp::default() | ||
}; | ||
trie.insert(addr_nibbles, rlp::encode(&account).to_vec()); | ||
trie | ||
}; | ||
|
||
let trie_roots_after = TrieRoots { | ||
state_root: state_trie_after.hash(), | ||
transactions_root: transactions_trie.hash(), | ||
receipts_root: receipts_trie.hash(), | ||
}; | ||
|
||
let inputs = GenerationInputs { | ||
signed_txns: vec![], | ||
withdrawals, | ||
tries: TrieInputs { | ||
state_trie: state_trie_before, | ||
transactions_trie, | ||
receipts_trie, | ||
storage_tries, | ||
}, | ||
trie_roots_after, | ||
contract_code, | ||
genesis_state_trie_root: HashedPartialTrie::from(Node::Empty).hash(), | ||
block_metadata, | ||
txn_number_before: 0.into(), | ||
gas_used_before: 0.into(), | ||
gas_used_after: 0.into(), | ||
block_bloom_before: [0.into(); 8], | ||
block_bloom_after: [0.into(); 8], | ||
block_hashes: BlockHashes { | ||
prev_hashes: vec![H256::default(); 256], | ||
cur_hash: H256::default(), | ||
}, | ||
addresses: vec![], | ||
}; | ||
|
||
let mut timing = TimingTree::new("prove", log::Level::Debug); | ||
let proof = prove::<F, C, D>(&all_stark, &config, inputs, &mut timing)?; | ||
timing.filter(Duration::from_millis(100)).print(); | ||
|
||
verify_proof(&all_stark, proof, &config) | ||
} | ||
|
||
fn init_logger() { | ||
let _ = try_init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info")); | ||
} |