github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/crypto/secp256k1/secp256.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2015 Jeffrey Wilcke、Felix Lange、Gustav Simonsson。版权所有。
    10  //此源代码的使用受BSD样式许可证的控制,该许可证可在
    11  //许可证文件。
    12  
    13  //包secp256k1包装比特币secp256k1 c库。
    14  package secp256k1
    15  
    16  /*
    17  CGO cflags:-I./libsecp256k1
    18  cgo cflags:-i./libsecp256k1/src/
    19  定义使用编号无
    20  定义使用域10x26
    21  定义使用诳字段诳inv诳builtin
    22  定义“使用”标量8x32
    23  定义使用覕scalar覕inv覕builtin
    24  定义nDebug
    25  包括“./libsecp256k1/src/secp256k1.c”
    26  包括“./libsecp256k1/src/modules/recovery/main_impl.h”
    27  包括“ext.h”
    28  
    29  typedef void(*callbackfunc)(const char*msg,void*data);
    30  extern void secp256k1gopanicilllegal(const char*msg,void*data);
    31  extern void secp256k1gopanicerror(const char*msg,void*data);
    32  **/
    33  
    34  import "C"
    35  
    36  import (
    37  	"errors"
    38  	"math/big"
    39  	"unsafe"
    40  )
    41  
    42  var context *C.secp256k1_context
    43  
    44  func init() {
    45  //在现代CPU上大约20毫秒。
    46  	context = C.secp256k1_context_create_sign_verify()
    47  	C.secp256k1_context_set_illegal_callback(context, C.callbackFunc(C.secp256k1GoPanicIllegal), nil)
    48  	C.secp256k1_context_set_error_callback(context, C.callbackFunc(C.secp256k1GoPanicError), nil)
    49  }
    50  
    51  var (
    52  	ErrInvalidMsgLen       = errors.New("invalid message length, need 32 bytes")
    53  	ErrInvalidSignatureLen = errors.New("invalid signature length")
    54  	ErrInvalidRecoveryID   = errors.New("invalid signature recovery id")
    55  	ErrInvalidKey          = errors.New("invalid private key")
    56  	ErrInvalidPubkey       = errors.New("invalid public key")
    57  	ErrSignFailed          = errors.New("signing failed")
    58  	ErrRecoverFailed       = errors.New("recovery failed")
    59  )
    60  
    61  //签名创建可恢复的ECDSA签名。
    62  //生成的签名采用65字节的格式,其中v为0或1。
    63  //
    64  //调用者负责确保不能选择消息
    65  //由攻击者直接攻击。通常最好使用密码
    66  //在将任何输入传递给此函数之前,对其执行哈希函数。
    67  func Sign(msg []byte, seckey []byte) ([]byte, error) {
    68  	if len(msg) != 32 {
    69  		return nil, ErrInvalidMsgLen
    70  	}
    71  	if len(seckey) != 32 {
    72  		return nil, ErrInvalidKey
    73  	}
    74  	seckeydata := (*C.uchar)(unsafe.Pointer(&seckey[0]))
    75  	if C.secp256k1_ec_seckey_verify(context, seckeydata) != 1 {
    76  		return nil, ErrInvalidKey
    77  	}
    78  
    79  	var (
    80  		msgdata   = (*C.uchar)(unsafe.Pointer(&msg[0]))
    81  		noncefunc = C.secp256k1_nonce_function_rfc6979
    82  		sigstruct C.secp256k1_ecdsa_recoverable_signature
    83  	)
    84  	if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, nil) == 0 {
    85  		return nil, ErrSignFailed
    86  	}
    87  
    88  	var (
    89  		sig     = make([]byte, 65)
    90  		sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
    91  		recid   C.int
    92  	)
    93  	C.secp256k1_ecdsa_recoverable_signature_serialize_compact(context, sigdata, &recid, &sigstruct)
    94  sig[64] = byte(recid) //加回recid得到65字节sig
    95  	return sig, nil
    96  }
    97  
    98  //CurrestPUBKEY返回签名者的公钥。
    99  //msg必须是要签名的消息的32字节哈希。
   100  //SIG必须是包含
   101  //作为最后一个元素的恢复ID。
   102  func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) {
   103  	if len(msg) != 32 {
   104  		return nil, ErrInvalidMsgLen
   105  	}
   106  	if err := checkSignature(sig); err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	var (
   111  		pubkey  = make([]byte, 65)
   112  		sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
   113  		msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
   114  	)
   115  	if C.secp256k1_ext_ecdsa_recover(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
   116  		return nil, ErrRecoverFailed
   117  	}
   118  	return pubkey, nil
   119  }
   120  
   121  //验证签名检查给定的PUBKEY在消息上创建签名。
   122  //签名应采用[R S]格式。
   123  func VerifySignature(pubkey, msg, signature []byte) bool {
   124  	if len(msg) != 32 || len(signature) != 64 || len(pubkey) == 0 {
   125  		return false
   126  	}
   127  	sigdata := (*C.uchar)(unsafe.Pointer(&signature[0]))
   128  	msgdata := (*C.uchar)(unsafe.Pointer(&msg[0]))
   129  	keydata := (*C.uchar)(unsafe.Pointer(&pubkey[0]))
   130  	return C.secp256k1_ext_ecdsa_verify(context, sigdata, msgdata, keydata, C.size_t(len(pubkey))) != 0
   131  }
   132  
   133  //解压缩PubKey以33字节的压缩格式解析公钥。
   134  //如果公钥有效,则返回非零坐标。
   135  func DecompressPubkey(pubkey []byte) (x, y *big.Int) {
   136  	if len(pubkey) != 33 {
   137  		return nil, nil
   138  	}
   139  	var (
   140  		pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
   141  		pubkeylen  = C.size_t(len(pubkey))
   142  		out        = make([]byte, 65)
   143  		outdata    = (*C.uchar)(unsafe.Pointer(&out[0]))
   144  		outlen     = C.size_t(len(out))
   145  	)
   146  	if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
   147  		return nil, nil
   148  	}
   149  	return new(big.Int).SetBytes(out[1:33]), new(big.Int).SetBytes(out[33:])
   150  }
   151  
   152  //compresspubkey将公钥编码为33字节的压缩格式。
   153  func CompressPubkey(x, y *big.Int) []byte {
   154  	var (
   155  		pubkey     = S256().Marshal(x, y)
   156  		pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
   157  		pubkeylen  = C.size_t(len(pubkey))
   158  		out        = make([]byte, 33)
   159  		outdata    = (*C.uchar)(unsafe.Pointer(&out[0]))
   160  		outlen     = C.size_t(len(out))
   161  	)
   162  	if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
   163  		panic("libsecp256k1 error")
   164  	}
   165  	return out
   166  }
   167  
   168  func checkSignature(sig []byte) error {
   169  	if len(sig) != 65 {
   170  		return ErrInvalidSignatureLen
   171  	}
   172  	if sig[64] >= 4 {
   173  		return ErrInvalidRecoveryID
   174  	}
   175  	return nil
   176  }