Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plonky2x): eth_getAccount #100

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions plonky2x/src/backend/circuit/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ use crate::frontend::eth::beacon::vars::{
};
use crate::frontend::eth::storage::generators::block::EthBlockGenerator;
use crate::frontend::eth::storage::generators::storage::{
EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
EthAccountProofGenerator, EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
};
use crate::frontend::eth::storage::vars::{EthAccountVariable, EthLogVariable, EthProofVariable};
use crate::frontend::hash::bit_operations::{XOR3Gate, XOR3Generator};
use crate::frontend::hash::keccak::keccak256::Keccak256Generator;
use crate::frontend::num::biguint::BigUintDivRemGenerator;
Expand Down Expand Up @@ -422,6 +423,9 @@ where
let eth_storage_proof_generator_id = EthStorageProofGenerator::<L, D>::id();
r.register_simple::<EthStorageProofGenerator<L, D>>(eth_storage_proof_generator_id);

let eth_account_proof_generator_id = EthAccountProofGenerator::<L, D>::id();
r.register_simple::<EthAccountProofGenerator<L, D>>(eth_account_proof_generator_id);

let eth_log_generator_id = EthLogGenerator::<L, D>::id();
r.register_simple::<EthLogGenerator<L, D>>(eth_log_generator_id);

Expand Down Expand Up @@ -498,7 +502,10 @@ where
BeaconBalancesVariable,
BeaconWithdrawalsVariable,
BeaconWithdrawalVariable,
BeaconValidatorVariable
BeaconValidatorVariable,
EthLogVariable,
EthProofVariable,
EthAccountVariable
);

r
Expand Down
3 changes: 2 additions & 1 deletion plonky2x/src/backend/circuit/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::frontend::eth::beacon::generators::withdrawal::BeaconWithdrawalGenera
use crate::frontend::eth::beacon::generators::withdrawals::BeaconWithdrawalsGenerator;
use crate::frontend::eth::storage::generators::block::EthBlockGenerator;
use crate::frontend::eth::storage::generators::storage::{
EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
EthAccountProofGenerator, EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
};
use crate::frontend::hash::bit_operations::{XOR3Gate, XOR3Generator};
use crate::frontend::hash::keccak::keccak256::Keccak256Generator;
Expand Down Expand Up @@ -160,6 +160,7 @@ where
ReducingExtensionGenerator<D>, "ReducingExtensionGenerator",
SplitGenerator, "SplitGenerator",
WireSplitGenerator, "WireSplitGenerator",
EthAccountProofGenerator<F, D>, "EthAccountProofGenerator",
EthStorageProofGenerator<F, D>, "EthStorageProofGenerator",
EthLogGenerator<F, D>, "EthLogGenerator",
EthBlockGenerator<F, D>, "EthBlockGenerator",
Expand Down
119 changes: 70 additions & 49 deletions plonky2x/src/frontend/eth/storage/builder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use ethers::types::Address;

use super::generators::block::EthBlockGenerator;
use super::generators::storage::{
EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
EthAccountProofGenerator, EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
};
use super::vars::{EthAccountVariable, EthHeaderVariable, EthLogVariable};
use crate::backend::config::PlonkParameters;
Expand Down Expand Up @@ -44,10 +42,12 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
#[allow(non_snake_case)]
pub fn eth_get_account(
&mut self,
_address: Address,
_block_hash: Bytes32Variable,
address: AddressVariable,
block_hash: Bytes32Variable,
) -> EthAccountVariable {
todo!()
let generator = EthAccountProofGenerator::new(self, block_hash, address);
self.add_simple_generator(&generator);
generator.value
}

#[allow(non_snake_case)]
Expand All @@ -71,16 +71,11 @@ mod tests {
use ethers::types::{U256, U64};

use super::*;
use crate::backend::circuit::serialization::{GateRegistry, WitnessGeneratorRegistry};
use crate::backend::config::DefaultParameters;
use crate::frontend::eth::storage::utils::get_map_storage_location;
use crate::frontend::eth::storage::vars::{EthHeader, EthLog};
use crate::frontend::eth::storage::vars::{EthAccount, EthHeader, EthLog};
use crate::prelude::CircuitBuilderX;
use crate::utils::{address, bytes32};

type L = DefaultParameters;
const D: usize = 2;

#[test]
#[cfg_attr(feature = "ci", ignore)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -129,14 +124,7 @@ mod tests {
bytes32!("0x0000000000000000000000dd4bc51496dc93a0c47008e820e0d80745476f2201"),
);

// initialize serializers
let gate_serializer = GateRegistry::<L, D>::new();
let generator_serializer = WitnessGeneratorRegistry::<L, D>::new();

// test serialization
let _ = circuit
.serialize(&gate_serializer, &generator_serializer)
.unwrap();
circuit.test_default_serializers()
}

#[test]
Expand All @@ -146,11 +134,11 @@ mod tests {
dotenv::dotenv().ok();
// This is the circuit definition
let mut builder = CircuitBuilderX::new();
let mapping_location = builder.read::<U256Variable>();
let map_key = builder.read::<Bytes32Variable>();
let mapping_location = builder.evm_read::<U256Variable>();
let map_key = builder.evm_read::<Bytes32Variable>();

let value = builder.get_storage_key_at(mapping_location, map_key);
builder.write(value);
builder.evm_write(value);

// Build your circuit.
let circuit = builder.build();
Expand All @@ -159,12 +147,12 @@ mod tests {
let mut input = circuit.input();
let mapping_location = U256::from("0x0");
// mapping_location
input.write::<U256Variable>(mapping_location);
input.evm_write::<U256Variable>(mapping_location);

let map_key =
bytes32!("0x281dc31bb78779a1ede7bf0f4d2bc5f07ddebc9f9d1155e413d8804384604bbe");
// map_key
input.write::<Bytes32Variable>(map_key);
input.evm_write::<Bytes32Variable>(map_key);

println!(
"storage key: {:?}",
Expand All @@ -178,21 +166,14 @@ mod tests {
circuit.verify(&proof, &input, &output);

// Read output.
let circuit_value = output.read::<Bytes32Variable>();
let circuit_value = output.evm_read::<Bytes32Variable>();
println!("{:?}", circuit_value);
assert_eq!(
circuit_value,
bytes32!("0xca77d4e79102603cb6842afffd8846a3123877159ed214aeadfc4333d595fd50"),
);

// initialize serializers
let gate_serializer = GateRegistry::<L, D>::new();
let generator_serializer = WitnessGeneratorRegistry::<L, D>::new();

// test serialization
let _ = circuit
.serialize(&gate_serializer, &generator_serializer)
.unwrap();
circuit.test_default_serializers()
}

#[test]
Expand Down Expand Up @@ -258,14 +239,7 @@ mod tests {
}
);

// initialize serializers
let gate_serializer = GateRegistry::<L, D>::new();
let generator_serializer = WitnessGeneratorRegistry::<L, D>::new();

// test serialization
let _ = circuit
.serialize(&gate_serializer, &generator_serializer)
.unwrap();
circuit.test_default_serializers()
}

#[test]
Expand Down Expand Up @@ -325,13 +299,60 @@ mod tests {
}
);

// initialize serializers
let gate_serializer = GateRegistry::<L, D>::new();
let generator_serializer = WitnessGeneratorRegistry::<L, D>::new();
circuit.test_default_serializers()
}

#[test]
#[cfg_attr(feature = "ci", ignore)]
#[allow(non_snake_case)]
fn test_eth_get_account() {
dotenv::dotenv().ok();
let rpc_url = env::var("RPC_1").unwrap();
let provider = Provider::<Http>::try_from(rpc_url).unwrap();

// This is the circuit definition
let mut builder = CircuitBuilderX::new();
builder.set_execution_client(provider);
let block_hash = builder.read::<Bytes32Variable>();
let address = builder.read::<AddressVariable>();

let value = builder.eth_get_account(address, block_hash);
builder.write(value);

// Build your circuit.
let circuit = builder.build();

// Write to the circuit input.
// These values are taken from Ethereum block https://etherscan.io/block/17880427
let mut input = circuit.input();
input.write::<Bytes32Variable>(bytes32!(
"0x281dc31bb78779a1ede7bf0f4d2bc5f07ddebc9f9d1155e413d8804384604bbe"
));
input.write::<AddressVariable>(address!("0x55032650b14df07b85bF18A3a3eC8E0Af2e028d5"));

// Generate a proof.
let (proof, mut output) = circuit.prove(&input);

// Verify proof.
circuit.verify(&proof, &input, &output);

// Read output.
let circuit_value = output.read::<EthAccountVariable>();
println!("{:?}", circuit_value);
assert_eq!(
circuit_value,
EthAccount {
nonce: U64::from(1),
balance: U256::from(0),
code_hash: bytes32!(
"0xb9c1c929064cd21734c102a698e68bf617feefcfa5a9f62407c45401546736bf"
),
storage_hash: bytes32!(
"0x073d71569b4b986bc20b6921dbbc1b74145588f765627dd5e566d65a6b7b33cc"
),
},
);

// test serialization
let _ = circuit
.serialize(&gate_serializer, &generator_serializer)
.unwrap();
circuit.test_default_serializers()
}
}
Loading
Loading