github.com/annchain/OG@v0.0.9/common/crypto/non_cgo_test.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // +build noncgo 15 16 package crypto 17 18 import ( 19 "crypto/ecdsa" 20 "encoding/hex" 21 "fmt" 22 "github.com/annchain/OG/arefactor/og_interface" 23 "github.com/annchain/OG/arefactor/ogcrypto" 24 "github.com/annchain/OG/common/math" 25 ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto" 26 "github.com/annchain/OG/deprecated/ogcrypto/secp256k1" 27 "github.com/annchain/OG/og/types/archive" 28 29 "github.com/annchain/OG/og/types" 30 ecdsabtcec "github.com/btcsuite/btcd/btcec" 31 log "github.com/sirupsen/logrus" 32 "testing" 33 "time" 34 ) 35 36 type SignerSecp256k1cgo struct { 37 } 38 39 func (s *SignerSecp256k1cgo) GetCryptoType() CryptoType { 40 return CryptoTypeSecp256k1 41 } 42 43 func (s *SignerSecp256k1cgo) Sign(privKey PrivateKey, msg []byte) Signature { 44 priv, _ := ogcrypto2.ToECDSA(privKey.KeyBytes) 45 hash := Sha256(msg) 46 if len(hash) != 32 { 47 log.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash)) 48 return Signature{} 49 } 50 seckey := math.PaddedBigBytes(priv.D, priv.Params().BitSize/8) 51 defer ogcrypto.zeroBytes(seckey) 52 sig, _ := secp256k1.Sign(hash, seckey) 53 54 return SignatureFromBytes(CryptoTypeSecp256k1, sig) 55 } 56 57 func (s *SignerSecp256k1cgo) PubKey(privKey PrivateKey) PublicKey { 58 _, ecdsapub := ecdsabtcec.PrivKeyFromBytes(ecdsabtcec.S256(), privKey.KeyBytes) 59 pub := ogcrypto2.FromECDSAPub((*ecdsa.PublicKey)(ecdsapub)) 60 return PublicKeyFromBytes(CryptoTypeSecp256k1, pub[:]) 61 } 62 63 func (s *SignerSecp256k1cgo) PublicKeyFromBytes(b []byte) PublicKey { 64 return PublicKeyFromBytes(s.GetCryptoType(), b) 65 } 66 67 func (s *SignerSecp256k1cgo) Verify(pubKey PublicKey, signature Signature, msg []byte) bool { 68 signature = s.DealRecoverID(signature) 69 sig := signature.SignatureBytes 70 return secp256k1.VerifySignature(pubKey.KeyBytes, Sha256(msg), sig) 71 } 72 73 func (s *SignerSecp256k1cgo) RandomKeyPair() (publicKey PublicKey, privateKey PrivateKey) { 74 privKeyBytes := [32]byte{} 75 copy(privKeyBytes[:], ogcrypto2.CRandBytes(32)) 76 77 privateKey = PrivateKeyFromBytes(CryptoTypeSecp256k1, privKeyBytes[:]) 78 publicKey = s.PubKey(privateKey) 79 return 80 } 81 82 func (s *SignerSecp256k1cgo) DealRecoverID(sig Signature) Signature { 83 l := len(sig.SignatureBytes) 84 if l == og_interface.sigLength+1 { 85 sig.SignatureBytes = sig.SignatureBytes[:l-1] 86 } 87 return sig 88 } 89 90 func TestSignerNewPrivKeyCGO(t *testing.T) { 91 t.Parallel() 92 signer := SignerSecp256k1cgo{} 93 signer2 := ogcrypto2.SignerSecp256k1{} 94 for i := 0; i < 10; i++ { 95 pk, priv := signer.RandomKeyPair() 96 //fmt.Println(priv.String()) 97 //fmt.Println(pk.String()) 98 b := []byte("foohhhhjkhhj3488984984984jjjdjsdjks") 99 sig := signer.Sign(priv, b) 100 if !signer.Verify(pk, sig, b) { 101 t.Fatalf("vertfy failed") 102 } 103 fmt.Println(hex.EncodeToString(sig.Bytes)) 104 105 sig2 := signer2.Sign(priv, b) 106 fmt.Println(hex.EncodeToString(sig2.SignatureBytes)) 107 if !signer2.Verify(pk, sig2, b) { 108 t.Fatalf("vertfy failed") 109 } 110 fmt.Println(" ", i) 111 } 112 113 } 114 115 func TestSignerNewPrivKeyCGOF(t *testing.T) { 116 t.Parallel() 117 signer := SignerSecp256k1cgo{} 118 signer2 := ogcrypto2.SignerSecp256k1{} 119 for i := 0; i < 1000; i++ { 120 pk, priv := signer.RandomKeyPair() 121 //fmt.Println(priv.String()) 122 //fmt.Println(pk.String()) 123 b := []byte("foohhhhjkhhj3488984984984jjjdjsdjks") 124 //sig := signer.Sign(priv, b) 125 //if !signer.Verify(pk, sig, b) { 126 // t.Fatalf("vertfy failed") 127 //} 128 //fmt.Println(hex.EncodeToString(sig.KeyBytes)) 129 130 sig2 := signer2.Sign(priv, b) 131 //fmt.Println(hex.EncodeToString(sig2.KeyBytes)) 132 if !signer2.Verify(pk, sig2, b) { 133 t.Fatalf("vertfy failed") 134 } 135 //fmt.Println(" ", i) 136 } 137 138 } 139 140 func TestSignBenchMarksCgo(t *testing.T) { 141 signer := SignerSecp256k1cgo{} 142 pk, priv := signer.RandomKeyPair() 143 signer2 := ogcrypto2.SignerSecp256k1{} 144 var txs1 types.Txis 145 var txs2 types.Txis 146 N := 10000 147 148 for i := 0; i < N; i++ { 149 txs1 = append(txs1, archive.RandomTx()) 150 } 151 for i := 0; i < N; i++ { 152 txs2 = append(txs2, archive.RandomTx()) 153 } 154 fmt.Println("started") 155 start := time.Now() 156 for i := 0; i < len(txs1); i++ { 157 b := txs1[i].SignatureTargets() 158 sig := signer.Sign(priv, b) 159 txs1[i].GetBase().Signature = sig.Bytes 160 //if !signer.Verify(pk, sig, b) { 161 // t.Fatalf("vertfy failed") 162 //} 163 } 164 fmt.Println("secp cgo used for signing ", time.Since(start)) 165 start = time.Now() 166 for i := 0; i < len(txs1); i++ { 167 b := txs1[i].SignatureTargets() 168 sig := signer2.Sign(priv, b) 169 txs2[i].GetBase().Signature = sig.SignatureBytes 170 //if !signer2.Verify(pk, sig, b) { 171 // t.Fatalf("vertfy failed") 172 //} 173 } 174 fmt.Println("secp go used for signing ", time.Since(start)) 175 start = time.Now() 176 for i := 0; i < len(txs2); i++ { 177 b := txs2[i].SignatureTargets() 178 sig := signer2.Sign(priv, b) 179 txs2[i].GetBase().Signature = sig.SignatureBytes 180 //if !signer2.Verify(pk, sig, b) { 181 // t.Fatalf("vertfy failed") 182 //} 183 } 184 fmt.Println("secp go used for signing ", time.Since(start)) 185 start = time.Now() 186 for i := 0; i < len(txs2); i++ { 187 b := txs2[i].SignatureTargets() 188 sig := signer.Sign(priv, b) 189 txs2[i].GetBase().Signature = sig.Bytes 190 //if !signer.Verify(pk, sig, b) { 191 // t.Fatalf("vertfy failed") 192 //} 193 } 194 fmt.Println("secp cgo used for signing ", time.Since(start)) 195 start = time.Now() 196 for i := 0; i < len(txs1); i++ { 197 b := txs1[i].SignatureTargets() 198 sig := txs1[i].GetBase().Signature 199 if !signer2.Verify(pk, SignatureFromBytes(CryptoTypeSecp256k1, sig), b) { 200 t.Fatalf("vertfy failed") 201 } 202 } 203 fmt.Println("secp go used for verifying ", time.Since(start)) 204 start = time.Now() 205 for i := 0; i < len(txs1); i++ { 206 b := txs1[i].SignatureTargets() 207 sig := txs1[i].GetBase().Signature 208 if !signer.Verify(pk, SignatureFromBytes(CryptoTypeSecp256k1, sig), b) { 209 t.Fatalf("vertfy failed") 210 } 211 } 212 fmt.Println("secp cgo used for verifying ", time.Since(start)) 213 start = time.Now() 214 for i := 0; i < len(txs1); i++ { 215 b := txs2[i].SignatureTargets() 216 sig := txs2[i].GetBase().Signature 217 if !signer.Verify(pk, SignatureFromBytes(CryptoTypeSecp256k1, sig), b) { 218 t.Fatalf("vertfy failed") 219 } 220 } 221 fmt.Println("secp cgo used for verifying ", time.Since(start)) 222 start = time.Now() 223 for i := 0; i < len(txs1); i++ { 224 b := txs1[i].SignatureTargets() 225 sig := txs1[i].GetBase().Signature 226 if !signer2.Verify(pk, SignatureFromBytes(CryptoTypeSecp256k1, sig), b) { 227 t.Fatalf("vertfy failed") 228 } 229 } 230 fmt.Println("secp go used for verifying ", time.Since(start)) 231 232 }