Skip to content

Commit

Permalink
Modify rvgo for local context support
Browse files Browse the repository at this point in the history
  • Loading branch information
pcw109550 committed Feb 5, 2024
1 parent e90bfb6 commit 676a74a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions rvgo/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ func stepEVM(t *testing.T, env *vm.EVM, wit *fast.StepWitness, addrs *Addresses,
snap := env.StateDB.Snapshot()

if wit.HasPreimage() {
input, err := wit.EncodePreimageOracleInput()
input, err := wit.EncodePreimageOracleInput(fast.LocalContext{})
require.NoError(t, err)
ret, leftOverGas, err := env.Call(vm.AccountRef(addrs.Sender), addrs.Oracle, input, startingGas, big.NewInt(0))
require.NoError(t, err, "evm must not fail (ret: %x, gas: %d)", ret, startingGas-leftOverGas)
}

input := wit.EncodeStepInput()
input := wit.EncodeStepInput(fast.LocalContext{})

ret, leftOverGas, err := env.Call(vm.AccountRef(addrs.Sender), addrs.RISCV, input, startingGas, big.NewInt(0))
require.NoError(t, err, "evm must not fail (ret: %x), at step %d", ret, step)
Expand Down
3 changes: 2 additions & 1 deletion rvgo/fast/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package fast
import "github.com/ethereum/go-ethereum/crypto"

var (
StepBytes4 = crypto.Keccak256([]byte("step(bytes,bytes)"))[:4]
StepBytes4 = crypto.Keccak256([]byte("step(bytes,bytes,bytes32)"))[:4]
CheatBytes4 = crypto.Keccak256([]byte("cheat(uint256,bytes32,bytes32,uint256)"))[:4]
CheatLocalKeyBytes4 = crypto.Keccak256([]byte("cheatLocalKey(uint256,bytes32,bytes32,uint256,bytes32)"))[:4]
LoadKeccak256PreimagePartBytes4 = crypto.Keccak256([]byte("loadKeccak256PreimagePart(uint256,bytes)"))[:4]
)
32 changes: 21 additions & 11 deletions rvgo/fast/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"errors"
"fmt"

"github.com/ethereum-optimism/optimism/op-preimage"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum/go-ethereum/common"
)

type LocalContext common.Hash

type StepWitness struct {
// encoded state witness
State []byte
Expand All @@ -25,29 +28,35 @@ func uint64ToBytes32(v uint64) []byte {
return out[:]
}

func (wit *StepWitness) EncodeStepInput() []byte {
func (wit *StepWitness) EncodeStepInput(localContext LocalContext) []byte {
abiStatePadding := (32 - (uint64(len(wit.State)) % 32)) % 32
abiProofPadding := (32 - (uint64(len(wit.MemProof)) % 32)) % 32

var input []byte
input = append(input, StepBytes4...)
input = append(input, uint64ToBytes32(32*2)...) // state data offset in bytes
input = append(input, uint64ToBytes32(32*2+32+uint64(len(wit.State))+abiStatePadding)...) // proof data offset in bytes
// TODO pad state data to multiple of 32 bytes
// TODO also pad proof data

input = append(input, uint64ToBytes32(uint64(len(wit.State)))...) // state data length in bytes
// state data offset in bytes
input = append(input, uint64ToBytes32(32*3)...)
// proof data offset in bytes
input = append(input, uint64ToBytes32(32*3+32+uint64(len(wit.State))+abiStatePadding)...)
// local context in bytes
input = append(input, common.Hash(localContext).Bytes()...)

// state data length in bytes
input = append(input, uint64ToBytes32(uint64(len(wit.State)))...)
input = append(input, wit.State[:]...)
input = append(input, make([]byte, abiStatePadding)...)
input = append(input, uint64ToBytes32(uint64(len(wit.MemProof)))...) // proof data length in bytes
// proof data length in bytes
input = append(input, uint64ToBytes32(uint64(len(wit.MemProof)))...)
input = append(input, wit.MemProof[:]...)
input = append(input, make([]byte, abiProofPadding)...)
return input
}

func (wit *StepWitness) HasPreimage() bool {
return wit.PreimageKey != ([32]byte{})
}

func (wit *StepWitness) EncodePreimageOracleInput() ([]byte, error) {
func (wit *StepWitness) EncodePreimageOracleInput(localContext LocalContext) ([]byte, error) {
if wit.PreimageKey == ([32]byte{}) {
return nil, errors.New("cannot encode pre-image oracle input, witness has no pre-image to proof")
}
Expand All @@ -59,13 +68,14 @@ func (wit *StepWitness) EncodePreimageOracleInput() ([]byte, error) {
// In production usage there should be an on-chain contract that exposes this,
// rather than going through the global keccak256 oracle.
var input []byte
input = append(input, CheatBytes4...)
input = append(input, CheatLocalKeyBytes4...)
input = append(input, uint64ToBytes32(wit.PreimageOffset)...)
input = append(input, wit.PreimageKey[:]...)
var tmp [32]byte
copy(tmp[:], wit.PreimageValue[wit.PreimageOffset:])
input = append(input, tmp[:]...)
input = append(input, uint64ToBytes32(uint64(len(wit.PreimageValue))-8)...)
input = append(input, common.Hash(localContext).Bytes()...)
// Note: we can pad calldata to 32 byte multiple, but don't strictly have to
return input, nil
case preimage.Keccak256KeyType:
Expand Down
5 changes: 3 additions & 2 deletions rvgo/slow/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package slow
import (
"encoding/binary"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
Expand Down Expand Up @@ -103,8 +104,8 @@ func Step(calldata []byte, po PreimageOracle) (stateHash common.Hash, outErr err

// TODO: validate abi offset values?

stateContentOffset := uint8(4 + 32 + 32 + 32)
if iszero(eq(b32asBEWord(calldataload(toU64(4+32*2))), shortToU256(stateSize))) {
stateContentOffset := uint8(4 + 32 + 32 + 32 + 32)
if iszero(eq(b32asBEWord(calldataload(toU64(4+32*3))), shortToU256(stateSize))) {
// user-provided state size must match expected state size
panic("invalid state size input")
}
Expand Down
2 changes: 1 addition & 1 deletion rvgo/vm_go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func fullTest(t *testing.T, vmState *fast.VMState, po *testOracle, symbols fast.
require.NoError(t, err)

if runSlow {
slowPostHash, err := slow.Step(wit.EncodeStepInput(), po)
slowPostHash, err := slow.Step(wit.EncodeStepInput(fast.LocalContext{}), po)
require.NoErrorf(t, err, "slow VM err at step %d, PC %08x: %v", i, vmState.PC, err)
require.Equal(t, fastStateHash, slowPostHash, "fast post-state must match slow post-state")
}
Expand Down
2 changes: 1 addition & 1 deletion rvgo/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func runSlowTestSuite(t *testing.T, path string) {
require.NoError(t, err)

// Now run the same in slow mode
input := wit.EncodeStepInput()
input := wit.EncodeStepInput(fast.LocalContext{})
post, err := slow.Step(input, nil)
require.NoErrorf(t, err, "slow VM err at step %d, PC %08x: %v", i, vmState.PC, err)

Expand Down

0 comments on commit 676a74a

Please sign in to comment.