-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shutter: validate decryption keys based on eon info and signatures (#…
- Loading branch information
Showing
23 changed files
with
1,516 additions
and
488 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright 2025 The Erigon Authors | ||
// This file is part of Erigon. | ||
// | ||
// Erigon is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Erigon is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with Erigon. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package shutter | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"errors" | ||
"fmt" | ||
"slices" | ||
|
||
libcommon "github.com/erigontech/erigon-lib/common" | ||
"github.com/erigontech/erigon-lib/common/hexutil" | ||
"github.com/erigontech/erigon-lib/crypto" | ||
"github.com/erigontech/erigon-lib/types/clonable" | ||
"github.com/erigontech/erigon/cl/cltypes/solid" | ||
merkletree "github.com/erigontech/erigon/cl/merkle_tree" | ||
) | ||
|
||
var ( | ||
ErrTooManyIdentityPreimages = errors.New("too many identity preimages") | ||
ErrIncorrectIdentityPreimageSize = errors.New("incorrect identity preimage size") | ||
) | ||
|
||
const ( | ||
identityPreimageSize = 52 | ||
identityPreimagesLimit = 1024 | ||
) | ||
|
||
type IdentityPreimage [identityPreimageSize]byte | ||
|
||
func (ip *IdentityPreimage) EncodingSizeSSZ() int { | ||
return identityPreimageSize | ||
} | ||
|
||
func (ip *IdentityPreimage) EncodeSSZ(dst []byte) ([]byte, error) { | ||
return append(dst, ip[:]...), nil | ||
} | ||
|
||
func (ip *IdentityPreimage) DecodeSSZ(buf []byte, _ int) error { | ||
if len(buf) != identityPreimageSize { | ||
return fmt.Errorf("%w: len=%d", ErrIncorrectIdentityPreimageSize, len(ip)) | ||
} | ||
|
||
var newIp IdentityPreimage | ||
copy(newIp[:], buf) | ||
*ip = newIp | ||
return nil | ||
} | ||
|
||
func (ip *IdentityPreimage) Clone() clonable.Clonable { | ||
clone := IdentityPreimage(slices.Clone(ip[:])) | ||
return &clone | ||
} | ||
|
||
func (ip *IdentityPreimage) HashSSZ() ([32]byte, error) { | ||
return merkletree.BytesRoot(ip[:]) | ||
} | ||
|
||
func (ip *IdentityPreimage) String() string { | ||
return hexutil.Encode(ip[:]) | ||
} | ||
|
||
func IdentityPreimageFromSSZ(b []byte) (*IdentityPreimage, error) { | ||
ip := new(IdentityPreimage) | ||
err := ip.DecodeSSZ(b, 0) | ||
return ip, err | ||
} | ||
|
||
type IdentityPreimages []*IdentityPreimage | ||
|
||
func (ips IdentityPreimages) ToListSSZ() *solid.ListSSZ[*IdentityPreimage] { | ||
return solid.NewStaticListSSZFromList(ips, identityPreimagesLimit, identityPreimageSize) | ||
} | ||
|
||
type DecryptionKeysSignatureData struct { | ||
InstanceId uint64 | ||
Eon EonIndex | ||
Slot uint64 | ||
TxnPointer uint64 | ||
IdentityPreimages *solid.ListSSZ[*IdentityPreimage] | ||
} | ||
|
||
func (d DecryptionKeysSignatureData) HashSSZ() ([32]byte, error) { | ||
if err := d.Validate(); err != nil { | ||
return [32]byte{}, err | ||
} | ||
|
||
r, err := merkletree.HashTreeRoot(d.InstanceId, uint64(d.Eon), d.Slot, d.TxnPointer, d.IdentityPreimages) | ||
if err != nil { | ||
return [32]byte{}, fmt.Errorf("%w: slot=%d, eon=%d", err, d.Slot, d.Eon) | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
func (d DecryptionKeysSignatureData) Sign(key *ecdsa.PrivateKey) ([]byte, error) { | ||
h, err := d.HashSSZ() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return crypto.Sign(h[:], key) | ||
} | ||
|
||
func (d DecryptionKeysSignatureData) Verify(signature []byte, address libcommon.Address) (bool, error) { | ||
h, err := d.HashSSZ() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
pubKey, err := crypto.SigToPub(h[:], signature) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return crypto.PubkeyToAddress(*pubKey) == address, nil | ||
} | ||
|
||
func (d DecryptionKeysSignatureData) Validate() error { | ||
if d.IdentityPreimages.Len() > identityPreimagesLimit { | ||
return fmt.Errorf("%w: len=%d", ErrTooManyIdentityPreimages, d.IdentityPreimages.Len()) | ||
} | ||
|
||
return nil | ||
} |
49 changes: 49 additions & 0 deletions
49
txnprovider/shutter/decryption_keys_signature_data_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package shutter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
libcommon "github.com/erigontech/erigon-lib/common" | ||
"github.com/erigontech/erigon/txnprovider/shutter" | ||
"github.com/erigontech/erigon/txnprovider/shutter/internal/testhelpers" | ||
) | ||
|
||
func TestIdentityPreimageEncodeDecodeSSZ(t *testing.T) { | ||
ip := testhelpers.Uint64ToIdentityPreimage(t, 123) | ||
buf, err := ip.EncodeSSZ(nil) | ||
require.NoError(t, err) | ||
ip2, err := shutter.IdentityPreimageFromSSZ(buf) | ||
require.NoError(t, err) | ||
require.Equal(t, ip, ip2) | ||
} | ||
|
||
func TestIdentityPreimageDecodeSSZWithInvalidLength(t *testing.T) { | ||
buf := make([]byte, 39) | ||
_, err := shutter.IdentityPreimageFromSSZ(buf) | ||
require.ErrorIs(t, err, shutter.ErrIncorrectIdentityPreimageSize) | ||
buf = make([]byte, 64) | ||
_, err = shutter.IdentityPreimageFromSSZ(buf) | ||
require.ErrorIs(t, err, shutter.ErrIncorrectIdentityPreimageSize) | ||
} | ||
|
||
func TestDecryptionKeysSignatureDataWithInvalidPreimagesLength(t *testing.T) { | ||
ips := testhelpers.MockIdentityPreimages(t, 1025) | ||
sigData := shutter.DecryptionKeysSignatureData{ | ||
InstanceId: 1, | ||
Eon: 2, | ||
Slot: 3, | ||
TxnPointer: 4, | ||
IdentityPreimages: ips.ToListSSZ(), | ||
} | ||
|
||
err := sigData.Validate() | ||
require.ErrorIs(t, err, shutter.ErrTooManyIdentityPreimages) | ||
_, err = sigData.HashSSZ() | ||
require.ErrorIs(t, err, shutter.ErrTooManyIdentityPreimages) | ||
_, err = sigData.Verify(nil, libcommon.Address{}) | ||
require.ErrorIs(t, err, shutter.ErrTooManyIdentityPreimages) | ||
_, err = sigData.Sign(nil) | ||
require.ErrorIs(t, err, shutter.ErrTooManyIdentityPreimages) | ||
} |
Oops, something went wrong.