Skip to content

Commit 89e4f94

Browse files
committed
Modify rvgo for local context support
1 parent 51138bc commit 89e4f94

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

rvgo/fast/evm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package fast
33
import "github.com/ethereum/go-ethereum/crypto"
44

55
var (
6-
StepBytes4 = crypto.Keccak256([]byte("step(bytes,bytes)"))[:4]
6+
StepBytes4 = crypto.Keccak256([]byte("step(bytes,bytes,bytes32)"))[:4]
77
CheatBytes4 = crypto.Keccak256([]byte("cheat(uint256,bytes32,bytes32,uint256)"))[:4]
8+
CheatLocalKeyBytes4 = crypto.Keccak256([]byte("cheatLocalKey(uint256,bytes32,bytes32,uint256,bytes32)"))[:4]
89
LoadKeccak256PreimagePartBytes4 = crypto.Keccak256([]byte("loadKeccak256PreimagePart(uint256,bytes)"))[:4]
910
)

rvgo/fast/witness.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"errors"
66
"fmt"
77

8-
"github.com/ethereum-optimism/optimism/op-preimage"
8+
preimage "github.com/ethereum-optimism/optimism/op-preimage"
9+
"github.com/ethereum/go-ethereum/common"
910
)
1011

12+
type LocalContext common.Hash
13+
1114
type StepWitness struct {
1215
// encoded state witness
1316
State []byte
@@ -25,29 +28,35 @@ func uint64ToBytes32(v uint64) []byte {
2528
return out[:]
2629
}
2730

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

3135
var input []byte
3236
input = append(input, StepBytes4...)
33-
input = append(input, uint64ToBytes32(32*2)...) // state data offset in bytes
34-
input = append(input, uint64ToBytes32(32*2+32+uint64(len(wit.State))+abiStatePadding)...) // proof data offset in bytes
35-
// TODO pad state data to multiple of 32 bytes
36-
// TODO also pad proof data
37-
38-
input = append(input, uint64ToBytes32(uint64(len(wit.State)))...) // state data length in bytes
37+
// state data offset in bytes
38+
input = append(input, uint64ToBytes32(32*3)...)
39+
// proof data offset in bytes
40+
input = append(input, uint64ToBytes32(32*3+32+uint64(len(wit.State))+abiStatePadding)...)
41+
// local context in bytes
42+
input = append(input, common.Hash(localContext).Bytes()...)
43+
44+
// state data length in bytes
45+
input = append(input, uint64ToBytes32(uint64(len(wit.State)))...)
3946
input = append(input, wit.State[:]...)
4047
input = append(input, make([]byte, abiStatePadding)...)
41-
input = append(input, uint64ToBytes32(uint64(len(wit.MemProof)))...) // proof data length in bytes
48+
// proof data length in bytes
49+
input = append(input, uint64ToBytes32(uint64(len(wit.MemProof)))...)
4250
input = append(input, wit.MemProof[:]...)
51+
input = append(input, make([]byte, abiProofPadding)...)
4352
return input
4453
}
4554

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

50-
func (wit *StepWitness) EncodePreimageOracleInput() ([]byte, error) {
59+
func (wit *StepWitness) EncodePreimageOracleInput(localContext LocalContext) ([]byte, error) {
5160
if wit.PreimageKey == ([32]byte{}) {
5261
return nil, errors.New("cannot encode pre-image oracle input, witness has no pre-image to proof")
5362
}
@@ -59,13 +68,14 @@ func (wit *StepWitness) EncodePreimageOracleInput() ([]byte, error) {
5968
// In production usage there should be an on-chain contract that exposes this,
6069
// rather than going through the global keccak256 oracle.
6170
var input []byte
62-
input = append(input, CheatBytes4...)
71+
input = append(input, CheatLocalKeyBytes4...)
6372
input = append(input, uint64ToBytes32(wit.PreimageOffset)...)
6473
input = append(input, wit.PreimageKey[:]...)
6574
var tmp [32]byte
6675
copy(tmp[:], wit.PreimageValue[wit.PreimageOffset:])
6776
input = append(input, tmp[:]...)
6877
input = append(input, uint64ToBytes32(uint64(len(wit.PreimageValue))-8)...)
78+
input = append(input, common.Hash(localContext).Bytes()...)
6979
// Note: we can pad calldata to 32 byte multiple, but don't strictly have to
7080
return input, nil
7181
case preimage.Keccak256KeyType:

rvgo/slow/vm.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package slow
33
import (
44
"encoding/binary"
55
"fmt"
6+
67
"github.com/ethereum/go-ethereum/common"
78
"github.com/ethereum/go-ethereum/crypto"
89
)
@@ -103,8 +104,8 @@ func Step(calldata []byte, po PreimageOracle) (stateHash common.Hash, outErr err
103104

104105
// TODO: validate abi offset values?
105106

106-
stateContentOffset := uint8(4 + 32 + 32 + 32)
107-
if iszero(eq(b32asBEWord(calldataload(toU64(4+32*2))), shortToU256(stateSize))) {
107+
stateContentOffset := uint8(4 + 32 + 32 + 32 + 32)
108+
if iszero(eq(b32asBEWord(calldataload(toU64(4+32*3))), shortToU256(stateSize))) {
108109
// user-provided state size must match expected state size
109110
panic("invalid state size input")
110111
}

rvgo/test/evm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ func stepEVM(t *testing.T, env *vm.EVM, wit *fast.StepWitness, addrs *Addresses,
149149
snap := env.StateDB.Snapshot()
150150

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

158-
input := wit.EncodeStepInput()
158+
input := wit.EncodeStepInput(fast.LocalContext{})
159159

160160
ret, leftOverGas, err := env.Call(vm.AccountRef(addrs.Sender), addrs.RISCV, input, startingGas, big.NewInt(0))
161161
require.NoError(t, err, "evm must not fail (ret: %x), at step %d", ret, step)

rvgo/test/vm_go_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func fullTest(t *testing.T, vmState *fast.VMState, po *testOracle, symbols fast.
108108
require.NoError(t, err)
109109

110110
if runSlow {
111-
slowPostHash, err := slow.Step(wit.EncodeStepInput(), po)
111+
slowPostHash, err := slow.Step(wit.EncodeStepInput(fast.LocalContext{}), po)
112112
require.NoErrorf(t, err, "slow VM err at step %d, PC %08x: %v", i, vmState.PC, err)
113113
require.Equal(t, fastStateHash, slowPostHash, "fast post-state must match slow post-state")
114114
}

rvgo/test/vm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func runSlowTestSuite(t *testing.T, path string) {
7575
require.NoError(t, err)
7676

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

0 commit comments

Comments
 (0)