github.com/amazechain/amc@v0.1.3/common/crypto/signature_cgo.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  //go:build !nacl && !js && cgo && !gofuzz
    18  
    19  package crypto
    20  
    21  import (
    22  	"crypto/ecdsa"
    23  	"crypto/elliptic"
    24  	"fmt"
    25  
    26  	"github.com/amazechain/amc/common/math"
    27  	"github.com/ledgerwatch/secp256k1"
    28  )
    29  
    30  // Ecrecover returns the uncompressed public key that created the given signature.
    31  func Ecrecover(hash, sig []byte) ([]byte, error) {
    32  	return secp256k1.RecoverPubkey(hash, sig)
    33  }
    34  
    35  // Ecrecover returns the uncompressed public key that created the given signature.
    36  func EcrecoverWithContext(context *secp256k1.Context, hash, sig []byte) ([]byte, error) {
    37  	return secp256k1.RecoverPubkeyWithContext(context, hash, sig, nil)
    38  }
    39  
    40  // SigToPub returns the public key that created the given signature.
    41  func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
    42  	s, err := Ecrecover(hash, sig)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	x, y := elliptic.Unmarshal(S256(), s)
    48  	return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}, nil
    49  }
    50  
    51  // Sign calculates an ECDSA signature.
    52  //
    53  // This function is susceptible to chosen plaintext attacks that can leak
    54  // information about the private key that is used for signing. Callers must
    55  // be aware that the given digest cannot be chosen by an adversery. Common
    56  // solution is to hash any input before calculating the signature.
    57  //
    58  // The produced signature is in the [R || S || V] format where V is 0 or 1.
    59  func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
    60  	if len(digestHash) != DigestLength {
    61  		return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(digestHash))
    62  	}
    63  	seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8)
    64  	defer zeroBytes(seckey)
    65  	return secp256k1.Sign(digestHash, seckey)
    66  }
    67  
    68  // VerifySignature checks that the given public key created signature over digest.
    69  // The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
    70  // The signature should have the 64 byte [R || S] format.
    71  func VerifySignature(pubkey, digestHash, signature []byte) bool {
    72  	return secp256k1.VerifySignature(pubkey, digestHash, signature)
    73  }
    74  
    75  // DecompressPubkey parses a public key in the 33-byte compressed format.
    76  func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
    77  	x, y := secp256k1.DecompressPubkey(pubkey)
    78  	if x == nil {
    79  		return nil, fmt.Errorf("invalid public key")
    80  	}
    81  	return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil
    82  }
    83  
    84  // CompressPubkey encodes a public key to the 33-byte compressed format.
    85  func CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
    86  	return secp256k1.CompressPubkey(pubkey.X, pubkey.Y)
    87  }
    88  
    89  // S256 returns an instance of the secp256k1 curve.
    90  func S256() elliptic.Curve {
    91  	return secp256k1.S256()
    92  }