ext.h 4.8 KB

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