github.com/prysmaticlabs/prysm@v1.4.4/shared/bls/blst/signature_test.go (about)

     1  // +build linux,amd64 linux,arm64 darwin,amd64 windows,amd64
     2  // +build !blst_disabled
     3  
     4  package blst
     5  
     6  import (
     7  	"bytes"
     8  	"errors"
     9  	"testing"
    10  
    11  	"github.com/prysmaticlabs/prysm/shared/bls/common"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    14  )
    15  
    16  func TestSignVerify(t *testing.T) {
    17  	priv, err := RandKey()
    18  	require.NoError(t, err)
    19  	pub := priv.PublicKey()
    20  	msg := []byte("hello")
    21  	sig := priv.Sign(msg)
    22  	assert.Equal(t, true, sig.Verify(pub, msg), "Signature did not verify")
    23  }
    24  
    25  func TestAggregateVerify(t *testing.T) {
    26  	pubkeys := make([]common.PublicKey, 0, 100)
    27  	sigs := make([]common.Signature, 0, 100)
    28  	var msgs [][32]byte
    29  	for i := 0; i < 100; i++ {
    30  		msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)}
    31  		priv, err := RandKey()
    32  		require.NoError(t, err)
    33  		pub := priv.PublicKey()
    34  		sig := priv.Sign(msg[:])
    35  		pubkeys = append(pubkeys, pub)
    36  		sigs = append(sigs, sig)
    37  		msgs = append(msgs, msg)
    38  	}
    39  	aggSig := AggregateSignatures(sigs)
    40  	assert.Equal(t, true, aggSig.AggregateVerify(pubkeys, msgs), "Signature did not verify")
    41  }
    42  
    43  func TestFastAggregateVerify(t *testing.T) {
    44  	pubkeys := make([]common.PublicKey, 0, 100)
    45  	sigs := make([]common.Signature, 0, 100)
    46  	msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
    47  	for i := 0; i < 100; i++ {
    48  		priv, err := RandKey()
    49  		require.NoError(t, err)
    50  		pub := priv.PublicKey()
    51  		sig := priv.Sign(msg[:])
    52  		pubkeys = append(pubkeys, pub)
    53  		sigs = append(sigs, sig)
    54  	}
    55  	aggSig := AggregateSignatures(sigs)
    56  	assert.Equal(t, true, aggSig.FastAggregateVerify(pubkeys, msg), "Signature did not verify")
    57  
    58  }
    59  
    60  func TestVerifyCompressed(t *testing.T) {
    61  	priv, err := RandKey()
    62  	require.NoError(t, err)
    63  	pub := priv.PublicKey()
    64  	msg := []byte("hello")
    65  	sig := priv.Sign(msg)
    66  	assert.Equal(t, true, sig.Verify(pub, msg), "Non compressed signature did not verify")
    67  	assert.Equal(t, true, VerifyCompressed(sig.Marshal(), pub.Marshal(), msg), "Compressed signatures and pubkeys did not verify")
    68  }
    69  
    70  func TestMultipleSignatureVerification(t *testing.T) {
    71  	pubkeys := make([]common.PublicKey, 0, 100)
    72  	sigs := make([][]byte, 0, 100)
    73  	var msgs [][32]byte
    74  	for i := 0; i < 100; i++ {
    75  		msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)}
    76  		priv, err := RandKey()
    77  		require.NoError(t, err)
    78  		pub := priv.PublicKey()
    79  		sig := priv.Sign(msg[:]).Marshal()
    80  		pubkeys = append(pubkeys, pub)
    81  		sigs = append(sigs, sig)
    82  		msgs = append(msgs, msg)
    83  	}
    84  	verify, err := VerifyMultipleSignatures(sigs, msgs, pubkeys)
    85  	assert.NoError(t, err, "Signature did not verify")
    86  	assert.Equal(t, true, verify, "Signature did not verify")
    87  }
    88  
    89  func TestFastAggregateVerify_ReturnsFalseOnEmptyPubKeyList(t *testing.T) {
    90  	var pubkeys []common.PublicKey
    91  	msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
    92  
    93  	aggSig := NewAggregateSignature()
    94  	assert.Equal(t, false, aggSig.FastAggregateVerify(pubkeys, msg), "Expected FastAggregateVerify to return false with empty input ")
    95  }
    96  
    97  func TestEth2FastAggregateVerify(t *testing.T) {
    98  	pubkeys := make([]common.PublicKey, 0, 100)
    99  	sigs := make([]common.Signature, 0, 100)
   100  	msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
   101  	for i := 0; i < 100; i++ {
   102  		priv, err := RandKey()
   103  		require.NoError(t, err)
   104  		pub := priv.PublicKey()
   105  		sig := priv.Sign(msg[:])
   106  		pubkeys = append(pubkeys, pub)
   107  		sigs = append(sigs, sig)
   108  	}
   109  	aggSig := AggregateSignatures(sigs)
   110  	assert.Equal(t, true, aggSig.Eth2FastAggregateVerify(pubkeys, msg), "Signature did not verify")
   111  
   112  }
   113  
   114  func TestEth2FastAggregateVerify_ReturnsFalseOnEmptyPubKeyList(t *testing.T) {
   115  	var pubkeys []common.PublicKey
   116  	msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
   117  
   118  	aggSig := NewAggregateSignature()
   119  	assert.Equal(t, false, aggSig.Eth2FastAggregateVerify(pubkeys, msg), "Expected Eth2FastAggregateVerify to return false with empty input ")
   120  }
   121  
   122  func TestEth2FastAggregateVerify_ReturnsTrueOnG2PointAtInfinity(t *testing.T) {
   123  	var pubkeys []common.PublicKey
   124  	msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
   125  
   126  	g2PointAtInfinity := append([]byte{0xC0}, make([]byte, 95)...)
   127  	aggSig, err := SignatureFromBytes(g2PointAtInfinity)
   128  	require.NoError(t, err)
   129  	assert.Equal(t, true, aggSig.Eth2FastAggregateVerify(pubkeys, msg))
   130  }
   131  
   132  func TestSignatureFromBytes(t *testing.T) {
   133  	tests := []struct {
   134  		name  string
   135  		input []byte
   136  		err   error
   137  	}{
   138  		{
   139  			name: "Nil",
   140  			err:  errors.New("signature must be 96 bytes"),
   141  		},
   142  		{
   143  			name:  "Empty",
   144  			input: []byte{},
   145  			err:   errors.New("signature must be 96 bytes"),
   146  		},
   147  		{
   148  			name:  "Short",
   149  			input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
   150  			err:   errors.New("signature must be 96 bytes"),
   151  		},
   152  		{
   153  			name:  "Long",
   154  			input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
   155  			err:   errors.New("signature must be 96 bytes"),
   156  		},
   157  		{
   158  			name:  "Bad",
   159  			input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
   160  			err:   errors.New("could not unmarshal bytes into signature"),
   161  		},
   162  		{
   163  			name:  "Good",
   164  			input: []byte{0xab, 0xb0, 0x12, 0x4c, 0x75, 0x74, 0xf2, 0x81, 0xa2, 0x93, 0xf4, 0x18, 0x5c, 0xad, 0x3c, 0xb2, 0x26, 0x81, 0xd5, 0x20, 0x91, 0x7c, 0xe4, 0x66, 0x65, 0x24, 0x3e, 0xac, 0xb0, 0x51, 0x00, 0x0d, 0x8b, 0xac, 0xf7, 0x5e, 0x14, 0x51, 0x87, 0x0c, 0xa6, 0xb3, 0xb9, 0xe6, 0xc9, 0xd4, 0x1a, 0x7b, 0x02, 0xea, 0xd2, 0x68, 0x5a, 0x84, 0x18, 0x8a, 0x4f, 0xaf, 0xd3, 0x82, 0x5d, 0xaf, 0x6a, 0x98, 0x96, 0x25, 0xd7, 0x19, 0xcc, 0xd2, 0xd8, 0x3a, 0x40, 0x10, 0x1f, 0x4a, 0x45, 0x3f, 0xca, 0x62, 0x87, 0x8c, 0x89, 0x0e, 0xca, 0x62, 0x23, 0x63, 0xf9, 0xdd, 0xb8, 0xf3, 0x67, 0xa9, 0x1e, 0x84},
   165  		},
   166  	}
   167  
   168  	for _, test := range tests {
   169  		t.Run(test.name, func(t *testing.T) {
   170  			res, err := SignatureFromBytes(test.input)
   171  			if test.err != nil {
   172  				assert.NotEqual(t, nil, err, "No error returned")
   173  				assert.ErrorContains(t, test.err.Error(), err, "Unexpected error returned")
   174  			} else {
   175  				assert.NoError(t, err)
   176  				assert.DeepEqual(t, 0, bytes.Compare(res.Marshal(), test.input))
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func TestCopy(t *testing.T) {
   183  	priv, err := RandKey()
   184  	require.NoError(t, err)
   185  	key, ok := priv.(*bls12SecretKey)
   186  	require.Equal(t, true, ok)
   187  
   188  	signatureA := &Signature{s: new(blstSignature).Sign(key.p, []byte("foo"), dst)}
   189  	signatureB, ok := signatureA.Copy().(*Signature)
   190  	require.Equal(t, true, ok)
   191  
   192  	assert.NotEqual(t, signatureA, signatureB)
   193  	assert.NotEqual(t, signatureA.s, signatureB.s)
   194  	assert.DeepEqual(t, signatureA, signatureB)
   195  
   196  	signatureA.s.Sign(key.p, []byte("bar"), dst)
   197  	assert.DeepNotEqual(t, signatureA, signatureB)
   198  }