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