Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
pcw109550 committed Feb 5, 2024
1 parent 19533d8 commit 3c72c74
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 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
1 change: 1 addition & 0 deletions rvgo/fast/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import "github.com/ethereum/go-ethereum/crypto"
var (
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]
)
14 changes: 9 additions & 5 deletions rvgo/fast/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"fmt"

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,7 +28,7 @@ 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

Expand All @@ -35,8 +38,8 @@ func (wit *StepWitness) EncodeStepInput() []byte {
input = append(input, uint64ToBytes32(32*3)...)
// proof data offset in bytes
input = append(input, uint64ToBytes32(32*3+32+uint64(len(wit.State))+abiStatePadding)...)
// dummy localContext
input = append(input, make([]byte, 32)...)
// local context in bytes
input = append(input, common.Hash(localContext).Bytes()...)

// state data length in bytes
input = append(input, uint64ToBytes32(uint64(len(wit.State)))...)
Expand All @@ -53,7 +56,7 @@ 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 @@ -65,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
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
26 changes: 26 additions & 0 deletions rvsol/src/PreimageOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ contract PreimageOracle {
preimageLengths[key] = size;
}

function localize(bytes32 _key, bytes32 _localContext) internal view returns (bytes32 localizedKey_) {
assembly {
// Grab the current free memory pointer to restore later.
let ptr := mload(0x40)
// Store the local data key and caller next to each other in memory for hashing.
mstore(0, _key)
mstore(0x20, caller())
mstore(0x40, _localContext)
// Localize the key with the above `localize` operation.
localizedKey_ := or(and(keccak256(0, 0x60), not(shl(248, 0xFF))), shl(248, 1))
// Restore the free memory pointer.
mstore(0x40, ptr)
}
}

// temporary method for localizeation
function cheatLocalKey(uint256 partOffset, bytes32 key, bytes32 part, uint256 size, bytes32 localContext) external {
// sanity check key is local key using prefix
require(uint8(key[0]) == 1, "must be used for local key");

bytes32 localizedKey = localize(key, localContext);
preimagePartOk[localizedKey][partOffset] = true;
preimageParts[localizedKey][partOffset] = part;
preimageLengths[localizedKey] = size;
}

// loadKeccak256PreimagePart prepares the pre-image to be read by keccak256 key,
// starting at the given offset, up to 32 bytes (clipped at preimage length, if out of data).
function loadKeccak256PreimagePart(uint256 _partOffset, bytes calldata _preimage) external {
Expand Down

0 comments on commit 3c72c74

Please sign in to comment.