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

op-preimage: Update verification for sha256 and blobs (#9432)

parent 9f5269e3
package preimage
import (
"crypto/sha256"
"errors"
"fmt"
"slices"
......@@ -28,6 +29,15 @@ func WithVerification(source PreimageGetter) PreimageGetter {
return nil, fmt.Errorf("%w for key %v, hash: %v data: %x", ErrIncorrectData, key, hash, data)
}
return data, nil
case Sha256KeyType:
hash := sha256.Sum256(data)
if !slices.Equal(hash[1:], key[1:]) {
return nil, fmt.Errorf("%w for key %v, hash: %v data: %x", ErrIncorrectData, key, hash, data)
}
return data, nil
case BlobKeyType:
// Can't verify an individual field element without having a kzg proof
return data, nil
default:
return nil, fmt.Errorf("%w: %v", ErrUnsupportedKeyType, key[0])
}
......
package preimage
import (
"crypto/sha256"
"errors"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/require"
......@@ -10,16 +13,20 @@ import (
func TestWithVerification(t *testing.T) {
validData := []byte{1, 2, 3, 4, 5, 6}
keccak256Key := Keccak256Key(Keccak256(validData))
sha256Key := Sha256Key(sha256.Sum256(validData))
anError := errors.New("boom")
tests := []struct {
validKeys := []Key{keccak256Key, sha256Key}
type testData struct {
name string
key Key
data []byte
err error
expectedErr error
expectedData []byte
}{
}
tests := []testData{
{
name: "LocalKey NoVerification",
key: LocalIndexKey(1),
......@@ -27,36 +34,47 @@ func TestWithVerification(t *testing.T) {
expectedData: []byte{4, 3, 5, 7, 3},
},
{
name: "Keccak256 Valid",
key: keccak256Key,
name: "BlobKey NoVerification",
key: BlobKey([32]byte{1, 2, 3, 4}),
data: []byte{4, 3, 5, 7, 3},
expectedData: []byte{4, 3, 5, 7, 3},
},
{
name: "UnknownKey",
key: invalidKey([32]byte{0xaa}),
data: []byte{},
expectedErr: ErrUnsupportedKeyType,
},
}
for _, key := range validKeys {
name := reflect.TypeOf(key).Name()
tests = append(tests,
testData{
name: fmt.Sprintf("%v-Valid", name),
key: key,
data: validData,
expectedData: validData,
},
{
name: "Keccak256 Error",
key: keccak256Key,
testData{
name: fmt.Sprintf("%v-Error", name),
key: key,
data: validData,
err: anError,
expectedErr: anError,
},
{
name: "Keccak256 InvalidData",
key: keccak256Key,
testData{
name: fmt.Sprintf("%v-InvalidData", name),
key: key,
data: []byte{6, 7, 8},
expectedErr: ErrIncorrectData,
},
{
name: "EmptyData",
key: keccak256Key,
testData{
name: fmt.Sprintf("%v-EmptyData", name),
key: key,
data: []byte{},
expectedErr: ErrIncorrectData,
},
{
name: "UnknownKey",
key: invalidKey([32]byte{0xaa}),
data: []byte{},
expectedErr: ErrUnsupportedKeyType,
},
})
}
for _, test := range tests {
......
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