github.com/annchain/OG@v0.0.9/deprecated/ogcrypto_interface/crypto_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  package ogcrypto_interface
    15  
    16  import (
    17  	"bytes"
    18  	"crypto/ecdsa"
    19  	"crypto/rand"
    20  	"encoding/hex"
    21  	"fmt"
    22  	"github.com/annchain/OG/deprecated"
    23  	ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto"
    24  	"github.com/annchain/OG/deprecated/ogcrypto/ecies"
    25  
    26  	"math/big"
    27  	"testing"
    28  )
    29  
    30  func NewSigner(cryptoType CryptoType) ISigner {
    31  	if cryptoType == CryptoTypeEd25519 {
    32  		return &ogcrypto2.SignerEd25519{}
    33  	} else if cryptoType == CryptoTypeSecp256k1 {
    34  		return &ogcrypto2.SignerSecp256k1{}
    35  	}
    36  	return nil
    37  }
    38  
    39  func TestPublicKey_Encrypt(t *testing.T) {
    40  	for i := 0; i < 2; i++ {
    41  		cType := CryptoType(i)
    42  		fmt.Println(cType)
    43  		s := NewSigner(cType)
    44  		pk, sk := s.RandomKeyPair()
    45  		fmt.Println("pk", pk.String())
    46  		fmt.Println("sk", sk.String())
    47  		msg := []byte("hello og  this is a secret msg , no one knows wipkhfdii75438048584653543543skj76895804iri4356345h" +
    48  			"ufidurehfkkjfri566878798y5rejiodijfjioi;454646855455uiyrsduihfi54sdodoootoprew5468rre")
    49  		fmt.Println(string(msg))
    50  		fmt.Println(len(msg))
    51  		cf, err := s.Encrypt(pk, msg)
    52  		fmt.Println(len(cf), hex.EncodeToString(cf), err)
    53  		m, err := s.Decrypt(sk, cf)
    54  		fmt.Println(string(m), err)
    55  		if !bytes.Equal(m, msg) {
    56  			t.Fatal("encrypt or decrypt error")
    57  		}
    58  	}
    59  }
    60  
    61  func TestPrivateKey_Decrypt(t *testing.T) {
    62  	x, _ := big.NewInt(0).SetString("25860957663402118420385219759979651311493566861420536707157658171589998503027", 0)
    63  	y, _ := big.NewInt(0).SetString("86240112990591864153796259798532127250481760794327649797956296817696385215375", 0)
    64  	D, _ := big.NewInt(0).SetString("112339918029554102382967166016087130388477159317095521880422462723592214463164", 0)
    65  	ecdsapub := ecdsa.PublicKey{
    66  		Curve: ogcrypto2.S256(),
    67  		X:     x,
    68  		Y:     y,
    69  	}
    70  	ecdsapriv := ecdsa.PrivateKey{ecdsapub, D}
    71  	fmt.Println(ecdsapriv)
    72  	pk := deprecated.PublicKeyFromBytes(CryptoTypeSecp256k1, ogcrypto2.FromECDSAPub(&ecdsapub))
    73  	sk := deprecated.PrivateKeyFromBytes(CryptoTypeSecp256k1, ogcrypto2.FromECDSA(&ecdsapriv))
    74  
    75  	//priv:= PrivateKey{PublicKey{}}
    76  	msg := []byte("hello og  this is a secret msg , no one knows wipkhfdii75438048584653543543skj76895804iri4356345h" +
    77  		"ufidurehfkkjfri566878798y5rejiodijfjioi;454646855455uiyrsduihfi54sdodoootoprew5468rre")
    78  	pub2, err := ogcrypto2.UnmarshalPubkey(pk.KeyBytes)
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	if ecdsapub.X.Cmp(pub2.X) != 0 || ecdsapub.Y.Cmp(pub2.Y) != 0 {
    83  		fmt.Println(*pub2)
    84  		fmt.Println(ecdsapub)
    85  		panic("not equal")
    86  	}
    87  	eciesPub := ecies.ImportECDSAPublic(pub2)
    88  	cf, err := ecies.Encrypt(rand.Reader, eciesPub, msg, nil, nil)
    89  	//cf, err := pk.Encrypt(msg)
    90  	fmt.Println(len(cf), hex.EncodeToString(cf), err)
    91  	prive, err := ogcrypto2.ToECDSA(sk.KeyBytes)
    92  	fmt.Println(prive)
    93  	ecisesPriv := ecies.ImportECDSA(prive)
    94  	fmt.Println(ecisesPriv)
    95  	m, err := ecisesPriv.Decrypt(cf, nil, nil)
    96  	//m ,err:= sk.Decrypt(cf)
    97  	fmt.Println(string(m), err)
    98  	if err != nil || !bytes.Equal(m, msg) {
    99  		t.Fatal("encrypt or decrypt error", err)
   100  	}
   101  }
   102  
   103  //tese signer benchmarks
   104  //func TestBenchMarks(t *testing.T) {
   105  //	type TestTx struct {
   106  //		types.Txi
   107  //		PlainData  []byte
   108  //		CipherData []byte
   109  //	}
   110  //	var txs []*TestTx
   111  //	var Txlen = 40000
   112  //	for i := 0; i < Txlen; i++ {
   113  //		tx := archive.RandomTx()
   114  //		tx.Data = []byte("jhfffhhgfhgf46666856544563544535636568654864546546ewfjnfdjlfjldkjkflkjflkdsl;kfdfkjjkfsd;lsdl;kdfl;kjfjfsj;sd54645656854545435454")
   115  //		testTx := TestTx{tx, tx.SignatureTargets(), nil}
   116  //		txs = append(txs, &testTx)
   117  //	}
   118  //	signerSecp := NewSigner(CryptoTypeSecp256k1)
   119  //	pubkey, privKey := signerSecp.RandomKeyPair()
   120  //	signerEd := NewSigner(CryptoTypeEd25519)
   121  //	pubEd, privEd := signerEd.RandomKeyPair()
   122  //	start := time.Now()
   123  //	fmt.Println("number of tx ", Txlen)
   124  //	for i, tx := range txs {
   125  //		txs[i].GetBase().Signature = signerSecp.Sign(privKey, tx.PlainData).SignatureBytes
   126  //	}
   127  //	second := time.Now()
   128  //	fmt.Println(second.Sub(start), "for  secp256k1 sign", " pubKeyLen:", len(pubkey.KeyBytes), "len signatue:",
   129  //		len(txs[0].GetBase().Signature), "len target:", len(txs[0].PlainData))
   130  //	for i, tx := range txs {
   131  //		ok := signerSecp.Verify(pubkey, Signature{signerSecp.GetCryptoType(), tx.GetBase().Signature}, tx.PlainData)
   132  //		if !ok {
   133  //			t.Fatal(ok, i, tx)
   134  //		}
   135  //	}
   136  //	third := time.Now()
   137  //	fmt.Println(third.Sub(second), "used for secp256k1 verify")
   138  //
   139  //	for i, tx := range txs {
   140  //		txs[i].GetBase().Signature = signerEd.Sign(privEd, tx.PlainData).SignatureBytes
   141  //	}
   142  //	four := time.Now()
   143  //	fmt.Println(four.Sub(third), "for ed25519 sign", " pubKeyLen:", len(pubEd.KeyBytes), "len signatue:",
   144  //		len(txs[0].GetBase().Signature), "len target:", len(txs[0].PlainData))
   145  //	for i, tx := range txs {
   146  //		ok := signerEd.Verify(pubEd, Signature{signerEd.GetCryptoType(), tx.GetBase().Signature}, tx.PlainData)
   147  //		if !ok {
   148  //			t.Fatal(ok, i, tx)
   149  //		}
   150  //	}
   151  //	five := time.Now()
   152  //	fmt.Println(five.Sub(four), "used for ed25519 verify")
   153  //
   154  //	for i, tx := range txs {
   155  //		var err error
   156  //		txs[i].CipherData, err = signerSecp.Encrypt(pubkey, tx.PlainData)
   157  //		if err != nil {
   158  //			t.Fatal(err, i)
   159  //		}
   160  //	}
   161  //	fmt.Println()
   162  //	six := time.Now()
   163  //	fmt.Println(six.Sub(five), "for secp256k1 enc", "len cipherdata:", len(txs[0].CipherData), "len plaindata:", len(txs[0].PlainData))
   164  //	for i, tx := range txs {
   165  //		d, err := signerSecp.Decrypt(privKey, tx.CipherData)
   166  //		if err != nil {
   167  //			t.Fatal(err, i, tx)
   168  //		}
   169  //		if !bytes.Equal(d, tx.PlainData) {
   170  //			t.Fatal(fmt.Sprintf("secp dec error , i %d got %v, want %v", i, d, tx.PlainData))
   171  //		}
   172  //	}
   173  //	seven := time.Now()
   174  //	fmt.Println(seven.Sub(six), "for secp256k1 dec")
   175  //	for i, tx := range txs {
   176  //		var err error
   177  //		txs[i].CipherData, err = signerEd.Encrypt(pubEd, tx.PlainData)
   178  //		if err != nil {
   179  //			t.Fatal(err, i)
   180  //		}
   181  //	}
   182  //	eight := time.Now()
   183  //	kyberKey := privEd.ToKyberEd25519PrivKey()
   184  //	fmt.Println(eight.Sub(seven), "for ed25519 enc", "len cipherdata:", len(txs[0].CipherData), "len plaindata:", len(txs[0].PlainData))
   185  //	for i, tx := range txs {
   186  //		d, err := kyberKey.Decrypt(tx.CipherData)
   187  //		if err != nil {
   188  //			t.Fatal(err, i, tx)
   189  //		}
   190  //		if !bytes.Equal(d, tx.PlainData) {
   191  //			t.Fatal(fmt.Sprintf("ed25519 dec error , i %d got %v, want %v", i, d, tx.PlainData))
   192  //		}
   193  //
   194  //	}
   195  //	nine := time.Now()
   196  //	fmt.Println(nine.Sub(eight), "for ed25519 dec")
   197  //}
   198  
   199  //=== RUN   TestBenchMarks
   200  //number of tx  40000
   201  //12.62s for  secp256k1 sign  pubKeyLen: 65 len signatue: 65 len target: 185
   202  //11.212s used for secp256k1 verify
   203  //4.433s for ed25519 sign  pubKeyLen: 32 len signatue: 64 len target: 185
   204  //11.887s used for ed25519 verify
   205  //
   206  //15.905s for secp256k1 enc len cipherdata: 298 len plaindata: 185
   207  //15.72s for secp256k1 dec
   208  //22.045s for ed25519 enc len cipherdata: 233 len plaindata: 185
   209  //11.402s for ed25519 dec
   210  //--- PASS: TestBenchMarks (105.89s)
   211  //PASS