secp256.go 5.9 KB

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