Commit aeed0da3 authored by Adrian Sutton's avatar Adrian Sutton Committed by GitHub

op-challenger: Switch Leaf.Index to uint64 instead of big.Int (#9229)

parent 78e3cbb8
...@@ -50,7 +50,7 @@ type PreimageOracleContract struct { ...@@ -50,7 +50,7 @@ type PreimageOracleContract struct {
func toPreimageOracleLeaf(l keccakTypes.Leaf) bindings.PreimageOracleLeaf { func toPreimageOracleLeaf(l keccakTypes.Leaf) bindings.PreimageOracleLeaf {
return bindings.PreimageOracleLeaf{ return bindings.PreimageOracleLeaf{
Input: l.Input[:], Input: l.Input[:],
Index: l.Index, Index: new(big.Int).SetUint64(l.Index),
StateCommitment: l.StateCommitment, StateCommitment: l.StateCommitment,
} }
} }
......
...@@ -138,13 +138,13 @@ func TestPreimageOracleContract_Squeeze(t *testing.T) { ...@@ -138,13 +138,13 @@ func TestPreimageOracleContract_Squeeze(t *testing.T) {
stateMatrix := matrix.NewStateMatrix() stateMatrix := matrix.NewStateMatrix()
preState := keccakTypes.Leaf{ preState := keccakTypes.Leaf{
Input: [keccakTypes.BlockSize]byte{0x12}, Input: [keccakTypes.BlockSize]byte{0x12},
Index: big.NewInt(123), Index: 123,
StateCommitment: common.Hash{0x34}, StateCommitment: common.Hash{0x34},
} }
preStateProof := merkle.Proof{{0x34}} preStateProof := merkle.Proof{{0x34}}
postState := keccakTypes.Leaf{ postState := keccakTypes.Leaf{
Input: [keccakTypes.BlockSize]byte{0x34}, Input: [keccakTypes.BlockSize]byte{0x34},
Index: big.NewInt(456), Index: 456,
StateCommitment: common.Hash{0x56}, StateCommitment: common.Hash{0x56},
} }
postStateProof := merkle.Proof{{0x56}} postStateProof := merkle.Proof{{0x56}}
...@@ -507,7 +507,7 @@ func TestChallenge_First(t *testing.T) { ...@@ -507,7 +507,7 @@ func TestChallenge_First(t *testing.T) {
Prestate: keccakTypes.Leaf{}, Prestate: keccakTypes.Leaf{},
Poststate: keccakTypes.Leaf{ Poststate: keccakTypes.Leaf{
Input: [136]byte{5, 4, 3, 2, 1}, Input: [136]byte{5, 4, 3, 2, 1},
Index: big.NewInt(0), Index: 0,
StateCommitment: common.Hash{0xbb}, StateCommitment: common.Hash{0xbb},
}, },
PoststateProof: merkle.Proof{common.Hash{0x01}, common.Hash{0x02}}, PoststateProof: merkle.Proof{common.Hash{0x01}, common.Hash{0x02}},
...@@ -517,7 +517,7 @@ func TestChallenge_First(t *testing.T) { ...@@ -517,7 +517,7 @@ func TestChallenge_First(t *testing.T) {
ident.Claimant, ident.UUID, ident.Claimant, ident.UUID,
bindings.PreimageOracleLeaf{ bindings.PreimageOracleLeaf{
Input: challenge.Poststate.Input[:], Input: challenge.Poststate.Input[:],
Index: challenge.Poststate.Index, Index: new(big.Int).SetUint64(challenge.Poststate.Index),
StateCommitment: challenge.Poststate.StateCommitment, StateCommitment: challenge.Poststate.StateCommitment,
}, },
challenge.PoststateProof, challenge.PoststateProof,
...@@ -539,13 +539,13 @@ func TestChallenge_NotFirst(t *testing.T) { ...@@ -539,13 +539,13 @@ func TestChallenge_NotFirst(t *testing.T) {
StateMatrix: bytes.Repeat([]byte{1, 2, 3, 4, 5, 6, 7, 8}, 25), StateMatrix: bytes.Repeat([]byte{1, 2, 3, 4, 5, 6, 7, 8}, 25),
Prestate: keccakTypes.Leaf{ Prestate: keccakTypes.Leaf{
Input: [136]byte{9, 8, 7, 6, 5}, Input: [136]byte{9, 8, 7, 6, 5},
Index: big.NewInt(3), Index: 3,
StateCommitment: common.Hash{0xcc}, StateCommitment: common.Hash{0xcc},
}, },
PrestateProof: merkle.Proof{common.Hash{0x01}, common.Hash{0x02}}, PrestateProof: merkle.Proof{common.Hash{0x01}, common.Hash{0x02}},
Poststate: keccakTypes.Leaf{ Poststate: keccakTypes.Leaf{
Input: [136]byte{5, 4, 3, 2, 1}, Input: [136]byte{5, 4, 3, 2, 1},
Index: big.NewInt(4), Index: 4,
StateCommitment: common.Hash{0xbb}, StateCommitment: common.Hash{0xbb},
}, },
PoststateProof: merkle.Proof{common.Hash{0x03}, common.Hash{0x04}}, PoststateProof: merkle.Proof{common.Hash{0x03}, common.Hash{0x04}},
...@@ -556,13 +556,13 @@ func TestChallenge_NotFirst(t *testing.T) { ...@@ -556,13 +556,13 @@ func TestChallenge_NotFirst(t *testing.T) {
abiEncodePackedState(challenge.StateMatrix), abiEncodePackedState(challenge.StateMatrix),
bindings.PreimageOracleLeaf{ bindings.PreimageOracleLeaf{
Input: challenge.Prestate.Input[:], Input: challenge.Prestate.Input[:],
Index: challenge.Prestate.Index, Index: new(big.Int).SetUint64(challenge.Prestate.Index),
StateCommitment: challenge.Prestate.StateCommitment, StateCommitment: challenge.Prestate.StateCommitment,
}, },
challenge.PrestateProof, challenge.PrestateProof,
bindings.PreimageOracleLeaf{ bindings.PreimageOracleLeaf{
Input: challenge.Poststate.Input[:], Input: challenge.Poststate.Input[:],
Index: challenge.Poststate.Index, Index: new(big.Int).SetUint64(challenge.Poststate.Index),
StateCommitment: challenge.Poststate.StateCommitment, StateCommitment: challenge.Poststate.StateCommitment,
}, },
challenge.PoststateProof, challenge.PoststateProof,
......
...@@ -206,12 +206,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) { ...@@ -206,12 +206,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) {
addCalls: 1, addCalls: 1,
prestateLeaf: keccakTypes.Leaf{ prestateLeaf: keccakTypes.Leaf{
Input: *fullLeaf, Input: *fullLeaf,
Index: big.NewInt(0), Index: 0,
StateCommitment: common.HexToHash("9788a3b3bc36c482525b5890767be37130c997917bceca6e91a6c93359a4d1c6"), StateCommitment: common.HexToHash("9788a3b3bc36c482525b5890767be37130c997917bceca6e91a6c93359a4d1c6"),
}, },
poststateLeaf: keccakTypes.Leaf{ poststateLeaf: keccakTypes.Leaf{
Input: [keccakTypes.BlockSize]byte{}, Input: [keccakTypes.BlockSize]byte{},
Index: big.NewInt(1), Index: 1,
StateCommitment: common.HexToHash("78358b902b7774b314bcffdf0948746f18d6044086e76e3924d585dca3486c7d"), StateCommitment: common.HexToHash("78358b902b7774b314bcffdf0948746f18d6044086e76e3924d585dca3486c7d"),
}, },
}, },
...@@ -221,12 +221,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) { ...@@ -221,12 +221,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) {
addCalls: 1, addCalls: 1,
prestateLeaf: keccakTypes.Leaf{ prestateLeaf: keccakTypes.Leaf{
Input: *fullLeaf, Input: *fullLeaf,
Index: big.NewInt(2), Index: 2,
StateCommitment: common.HexToHash("e3deed8ab6f8bbcf3d4fe825d74f703b3f2fc2f5b0afaa2574926fcfd0d4c895"), StateCommitment: common.HexToHash("e3deed8ab6f8bbcf3d4fe825d74f703b3f2fc2f5b0afaa2574926fcfd0d4c895"),
}, },
poststateLeaf: keccakTypes.Leaf{ poststateLeaf: keccakTypes.Leaf{
Input: [keccakTypes.BlockSize]byte{}, Input: [keccakTypes.BlockSize]byte{},
Index: big.NewInt(3), Index: 3,
StateCommitment: common.HexToHash("79115eeab1ff2eccf5baf3ea2dda13bc79c548ce906bdd16433a23089c679df2"), StateCommitment: common.HexToHash("79115eeab1ff2eccf5baf3ea2dda13bc79c548ce906bdd16433a23089c679df2"),
}, },
}, },
...@@ -236,12 +236,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) { ...@@ -236,12 +236,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) {
addCalls: 1, addCalls: 1,
prestateLeaf: keccakTypes.Leaf{ prestateLeaf: keccakTypes.Leaf{
Input: *fullLeaf, Input: *fullLeaf,
Index: big.NewInt(1), Index: 1,
StateCommitment: common.HexToHash("b5ea400e375b2c1ce348f3cc4ad5b6ad28e1b36759ddd2aba155f0b1d476b015"), StateCommitment: common.HexToHash("b5ea400e375b2c1ce348f3cc4ad5b6ad28e1b36759ddd2aba155f0b1d476b015"),
}, },
poststateLeaf: keccakTypes.Leaf{ poststateLeaf: keccakTypes.Leaf{
Input: [keccakTypes.BlockSize]byte{byte(9)}, Input: [keccakTypes.BlockSize]byte{byte(9)},
Index: big.NewInt(2), Index: 2,
StateCommitment: common.HexToHash("fa87e115dc4786e699bf80cc75d13ac1e2db0708c1418fc8cbc9800d17b5811a"), StateCommitment: common.HexToHash("fa87e115dc4786e699bf80cc75d13ac1e2db0708c1418fc8cbc9800d17b5811a"),
}, },
}, },
...@@ -251,12 +251,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) { ...@@ -251,12 +251,12 @@ func TestLargePreimageUploader_UploadPreimage_Succeeds(t *testing.T) {
addCalls: 2, addCalls: 2,
prestateLeaf: keccakTypes.Leaf{ prestateLeaf: keccakTypes.Leaf{
Input: *fullLeaf, Input: *fullLeaf,
Index: big.NewInt(301), Index: 301,
StateCommitment: common.HexToHash("4e9c55542478939feca4ff55ee98fbc632bb65a784a55b94536644bc87298ca4"), StateCommitment: common.HexToHash("4e9c55542478939feca4ff55ee98fbc632bb65a784a55b94536644bc87298ca4"),
}, },
poststateLeaf: keccakTypes.Leaf{ poststateLeaf: keccakTypes.Leaf{
Input: [keccakTypes.BlockSize]byte{}, Input: [keccakTypes.BlockSize]byte{},
Index: big.NewInt(302), Index: 302,
StateCommitment: common.HexToHash("775020bfcaa93700263d040a4eeec3c8c3cf09e178457d04044594beaaf5e20b"), StateCommitment: common.HexToHash("775020bfcaa93700263d040a4eeec3c8c3cf09e178457d04044594beaaf5e20b"),
}, },
}, },
......
...@@ -74,9 +74,9 @@ func Challenge(data io.Reader, commitments []common.Hash) (types.Challenge, erro ...@@ -74,9 +74,9 @@ func Challenge(data io.Reader, commitments []common.Hash) (types.Challenge, erro
if firstInvalidLeaf != (types.Leaf{}) { if firstInvalidLeaf != (types.Leaf{}) {
var prestateProof merkle.Proof var prestateProof merkle.Proof
if lastValidLeaf != (types.Leaf{}) { if lastValidLeaf != (types.Leaf{}) {
prestateProof = s.merkleTree.ProofAtIndex(lastValidLeaf.IndexUint64()) prestateProof = s.merkleTree.ProofAtIndex(lastValidLeaf.Index)
} }
poststateProof := s.merkleTree.ProofAtIndex(firstInvalidLeaf.IndexUint64()) poststateProof := s.merkleTree.ProofAtIndex(firstInvalidLeaf.Index)
return types.Challenge{ return types.Challenge{
StateMatrix: lastValidState, StateMatrix: lastValidState,
Prestate: lastValidLeaf, Prestate: lastValidLeaf,
...@@ -113,7 +113,7 @@ func (d *StateMatrix) PackState() []byte { ...@@ -113,7 +113,7 @@ func (d *StateMatrix) PackState() []byte {
} }
// newLeafWithPadding creates a new [Leaf] from inputs, padding the input to the [BlockSize]. // newLeafWithPadding creates a new [Leaf] from inputs, padding the input to the [BlockSize].
func newLeafWithPadding(input []byte, index *big.Int, commitment common.Hash) types.Leaf { func newLeafWithPadding(input []byte, index uint64, commitment common.Hash) types.Leaf {
// TODO(client-pod#480): Add actual keccak padding to ensure the merkle proofs are correct (for readData) // TODO(client-pod#480): Add actual keccak padding to ensure the merkle proofs are correct (for readData)
var paddedInput [types.BlockSize]byte var paddedInput [types.BlockSize]byte
copy(paddedInput[:], input) copy(paddedInput[:], input)
...@@ -156,13 +156,13 @@ func (d *StateMatrix) AbsorbUpTo(in io.Reader, maxLen int) (types.InputData, err ...@@ -156,13 +156,13 @@ func (d *StateMatrix) AbsorbUpTo(in io.Reader, maxLen int) (types.InputData, err
// PrestateWithProof returns the prestate leaf with its merkle proof. // PrestateWithProof returns the prestate leaf with its merkle proof.
func (d *StateMatrix) PrestateWithProof() (types.Leaf, merkle.Proof) { func (d *StateMatrix) PrestateWithProof() (types.Leaf, merkle.Proof) {
proof := d.merkleTree.ProofAtIndex(d.prestateLeaf.IndexUint64()) proof := d.merkleTree.ProofAtIndex(d.prestateLeaf.Index)
return d.prestateLeaf, proof return d.prestateLeaf, proof
} }
// PoststateWithProof returns the poststate leaf with its merkle proof. // PoststateWithProof returns the poststate leaf with its merkle proof.
func (d *StateMatrix) PoststateWithProof() (types.Leaf, merkle.Proof) { func (d *StateMatrix) PoststateWithProof() (types.Leaf, merkle.Proof) {
proof := d.merkleTree.ProofAtIndex(d.poststateLeaf.IndexUint64()) proof := d.merkleTree.ProofAtIndex(d.poststateLeaf.Index)
return d.poststateLeaf, proof return d.poststateLeaf, proof
} }
...@@ -193,10 +193,10 @@ func (d *StateMatrix) absorbNextLeafInput(in io.Reader, stateCommitment func() c ...@@ -193,10 +193,10 @@ func (d *StateMatrix) absorbNextLeafInput(in io.Reader, stateCommitment func() c
commitment := stateCommitment() commitment := stateCommitment()
if d.poststateLeaf == (types.Leaf{}) { if d.poststateLeaf == (types.Leaf{}) {
d.prestateLeaf = types.Leaf{} d.prestateLeaf = types.Leaf{}
d.poststateLeaf = newLeafWithPadding(input, big.NewInt(0), commitment) d.poststateLeaf = newLeafWithPadding(input, 0, commitment)
} else { } else {
d.prestateLeaf = d.poststateLeaf d.prestateLeaf = d.poststateLeaf
d.poststateLeaf = newLeafWithPadding(input, new(big.Int).Add(d.prestateLeaf.Index, big.NewInt(1)), commitment) d.poststateLeaf = newLeafWithPadding(input, d.prestateLeaf.Index+1, commitment)
} }
d.merkleTree.AddLeaf(d.poststateLeaf.Hash()) d.merkleTree.AddLeaf(d.poststateLeaf.Hash())
if final { if final {
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/big"
"math/rand" "math/rand"
"testing" "testing"
...@@ -291,7 +290,7 @@ func TestVerifyPreimage(t *testing.T) { ...@@ -291,7 +290,7 @@ func TestVerifyPreimage(t *testing.T) {
for i, commitment := range commitments { for i, commitment := range commitments {
leaf := types.Leaf{ leaf := types.Leaf{
Input: leafData(i), Input: leafData(i),
Index: big.NewInt(int64(i)), Index: uint64(i),
StateCommitment: commitment, StateCommitment: commitment,
} }
m.AddLeaf(leaf.Hash()) m.AddLeaf(leaf.Hash())
...@@ -312,14 +311,14 @@ func TestVerifyPreimage(t *testing.T) { ...@@ -312,14 +311,14 @@ func TestVerifyPreimage(t *testing.T) {
StateMatrix: s.PackState(), StateMatrix: s.PackState(),
Prestate: types.Leaf{ Prestate: types.Leaf{
Input: prestateLeaf, Input: prestateLeaf,
Index: big.NewInt(int64(invalidIdx - 1)), Index: uint64(invalidIdx - 1),
StateCommitment: commitments[invalidIdx-1], StateCommitment: commitments[invalidIdx-1],
}, },
PrestateProof: fullMerkle.ProofAtIndex(uint64(invalidIdx - 1)), PrestateProof: fullMerkle.ProofAtIndex(uint64(invalidIdx - 1)),
Poststate: types.Leaf{ Poststate: types.Leaf{
Input: poststateLeaf, Input: poststateLeaf,
Index: big.NewInt(int64(invalidIdx)), Index: uint64(invalidIdx),
StateCommitment: commitments[invalidIdx], StateCommitment: commitments[invalidIdx],
}, },
PoststateProof: fullMerkle.ProofAtIndex(uint64(invalidIdx)), PoststateProof: fullMerkle.ProofAtIndex(uint64(invalidIdx)),
...@@ -353,7 +352,7 @@ func TestVerifyPreimage(t *testing.T) { ...@@ -353,7 +352,7 @@ func TestVerifyPreimage(t *testing.T) {
Prestate: types.Leaf{}, Prestate: types.Leaf{},
Poststate: types.Leaf{ Poststate: types.Leaf{
Input: poststateLeaf, Input: poststateLeaf,
Index: big.NewInt(int64(0)), Index: 0,
StateCommitment: common.Hash{0xaa}, StateCommitment: common.Hash{0xaa},
}, },
PoststateProof: merkleTree(incorrectFirstCommitment).ProofAtIndex(0), PoststateProof: merkleTree(incorrectFirstCommitment).ProofAtIndex(0),
......
...@@ -18,30 +18,17 @@ type Leaf struct { ...@@ -18,30 +18,17 @@ type Leaf struct {
// Input is the data absorbed for the block, exactly 136 bytes // Input is the data absorbed for the block, exactly 136 bytes
Input [BlockSize]byte Input [BlockSize]byte
// Index of the block in the absorption process // Index of the block in the absorption process
Index *big.Int Index uint64
// StateCommitment is the hash of the internal state after absorbing the input. // StateCommitment is the hash of the internal state after absorbing the input.
StateCommitment common.Hash StateCommitment common.Hash
} }
func (l Leaf) IndexUint64() uint64 {
if l.Index == nil {
return 0
}
return l.Index.Uint64()
}
// Hash returns the hash of the leaf data. That is the // Hash returns the hash of the leaf data. That is the
// bytewise concatenation of the input, index, and state commitment. // bytewise concatenation of the input, index, and state commitment.
func (l Leaf) Hash() common.Hash { func (l Leaf) Hash() common.Hash {
concatted := make([]byte, 0, 136+32+32) concatted := make([]byte, 0, 136+32+32)
concatted = append(concatted, l.Input[:]...) concatted = append(concatted, l.Input[:]...)
var indexBytes []byte concatted = append(concatted, new(big.Int).SetUint64(l.Index).Bytes()...)
if l.Index != nil {
indexBytes = l.Index.Bytes()
} else {
indexBytes = big.NewInt(0).Bytes()
}
concatted = append(concatted, indexBytes...)
concatted = append(concatted, l.StateCommitment.Bytes()...) concatted = append(concatted, l.StateCommitment.Bytes()...)
return crypto.Keccak256Hash(concatted) return crypto.Keccak256Hash(concatted)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment