Skip to content

Commit

Permalink
Make bls.Signer api fallible
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle committed Feb 4, 2025
1 parent 1009fd3 commit 9fcc414
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 164 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/DataDog/zstd v1.5.2
github.com/NYTimes/gziphandler v1.1.1
github.com/antithesishq/antithesis-sdk-go v0.3.8
github.com/ava-labs/coreth v0.14.2-verify-interface4
github.com/ava-labs/coreth v0.14.1-rc.1.0.20250204181539-66241eebcbed
github.com/ava-labs/ledger-avalanche/go v0.0.0-20241009183145-e6f90a8a1a60
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/antithesishq/antithesis-sdk-go v0.3.8/go.mod h1:IUpT2DPAKh6i/YhSbt6Gl
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/ava-labs/coreth v0.14.2-verify-interface4 h1:AYeN8R6ZnNu/K8KwBQD4ELphvLpvNxAjkX3SBcJ+bps=
github.com/ava-labs/coreth v0.14.2-verify-interface4/go.mod h1:wQaeiolUP0vCHS1mC0lIMXzHF05vbjugSLCBFDnO4Gs=
github.com/ava-labs/coreth v0.14.1-rc.1.0.20250204181539-66241eebcbed h1:m9JXpuXXx9a/pcwHUQUJ8R1Vp3nndsJSnQxsT5brxDc=
github.com/ava-labs/coreth v0.14.1-rc.1.0.20250204181539-66241eebcbed/go.mod h1:cfL8GjzBa87pxGWIAG4iB4f6vSMyuBHtkmcUk9ms4sE=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20241009183145-e6f90a8a1a60 h1:EL66gtXOAwR/4KYBjOV03LTWgkEXvLePribLlJNu4g0=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20241009183145-e6f90a8a1a60/go.mod h1:/7qKobTfbzBu7eSTVaXMTr56yTYk4j2Px6/8G+idxHo=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down
12 changes: 10 additions & 2 deletions network/peer/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ func (ip *UnsignedIP) Sign(tlsSigner crypto.Signer, blsSigner bls.Signer) (*Sign
hashing.ComputeHash256(ipBytes),
crypto.SHA256,
)
blsSignature := blsSigner.SignProofOfPossession(ipBytes)
if err != nil {
return nil, err
}

blsSignature, err := blsSigner.SignProofOfPossession(ipBytes)
if err != nil {
return nil, err
}

return &SignedIP{
UnsignedIP: *ip,
TLSSignature: tlsSignature,
BLSSignature: blsSignature,
BLSSignatureBytes: bls.SignatureToBytes(blsSignature),
}, err
}, nil
}

func (ip *UnsignedIP) bytes() []byte {
Expand Down
25 changes: 21 additions & 4 deletions network/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/crypto/bls/signer/localsigner"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/math/meter"
Expand Down Expand Up @@ -556,7 +557,11 @@ func TestShouldDisconnect(t *testing.T) {
id: peerID,
version: version.CurrentApp,
ip: &SignedIP{
BLSSignature: blsKey.SignProofOfPossession([]byte("wrong message")),
BLSSignature: (func() *bls.Signature {
sig, err := blsKey.SignProofOfPossession([]byte("wrong message"))
require.NoError(t, err)
return sig
})(),
},
},
expectedPeer: &peer{
Expand All @@ -578,7 +583,11 @@ func TestShouldDisconnect(t *testing.T) {
id: peerID,
version: version.CurrentApp,
ip: &SignedIP{
BLSSignature: blsKey.SignProofOfPossession([]byte("wrong message")),
BLSSignature: (func() *bls.Signature {
sig, err := blsKey.SignProofOfPossession([]byte("wrong message"))
require.NoError(t, err)
return sig
})(),
},
},
expectedShouldDisconnect: true,
Expand All @@ -604,7 +613,11 @@ func TestShouldDisconnect(t *testing.T) {
id: peerID,
version: version.CurrentApp,
ip: &SignedIP{
BLSSignature: blsKey.SignProofOfPossession((&UnsignedIP{}).bytes()),
BLSSignature: (func() *bls.Signature {
sig, err := blsKey.SignProofOfPossession((&UnsignedIP{}).bytes())
require.NoError(t, err)
return sig
})(),
},
},
expectedPeer: &peer{
Expand All @@ -626,7 +639,11 @@ func TestShouldDisconnect(t *testing.T) {
id: peerID,
version: version.CurrentApp,
ip: &SignedIP{
BLSSignature: blsKey.SignProofOfPossession((&UnsignedIP{}).bytes()),
BLSSignature: (func() *bls.Signature {
sig, err := blsKey.SignProofOfPossession((&UnsignedIP{}).bytes())
require.NoError(t, err)
return sig
})(),
},
txIDOfVerifiedBLSKey: txID,
},
Expand Down
4 changes: 2 additions & 2 deletions utils/crypto/bls/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ package bls

type Signer interface {
PublicKey() *PublicKey
Sign(msg []byte) *Signature
SignProofOfPossession(msg []byte) *Signature
Sign(msg []byte) (*Signature, error)
SignProofOfPossession(msg []byte) (*Signature, error)
}
4 changes: 2 additions & 2 deletions utils/crypto/bls/signer/localsigner/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
)

func BenchmarkSign(b *testing.B) {
privateKey, _ := NewKeyPair(require.New(b))
privateKey := NewKeyPair(require.New(b)).signer
for _, messageSize := range blstest.BenchmarkSizes {
b.Run(strconv.Itoa(messageSize), func(b *testing.B) {
message := utils.RandomBytes(messageSize)

b.ResetTimer()

for n := 0; n < b.N; n++ {
_ = privateKey.Sign(message)
_, _ = privateKey.Sign(message)
}
})
}
Expand Down
Loading

0 comments on commit 9fcc414

Please sign in to comment.