Skip to content

Commit

Permalink
Salt field popped from stack should be RLC with EVM word input (scrol…
Browse files Browse the repository at this point in the history
…l-tech#441)

* fix: salt field popped from stack

* revert commit c89f329
  • Loading branch information
roynalnaruto authored Apr 3, 2023
1 parent a673720 commit c95f4ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<F: Field, const IS_CREATE2: bool, const S: ExecutionState> ExecutionGadget<
cb.stack_pop(init_code.offset_rlc());
cb.stack_pop(init_code.length_rlc());
cb.condition(IS_CREATE2.expr(), |cb| {
cb.stack_pop(create.salt_keccak_rlc());
cb.stack_pop(create.salt_word_rlc(cb));
});

cb.stack_push(callee_is_success.expr() * new_address_rlc);
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution/return_revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ mod test {
bytecode.write_op(OpcodeId::MSTORE);
}
bytecode.append(&bytecode! {
PUSH3(0x12) // salt
PUSH3(0x123456) // salt
PUSH1(initializer.len()) // length
PUSH1(0) // offset
PUSH1(0) // value
Expand Down
41 changes: 31 additions & 10 deletions zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub struct ContractCreateGadget<F, const IS_CREATE2: bool> {
/// appropriate RLC wherever needed.
code_hash: [Cell<F>; N_BYTES_WORD],
/// Random salt for CREATE2.
salt: RandomLinearCombination<F, N_BYTES_WORD>,
salt: [Cell<F>; N_BYTES_WORD],
}

impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
Expand All @@ -200,7 +200,7 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
let caller_address = cb.query_keccak_rlc();
let nonce = RlpU64Gadget::construct(cb);
let code_hash = array_init::array_init(|_| cb.query_byte());
let salt = cb.query_keccak_rlc();
let salt = array_init::array_init(|_| cb.query_byte());

Self {
caller_address,
Expand All @@ -227,18 +227,20 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {

self.nonce.assign(region, offset, caller_nonce)?;

self.salt.assign(
region,
offset,
Some(salt.map(|v| v.to_le_bytes()).unwrap_or_default()),
)?;
for (c, v) in self
.code_hash
.iter()
.zip(code_hash.map(|v| v.to_le_bytes()).unwrap_or_default())
{
c.assign(region, offset, Value::known(F::from(v as u64)))?;
}
for (c, v) in self
.salt
.iter()
.zip(salt.map(|v| v.to_le_bytes()).unwrap_or_default())
{
c.assign(region, offset, Value::known(F::from(v as u64)))?;
}

Ok(())
}
Expand Down Expand Up @@ -277,9 +279,28 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
)
}

/// Salt EVM word RLC.
pub(crate) fn salt_word_rlc(&self, cb: &ConstraintBuilder<F>) -> Expression<F> {
cb.word_rlc::<N_BYTES_WORD>(
self.salt
.iter()
.map(Expr::expr)
.collect::<Vec<_>>()
.try_into()
.unwrap(),
)
}

/// Salt keccak RLC.
pub(crate) fn salt_keccak_rlc(&self) -> Expression<F> {
self.salt.expr()
pub(crate) fn salt_keccak_rlc(&self, cb: &ConstraintBuilder<F>) -> Expression<F> {
cb.keccak_rlc::<N_BYTES_WORD>(
self.salt
.iter()
.map(Expr::expr)
.collect::<Vec<_>>()
.try_into()
.unwrap(),
)
}

/// Caller address' RLC value.
Expand Down Expand Up @@ -323,7 +344,7 @@ impl<F: Field, const IS_CREATE2: bool> ContractCreateGadget<F, IS_CREATE2> {
let challenge_power_84 = challenge_power_64.clone() * challenge_power_20;
(0xff.expr() * challenge_power_84)
+ (self.caller_address_rlc() * challenge_power_64)
+ (self.salt_keccak_rlc() * challenge_power_32)
+ (self.salt_keccak_rlc(cb) * challenge_power_32)
+ self.code_hash_keccak_rlc(cb)
} else {
// RLC(RLP([caller_address, caller_nonce]))
Expand Down

0 comments on commit c95f4ae

Please sign in to comment.