github.com/turingchain2020/turingchain@v1.1.21/common/crypto/crypto_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package crypto_test 6 7 import ( 8 "encoding/json" 9 "errors" 10 "strings" 11 "testing" 12 13 "github.com/turingchain2020/turingchain/system/crypto/ed25519" 14 "github.com/turingchain2020/turingchain/system/crypto/none" 15 "github.com/turingchain2020/turingchain/system/crypto/secp256k1" 16 17 "github.com/turingchain2020/turingchain/common/crypto" 18 _ "github.com/turingchain2020/turingchain/system/crypto/init" 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestGet(t *testing.T) { 23 require := require.New(t) 24 25 name := crypto.GetName(1) 26 require.Equal("secp256k1", name) 27 name = crypto.GetName(2) 28 require.Equal("ed25519", name) 29 name = crypto.GetName(258) 30 require.Equal("auth_sm2", name) 31 32 ty := crypto.GetType("secp256k1") 33 require.True(ty == 1) 34 ty = crypto.GetType("ed25519") 35 require.True(ty == 2) 36 37 ty = crypto.GetType("auth_sm2") 38 require.True(ty == 258) 39 } 40 41 func TestRipemd160(t *testing.T) { 42 b := crypto.Ripemd160([]byte("test")) 43 require.NotNil(t, b) 44 } 45 46 func TestSm3Hash(t *testing.T) { 47 require := require.New(t) 48 b := crypto.Sm3Hash([]byte("test")) 49 require.NotNil(b) 50 } 51 52 func TestAll(t *testing.T) { 53 testCrypto(t, "ed25519") 54 testFromBytes(t, "ed25519") 55 testCrypto(t, "secp256k1") 56 testFromBytes(t, "secp256k1") 57 58 c, err := crypto.New("none") 59 require.Nil(t, err) 60 pub, err := c.PubKeyFromBytes([]byte("test")) 61 require.Nil(t, pub) 62 require.Nil(t, err) 63 sig, err := c.SignatureFromBytes([]byte("test")) 64 require.Nil(t, sig) 65 require.Nil(t, err) 66 require.Nil(t, c.Validate([]byte("test"), nil, nil)) 67 testCrypto(t, "auth_sm2") 68 testFromBytes(t, "auth_sm2") 69 testCrypto(t, "auth_ecdsa") 70 testFromBytes(t, "auth_ecdsa") 71 } 72 73 func testFromBytes(t *testing.T, name string) { 74 require := require.New(t) 75 76 c, err := crypto.New(name) 77 require.Nil(err) 78 79 priv, err := c.GenKey() 80 require.Nil(err) 81 82 priv2, err := c.PrivKeyFromBytes(priv.Bytes()) 83 require.Nil(err) 84 require.Equal(true, priv.Equals(priv2)) 85 86 s1 := string(priv.Bytes()) 87 s2 := string(priv2.Bytes()) 88 require.Equal(0, strings.Compare(s1, s2)) 89 90 pub := priv.PubKey() 91 require.NotNil(pub) 92 93 pub2, err := c.PubKeyFromBytes(pub.Bytes()) 94 require.Nil(err) 95 require.Equal(true, pub.Equals(pub2)) 96 97 s1 = string(pub.Bytes()) 98 s2 = string(pub2.Bytes()) 99 require.Equal(0, strings.Compare(s1, s2)) 100 101 var msg = []byte("hello world") 102 sign1 := priv.Sign(msg) 103 sign2 := priv2.Sign(msg) 104 105 sign3, err := c.SignatureFromBytes(sign1.Bytes()) 106 require.Nil(err) 107 require.Equal(true, sign3.Equals(sign1)) 108 109 require.Equal(true, pub.VerifyBytes(msg, sign1)) 110 require.Equal(true, pub2.VerifyBytes(msg, sign1)) 111 require.Equal(true, pub.VerifyBytes(msg, sign2)) 112 require.Equal(true, pub2.VerifyBytes(msg, sign2)) 113 require.Equal(true, pub.VerifyBytes(msg, sign3)) 114 require.Equal(true, pub2.VerifyBytes(msg, sign3)) 115 require.Nil(c.Validate(msg, pub.Bytes(), sign1.Bytes())) 116 } 117 118 func testCrypto(t *testing.T, name string) { 119 require := require.New(t) 120 121 c, err := crypto.New(name) 122 require.Nil(err) 123 124 priv, err := c.GenKey() 125 require.Nil(err) 126 t.Logf("%s priv:%X, len:%d", name, priv.Bytes(), len(priv.Bytes())) 127 128 pub := priv.PubKey() 129 require.NotNil(pub) 130 t.Logf("%s pub:%X, len:%d", name, pub.Bytes(), len(pub.Bytes())) 131 132 msg := []byte("hello world") 133 signature := priv.Sign(msg) 134 t.Logf("%s sign:%X, len:%d", name, signature.Bytes(), len(signature.Bytes())) 135 136 ok := pub.VerifyBytes(msg, signature) 137 require.Equal(true, ok) 138 } 139 140 func BenchmarkSignEd25519(b *testing.B) { 141 benchSign(b, "ed25519") 142 } 143 144 func BenchmarkVerifyEd25519(b *testing.B) { 145 benchVerify(b, "ed25519") 146 } 147 148 func BenchmarkSignSecp256k1(b *testing.B) { 149 benchSign(b, "secp256k1") 150 } 151 152 func BenchmarkVerifySecp256k1(b *testing.B) { 153 benchVerify(b, "secp256k1") 154 } 155 156 func BenchmarkSignSm2(b *testing.B) { 157 benchSign(b, "sm2") 158 } 159 160 func BenchmarkVerifySm2(b *testing.B) { 161 benchVerify(b, "sm2") 162 } 163 164 func benchSign(b *testing.B, name string) { 165 c, _ := crypto.New(name) 166 priv, _ := c.GenKey() 167 msg := []byte("hello world") 168 for i := 0; i < b.N; i++ { 169 priv.Sign(msg) 170 } 171 } 172 173 func benchVerify(b *testing.B, name string) { 174 c, _ := crypto.New(name) 175 priv, _ := c.GenKey() 176 pub := priv.PubKey() 177 msg := []byte("hello world") 178 sign := priv.Sign(msg) 179 for i := 0; i < b.N; i++ { 180 pub.VerifyBytes(msg, sign) 181 } 182 } 183 184 func TestAggregate(t *testing.T) { 185 c, err := crypto.New("secp256k1") 186 if err != nil { 187 panic(err) 188 } 189 _, err = crypto.ToAggregate(c) 190 require.Equal(t, err, crypto.ErrNotSupportAggr) 191 192 c = democrypto{} 193 aggr, err := crypto.ToAggregate(c) 194 require.Nil(t, err) 195 sig, err := aggr.Aggregate(nil) 196 require.Nil(t, sig) 197 require.Nil(t, err) 198 } 199 200 type democrypto struct{} 201 202 func (d democrypto) GenKey() (crypto.PrivKey, error) { 203 return nil, nil 204 } 205 206 func (d democrypto) SignatureFromBytes([]byte) (crypto.Signature, error) { 207 return nil, nil 208 } 209 func (d democrypto) PrivKeyFromBytes([]byte) (crypto.PrivKey, error) { 210 return nil, nil 211 } 212 func (d democrypto) PubKeyFromBytes([]byte) (crypto.PubKey, error) { 213 return nil, nil 214 } 215 func (d democrypto) Validate(msg, pub, sig []byte) error { 216 return nil 217 } 218 219 //AggregateCrypto 聚合签名 220 221 func (d democrypto) Aggregate(sigs []crypto.Signature) (crypto.Signature, error) { 222 return nil, nil 223 } 224 func (d democrypto) AggregatePublic(pubs []crypto.PubKey) (crypto.PubKey, error) { 225 return nil, nil 226 } 227 func (d democrypto) VerifyAggregatedOne(pubs []crypto.PubKey, m []byte, sig crypto.Signature) error { 228 return nil 229 } 230 func (d democrypto) VerifyAggregatedN(pubs []crypto.PubKey, ms [][]byte, sig crypto.Signature) error { 231 return nil 232 } 233 234 type democryptoCGO struct { 235 democrypto 236 } 237 238 func (d democryptoCGO) GenKey() (crypto.PrivKey, error) { 239 return nil, errors.New("testCGO") 240 } 241 242 func TestRegister(t *testing.T) { 243 c, err := crypto.New("secp256k1") 244 require.Nil(t, err) 245 p, err := c.GenKey() 246 require.Nil(t, err) 247 require.NotNil(t, p) 248 require.Panics(t, func() { crypto.Register(secp256k1.Name, democryptoCGO{}, crypto.WithOptionTypeID(secp256k1.ID)) }) 249 //注册cgo版本,替换 250 crypto.Register(secp256k1.Name, democryptoCGO{}, crypto.WithOptionCGO(), crypto.WithOptionTypeID(secp256k1.ID)) 251 //重复注册非cgo版本,不会报错 252 crypto.Register(secp256k1.Name, democryptoCGO{}, crypto.WithOptionTypeID(secp256k1.ID)) 253 require.Panics(t, func() { 254 crypto.Register(secp256k1.Name, democryptoCGO{}, crypto.WithOptionCGO(), crypto.WithOptionTypeID(1024)) 255 }) 256 require.Panics(t, func() { crypto.Register(secp256k1.Name+"cgo", democryptoCGO{}, crypto.WithOptionTypeID(secp256k1.ID)) }) 257 258 c, err = crypto.New("secp256k1") 259 require.Nil(t, err) 260 p, err = c.GenKey() 261 require.Nil(t, p) 262 require.Equal(t, errors.New("testCGO"), err) 263 } 264 265 func TestInitCfg(t *testing.T) { 266 267 cfg := &crypto.Config{} 268 crypto.Init(cfg, nil) 269 must := require.New(t) 270 must.False(crypto.IsEnable(none.Name, 0)) 271 must.True(crypto.IsEnable(secp256k1.Name, 0)) 272 must.True(crypto.IsEnable(ed25519.Name, 0)) 273 cfg.EnableTypes = []string{secp256k1.Name, none.Name} 274 cfg.EnableHeight = make(map[string]int64) 275 cfg.EnableHeight[ed25519.Name] = 10 276 cfg.EnableHeight[none.Name] = 100 277 crypto.Init(cfg, nil) 278 must.False(crypto.IsEnable(none.Name, 0)) 279 must.True(crypto.IsEnable(none.Name, 100)) 280 must.True(crypto.IsEnable(secp256k1.Name, 0)) 281 must.False(crypto.IsEnable(ed25519.Name, 0)) 282 } 283 284 type testSubCfg struct { 285 Name string 286 Height int64 287 } 288 289 func TestInitSubCfg(t *testing.T) { 290 291 cfg := &crypto.Config{} 292 subCfg := make(map[string][]byte) 293 294 sub1 := &testSubCfg{Name: "test", Height: 100} 295 bsub, err := json.Marshal(sub1) 296 require.Nil(t, err) 297 initFn := func(b []byte) { 298 sub2 := &testSubCfg{} 299 err := json.Unmarshal(b, sub2) 300 require.Nil(t, err) 301 require.Equal(t, sub1, sub2) 302 } 303 crypto.Register("test", democrypto{}, crypto.WithOptionInitFunc(initFn)) 304 subCfg[sub1.Name] = bsub 305 crypto.Init(cfg, subCfg) 306 } 307 308 func TestGenDriverTypeID(t *testing.T) { 309 id := crypto.GenDriverTypeID("TestGenDriverTypeID") 310 require.Equal(t, int32(81208513), id) 311 } 312 313 func TestWithOption(t *testing.T) { 314 driver := &crypto.Driver{} 315 option := crypto.WithOptionTypeID(-1) 316 require.NotNil(t, option(driver)) 317 option = crypto.WithOptionTypeID(crypto.MaxManualTypeID) 318 require.Nil(t, option(driver)) 319 option = crypto.WithOptionTypeID(crypto.MaxManualTypeID + 1) 320 require.NotNil(t, option(driver)) 321 option = crypto.WithOptionInitFunc(nil) 322 require.NotNil(t, option(driver)) 323 }