github.com/annchain/OG@v0.0.9/deprecated/ogcrypto/signer_secp256k1.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 ogcrypto
    17  
    18  import (
    19  	"crypto/ecdsa"
    20  	"crypto/rand"
    21  	"fmt"
    22  	"github.com/annchain/OG/arefactor/common/math"
    23  	"github.com/annchain/OG/arefactor/ogcrypto"
    24  	"github.com/annchain/OG/deprecated"
    25  	"github.com/annchain/OG/deprecated/ogcrypto/ecies"
    26  	"github.com/annchain/OG/deprecated/ogcrypto/secp256k1"
    27  	"github.com/annchain/OG/deprecated/ogcrypto_interface"
    28  	ecdsabtcec "github.com/btcsuite/btcd/btcec"
    29  	"github.com/sirupsen/logrus"
    30  )
    31  
    32  type SignerSecp256k1 struct {
    33  }
    34  
    35  func (s *SignerSecp256k1) GetCryptoType() ogcrypto_interface.CryptoType {
    36  	return ogcrypto_interface.CryptoTypeSecp256k1
    37  }
    38  
    39  func (s *SignerSecp256k1) Sign(privKey ogcrypto_interface.PrivateKey, msg []byte) ogcrypto_interface.Signature {
    40  	priv, err := ToECDSA(privKey.KeyBytes)
    41  	if err != nil {
    42  		fmt.Println(fmt.Sprintf("ToECDSA error: %v. priv bytes: %x", err, privKey.KeyBytes))
    43  	}
    44  	hash := deprecated.Sha256(msg)
    45  	if len(hash) != 32 {
    46  		logrus.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
    47  		return ogcrypto_interface.Signature{}
    48  	}
    49  	seckey := math.PaddedBigBytes(priv.D, priv.Params().BitSize/8)
    50  	defer zeroBytes(seckey)
    51  	sig, _ := secp256k1.Sign(hash, seckey)
    52  
    53  	return deprecated.SignatureFromBytes(ogcrypto_interface.CryptoTypeSecp256k1, sig)
    54  }
    55  
    56  func (s *SignerSecp256k1) PubKey(privKey ogcrypto_interface.PrivateKey) ogcrypto_interface.PublicKey {
    57  	_, ecdsapub := ecdsabtcec.PrivKeyFromBytes(ecdsabtcec.S256(), privKey.KeyBytes)
    58  	pub := FromECDSAPub((*ecdsa.PublicKey)(ecdsapub))
    59  	return deprecated.PublicKeyFromBytes(ogcrypto_interface.CryptoTypeSecp256k1, pub[:])
    60  }
    61  
    62  func (s *SignerSecp256k1) PublicKeyFromBytes(b []byte) ogcrypto_interface.PublicKey {
    63  	return deprecated.PublicKeyFromBytes(s.GetCryptoType(), b)
    64  }
    65  
    66  func (s *SignerSecp256k1) Verify(pubKey ogcrypto_interface.PublicKey, signature ogcrypto_interface.Signature, msg []byte) bool {
    67  	signature = s.DealRecoverID(signature)
    68  	sig := signature.SignatureBytes
    69  
    70  	//fmt.Println(fmt.Sprintf("pubkey bytes: %x", pubKey.KeyBytes))
    71  	//fmt.Println(fmt.Sprintf("msg: %x", msg))
    72  	//fmt.Println(fmt.Sprintf("sig: %x", sig))
    73  
    74  	return secp256k1.VerifySignature(pubKey.KeyBytes, deprecated.Sha256(msg), sig)
    75  }
    76  
    77  func (s *SignerSecp256k1) RandomKeyPair() (publicKey ogcrypto_interface.PublicKey, privateKey ogcrypto_interface.PrivateKey) {
    78  	privKeyBytes := [32]byte{}
    79  	copy(privKeyBytes[:], ogcrypto.CRandBytes(32))
    80  
    81  	privateKey = deprecated.PrivateKeyFromBytes(ogcrypto_interface.CryptoTypeSecp256k1, privKeyBytes[:])
    82  	publicKey = s.PubKey(privateKey)
    83  	return
    84  }
    85  
    86  func (s *SignerSecp256k1) Encrypt(p ogcrypto_interface.PublicKey, m []byte) (ct []byte, err error) {
    87  	pub, err := UnmarshalPubkey(p.KeyBytes)
    88  	if err != nil {
    89  		panic(err)
    90  	}
    91  	eciesPub := ecies.ImportECDSAPublic(pub)
    92  	return ecies.Encrypt(rand.Reader, eciesPub, m, nil, nil)
    93  }
    94  
    95  func (s *SignerSecp256k1) Decrypt(p ogcrypto_interface.PrivateKey, ct []byte) (m []byte, err error) {
    96  	prive, err := ToECDSA(p.KeyBytes)
    97  	ecisesPriv := ecies.ImportECDSA(prive)
    98  	return ecisesPriv.Decrypt(ct, nil, nil)
    99  }
   100  
   101  const sigLength int = 64
   102  
   103  func (s *SignerSecp256k1) DealRecoverID(sig ogcrypto_interface.Signature) ogcrypto_interface.Signature {
   104  	l := len(sig.SignatureBytes)
   105  	if l == sigLength+1 {
   106  		sig.SignatureBytes = sig.SignatureBytes[:l-1]
   107  	}
   108  	return sig
   109  }
   110  
   111  // Ecrecover returns the uncompressed public key that created the given signature.
   112  func Ecrecover(hash, sig []byte) ([]byte, error) {
   113  	return secp256k1.RecoverPubkey(hash, sig)
   114  }
   115  
   116  //// SigToPub returns the public key that created the given signature.
   117  //func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
   118  //	s, err := Ecrecover(hash, sig)
   119  //	if err != nil {
   120  //		return nil, err
   121  //	}
   122  //
   123  //	x, y := elliptic.Unmarshal(S256(), s)
   124  //	return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}, nil
   125  //}
   126  
   127  func (s *SignerSecp256k1) CanRecoverPubFromSig() bool {
   128  	//return true
   129  	return false
   130  }