Skip to content

Commit c21fe13

Browse files
committed
Add more common _raw functions for the values extraction identifiers computation.
1 parent 051bf96 commit c21fe13

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

mp2-v1/src/values_extraction/mod.rs

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,34 @@ pub fn identifier_block_column() -> u64 {
102102
/// Compute identifier for value column.
103103
///
104104
/// The value column could be either simple or mapping slot.
105-
/// `id = H(slot || byte_offset || length || evm_word || contract_address || chain_id)[0]`
105+
/// `id = H(slot || byte_offset || length || evm_word || contract_address || chain_id || extra)[0]`
106106
pub fn identifier_for_value_column(
107107
input: &SlotInput,
108108
contract_address: &Address,
109109
chain_id: u64,
110110
extra: Vec<u8>,
111111
) -> u64 {
112+
let extra = contract_address
113+
.0
114+
.into_iter()
115+
.chain(chain_id.to_be_bytes())
116+
.chain(extra)
117+
.collect_vec();
118+
119+
identifier_for_value_column_raw(input, extra)
120+
}
121+
122+
/// Compute identifier for value column in raw mode.
123+
/// The value column could be either simple or mapping slot.
124+
/// `id = H(slot || byte_offset || length || evm_word || extra)[0]`
125+
///
126+
/// We could custom the `extra` argument, if it's set to `(contract_address || chain_id || extra)`,
127+
/// It's same with `identifier_for_mapping_key_column`.
128+
pub fn identifier_for_value_column_raw(input: &SlotInput, extra: Vec<u8>) -> u64 {
112129
let inputs = once(input.slot)
113130
.chain(input.byte_offset.to_be_bytes())
114131
.chain(input.length.to_be_bytes())
115132
.chain(input.evm_word.to_be_bytes())
116-
.chain(contract_address.0.to_vec())
117-
.chain(chain_id.to_be_bytes())
118133
.chain(extra)
119134
.map(F::from_canonical_u8)
120135
.collect_vec();
@@ -133,6 +148,15 @@ pub fn identifier_for_mapping_key_column(
133148
compute_id_with_prefix(KEY_ID_PREFIX, slot, contract_address, chain_id, extra)
134149
}
135150

151+
/// Compute key indetifier for mapping variable in raw mode.
152+
/// `key_id = H(KEY || slot || contract_address || chain_id)[0]`
153+
///
154+
/// We could custom the `extra` argument, if it's set to `(contract_address || chain_id || extra)`,
155+
/// It's same with `identifier_for_mapping_key_column`.
156+
pub fn identifier_for_mapping_key_column_raw(slot: u8, extra: Vec<u8>) -> u64 {
157+
compute_id_with_prefix_raw(KEY_ID_PREFIX, slot, extra)
158+
}
159+
136160
/// Compute outer key indetifier for mapping of mappings variable.
137161
/// `outer_key_id = H(OUT_KEY || slot || contract_address || chain_id)[0]`
138162
pub fn identifier_for_outer_mapping_key_column(
@@ -144,6 +168,14 @@ pub fn identifier_for_outer_mapping_key_column(
144168
compute_id_with_prefix(OUTER_KEY_ID_PREFIX, slot, contract_address, chain_id, extra)
145169
}
146170

171+
/// Compute outer key indetifier for mapping of mappings variable in raw mode.
172+
/// `outer_key_id = H(OUT_KEY || slot || contract_address || chain_id)[0]`
173+
///
174+
/// We could custom the `extra` argument, if it's set to `(contract_address || chain_id || extra)`,
175+
/// It's same with `identifier_for_outer_mapping_key_column`.
176+
pub fn identifier_for_outer_mapping_key_column_raw(slot: u8, extra: Vec<u8>) -> u64 {
177+
compute_id_with_prefix_raw(OUTER_KEY_ID_PREFIX, slot, extra)
178+
}
147179
/// Compute inner key indetifier for mapping of mappings variable.
148180
/// `inner_key_id = H(IN_KEY || slot || contract_address || chain_id)[0]`
149181
pub fn identifier_for_inner_mapping_key_column(
@@ -155,6 +187,15 @@ pub fn identifier_for_inner_mapping_key_column(
155187
compute_id_with_prefix(INNER_KEY_ID_PREFIX, slot, contract_address, chain_id, extra)
156188
}
157189

190+
/// Compute inner key indetifier for mapping of mappings variable in raw mode.
191+
/// `inner_key_id = H(IN_KEY || slot || extra)[0]`
192+
///
193+
/// We could custom the `extra` argument, if it's set to `(contract_address || chain_id || extra)`,
194+
/// It's same with `identifier_for_inner_mapping_key_column`.
195+
pub fn identifier_for_inner_mapping_key_column_raw(slot: u8, extra: Vec<u8>) -> u64 {
196+
compute_id_with_prefix_raw(INNER_KEY_ID_PREFIX, slot, extra)
197+
}
198+
158199
/// Calculate ID with prefix.
159200
fn compute_id_with_prefix(
160201
prefix: &[u8],
@@ -163,14 +204,27 @@ fn compute_id_with_prefix(
163204
chain_id: u64,
164205
extra: Vec<u8>,
165206
) -> u64 {
207+
let extra = contract_address
208+
.0
209+
.into_iter()
210+
.chain(chain_id.to_be_bytes())
211+
.chain(extra)
212+
.collect_vec();
213+
214+
compute_id_with_prefix_raw(prefix, slot, extra)
215+
}
216+
217+
/// Calculate ID with prefix in raw mode.
218+
///
219+
/// We could custom the `extra` argument, if it's set to `(contract_address || chain_id || extra)`,
220+
/// It's same with `compute_id_with_prefix`.
221+
fn compute_id_with_prefix_raw(prefix: &[u8], slot: u8, extra: Vec<u8>) -> u64 {
166222
let inputs: Vec<F> = prefix
167223
.iter()
168224
.cloned()
169225
.chain(once(slot))
170-
.chain(contract_address.0)
171-
.chain(chain_id.to_be_bytes())
172226
.chain(extra)
173-
.collect::<Vec<u8>>()
227+
.collect_vec()
174228
.to_fields();
175229

176230
H::hash_no_pad(&inputs).elements[0].to_canonical_u64()

mp2-v1/tests/common/cases/table_source.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::common::{
4646
proof_storage::{ProofKey, ProofStorage},
4747
rowtree::SecondaryIndexCell,
4848
table::CellsUpdate,
49-
MetadataGadget, StorageSlotInfo, TestContext, TEST_MAX_COLUMNS, TEST_MAX_FIELD_PER_EVM,
49+
ColumnsMetadata, StorageSlotInfo, TestContext, TEST_MAX_COLUMNS, TEST_MAX_FIELD_PER_EVM,
5050
};
5151

5252
use super::{
@@ -706,12 +706,12 @@ impl SingleExtractionArgs {
706706
})
707707
}
708708

709-
fn metadata(&self, contract: &Contract) -> Vec<MetadataGadget> {
709+
fn metadata(&self, contract: &Contract) -> Vec<ColumnsMetadata> {
710710
let table_info = table_info(contract, self.slot_inputs.clone());
711711
metadata(&table_info)
712712
}
713713

714-
fn storage_slots(&self, metadata: &[MetadataGadget]) -> Vec<StorageSlot> {
714+
fn storage_slots(&self, metadata: &[ColumnsMetadata]) -> Vec<StorageSlot> {
715715
metadata
716716
.iter()
717717
.map(|metadata| {
@@ -733,7 +733,7 @@ impl SingleExtractionArgs {
733733
.collect()
734734
}
735735

736-
fn storage_slot_info(&self, metadata: &[MetadataGadget]) -> Vec<StorageSlotInfo> {
736+
fn storage_slot_info(&self, metadata: &[ColumnsMetadata]) -> Vec<StorageSlotInfo> {
737737
let storage_slots = self.storage_slots(metadata);
738738

739739
metadata
@@ -1225,7 +1225,7 @@ where
12251225
fn storage_slot_info(
12261226
&self,
12271227
key_id: u64,
1228-
metadata: &MetadataGadget,
1228+
metadata: &ColumnsMetadata,
12291229
mapping_key: Vec<u8>,
12301230
) -> StorageSlotInfo {
12311231
let storage_slot = V::mapping_storage_slot(self.slot, metadata.evm_word(), mapping_key);
@@ -1287,7 +1287,7 @@ where
12871287
V::from_u256_slice(&extracted_values)
12881288
}
12891289

1290-
fn metadata(&self, contract: &Contract) -> Vec<MetadataGadget> {
1290+
fn metadata(&self, contract: &Contract) -> Vec<ColumnsMetadata> {
12911291
let table_info = table_info(contract, self.slot_inputs.clone());
12921292
metadata(&table_info)
12931293
}
@@ -1299,7 +1299,7 @@ fn table_info(contract: &Contract, slot_inputs: Vec<SlotInput>) -> Vec<ColumnInf
12991299
}
13001300

13011301
/// Construct the metadata for each slot and EVM word.
1302-
fn metadata(table_info: &[ColumnInfo]) -> Vec<MetadataGadget> {
1302+
fn metadata(table_info: &[ColumnInfo]) -> Vec<ColumnsMetadata> {
13031303
// Initialize a mapping of `(slot, evm_word) -> column_Identifier`.
13041304
let mut slots_ids = HashMap::new();
13051305
table_info.iter().for_each(|col| {
@@ -1314,7 +1314,7 @@ fn metadata(table_info: &[ColumnInfo]) -> Vec<MetadataGadget> {
13141314
slots_ids
13151315
.into_iter()
13161316
.map(|((_, evm_word), ids)| {
1317-
MetadataGadget::new(
1317+
ColumnsMetadata::new(
13181318
table_info.to_vec(),
13191319
&ids,
13201320
u32::try_from(evm_word.to_canonical_u64()).unwrap(),

mp2-v1/tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const TEST_MAX_FIELD_PER_EVM: usize = 32;
3838
type ColumnIdentifier = u64;
3939
type StorageSlotInfo =
4040
mp2_v1::values_extraction::StorageSlotInfo<TEST_MAX_COLUMNS, TEST_MAX_FIELD_PER_EVM>;
41-
type MetadataGadget = mp2_v1::values_extraction::gadgets::metadata_gadget::ColumnsMetadata<
41+
type ColumnsMetadata = mp2_v1::values_extraction::gadgets::metadata_gadget::ColumnsMetadata<
4242
TEST_MAX_COLUMNS,
4343
TEST_MAX_FIELD_PER_EVM,
4444
>;

0 commit comments

Comments
 (0)