Skip to content

Commit

Permalink
Add gas input to precompile pre-images (#186) (#252)
Browse files Browse the repository at this point in the history
* contracts: Add gas input to precompile pre-images (#186)

Also update the cannon evm tests to use the new precompile preimage scheme.

---------

Co-authored-by: Adrian Sutton <[email protected]>

* op-challenger: Support uploading data in new format. (#188)

* op-program: Add required gas to precompile oracle key (#176)

* op-challenger: Support multiple versions of the preimage oracle contract

---------

Co-authored-by: Adrian Sutton <[email protected]>

---------

Co-authored-by: inphi <[email protected]>

---------

Co-authored-by: Adrian Sutton <[email protected]>
  • Loading branch information
Inphi and ajsutton committed Aug 15, 2024
1 parent f54765c commit db61d2b
Show file tree
Hide file tree
Showing 43 changed files with 2,146 additions and 598 deletions.
Binary file modified cannon/mipsevm/tests/open_mips_tests/test/bin/oracle_kzg.bin
Binary file not shown.
39 changes: 20 additions & 19 deletions cannon/mipsevm/tests/open_mips_tests/test/oracle_kzg.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,37 @@
.ent test

# load hash at 0x30001000
# point evaluation precompile input - 01e798154708fe7789429634053cbf9f99b619f9f084048927333fce637f549b564c0a11a0f704f4fc3e8acfe0f8245f0ad1347b378fbf96e206da11a5d3630624d25032e67a7e6a4910df5834b8fe70e6bcfeeac0352434196bdf4b2485d5a18f59a8d2a1a625a17f3fea0fe5eb8c896db3764f3185481bc22f91b4aaffcca25f26936857bc3a7c2539ea8ec3a952b7873033e038326e87ed3e1276fd140253fa08e9fc25fb2d9a98527fc22a2c9612fbeafdad446cbc7bcdbdcd780af2c16a
# 0x0a44472c cb798bc5 954fc466 e6ee2c31 e1ca8a87 d000966c 629d679a 4a29921f = keccak(address(0xa) ++ precompile_input)
# 0x0644472c cb798bc5 954fc466 e6ee2c31 e1ca8a87 d000966c 629d679a 4a29921f = keccak(address(0xa) ++ precompile_input).key (precompile)
# requiredGas is 50_000
# point evaluation precompile input (requiredGas ++ precompileInput) - 000000000000c35001e798154708fe7789429634053cbf9f99b619f9f084048927333fce637f549b564c0a11a0f704f4fc3e8acfe0f8245f0ad1347b378fbf96e206da11a5d3630624d25032e67a7e6a4910df5834b8fe70e6bcfeeac0352434196bdf4b2485d5a18f59a8d2a1a625a17f3fea0fe5eb8c896db3764f3185481bc22f91b4aaffcca25f26936857bc3a7c2539ea8ec3a952b7873033e038326e87ed3e1276fd140253fa08e9fc25fb2d9a98527fc22a2c9612fbeafdad446cbc7bcdbdcd780af2c16a
# 0x3efd5c3c 1c555298 0c63aee5 4570c276 cbff7532 796b4d75 3132d51a 6bedf0c6 = keccak(address(0xa) ++ required_gas ++ precompile_input)
# 0x06fd5c3c 1c555298 0c63aee5 4570c276 cbff7532 796b4d75 3132d51a 6bedf0c6 = keccak(address(0xa) ++ required_gas ++ precompile_input).key (precompile)
test:
lui $s0, 0x3000
ori $s0, 0x1000

lui $t0, 0x0644
ori $t0, 0x472c
lui $t0, 0x06fd
ori $t0, 0x5c3c
sw $t0, 0($s0)
lui $t0, 0xcb79
ori $t0, 0x8bc5
lui $t0, 0x1c55
ori $t0, 0x5298
sw $t0, 4($s0)
lui $t0, 0x954f
ori $t0, 0xc466
lui $t0, 0x0c63
ori $t0, 0xaee5
sw $t0, 8($s0)
lui $t0, 0xe6ee
ori $t0, 0x2c31
lui $t0, 0x4570
ori $t0, 0xc276
sw $t0, 0xc($s0)
lui $t0, 0xe1ca
ori $t0, 0x8a87
lui $t0, 0xcbff
ori $t0, 0x7532
sw $t0, 0x10($s0)
lui $t0, 0xd000
ori $t0, 0x966c
lui $t0, 0x796b
ori $t0, 0x4d75
sw $t0, 0x14($s0)
lui $t0, 0x629d
ori $t0, 0x679a
lui $t0, 0x3132
ori $t0, 0xd51a
sw $t0, 0x18($s0)
lui $t0, 0x4a29
ori $t0, 0x921f
lui $t0, 0x6bed
ori $t0, 0xf0c6
sw $t0, 0x1c($s0)

# preimage request - write(fdPreimageWrite, preimageData, 32)
Expand Down
5 changes: 4 additions & 1 deletion cannon/mipsevm/testutil/mips.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutil

import (
"encoding/binary"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -121,11 +122,13 @@ func EncodePreimageOracleInput(t *testing.T, wit *mipsevm.StepWitness, localCont
}
preimage := localOracle.GetPreimage(preimage.Keccak256Key(wit.PreimageKey).PreimageKey())
precompile := common.BytesToAddress(preimage[:20])
callInput := preimage[20:]
requiredGas := binary.BigEndian.Uint64(preimage[20:28])
callInput := preimage[28:]
input, err := oracle.ABI.Pack(
"loadPrecompilePreimagePart",
new(big.Int).SetUint64(uint64(wit.PreimageOffset)),
precompile,
requiredGas,
callInput,
)
require.NoError(t, err)
Expand Down
9 changes: 6 additions & 3 deletions cannon/mipsevm/testutil/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ func StaticOracle(t *testing.T, preimageData []byte) *TestOracle {
}
}

func StaticPrecompileOracle(t *testing.T, precompile common.Address, input []byte, result []byte) *TestOracle {
func StaticPrecompileOracle(t *testing.T, precompile common.Address, requiredGas uint64, input []byte, result []byte) *TestOracle {
return &TestOracle{
hint: func(v []byte) {},
getPreimage: func(k [32]byte) []byte {
keyData := append(precompile.Bytes(), input...)
requiredGasB := binary.BigEndian.AppendUint64(nil, requiredGas)
keyData := append(precompile.Bytes(), requiredGasB...)
keyData = append(keyData, input...)
switch k[0] {
case byte(preimage.Keccak256KeyType):
if k != preimage.Keccak256Key(crypto.Keccak256Hash(keyData)).PreimageKey() {
Expand Down Expand Up @@ -135,7 +137,8 @@ func SelectOracleFixture(t *testing.T, programName string) mipsevm.PreimageOracl
precompile := common.BytesToAddress([]byte{0xa})
input := common.FromHex("01e798154708fe7789429634053cbf9f99b619f9f084048927333fce637f549b564c0a11a0f704f4fc3e8acfe0f8245f0ad1347b378fbf96e206da11a5d3630624d25032e67a7e6a4910df5834b8fe70e6bcfeeac0352434196bdf4b2485d5a18f59a8d2a1a625a17f3fea0fe5eb8c896db3764f3185481bc22f91b4aaffcca25f26936857bc3a7c2539ea8ec3a952b7873033e038326e87ed3e1276fd140253fa08e9fc25fb2d9a98527fc22a2c9612fbeafdad446cbc7bcdbdcd780af2c16a")
blobPrecompileReturnValue := common.FromHex("000000000000000000000000000000000000000000000000000000000000100073eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
return StaticPrecompileOracle(t, precompile, input, append([]byte{0x1}, blobPrecompileReturnValue...))
requiredGas := uint64(50_000)
return StaticPrecompileOracle(t, precompile, requiredGas, input, append([]byte{0x1}, blobPrecompileReturnValue...))
} else if strings.HasPrefix(programName, "oracle") {
return StaticOracle(t, []byte("hello world"))
} else {
Expand Down
Loading

0 comments on commit db61d2b

Please sign in to comment.