Skip to content

Commit

Permalink
Save is_warm_prev of access list to copy circuit. (privacy-scaling-…
Browse files Browse the repository at this point in the history
  • Loading branch information
silathdiir authored Jan 19, 2024
1 parent 5252666 commit 38b79b8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
14 changes: 13 additions & 1 deletion bus-mapping/src/circuit_input_builder/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ impl CopyBytes {
}

/// Transaction access list for copy event
/// Save address, storage_key, storage_key_index and is_warm_prev
/// to column value_word_rlc, value_word_rlc_prev, value and
/// value_prev in copy circuit.
#[derive(Clone, Debug)]
pub struct CopyAccessList {
/// Access list address
Expand All @@ -416,15 +419,24 @@ pub struct CopyAccessList {
/// storage key, it saves the internal index of storage key, which starts
/// from zero for each address list.
pub storage_key_index: u64,
/// Identify if address or storage key is warm previously, since the access
/// list could have duplicate addresses or storage keys.
pub is_warm_prev: bool,
}

impl CopyAccessList {
/// Create a copy access list.
pub fn new(address: Address, storage_key: Word, storage_key_index: u64) -> Self {
pub fn new(
address: Address,
storage_key: Word,
storage_key_index: u64,
is_warm_prev: bool,
) -> Self {
Self {
address,
storage_key,
storage_key_index,
is_warm_prev,
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions bus-mapping/src/evm/opcodes/begin_end_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,15 @@ fn add_access_list_address_copy_event(
is_warm_prev,
)?;

Ok(CopyAccessList::new(item.address, Word::zero(), 0))
// Save address, storage_key, storage_key_index and is_warm_prev
// to column value_word_rlc, value_word_rlc_prev, value and
// value_prev in copy circuit.
Ok(CopyAccessList::new(
item.address,
Word::zero(),
0,
is_warm_prev,
))
})
.collect::<Result<Vec<_>, Error>>()
})?;
Expand Down Expand Up @@ -855,7 +863,15 @@ fn add_access_list_storage_key_copy_event(
is_warm_prev,
)?;

Ok(CopyAccessList::new(item.address, sk, idx as u64))
// Save address, storage_key, storage_key_index and is_warm_prev
// to column value_word_rlc, value_word_rlc_prev, value and
// value_prev in copy circuit.
Ok(CopyAccessList::new(
item.address,
sk,
idx as u64,
is_warm_prev,
))
})
.collect::<Result<Vec<_>, _>>()
})
Expand Down
12 changes: 7 additions & 5 deletions zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {

let tx_id = meta.query_advice(id, CURRENT);
let address = meta.query_advice(value_word_rlc, CURRENT);
let is_warm_prev = meta.query_advice(value_prev, CURRENT);

vec![
1.expr(),
Expand All @@ -488,8 +489,8 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
address, // access list address
0.expr(),
0.expr(),
1.expr(), // is_warm
0.expr(), // is_warm_prev
1.expr(), // is_warm
is_warm_prev, // is_warm_prev
0.expr(),
0.expr(),
]
Expand Down Expand Up @@ -527,6 +528,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
let tx_id = meta.query_advice(id, CURRENT);
let address = meta.query_advice(value_word_rlc, CURRENT);
let storage_key = meta.query_advice(value_word_rlc_prev, CURRENT);
let is_warm_prev = meta.query_advice(value_prev, CURRENT);

vec![
1.expr(),
Expand All @@ -536,9 +538,9 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
tx_id,
address, // access list address
0.expr(),
storage_key, // access list storage key
1.expr(), // is_warm
0.expr(), // is_warm_prev
storage_key, // access list storage key
1.expr(), // is_warm
is_warm_prev, // is_warm_prev
0.expr(),
0.expr(),
]
Expand Down
30 changes: 19 additions & 11 deletions zkevm-circuits/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1868,18 +1868,23 @@ impl CopyTable {

let is_pad = is_read_step && thread.addr >= thread.addr_end;

let [value, value_prev] = [copy_step.value, copy_step.prev_value]
.map(|val| Value::known(F::from(val as u64)));

let value = if copy_event.src_type == CopyDataType::AccessListStorageKeys {
// If the copy data type is access list storage key, saves
// storage key internal index to the `value` column, it starts
// from zero for each access list address.
let storage_key_index = copy_event.access_list[step_idx / 2].storage_key_index;
Value::known(F::from(storage_key_index))
let [value, value_prev] = if is_access_list {
// Save address, storage_key, storage_key_index and is_warm_prev
// to column value_word_rlc, value_word_rlc_prev, value and
// value_prev in copy circuit.
let access_list = &copy_event.access_list[step_idx / 2];

[
F::from(access_list.storage_key_index),
F::from(access_list.is_warm_prev),
]
} else {
value
};
[
F::from(copy_step.value as u64),
F::from(copy_step.prev_value as u64),
]
}
.map(Value::known);

let value_or_pad = if is_pad {
Value::known(F::zero())
Expand All @@ -1904,6 +1909,9 @@ impl CopyTable {
};

if is_access_list {
// Save address, storage_key, storage_key_index and is_warm_prev
// to column value_word_rlc, value_word_rlc_prev, value and
// value_prev in copy circuit.
let access_list = &copy_event.access_list[step_idx / 2];

thread.word_rlc = Value::known(access_list.address.to_scalar().unwrap());
Expand Down

0 comments on commit 38b79b8

Please sign in to comment.