github.com/amazechain/amc@v0.1.3/common/crypto/bls/lbs_test.go (about) 1 //go:build ((linux && amd64) || (linux && arm64) || (darwin && amd64) || (darwin && arm64) || (windows && amd64)) && !blst_disabled 2 3 package bls 4 5 import ( 6 "bytes" 7 "encoding/hex" 8 "errors" 9 "github.com/amazechain/amc/common/crypto/bls/blst" 10 "github.com/amazechain/amc/common/crypto/bls/common" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 "testing" 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 TestAggregateVerify_CompressedSignatures(t *testing.T) { 44 pubkeys := make([]common.PublicKey, 0, 100) 45 sigs := make([]common.Signature, 0, 100) 46 var sigBytes [][]byte 47 var msgs [][32]byte 48 for i := 0; i < 100; i++ { 49 msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)} 50 priv, err := RandKey() 51 require.NoError(t, err) 52 pub := priv.PublicKey() 53 sig := priv.Sign(msg[:]) 54 pubkeys = append(pubkeys, pub) 55 sigs = append(sigs, sig) 56 sigBytes = append(sigBytes, sig.Marshal()) 57 msgs = append(msgs, msg) 58 } 59 aggSig := AggregateSignatures(sigs) 60 assert.Equal(t, true, aggSig.AggregateVerify(pubkeys, msgs), "Signature did not verify") 61 62 aggSig2, err := AggregateCompressedSignatures(sigBytes) 63 assert.NoError(t, err) 64 assert.Equal(t, aggSig.Marshal(), aggSig2.Marshal(), "Signature did not match up") 65 } 66 67 func TestFastAggregateVerify(t *testing.T) { 68 pubkeys := make([]common.PublicKey, 0, 100) 69 sigs := make([]common.Signature, 0, 100) 70 msg := [32]byte{'h', 'e', 'l', 'l', 'o'} 71 for i := 0; i < 100; i++ { 72 priv, err := RandKey() 73 require.NoError(t, err) 74 pub := priv.PublicKey() 75 sig := priv.Sign(msg[:]) 76 pubkeys = append(pubkeys, pub) 77 sigs = append(sigs, sig) 78 } 79 aggSig := AggregateSignatures(sigs) 80 assert.Equal(t, true, aggSig.FastAggregateVerify(pubkeys, msg), "Signature did not verify") 81 82 } 83 84 func TestVerifyCompressed(t *testing.T) { 85 priv, err := RandKey() 86 require.NoError(t, err) 87 pub := priv.PublicKey() 88 msg := []byte("hello") 89 sig := priv.Sign(msg) 90 assert.Equal(t, true, sig.Verify(pub, msg), "Non compressed signature did not verify") 91 assert.Equal(t, true, blst.VerifyCompressed(sig.Marshal(), pub.Marshal(), msg), "Compressed signatures and pubkeys did not verify") 92 } 93 94 func TestMultipleSignatureVerification(t *testing.T) { 95 pubkeys := make([]common.PublicKey, 0, 100) 96 sigs := make([][]byte, 0, 100) 97 var msgs [][32]byte 98 for i := 0; i < 100; i++ { 99 msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)} 100 priv, err := RandKey() 101 require.NoError(t, err) 102 pub := priv.PublicKey() 103 sig := priv.Sign(msg[:]).Marshal() 104 pubkeys = append(pubkeys, pub) 105 sigs = append(sigs, sig) 106 msgs = append(msgs, msg) 107 } 108 verify, err := VerifyMultipleSignatures(sigs, msgs, pubkeys) 109 assert.NoError(t, err, "Signature did not verify") 110 assert.Equal(t, true, verify, "Signature did not verify") 111 } 112 113 func TestFastAggregateVerify_ReturnsFalseOnEmptyPubKeyList(t *testing.T) { 114 var pubkeys []common.PublicKey 115 msg := [32]byte{'h', 'e', 'l', 'l', 'o'} 116 117 aggSig := NewAggregateSignature() 118 assert.Equal(t, false, aggSig.FastAggregateVerify(pubkeys, msg), "Expected FastAggregateVerify to return false with empty input ") 119 } 120 121 func TestEth2FastAggregateVerify(t *testing.T) { 122 pubkeys := make([]common.PublicKey, 0, 100) 123 sigs := make([]common.Signature, 0, 100) 124 msg := [32]byte{'h', 'e', 'l', 'l', 'o'} 125 for i := 0; i < 100; i++ { 126 priv, err := RandKey() 127 require.NoError(t, err) 128 pub := priv.PublicKey() 129 sig := priv.Sign(msg[:]) 130 pubkeys = append(pubkeys, pub) 131 sigs = append(sigs, sig) 132 } 133 aggSig := AggregateSignatures(sigs) 134 assert.Equal(t, true, aggSig.Eth2FastAggregateVerify(pubkeys, msg), "Signature did not verify") 135 136 } 137 138 func TestEth2FastAggregateVerify_ReturnsFalseOnEmptyPubKeyList(t *testing.T) { 139 var pubkeys []common.PublicKey 140 msg := [32]byte{'h', 'e', 'l', 'l', 'o'} 141 142 aggSig := NewAggregateSignature() 143 assert.Equal(t, false, aggSig.Eth2FastAggregateVerify(pubkeys, msg), "Expected Eth2FastAggregateVerify to return false with empty input ") 144 } 145 146 func TestEth2FastAggregateVerify_ReturnsTrueOnG2PointAtInfinity(t *testing.T) { 147 var pubkeys []common.PublicKey 148 msg := [32]byte{'h', 'e', 'l', 'l', 'o'} 149 150 g2PointAtInfinity := append([]byte{0xC0}, make([]byte, 95)...) 151 aggSig, err := SignatureFromBytes(g2PointAtInfinity) 152 require.NoError(t, err) 153 assert.Equal(t, true, aggSig.Eth2FastAggregateVerify(pubkeys, msg)) 154 } 155 156 func TestSignatureFromBytes(t *testing.T) { 157 tests := []struct { 158 name string 159 input []byte 160 err error 161 }{ 162 { 163 name: "Nil", 164 err: errors.New("signature must be 96 bytes"), 165 }, 166 { 167 name: "Empty", 168 input: []byte{}, 169 err: errors.New("signature must be 96 bytes"), 170 }, 171 { 172 name: "Short", 173 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}, 174 err: errors.New("signature must be 96 bytes"), 175 }, 176 { 177 name: "Long", 178 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}, 179 err: errors.New("signature must be 96 bytes"), 180 }, 181 { 182 name: "Bad", 183 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}, 184 err: errors.New("could not unmarshal bytes into signature"), 185 }, 186 { 187 name: "Good", 188 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}, 189 }, 190 } 191 192 for _, test := range tests { 193 t.Run(test.name, func(t *testing.T) { 194 res, err := SignatureFromBytes(test.input) 195 if test.err != nil { 196 assert.NotEqual(t, nil, err, "No error returned") 197 assert.ErrorContains(t, test.err, err.Error(), "Unexpected error returned") 198 } else { 199 assert.NoError(t, err) 200 assert.Equal(t, 0, bytes.Compare(res.Marshal(), test.input)) 201 } 202 }) 203 } 204 } 205 206 func TestMultipleSignatureFromBytes(t *testing.T) { 207 tests := []struct { 208 name string 209 input [][]byte 210 err error 211 }{ 212 { 213 name: "Nil", 214 err: errors.New("0 signatures provided to the method"), 215 }, 216 { 217 name: "Empty", 218 input: [][]byte{}, 219 err: errors.New("0 signatures provided to the method"), 220 }, 221 { 222 name: "Short", 223 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}}, 224 err: errors.New("signature must be 96 bytes"), 225 }, 226 { 227 name: "Long", 228 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}}, 229 err: errors.New("signature must be 96 bytes"), 230 }, 231 { 232 name: "Bad", 233 input: [][]byte{{0x8f, 0xc0, 0xb4, 0x9e, 0x2e, 0xac, 0x50, 0x86, 0xe2, 0xe2, 0xaa, 0xf, 0xdc, 0x54, 0x23, 0x51, 0x6, 0xd8, 0x29, 0xf5, 0xae, 0x3, 0x5d, 0xb8, 0x31, 0x4d, 0x26, 0x3, 0x48, 0x18, 0xb9, 0x1f, 0x6b, 0xd7, 0x86, 0xb4, 0xa2, 0x69, 0xc7, 0xe7, 0xf5, 0xc0, 0x93, 0x19, 0x6e, 0xfd, 0x33, 0xb8, 0x1, 0xe1, 0x1f, 0x4e, 0xb4, 0xb1, 0xa0, 0x1, 0x30, 0x48, 0x8a, 0x6c, 0x97, 0x29, 0xd6, 0xcb, 0x1c, 0x45, 0xef, 0x87, 0xba, 0x4f, 0xce, 0x22, 0x84, 0x48, 0xad, 0x16, 0xf7, 0x5c, 0xb2, 0xa8, 0x34, 0xb9, 0xee, 0xb8, 0xbf, 0xe5, 0x58, 0x2c, 0x44, 0x7b, 0x1f, 0x9c, 0x22, 0x26, 0x3a, 0x22}, 234 {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}}, 235 err: errors.New("could not unmarshal bytes into signature"), 236 }, 237 { 238 name: "Good", 239 input: [][]byte{ 240 {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}, 241 {0xb7, 0x86, 0xe5, 0x7, 0x43, 0xe2, 0x53, 0x6c, 0x15, 0x51, 0x9c, 0x6, 0x2a, 0xa7, 0xe5, 0x12, 0xf9, 0xb7, 0x77, 0x93, 0x3f, 0x55, 0xb3, 0xaf, 0x38, 0xf7, 0x39, 0xe4, 0x84, 0x6d, 0x88, 0x44, 0x52, 0x77, 0x65, 0x42, 0x95, 0xd9, 0x79, 0x93, 0x7e, 0xc8, 0x12, 0x60, 0xe3, 0x24, 0xea, 0x8, 0x10, 0x52, 0xcd, 0xd2, 0x7f, 0x5d, 0x25, 0x3a, 0xa8, 0x9b, 0xb7, 0x65, 0xa9, 0x31, 0xea, 0x7c, 0x85, 0x13, 0x53, 0xc0, 0xa3, 0x88, 0xd1, 0xa5, 0x54, 0x85, 0x2, 0x2d, 0xf8, 0xa1, 0xd7, 0xc1, 0x60, 0x58, 0x93, 0xec, 0x7c, 0xf9, 0x33, 0x43, 0x4, 0x48, 0x40, 0x97, 0xef, 0x67, 0x2a, 0x27}, 242 {0xb2, 0x12, 0xd0, 0xec, 0x46, 0x76, 0x6b, 0x24, 0x71, 0x91, 0x2e, 0xa8, 0x53, 0x9a, 0x48, 0xa3, 0x78, 0x30, 0xc, 0xe8, 0xf0, 0x86, 0xa3, 0x68, 0xec, 0xe8, 0x96, 0x43, 0x34, 0xda, 0xf, 0xf4, 0x65, 0x48, 0xbb, 0xe0, 0x92, 0xa1, 0x8, 0x12, 0x18, 0x46, 0xe6, 0x4a, 0xd6, 0x92, 0x88, 0xe, 0x2, 0xf5, 0xf3, 0x2a, 0x96, 0xb1, 0x4, 0xf1, 0x11, 0xa9, 0x92, 0x79, 0x52, 0x0, 0x64, 0x34, 0xeb, 0x25, 0xe, 0xf4, 0x29, 0x6b, 0x39, 0x4e, 0x28, 0x78, 0xfe, 0x25, 0xa3, 0xc0, 0x88, 0x5a, 0x40, 0xfd, 0x71, 0x37, 0x63, 0x79, 0xcd, 0x6b, 0x56, 0xda, 0xee, 0x91, 0x26, 0x72, 0xfc, 0xbc}, 243 {0x8f, 0xc0, 0xb4, 0x9e, 0x2e, 0xac, 0x50, 0x86, 0xe2, 0xe2, 0xaa, 0xf, 0xdc, 0x54, 0x23, 0x51, 0x6, 0xd8, 0x29, 0xf5, 0xae, 0x3, 0x5d, 0xb8, 0x31, 0x4d, 0x26, 0x3, 0x48, 0x18, 0xb9, 0x1f, 0x6b, 0xd7, 0x86, 0xb4, 0xa2, 0x69, 0xc7, 0xe7, 0xf5, 0xc0, 0x93, 0x19, 0x6e, 0xfd, 0x33, 0xb8, 0x1, 0xe1, 0x1f, 0x4e, 0xb4, 0xb1, 0xa0, 0x1, 0x30, 0x48, 0x8a, 0x6c, 0x97, 0x29, 0xd6, 0xcb, 0x1c, 0x45, 0xef, 0x87, 0xba, 0x4f, 0xce, 0x22, 0x84, 0x48, 0xad, 0x16, 0xf7, 0x5c, 0xb2, 0xa8, 0x34, 0xb9, 0xee, 0xb8, 0xbf, 0xe5, 0x58, 0x2c, 0x44, 0x7b, 0x1f, 0x9c, 0x22, 0x26, 0x3a, 0x22}, 244 }, 245 }, 246 } 247 248 for _, test := range tests { 249 t.Run(test.name, func(t *testing.T) { 250 res, err := MultipleSignaturesFromBytes(test.input) 251 if test.err != nil { 252 assert.NotEqual(t, nil, err, "No error returned") 253 assert.ErrorContains(t, test.err, err.Error(), "Unexpected error returned") 254 } else { 255 assert.NoError(t, err) 256 for i, s := range res { 257 assert.Equal(t, 0, bytes.Compare(s.Marshal(), test.input[i])) 258 } 259 } 260 }) 261 } 262 } 263 264 func TestCopy(t *testing.T) { 265 priv, err := RandKey() 266 require.NoError(t, err) 267 268 signatureA := priv.Sign([]byte("foo")) 269 signatureB := signatureA.Copy() 270 271 if signatureA == signatureB { 272 t.Fatal("signatureA equal to signatureB") 273 } 274 275 signatureC := priv.Sign([]byte("bar")) 276 assert.NotEqual(t, signatureC, signatureB) 277 } 278 279 func TestSecretKeyFromBytes(t *testing.T) { 280 b, _ := hex.DecodeString("5de474bbf3fee5dfda9047287f596c6e6c0271876305357e399fffba6a5f9a9f") 281 tests := []struct { 282 name string 283 input []byte 284 err error 285 }{ 286 { 287 name: "Nil", 288 err: errors.New("secret key must be 32 bytes"), 289 }, 290 { 291 name: "Empty", 292 input: []byte{}, 293 err: errors.New("secret key must be 32 bytes"), 294 }, 295 { 296 name: "Short", 297 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}, 298 err: errors.New("secret key must be 32 bytes"), 299 }, 300 { 301 name: "Long", 302 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}, 303 err: errors.New("secret key must be 32 bytes"), 304 }, 305 { 306 name: "Bad", 307 input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 308 err: common.ErrSecretUnmarshal, 309 }, 310 { 311 name: "Good", 312 input: []byte{0x25, 0x29, 0x5f, 0x0d, 0x1d, 0x59, 0x2a, 0x90, 0xb3, 0x33, 0xe2, 0x6e, 0x85, 0x14, 0x97, 0x08, 0x20, 0x8e, 0x9f, 0x8e, 0x8b, 0xc1, 0x8f, 0x6c, 0x77, 0xbd, 0x62, 0xf8, 0xad, 0x7a, 0x68, 0x66}, 313 }, 314 { 315 name: "myTest", 316 input: b, 317 }, 318 } 319 320 for _, test := range tests { 321 t.Run(test.name, func(t *testing.T) { 322 res, err := SecretKeyFromBytes(test.input) 323 if test.err != nil { 324 assert.NotEqual(t, nil, err, "No error returned") 325 assert.ErrorContains(t, test.err, err.Error(), "Unexpected error returned") 326 } else { 327 assert.NoError(t, err) 328 assert.Equal(t, 0, bytes.Compare(res.Marshal(), test.input)) 329 } 330 }) 331 } 332 }