github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/crypto/signature_cgo.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  //go:build !nacl && !js && cgo && !gofuzz
    18  // +build !nacl,!js,cgo,!gofuzz
    19  
    20  package crypto
    21  
    22  import (
    23  	"crypto/ecdsa"
    24  	"errors"
    25  	"fmt"
    26  
    27  	"github.com/ethereum/go-ethereum/common/math"
    28  	"github.com/ethereum/go-ethereum/crypto/secp256k1"
    29  )
    30  
    31  // Ecrecover returns the uncompressed public key that created the given signature.
    32  func Ecrecover(hash, sig []byte) ([]byte, error) {
    33  	return secp256k1.RecoverPubkey(hash, sig)
    34  }
    35  
    36  // SigToPub returns the public key that created the given signature.
    37  func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
    38  	s, err := Ecrecover(hash, sig)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	return UnmarshalPubkey(s)
    43  }
    44  
    45  // Sign calculates an ECDSA signature.
    46  //
    47  // This function is susceptible to chosen plaintext attacks that can leak
    48  // information about the private key that is used for signing. Callers must
    49  // be aware that the given digest cannot be chosen by an adversary. Common
    50  // solution is to hash any input before calculating the signature.
    51  //
    52  // The produced signature is in the [R || S || V] format where V is 0 or 1.
    53  func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
    54  	if len(digestHash) != DigestLength {
    55  		return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(digestHash))
    56  	}
    57  	seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8)
    58  	defer zeroBytes(seckey)
    59  	return secp256k1.Sign(digestHash, seckey)
    60  }
    61  
    62  // VerifySignature checks that the given public key created signature over digest.
    63  // The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
    64  // The signature should have the 64 byte [R || S] format.
    65  func VerifySignature(pubkey, digestHash, signature []byte) bool {
    66  	return secp256k1.VerifySignature(pubkey, digestHash, signature)
    67  }
    68  
    69  // DecompressPubkey parses a public key in the 33-byte compressed format.
    70  func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
    71  	x, y := secp256k1.DecompressPubkey(pubkey)
    72  	if x == nil {
    73  		return nil, errors.New("invalid public key")
    74  	}
    75  	return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil
    76  }
    77  
    78  // CompressPubkey encodes a public key to the 33-byte compressed format.
    79  func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
    80  	return secp256k1.CompressPubkey(pubkey.X, pubkey.Y)
    81  }
    82  
    83  // S256 returns an instance of the secp256k1 curve.
    84  func S256() EllipticCurve {
    85  	return secp256k1.S256()
    86  }