|
| 1 | +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package simplex |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/ava-labs/simplex" |
| 11 | + |
| 12 | + "github.com/ava-labs/avalanchego/ids" |
| 13 | + "github.com/ava-labs/avalanchego/utils/crypto/bls" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + errSignatureVerificationFailed = errors.New("signature verification failed") |
| 18 | + errSignerNotFound = errors.New("signer not found in the membership set") |
| 19 | + errInvalidNodeID = errors.New("unable to parse node ID") |
| 20 | + errFailedToParseSignature = errors.New("failed to parse signature") |
| 21 | +) |
| 22 | + |
| 23 | +var _ simplex.Signer = (*BLSSigner)(nil) |
| 24 | + |
| 25 | +type SignFunc func(msg []byte) (*bls.Signature, error) |
| 26 | + |
| 27 | +// BLSSigner signs messages encoded with the provided ChainID and NetworkID. |
| 28 | +// using the SignBLS function. |
| 29 | +type BLSSigner struct { |
| 30 | + chainID ids.ID |
| 31 | + networkID uint32 |
| 32 | + // signBLS is passed in because we support both software and hardware BLS signing. |
| 33 | + signBLS SignFunc |
| 34 | +} |
| 35 | + |
| 36 | +type BLSVerifier struct { |
| 37 | + nodeID2PK map[ids.NodeID]*bls.PublicKey |
| 38 | + networkID uint32 |
| 39 | + chainID ids.ID |
| 40 | +} |
| 41 | + |
| 42 | +func NewBLSAuth(config *Config) (BLSSigner, BLSVerifier) { |
| 43 | + verifier := createVerifier(config) |
| 44 | + |
| 45 | + return BLSSigner{ |
| 46 | + chainID: config.Ctx.ChainID, |
| 47 | + networkID: config.Ctx.NetworkID, |
| 48 | + signBLS: config.SignBLS, |
| 49 | + }, verifier |
| 50 | +} |
| 51 | + |
| 52 | +// Sign returns a signature on the given message using BLS signature scheme. |
| 53 | +// It encodes the message to sign with the chain ID, and network ID, |
| 54 | +func (s *BLSSigner) Sign(message []byte) ([]byte, error) { |
| 55 | + message2Sign, err := encodeMessageToSign(message, s.chainID, s.networkID) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to encode message to sign: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + sig, err := s.signBLS(message2Sign) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + sigBytes := bls.SignatureToBytes(sig) |
| 66 | + return sigBytes, nil |
| 67 | +} |
| 68 | + |
| 69 | +type encodedSimplexSignedPayload struct { |
| 70 | + NewtorkID uint32 `serialize:"true"` |
| 71 | + ChainID ids.ID `serialize:"true"` |
| 72 | + Message []byte `serialize:"true"` |
| 73 | +} |
| 74 | + |
| 75 | +func encodeMessageToSign(message []byte, chainID ids.ID, networkID uint32) ([]byte, error) { |
| 76 | + encodedSimplexMessage := encodedSimplexSignedPayload{ |
| 77 | + Message: message, |
| 78 | + ChainID: chainID, |
| 79 | + NewtorkID: networkID, |
| 80 | + } |
| 81 | + return Codec.Marshal(CodecVersion, &encodedSimplexMessage) |
| 82 | +} |
| 83 | + |
| 84 | +func (v BLSVerifier) Verify(message []byte, signature []byte, signer simplex.NodeID) error { |
| 85 | + key, err := ids.ToNodeID(signer) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("%w: %w", errInvalidNodeID, err) |
| 88 | + } |
| 89 | + |
| 90 | + pk, exists := v.nodeID2PK[key] |
| 91 | + if !exists { |
| 92 | + return fmt.Errorf("%w: signer %x", errSignerNotFound, key) |
| 93 | + } |
| 94 | + |
| 95 | + sig, err := bls.SignatureFromBytes(signature) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("%w: %w", errFailedToParseSignature, err) |
| 98 | + } |
| 99 | + |
| 100 | + message2Verify, err := encodeMessageToSign(message, v.chainID, v.networkID) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("failed to encode message to verify: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + if !bls.Verify(pk, sig, message2Verify) { |
| 106 | + return errSignatureVerificationFailed |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func createVerifier(config *Config) BLSVerifier { |
| 113 | + verifier := BLSVerifier{ |
| 114 | + nodeID2PK: make(map[ids.NodeID]*bls.PublicKey), |
| 115 | + networkID: config.Ctx.NetworkID, |
| 116 | + chainID: config.Ctx.ChainID, |
| 117 | + } |
| 118 | + |
| 119 | + for _, node := range config.Validators { |
| 120 | + verifier.nodeID2PK[node.NodeID] = node.PublicKey |
| 121 | + } |
| 122 | + |
| 123 | + return verifier |
| 124 | +} |
0 commit comments