Skip to content

Commit

Permalink
Update integration test for generic extraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
silathdiir committed Nov 3, 2024
1 parent 5ce5ca5 commit 7c95a5b
Show file tree
Hide file tree
Showing 33 changed files with 2,970 additions and 738 deletions.
16 changes: 15 additions & 1 deletion mp2-common/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use anyhow::{bail, Result};
use eth_trie::{EthTrie, MemoryDB, Trie};
use ethereum_types::H256;
use itertools::Itertools;
use log::debug;
use log::warn;
use rlp::Rlp;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -214,6 +215,10 @@ impl StorageSlot {
.checked_add(U256::from(*evm_offset))
.unwrap()
.to_be_bytes();
debug!(
"Storage slot struct: parent_location = {}, evm_offset = {}",
parent_location, evm_offset,
);
B256::from_slice(&location)
}
}
Expand All @@ -237,6 +242,9 @@ impl StorageSlot {
}
}
impl ProofQuery {
pub fn new(contract: Address, slot: StorageSlot) -> Self {
Self { contract, slot }
}
pub fn new_simple_slot(address: Address, slot: usize) -> Self {
Self {
contract: address,
Expand All @@ -256,8 +264,14 @@ impl ProofQuery {
) -> Result<EIP1186AccountProofResponse> {
// Query the MPT proof with retries.
for i in 0..RETRY_NUM {
let location = self.slot.location();
debug!(
"Querying MPT proof:\n\tslot = {:?}, location = {:?}",
self.slot,
hex::encode(location.as_slice()),
);
match provider
.get_proof(self.contract, vec![self.slot.location()])
.get_proof(self.contract, vec![location])
.block_id(block.into())
.await
{
Expand Down
6 changes: 6 additions & 0 deletions mp2-common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ impl From<HashOut<F>> for HashOutput {
}
}

impl From<&HashOut<F>> for HashOutput {
fn from(value: &HashOut<F>) -> Self {
value.to_bytes().try_into().unwrap()
}
}

impl From<HashOutput> for HashOut<F> {
fn from(value: HashOutput) -> Self {
Self::from_bytes(&value.0)
Expand Down
13 changes: 11 additions & 2 deletions mp2-v1/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
use alloy::primitives::Address;
use anyhow::Result;
use itertools::Itertools;
use log::debug;
use mp2_common::{
digest::Digest,
group_hashing::map_to_curve_point,
Expand Down Expand Up @@ -209,7 +210,7 @@ pub enum SlotInputs {
MappingWithLength(Vec<SlotInput>, u8),
}

#[derive(Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct SlotInput {
/// Slot information of the variable
pub(crate) slot: u8,
Expand Down Expand Up @@ -338,7 +339,7 @@ fn value_metadata<const MAX_COLUMNS: usize, const MAX_FIELD_PER_EVM: usize>(
}

/// Compute the table information for the value columns.
fn compute_table_info(
pub fn compute_table_info(
inputs: Vec<SlotInput>,
address: &Address,
chain_id: u64,
Expand Down Expand Up @@ -438,8 +439,16 @@ pub fn metadata_hash<const MAX_COLUMNS: usize, const MAX_FIELD_PER_EVM: usize>(
chain_id,
extra,
);
// Correspond to the computation of final extraction base circuit.
let value_digest = map_to_curve_point(&value_digest.to_fields());
// add contract digest
let contract_digest = contract_metadata_digest(contract_address);
debug!(
"METADATA_HASH ->\n\tvalues_ext_md = {:?}\n\tcontract_md = {:?}\n\tfinal_ex_md(contract + values_ex) = {:?}",
value_digest.to_weierstrass(),
contract_digest.to_weierstrass(),
(contract_digest + value_digest).to_weierstrass(),
);
// compute final hash
combine_digest_and_block(contract_digest + value_digest)
}
4 changes: 3 additions & 1 deletion mp2-v1/src/final_extraction/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ impl CircuitInput {
Ok(Self::MergeTable(MergeCircuitInput {
base,
is_table_a_multiplier: true,
table_a_dimension: TableDimension::Single,
// TODO: May delete `TableDimension::Single`, we don't compute the wrapping digest for
// single dimension, otherwise the final digest is different with the block tree digest.
table_a_dimension: TableDimension::Compound,
table_b_dimension: TableDimension::Compound,
}))
}
Expand Down
3 changes: 1 addition & 2 deletions mp2-v1/src/final_extraction/merge_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use mp2_common::{
digest::{SplitDigestTarget, TableDimension, TableDimensionWire},
serialization::{deserialize, serialize},
types::CBuilder,
utils::{SliceConnector, ToTargets},
utils::ToTargets,
D, F,
};
use plonky2::{
Expand All @@ -19,7 +19,6 @@ use plonky2::{
},
plonk::circuit_builder::CircuitBuilder,
};
use plonky2_ecgfp5::gadgets::curve::CircuitBuilderEcGFp5;
use recursion_framework::circuit_builder::CircuitLogicWires;
use serde::{Deserialize, Serialize};
use verifiable_db::extraction::ExtractionPI;
Expand Down
6 changes: 5 additions & 1 deletion mp2-v1/src/final_extraction/public_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mp2_common::{
public_inputs::{PublicInputCommon, PublicInputRange},
types::{CBuilder, CURVE_TARGET_LEN},
u256::{self, UInt256Target},
utils::{FromFields, FromTargets, ToTargets},
utils::{FromFields, FromTargets, ToTargets, TryIntoBool},
F,
};
use plonky2::iop::target::{BoolTarget, Target};
Expand Down Expand Up @@ -110,6 +110,10 @@ impl<'a> PublicInputs<'a, F> {
pub fn block_number(&self) -> u64 {
U256::from_fields(self.bn).to()
}
/// Get the merge flag
pub fn merge_flag(&self) -> bool {
self.merge[0].try_into_bool().unwrap()
}
}

impl<'a, T> PublicInputs<'a, T> {
Expand Down
5 changes: 5 additions & 0 deletions mp2-v1/src/indexing/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ pub struct RowPayload<PrimaryIndex: PartialEq + Eq + Default> {
pub max: U256,
/// Hash of this node
pub hash: HashOutput,
/// Row unique data
pub row_unique_data: HashOutput,
}

impl<PrimaryIndex: std::fmt::Debug + Clone + Default + PartialEq + Eq> RowPayload<PrimaryIndex> {
Expand Down Expand Up @@ -199,6 +201,9 @@ impl<PrimaryIndex: std::fmt::Debug + Clone + Default + PartialEq + Eq> RowPayloa
}
}

pub fn column_value(&self, column_id: ColumnID) -> Option<U256> {
self.cells.get(&column_id).map(|c| c.value)
}
pub fn secondary_index_value(&self) -> U256 {
self.cells
.get(&self.secondary_index_column)
Expand Down
49 changes: 25 additions & 24 deletions mp2-v1/src/values_extraction/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,12 @@ mod tests {
}
// Mapping variable
StorageSlot::Mapping(mapping_key, slot) => {
let outer_key_id = test_slot.outer_key_id.unwrap();
let metadata_digest = compute_leaf_mapping_metadata_digest::<
TEST_MAX_COLUMNS,
TEST_MAX_FIELD_PER_EVM,
>(
table_info.clone(), slot as u8, test_slot.outer_key_id
table_info.clone(), slot as u8, outer_key_id
);

let values_digest = compute_leaf_mapping_values_digest::<TEST_MAX_FIELD_PER_EVM>(
Expand All @@ -912,14 +913,14 @@ mod tests {
value,
mapping_key.clone(),
evm_word,
test_slot.outer_key_id,
outer_key_id,
);

let circuit_input = CircuitInput::new_mapping_variable_leaf(
node,
slot as u8,
mapping_key,
test_slot.outer_key_id,
outer_key_id,
metadata,
);

Expand All @@ -946,27 +947,27 @@ mod tests {
}
// Mapping Struct
StorageSlot::Mapping(mapping_key, slot) => {
let metadata_digest = compute_leaf_mapping_metadata_digest::<
TEST_MAX_COLUMNS,
TEST_MAX_FIELD_PER_EVM,
>(
table_info.clone(), slot as u8, test_slot.outer_key_id
);
let outer_key_id = test_slot.outer_key_id.unwrap();
let metadata_digest =
compute_leaf_mapping_metadata_digest::<
TEST_MAX_COLUMNS,
TEST_MAX_FIELD_PER_EVM,
>(table_info.clone(), slot as u8, outer_key_id);

let values_digest = compute_leaf_mapping_values_digest::<TEST_MAX_FIELD_PER_EVM>(
table_info,
&extracted_column_identifiers,
value,
mapping_key.clone(),
evm_word,
test_slot.outer_key_id,
outer_key_id,
);

let circuit_input = CircuitInput::new_mapping_variable_leaf(
node,
slot as u8,
mapping_key,
test_slot.outer_key_id,
outer_key_id,
metadata,
);

Expand All @@ -976,15 +977,15 @@ mod tests {
StorageSlot::Node(StorageSlotNode::Mapping(grand, inner_mapping_key)) => {
match *grand {
StorageSlot::Mapping(outer_mapping_key, slot) => {
let metadata_digest = compute_leaf_mapping_of_mappings_metadata_digest::<
TEST_MAX_COLUMNS,
TEST_MAX_FIELD_PER_EVM,
>(
table_info.clone(),
slot as u8,
test_slot.outer_key_id,
test_slot.inner_key_id,
);
let outer_key_id = test_slot.outer_key_id.unwrap();
let inner_key_id = test_slot.inner_key_id.unwrap();
let metadata_digest =
compute_leaf_mapping_of_mappings_metadata_digest::<
TEST_MAX_COLUMNS,
TEST_MAX_FIELD_PER_EVM,
>(
table_info.clone(), slot as u8, outer_key_id, inner_key_id
);

let values_digest = compute_leaf_mapping_of_mappings_values_digest::<
TEST_MAX_FIELD_PER_EVM,
Expand All @@ -995,17 +996,17 @@ mod tests {
evm_word,
outer_mapping_key.clone(),
inner_mapping_key.clone(),
test_slot.outer_key_id,
test_slot.inner_key_id,
outer_key_id,
inner_key_id,
);

let circuit_input = CircuitInput::new_mapping_of_mappings_leaf(
node,
slot as u8,
outer_mapping_key,
inner_mapping_key,
test_slot.outer_key_id,
test_slot.inner_key_id,
outer_key_id,
inner_key_id,
metadata,
);

Expand Down
Loading

0 comments on commit 7c95a5b

Please sign in to comment.