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