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

fix: add single and mapping struct to integration test for generic extraction (Part 4) #397

Open
wants to merge 24 commits into
base: generic-extraction-row-id-update
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
93c80de
Update integration test for generic extraction.
silathdiir Oct 24, 2024
a2c4cfc
Remove `bit_offset` in API.
silathdiir Nov 7, 2024
d7607cf
Merge remote-tracking branch 'origin/generic-extraction-row-id-update…
silathdiir Nov 7, 2024
51768ea
Merge remote-tracking branch 'origin/generic-extraction-row-id-update…
silathdiir Nov 7, 2024
b33cd73
Combine the single and mapping test cases, and update the merge test …
silathdiir Nov 8, 2024
911aadb
Rename to `ContractController` and update the trait function names.
silathdiir Nov 12, 2024
6b8d4bc
Add TODO to the deprecated `bit_offset`.
silathdiir Nov 12, 2024
e2b717c
Fix the wrong log.
silathdiir Nov 12, 2024
302b17e
Add back the MPT key and ptr check.
silathdiir Nov 12, 2024
6155a66
Fix `last_byte_offset` to not restrict the maximum length.
silathdiir Nov 12, 2024
051bf96
Rename `MetadataGadget` to `ColumnsMetadata`, and leave `build` and `…
silathdiir Nov 12, 2024
c21fe13
Add more common `_raw` functions for the values extraction identifier…
silathdiir Nov 12, 2024
29fd77b
Remove `TableDimension`.
silathdiir Nov 13, 2024
2d1aa8a
Fix the row unique ID always get from the key ID column.
silathdiir Nov 13, 2024
38d6d30
Fix to the value column as the secondary index column in mapping stru…
silathdiir Nov 13, 2024
be3db69
Merge the match arms for mapping update.
silathdiir Nov 14, 2024
973325d
Set to simple rest for the row key.
silathdiir Nov 14, 2024
c423611
Fix the slot checking logic in the storage trie.
silathdiir Nov 14, 2024
50a58d9
Refactor the columns metadata and APIs.
silathdiir Nov 14, 2024
f53f0ee
Fix test.
silathdiir Nov 15, 2024
45deacf
Fix test.
silathdiir Nov 15, 2024
8e1fb40
Merge remote-tracking branch 'origin/generic-extraction-row-id-update…
silathdiir Nov 18, 2024
a22e6d7
Merge remote-tracking branch 'origin/generic-extraction-row-id-update…
silathdiir Dec 13, 2024
47a5784
Merge branch 'generic-extraction-row-id-update' into generic-extracti…
nicholas-mainardi Jan 24, 2025
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
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,
U256::from_be_slice(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
41 changes: 16 additions & 25 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,15 +210,12 @@ pub enum SlotInputs {
MappingWithLength(Vec<SlotInput>, u8),
}

#[derive(Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SlotInput::new() <-- now that we now there is no bitpacking level, then we can get rid of the bit_offset argument and just set it to 0 for the moment, until we refactor the circuit to be simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove bit_offset in the SlotInput and the new function in commit a2c4cfc.

pub struct SlotInput {
/// Slot information of the variable
pub(crate) slot: u8,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant information, can we extract it and put it separately ?
i.e. a table column's set is represented by (slot, Vec<SlotInput>)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but I make this SlotInput Struct corresponding to ColumnInfo, and seems it makes the conversion complicated. Please correct me if I was wrong, thanks.

/// The offset in bytes where to extract this column in a given EVM word
pub(crate) byte_offset: usize,
/// The starting offset in `byte_offset` of the bits to be extracted for this column.
/// The column bits will start at `byte_offset * 8 + bit_offset`.
pub(crate) bit_offset: usize,
/// The length (in bits) of the field to extract in the EVM word
pub(crate) length: usize,
/// At which EVM word is this column extracted from. For simple variables,
Expand All @@ -229,30 +227,19 @@ pub struct SlotInput {
impl From<&ColumnInfo> for SlotInput {
fn from(column_info: &ColumnInfo) -> Self {
let slot = u8::try_from(column_info.slot.to_canonical_u64()).unwrap();
let [byte_offset, bit_offset, length] = [
column_info.byte_offset,
column_info.bit_offset,
column_info.length,
]
.map(|f| usize::try_from(f.to_canonical_u64()).unwrap());
let [byte_offset, length] = [column_info.byte_offset, column_info.length]
.map(|f| usize::try_from(f.to_canonical_u64()).unwrap());
let evm_word = u32::try_from(column_info.evm_word.to_canonical_u64()).unwrap();

SlotInput::new(slot, byte_offset, bit_offset, length, evm_word)
SlotInput::new(slot, byte_offset, length, evm_word)
}
}

impl SlotInput {
pub fn new(
slot: u8,
byte_offset: usize,
bit_offset: usize,
length: usize,
evm_word: u32,
) -> Self {
pub fn new(slot: u8, byte_offset: usize, length: usize, evm_word: u32) -> Self {
Self {
slot,
byte_offset,
bit_offset,
length,
evm_word,
}
Expand All @@ -266,10 +253,6 @@ impl SlotInput {
self.byte_offset
}

pub fn bit_offset(&self) -> usize {
self.bit_offset
}

pub fn length(&self) -> usize {
self.length
}
Expand Down Expand Up @@ -338,7 +321,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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are doing big changes, let's take a shot at having a better unified API.
For example, the address chain_id fields could be directly put inside extra no ? Since we don't prove the computation of these identifiers, we dont need to have special ordering or whatever.
Then we could add if we want another API endpoint that call this one with explicit chain_id address genesis_block for example since those are the information we put. But we can do that on DQ side it's fine.

Copy link
Contributor Author

@silathdiir silathdiir Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, in commit c21fe13 I added the similar functions (*_raw) for this and other identifiers computation which could only pass the extra argument, the original functions (have contract_address and chain_id arguments) call these functions. If we set extra = (contract_address || chain_id || extra), the result of two similar functions should be same.

inputs: Vec<SlotInput>,
address: &Address,
chain_id: u64,
Expand All @@ -353,7 +336,7 @@ fn compute_table_info(
input.slot,
id,
input.byte_offset,
input.bit_offset,
0, // bit_offset
input.length,
input.evm_word,
)
Expand Down Expand Up @@ -438,8 +421,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());
nicholas-mainardi marked this conversation as resolved.
Show resolved Hide resolved
// 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
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 PublicInputs<'_, 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
3 changes: 3 additions & 0 deletions mp2-v1/src/indexing/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,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
Loading
Loading