diff --git a/rvgo/evm_test.go b/rvgo/evm_test.go index 1580654d..b9ca12cb 100644 --- a/rvgo/evm_test.go +++ b/rvgo/evm_test.go @@ -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) diff --git a/rvgo/fast/evm.go b/rvgo/fast/evm.go index f90ed3d5..11503588 100644 --- a/rvgo/fast/evm.go +++ b/rvgo/fast/evm.go @@ -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] ) diff --git a/rvgo/fast/witness.go b/rvgo/fast/witness.go index 1fa2a885..6f47c28c 100644 --- a/rvgo/fast/witness.go +++ b/rvgo/fast/witness.go @@ -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 @@ -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 @@ -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)))...) @@ -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") } @@ -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: diff --git a/rvgo/vm_go_test.go b/rvgo/vm_go_test.go index a484b448..05400e24 100644 --- a/rvgo/vm_go_test.go +++ b/rvgo/vm_go_test.go @@ -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") } diff --git a/rvgo/vm_test.go b/rvgo/vm_test.go index 081b84eb..76f96d9e 100644 --- a/rvgo/vm_test.go +++ b/rvgo/vm_test.go @@ -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) diff --git a/rvsol/src/PreimageOracle.sol b/rvsol/src/PreimageOracle.sol index 31eec369..27a4298b 100644 --- a/rvsol/src/PreimageOracle.sol +++ b/rvsol/src/PreimageOracle.sol @@ -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 {