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