ext.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // secp256k1_context_create_sign_verify creates a context for signing and signature verification.
  17. static secp256k1_context* secp256k1_context_create_sign_verify() {
  18. return secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
  19. }
  20. // secp256k1_ext_ecdsa_recover recovers the public key of an encoded compact signature.
  21. //
  22. // Returns: 1: recovery was successful
  23. // 0: recovery was not successful
  24. // Args: ctx: pointer to a context object (cannot be NULL)
  25. // Out: pubkey_out: the serialized 65-byte public key of the signer (cannot be NULL)
  26. // In: sigdata: pointer to a 65-byte signature with the recovery id at the end (cannot be NULL)
  27. // msgdata: pointer to a 32-byte message (cannot be NULL)
  28. static int secp256k1_ext_ecdsa_recover(
  29. const secp256k1_context* ctx,
  30. unsigned char *pubkey_out,
  31. const unsigned char *sigdata,
  32. const unsigned char *msgdata
  33. ) {
  34. secp256k1_ecdsa_recoverable_signature sig;
  35. secp256k1_pubkey pubkey;
  36. if (!secp256k1_ecdsa_recoverable_signature_parse_compact(ctx, &sig, sigdata, (int)sigdata[64])) {
  37. return 0;
  38. }
  39. if (!secp256k1_ecdsa_recover(ctx, &pubkey, &sig, msgdata)) {
  40. return 0;
  41. }
  42. size_t outputlen = 65;
  43. return secp256k1_ec_pubkey_serialize(ctx, pubkey_out, &outputlen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
  44. }
  45. // secp256k1_ext_ecdsa_verify verifies an encoded compact signature.
  46. //
  47. // Returns: 1: signature is valid
  48. // 0: signature is invalid
  49. // Args: ctx: pointer to a context object (cannot be NULL)
  50. // In: sigdata: pointer to a 64-byte signature (cannot be NULL)
  51. // msgdata: pointer to a 32-byte message (cannot be NULL)
  52. // pubkeydata: pointer to public key data (cannot be NULL)
  53. // pubkeylen: length of pubkeydata
  54. static int secp256k1_ext_ecdsa_verify(
  55. const secp256k1_context* ctx,
  56. const unsigned char *sigdata,
  57. const unsigned char *msgdata,
  58. const unsigned char *pubkeydata,
  59. size_t pubkeylen
  60. ) {
  61. secp256k1_ecdsa_signature sig;
  62. secp256k1_pubkey pubkey;
  63. if (!secp256k1_ecdsa_signature_parse_compact(ctx, &sig, sigdata)) {
  64. return 0;
  65. }
  66. if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, pubkeylen)) {
  67. return 0;
  68. }
  69. return secp256k1_ecdsa_verify(ctx, &sig, msgdata, &pubkey);
  70. }
  71. // secp256k1_ext_reencode_pubkey decodes then encodes a public key. It can be used to
  72. // convert between public key formats. The input/output formats are chosen depending on the
  73. // length of the input/output buffers.
  74. //
  75. // Returns: 1: conversion successful
  76. // 0: conversion unsuccessful
  77. // Args: ctx: pointer to a context object (cannot be NULL)
  78. // Out: out: output buffer that will contain the reencoded key (cannot be NULL)
  79. // In: outlen: length of out (33 for compressed keys, 65 for uncompressed keys)
  80. // pubkeydata: the input public key (cannot be NULL)
  81. // pubkeylen: length of pubkeydata
  82. static int secp256k1_ext_reencode_pubkey(
  83. const secp256k1_context* ctx,
  84. unsigned char *out,
  85. size_t outlen,
  86. const unsigned char *pubkeydata,
  87. size_t pubkeylen
  88. ) {
  89. secp256k1_pubkey pubkey;
  90. if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, pubkeylen)) {
  91. return 0;
  92. }
  93. unsigned int flag = (outlen == 33) ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED;
  94. return secp256k1_ec_pubkey_serialize(ctx, out, &outlen, &pubkey, flag);
  95. }
  96. // secp256k1_ext_scalar_mul multiplies a point by a scalar in constant time.
  97. //
  98. // Returns: 1: multiplication was successful
  99. // 0: scalar was invalid (zero or overflow)
  100. // Args: ctx: pointer to a context object (cannot be NULL)
  101. // Out: point: the multiplied point (usually secret)
  102. // In: point: pointer to a 64-byte public point,
  103. // encoded as two 256bit big-endian numbers.
  104. // scalar: a 32-byte scalar with which to multiply the point
  105. int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) {
  106. int ret = 0;
  107. int overflow = 0;
  108. secp256k1_fe feX, feY;
  109. secp256k1_gej res;
  110. secp256k1_ge ge;
  111. secp256k1_scalar s;
  112. ARG_CHECK(point != NULL);
  113. ARG_CHECK(scalar != NULL);
  114. (void)ctx;
  115. secp256k1_fe_set_b32(&feX, point);
  116. secp256k1_fe_set_b32(&feY, point+32);
  117. secp256k1_ge_set_xy(&ge, &feX, &feY);
  118. secp256k1_scalar_set_b32(&s, scalar, &overflow);
  119. if (overflow || secp256k1_scalar_is_zero(&s)) {
  120. ret = 0;
  121. } else {
  122. secp256k1_ecmult_const(&res, &ge, &s);
  123. secp256k1_ge_set_gej(&ge, &res);
  124. /* Note: can't use secp256k1_pubkey_save here because it is not constant time. */
  125. secp256k1_fe_normalize(&ge.x);
  126. secp256k1_fe_normalize(&ge.y);
  127. secp256k1_fe_get_b32(point, &ge.x);
  128. secp256k1_fe_get_b32(point+32, &ge.y);
  129. ret = 1;
  130. }
  131. secp256k1_scalar_clear(&s);
  132. return ret;
  133. }