signature_nocgo.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2017 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. // +build nacl js nocgo
  17. package crypto
  18. import (
  19. "crypto/ecdsa"
  20. "crypto/elliptic"
  21. "fmt"
  22. "github.com/btcsuite/btcd/btcec"
  23. )
  24. func Ecrecover(hash, sig []byte) ([]byte, error) {
  25. pub, err := SigToPub(hash, sig)
  26. if err != nil {
  27. return nil, err
  28. }
  29. bytes := (*btcec.PublicKey)(pub).SerializeUncompressed()
  30. return bytes, err
  31. }
  32. func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
  33. // Convert to btcec input format with 'recovery id' v at the beginning.
  34. btcsig := make([]byte, 65)
  35. btcsig[0] = sig[64] + 27
  36. copy(btcsig[1:], sig)
  37. pub, _, err := btcec.RecoverCompact(btcec.S256(), btcsig, hash)
  38. return (*ecdsa.PublicKey)(pub), err
  39. }
  40. // Sign calculates an ECDSA signature.
  41. //
  42. // This function is susceptible to chosen plaintext attacks that can leak
  43. // information about the private key that is used for signing. Callers must
  44. // be aware that the given hash cannot be chosen by an adversery. Common
  45. // solution is to hash any input before calculating the signature.
  46. //
  47. // The produced signature is in the [R || S || V] format where V is 0 or 1.
  48. func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error) {
  49. if len(hash) != 32 {
  50. return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
  51. }
  52. if prv.Curve != btcec.S256() {
  53. return nil, fmt.Errorf("private key curve is not secp256k1")
  54. }
  55. sig, err := btcec.SignCompact(btcec.S256(), (*btcec.PrivateKey)(prv), hash, false)
  56. if err != nil {
  57. return nil, err
  58. }
  59. // Convert to Ethereum signature format with 'recovery id' v at the end.
  60. v := sig[0] - 27
  61. copy(sig, sig[1:])
  62. sig[64] = v
  63. return sig, nil
  64. }
  65. // S256 returns an instance of the secp256k1 curve.
  66. func S256() elliptic.Curve {
  67. return btcec.S256()
  68. }