github.com/theQRL/go-zond@v0.1.1/crypto/secp256k1/secp256.go (about)

     1  // Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  //go:build !gofuzz && cgo
     6  // +build !gofuzz,cgo
     7  
     8  // Package secp256k1 wraps the bitcoin secp256k1 C library.
     9  package secp256k1
    10  
    11  /*
    12  #cgo CFLAGS: -I./libsecp256k1
    13  #cgo CFLAGS: -I./libsecp256k1/src/
    14  
    15  #ifdef __SIZEOF_INT128__
    16  #  define HAVE___INT128
    17  #  define USE_FIELD_5X52
    18  #  define USE_SCALAR_4X64
    19  #else
    20  #  define USE_FIELD_10X26
    21  #  define USE_SCALAR_8X32
    22  #endif
    23  
    24  #ifndef NDEBUG
    25  #  define NDEBUG
    26  #endif
    27  
    28  #define USE_ENDOMORPHISM
    29  #define USE_NUM_NONE
    30  #define USE_FIELD_INV_BUILTIN
    31  #define USE_SCALAR_INV_BUILTIN
    32  #include "./libsecp256k1/src/secp256k1.c"
    33  #include "./libsecp256k1/src/modules/recovery/main_impl.h"
    34  #include "ext.h"
    35  
    36  typedef void (*callbackFunc) (const char* msg, void* data);
    37  extern void secp256k1GoPanicIllegal(const char* msg, void* data);
    38  extern void secp256k1GoPanicError(const char* msg, void* data);
    39  */
    40  import "C"
    41  
    42  import (
    43  	"errors"
    44  	"math/big"
    45  	"unsafe"
    46  )
    47  
    48  var context *C.secp256k1_context
    49  
    50  func init() {
    51  	// around 20 ms on a modern CPU.
    52  	context = C.secp256k1_context_create_sign_verify()
    53  	C.secp256k1_context_set_illegal_callback(context, C.callbackFunc(C.secp256k1GoPanicIllegal), nil)
    54  	C.secp256k1_context_set_error_callback(context, C.callbackFunc(C.secp256k1GoPanicError), nil)
    55  }
    56  
    57  var (
    58  	ErrInvalidMsgLen       = errors.New("invalid message length, need 32 bytes")
    59  	ErrInvalidSignatureLen = errors.New("invalid signature length")
    60  	ErrInvalidRecoveryID   = errors.New("invalid signature recovery id")
    61  	ErrInvalidKey          = errors.New("invalid private key")
    62  	ErrInvalidPubkey       = errors.New("invalid public key")
    63  	ErrSignFailed          = errors.New("signing failed")
    64  	ErrRecoverFailed       = errors.New("recovery failed")
    65  )
    66  
    67  // Sign creates a recoverable ECDSA signature.
    68  // The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1.
    69  //
    70  // The caller is responsible for ensuring that msg cannot be chosen
    71  // directly by an attacker. It is usually preferable to use a cryptographic
    72  // hash function on any input before handing it to this function.
    73  func Sign(msg []byte, seckey []byte) ([]byte, error) {
    74  	if len(msg) != 32 {
    75  		return nil, ErrInvalidMsgLen
    76  	}
    77  	if len(seckey) != 32 {
    78  		return nil, ErrInvalidKey
    79  	}
    80  	seckeydata := (*C.uchar)(unsafe.Pointer(&seckey[0]))
    81  	if C.secp256k1_ec_seckey_verify(context, seckeydata) != 1 {
    82  		return nil, ErrInvalidKey
    83  	}
    84  
    85  	var (
    86  		msgdata   = (*C.uchar)(unsafe.Pointer(&msg[0]))
    87  		noncefunc = C.secp256k1_nonce_function_rfc6979
    88  		sigstruct C.secp256k1_ecdsa_recoverable_signature
    89  	)
    90  	if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, nil) == 0 {
    91  		return nil, ErrSignFailed
    92  	}
    93  
    94  	var (
    95  		sig     = make([]byte, 65)
    96  		sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
    97  		recid   C.int
    98  	)
    99  	C.secp256k1_ecdsa_recoverable_signature_serialize_compact(context, sigdata, &recid, &sigstruct)
   100  	sig[64] = byte(recid) // add back recid to get 65 bytes sig
   101  	return sig, nil
   102  }
   103  
   104  // RecoverPubkey returns the public key of the signer.
   105  // msg must be the 32-byte hash of the message to be signed.
   106  // sig must be a 65-byte compact ECDSA signature containing the
   107  // recovery id as the last element.
   108  func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) {
   109  	if len(msg) != 32 {
   110  		return nil, ErrInvalidMsgLen
   111  	}
   112  	if err := checkSignature(sig); err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	var (
   117  		pubkey  = make([]byte, 65)
   118  		sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
   119  		msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
   120  	)
   121  	if C.secp256k1_ext_ecdsa_recover(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
   122  		return nil, ErrRecoverFailed
   123  	}
   124  	return pubkey, nil
   125  }
   126  
   127  // VerifySignature checks that the given pubkey created signature over message.
   128  // The signature should be in [R || S] format.
   129  func VerifySignature(pubkey, msg, signature []byte) bool {
   130  	if len(msg) != 32 || len(signature) != 64 || len(pubkey) == 0 {
   131  		return false
   132  	}
   133  	sigdata := (*C.uchar)(unsafe.Pointer(&signature[0]))
   134  	msgdata := (*C.uchar)(unsafe.Pointer(&msg[0]))
   135  	keydata := (*C.uchar)(unsafe.Pointer(&pubkey[0]))
   136  	return C.secp256k1_ext_ecdsa_verify(context, sigdata, msgdata, keydata, C.size_t(len(pubkey))) != 0
   137  }
   138  
   139  // DecompressPubkey parses a public key in the 33-byte compressed format.
   140  // It returns non-nil coordinates if the public key is valid.
   141  func DecompressPubkey(pubkey []byte) (x, y *big.Int) {
   142  	if len(pubkey) != 33 {
   143  		return nil, nil
   144  	}
   145  	var (
   146  		pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
   147  		pubkeylen  = C.size_t(len(pubkey))
   148  		out        = make([]byte, 65)
   149  		outdata    = (*C.uchar)(unsafe.Pointer(&out[0]))
   150  		outlen     = C.size_t(len(out))
   151  	)
   152  	if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
   153  		return nil, nil
   154  	}
   155  	return new(big.Int).SetBytes(out[1:33]), new(big.Int).SetBytes(out[33:])
   156  }
   157  
   158  // CompressPubkey encodes a public key to 33-byte compressed format.
   159  func CompressPubkey(x, y *big.Int) []byte {
   160  	var (
   161  		pubkey     = S256().Marshal(x, y)
   162  		pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
   163  		pubkeylen  = C.size_t(len(pubkey))
   164  		out        = make([]byte, 33)
   165  		outdata    = (*C.uchar)(unsafe.Pointer(&out[0]))
   166  		outlen     = C.size_t(len(out))
   167  	)
   168  	if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
   169  		panic("libsecp256k1 error")
   170  	}
   171  	return out
   172  }
   173  
   174  func checkSignature(sig []byte) error {
   175  	if len(sig) != 65 {
   176  		return ErrInvalidSignatureLen
   177  	}
   178  	if sig[64] >= 4 {
   179  		return ErrInvalidRecoveryID
   180  	}
   181  	return nil
   182  }