Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions protocol/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"errors"
"fmt"
"math"
"math/big"
"sort"

Expand Down Expand Up @@ -151,11 +152,13 @@ func EncodeSignatures(signatures []Data) ([]byte, error) {
SortSignaturesBySigner(sortedSignatures)

// Calculate signature length (each signature is 64 bytes: 32 R + 32 S)
//nolint:gosec // disable G115
signatureLength := uint16(len(sortedSignatures) * 64)
signatureLength := len(sortedSignatures) * 64
if signatureLength > math.MaxUint16 {
return nil, fmt.Errorf("too many signatures: %d exceeds max uint16", len(sortedSignatures))
}

// Create result buffer
result := make([]byte, 2+int(signatureLength))
result := make([]byte, 2+signatureLength)

// Write signature length as first 2 bytes (big-endian uint16)
result[0] = byte(signatureLength >> 8)
Expand Down
27 changes: 27 additions & 0 deletions protocol/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package protocol

import (
"crypto/ecdsa"
"math/big"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -302,3 +304,28 @@ func TestLeftPad32_InputTooLong(t *testing.T) {
})
}
}

func TestEncodeSignaturesTooMany(t *testing.T) {
// 1024 signatures -> 1024 * 64 = 65536 > math.MaxUint16 (65535)
const n = 1024
sigs := make([]Data, n)
for i := range n {
var r, s [32]byte
r[31] = 1
s[31] = 2
sigs[i] = Data{
R: r,
S: s,
Signer: common.BigToAddress(big.NewInt(int64(i))),
}
}

_, err := EncodeSignatures(sigs)
if err == nil {
t.Fatalf("expected error for too many signatures, got nil")
}

if !strings.Contains(err.Error(), "too many signatures") {
t.Fatalf("unexpected error: %v", err)
}
}
Loading