@@ -5,9 +5,12 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
8
- "github.com/ethereum-optimism/optimism/op-preimage"
8
+ preimage "github.com/ethereum-optimism/optimism/op-preimage"
9
+ "github.com/ethereum/go-ethereum/common"
9
10
)
10
11
12
+ type LocalContext common.Hash
13
+
11
14
type StepWitness struct {
12
15
// encoded state witness
13
16
State []byte
@@ -25,29 +28,35 @@ func uint64ToBytes32(v uint64) []byte {
25
28
return out [:]
26
29
}
27
30
28
- func (wit * StepWitness ) EncodeStepInput () []byte {
31
+ func (wit * StepWitness ) EncodeStepInput (localContext LocalContext ) []byte {
29
32
abiStatePadding := (32 - (uint64 (len (wit .State )) % 32 )) % 32
33
+ abiProofPadding := (32 - (uint64 (len (wit .MemProof )) % 32 )) % 32
30
34
31
35
var input []byte
32
36
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 )))... )
39
46
input = append (input , wit .State [:]... )
40
47
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 )))... )
42
50
input = append (input , wit .MemProof [:]... )
51
+ input = append (input , make ([]byte , abiProofPadding )... )
43
52
return input
44
53
}
45
54
46
55
func (wit * StepWitness ) HasPreimage () bool {
47
56
return wit .PreimageKey != ([32 ]byte {})
48
57
}
49
58
50
- func (wit * StepWitness ) EncodePreimageOracleInput () ([]byte , error ) {
59
+ func (wit * StepWitness ) EncodePreimageOracleInput (localContext LocalContext ) ([]byte , error ) {
51
60
if wit .PreimageKey == ([32 ]byte {}) {
52
61
return nil , errors .New ("cannot encode pre-image oracle input, witness has no pre-image to proof" )
53
62
}
@@ -59,13 +68,14 @@ func (wit *StepWitness) EncodePreimageOracleInput() ([]byte, error) {
59
68
// In production usage there should be an on-chain contract that exposes this,
60
69
// rather than going through the global keccak256 oracle.
61
70
var input []byte
62
- input = append (input , CheatBytes4 ... )
71
+ input = append (input , CheatLocalKeyBytes4 ... )
63
72
input = append (input , uint64ToBytes32 (wit .PreimageOffset )... )
64
73
input = append (input , wit .PreimageKey [:]... )
65
74
var tmp [32 ]byte
66
75
copy (tmp [:], wit .PreimageValue [wit .PreimageOffset :])
67
76
input = append (input , tmp [:]... )
68
77
input = append (input , uint64ToBytes32 (uint64 (len (wit .PreimageValue ))- 8 )... )
78
+ input = append (input , common .Hash (localContext ).Bytes ()... )
69
79
// Note: we can pad calldata to 32 byte multiple, but don't strictly have to
70
80
return input , nil
71
81
case preimage .Keccak256KeyType :
0 commit comments