secp256.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. //go:build !gofuzz && cgo
  5. // +build !gofuzz,cgo
  6. // Package secp256k1 wraps the bitcoin secp256k1 C library.
  7. package secp256k1
  8. /*
  9. #cgo CFLAGS: -I./libsecp256k1
  10. #cgo CFLAGS: -I./libsecp256k1/src/
  11. #ifdef __SIZEOF_INT128__
  12. # define HAVE___INT128
  13. # define USE_FIELD_5X52
  14. # define USE_SCALAR_4X64
  15. #else
  16. # define USE_FIELD_10X26
  17. # define USE_SCALAR_8X32
  18. #endif
  19. #define USE_ENDOMORPHISM
  20. #define USE_NUM_NONE
  21. #define USE_FIELD_INV_BUILTIN
  22. #define USE_SCALAR_INV_BUILTIN
  23. #define NDEBUG
  24. #include "./libsecp256k1/src/secp256k1.c"
  25. #include "./libsecp256k1/src/modules/recovery/main_impl.h"
  26. #include "ext.h"
  27. typedef void (*callbackFunc) (const char* msg, void* data);
  28. extern void secp256k1GoPanicIllegal(const char* msg, void* data);
  29. extern void secp256k1GoPanicError(const char* msg, void* data);
  30. */
  31. import "C"
  32. import (
  33. "errors"
  34. "math/big"
  35. "unsafe"
  36. )
  37. var context *C.secp256k1_context
  38. func init() {
  39. // around 20 ms on a modern CPU.
  40. context = C.secp256k1_context_create_sign_verify()
  41. C.secp256k1_context_set_illegal_callback(context, C.callbackFunc(C.secp256k1GoPanicIllegal), nil)
  42. C.secp256k1_context_set_error_callback(context, C.callbackFunc(C.secp256k1GoPanicError), nil)
  43. }
  44. var (
  45. ErrInvalidMsgLen = errors.New("invalid message length, need 32 bytes")
  46. ErrInvalidSignatureLen = errors.New("invalid signature length")
  47. ErrInvalidRecoveryID = errors.New("invalid signature recovery id")
  48. ErrInvalidKey = errors.New("invalid private key")
  49. ErrInvalidPubkey = errors.New("invalid public key")
  50. ErrSignFailed = errors.New("signing failed")
  51. ErrRecoverFailed = errors.New("recovery failed")
  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. var (
  71. msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
  72. noncefunc = C.secp256k1_nonce_function_rfc6979
  73. sigstruct C.secp256k1_ecdsa_recoverable_signature
  74. )
  75. if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, nil) == 0 {
  76. return nil, ErrSignFailed
  77. }
  78. var (
  79. sig = make([]byte, 65)
  80. sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
  81. recid C.int
  82. )
  83. C.secp256k1_ecdsa_recoverable_signature_serialize_compact(context, sigdata, &recid, &sigstruct)
  84. sig[64] = byte(recid) // add back recid to get 65 bytes sig
  85. return sig, nil
  86. }
  87. // RecoverPubkey returns the public key of the signer.
  88. // msg must be the 32-byte hash of the message to be signed.
  89. // sig must be a 65-byte compact ECDSA signature containing the
  90. // recovery id as the last element.
  91. func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) {
  92. if len(msg) != 32 {
  93. return nil, ErrInvalidMsgLen
  94. }
  95. if err := checkSignature(sig); err != nil {
  96. return nil, err
  97. }
  98. var (
  99. pubkey = make([]byte, 65)
  100. sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
  101. msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
  102. )
  103. if C.secp256k1_ext_ecdsa_recover(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
  104. return nil, ErrRecoverFailed
  105. }
  106. return pubkey, nil
  107. }
  108. // VerifySignature checks that the given pubkey created signature over message.
  109. // The signature should be in [R || S] format.
  110. func VerifySignature(pubkey, msg, signature []byte) bool {
  111. if len(msg) != 32 || len(signature) != 64 || len(pubkey) == 0 {
  112. return false
  113. }
  114. sigdata := (*C.uchar)(unsafe.Pointer(&signature[0]))
  115. msgdata := (*C.uchar)(unsafe.Pointer(&msg[0]))
  116. keydata := (*C.uchar)(unsafe.Pointer(&pubkey[0]))
  117. return C.secp256k1_ext_ecdsa_verify(context, sigdata, msgdata, keydata, C.size_t(len(pubkey))) != 0
  118. }
  119. // DecompressPubkey parses a public key in the 33-byte compressed format.
  120. // It returns non-nil coordinates if the public key is valid.
  121. func DecompressPubkey(pubkey []byte) (x, y *big.Int) {
  122. if len(pubkey) != 33 {
  123. return nil, nil
  124. }
  125. var (
  126. pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
  127. pubkeylen = C.size_t(len(pubkey))
  128. out = make([]byte, 65)
  129. outdata = (*C.uchar)(unsafe.Pointer(&out[0]))
  130. outlen = C.size_t(len(out))
  131. )
  132. if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
  133. return nil, nil
  134. }
  135. return new(big.Int).SetBytes(out[1:33]), new(big.Int).SetBytes(out[33:])
  136. }
  137. // CompressPubkey encodes a public key to 33-byte compressed format.
  138. func CompressPubkey(x, y *big.Int) []byte {
  139. var (
  140. pubkey = S256().Marshal(x, y)
  141. pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
  142. pubkeylen = C.size_t(len(pubkey))
  143. out = make([]byte, 33)
  144. outdata = (*C.uchar)(unsafe.Pointer(&out[0]))
  145. outlen = C.size_t(len(out))
  146. )
  147. if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
  148. panic("libsecp256k1 error")
  149. }
  150. return out
  151. }
  152. func checkSignature(sig []byte) error {
  153. if len(sig) != 65 {
  154. return ErrInvalidSignatureLen
  155. }
  156. if sig[64] >= 4 {
  157. return ErrInvalidRecoveryID
  158. }
  159. return nil
  160. }