/src/secp256k1/src/modules/extrakeys/main_impl.h

https://github.com/denis2342/bitcoin · C Header · 287 lines · 212 code · 49 blank · 26 comment · 62 complexity · 9492f763b9687446ab50f09ff46b5ae0 MD5 · raw file

  1. /***********************************************************************
  2. * Copyright (c) 2020 Jonas Nick *
  3. * Distributed under the MIT software license, see the accompanying *
  4. * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
  5. ***********************************************************************/
  6. #ifndef SECP256K1_MODULE_EXTRAKEYS_MAIN_H
  7. #define SECP256K1_MODULE_EXTRAKEYS_MAIN_H
  8. #include "../../../include/secp256k1.h"
  9. #include "../../../include/secp256k1_extrakeys.h"
  10. static SECP256K1_INLINE int secp256k1_xonly_pubkey_load(const secp256k1_context* ctx, secp256k1_ge *ge, const secp256k1_xonly_pubkey *pubkey) {
  11. return secp256k1_pubkey_load(ctx, ge, (const secp256k1_pubkey *) pubkey);
  12. }
  13. static SECP256K1_INLINE void secp256k1_xonly_pubkey_save(secp256k1_xonly_pubkey *pubkey, secp256k1_ge *ge) {
  14. secp256k1_pubkey_save((secp256k1_pubkey *) pubkey, ge);
  15. }
  16. int secp256k1_xonly_pubkey_parse(const secp256k1_context* ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32) {
  17. secp256k1_ge pk;
  18. secp256k1_fe x;
  19. VERIFY_CHECK(ctx != NULL);
  20. ARG_CHECK(pubkey != NULL);
  21. memset(pubkey, 0, sizeof(*pubkey));
  22. ARG_CHECK(input32 != NULL);
  23. if (!secp256k1_fe_set_b32(&x, input32)) {
  24. return 0;
  25. }
  26. if (!secp256k1_ge_set_xo_var(&pk, &x, 0)) {
  27. return 0;
  28. }
  29. if (!secp256k1_ge_is_in_correct_subgroup(&pk)) {
  30. return 0;
  31. }
  32. secp256k1_xonly_pubkey_save(pubkey, &pk);
  33. return 1;
  34. }
  35. int secp256k1_xonly_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) {
  36. secp256k1_ge pk;
  37. VERIFY_CHECK(ctx != NULL);
  38. ARG_CHECK(output32 != NULL);
  39. memset(output32, 0, 32);
  40. ARG_CHECK(pubkey != NULL);
  41. if (!secp256k1_xonly_pubkey_load(ctx, &pk, pubkey)) {
  42. return 0;
  43. }
  44. secp256k1_fe_get_b32(output32, &pk.x);
  45. return 1;
  46. }
  47. int secp256k1_xonly_pubkey_cmp(const secp256k1_context* ctx, const secp256k1_xonly_pubkey* pk0, const secp256k1_xonly_pubkey* pk1) {
  48. unsigned char out[2][32];
  49. const secp256k1_xonly_pubkey* pk[2];
  50. int i;
  51. VERIFY_CHECK(ctx != NULL);
  52. pk[0] = pk0; pk[1] = pk1;
  53. for (i = 0; i < 2; i++) {
  54. /* If the public key is NULL or invalid, xonly_pubkey_serialize will
  55. * call the illegal_callback and return 0. In that case we will
  56. * serialize the key as all zeros which is less than any valid public
  57. * key. This results in consistent comparisons even if NULL or invalid
  58. * pubkeys are involved and prevents edge cases such as sorting
  59. * algorithms that use this function and do not terminate as a
  60. * result. */
  61. if (!secp256k1_xonly_pubkey_serialize(ctx, out[i], pk[i])) {
  62. /* Note that xonly_pubkey_serialize should already set the output to
  63. * zero in that case, but it's not guaranteed by the API, we can't
  64. * test it and writing a VERIFY_CHECK is more complex than
  65. * explicitly memsetting (again). */
  66. memset(out[i], 0, sizeof(out[i]));
  67. }
  68. }
  69. return secp256k1_memcmp_var(out[0], out[1], sizeof(out[1]));
  70. }
  71. /** Keeps a group element as is if it has an even Y and otherwise negates it.
  72. * y_parity is set to 0 in the former case and to 1 in the latter case.
  73. * Requires that the coordinates of r are normalized. */
  74. static int secp256k1_extrakeys_ge_even_y(secp256k1_ge *r) {
  75. int y_parity = 0;
  76. VERIFY_CHECK(!secp256k1_ge_is_infinity(r));
  77. if (secp256k1_fe_is_odd(&r->y)) {
  78. secp256k1_fe_negate(&r->y, &r->y, 1);
  79. y_parity = 1;
  80. }
  81. return y_parity;
  82. }
  83. int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context* ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) {
  84. secp256k1_ge pk;
  85. int tmp;
  86. VERIFY_CHECK(ctx != NULL);
  87. ARG_CHECK(xonly_pubkey != NULL);
  88. ARG_CHECK(pubkey != NULL);
  89. if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
  90. return 0;
  91. }
  92. tmp = secp256k1_extrakeys_ge_even_y(&pk);
  93. if (pk_parity != NULL) {
  94. *pk_parity = tmp;
  95. }
  96. secp256k1_xonly_pubkey_save(xonly_pubkey, &pk);
  97. return 1;
  98. }
  99. int secp256k1_xonly_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) {
  100. secp256k1_ge pk;
  101. VERIFY_CHECK(ctx != NULL);
  102. ARG_CHECK(output_pubkey != NULL);
  103. memset(output_pubkey, 0, sizeof(*output_pubkey));
  104. ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
  105. ARG_CHECK(internal_pubkey != NULL);
  106. ARG_CHECK(tweak32 != NULL);
  107. if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
  108. || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
  109. return 0;
  110. }
  111. secp256k1_pubkey_save(output_pubkey, &pk);
  112. return 1;
  113. }
  114. int secp256k1_xonly_pubkey_tweak_add_check(const secp256k1_context* ctx, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) {
  115. secp256k1_ge pk;
  116. unsigned char pk_expected32[32];
  117. VERIFY_CHECK(ctx != NULL);
  118. ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
  119. ARG_CHECK(internal_pubkey != NULL);
  120. ARG_CHECK(tweaked_pubkey32 != NULL);
  121. ARG_CHECK(tweak32 != NULL);
  122. if (!secp256k1_xonly_pubkey_load(ctx, &pk, internal_pubkey)
  123. || !secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32)) {
  124. return 0;
  125. }
  126. secp256k1_fe_normalize_var(&pk.x);
  127. secp256k1_fe_normalize_var(&pk.y);
  128. secp256k1_fe_get_b32(pk_expected32, &pk.x);
  129. return secp256k1_memcmp_var(&pk_expected32, tweaked_pubkey32, 32) == 0
  130. && secp256k1_fe_is_odd(&pk.y) == tweaked_pk_parity;
  131. }
  132. static void secp256k1_keypair_save(secp256k1_keypair *keypair, const secp256k1_scalar *sk, secp256k1_ge *pk) {
  133. secp256k1_scalar_get_b32(&keypair->data[0], sk);
  134. secp256k1_pubkey_save((secp256k1_pubkey *)&keypair->data[32], pk);
  135. }
  136. static int secp256k1_keypair_seckey_load(const secp256k1_context* ctx, secp256k1_scalar *sk, const secp256k1_keypair *keypair) {
  137. int ret;
  138. ret = secp256k1_scalar_set_b32_seckey(sk, &keypair->data[0]);
  139. /* We can declassify ret here because sk is only zero if a keypair function
  140. * failed (which zeroes the keypair) and its return value is ignored. */
  141. secp256k1_declassify(ctx, &ret, sizeof(ret));
  142. ARG_CHECK(ret);
  143. return ret;
  144. }
  145. /* Load a keypair into pk and sk (if non-NULL). This function declassifies pk
  146. * and ARG_CHECKs that the keypair is not invalid. It always initializes sk and
  147. * pk with dummy values. */
  148. static int secp256k1_keypair_load(const secp256k1_context* ctx, secp256k1_scalar *sk, secp256k1_ge *pk, const secp256k1_keypair *keypair) {
  149. int ret;
  150. const secp256k1_pubkey *pubkey = (const secp256k1_pubkey *)&keypair->data[32];
  151. /* Need to declassify the pubkey because pubkey_load ARG_CHECKs if it's
  152. * invalid. */
  153. secp256k1_declassify(ctx, pubkey, sizeof(*pubkey));
  154. ret = secp256k1_pubkey_load(ctx, pk, pubkey);
  155. if (sk != NULL) {
  156. ret = ret && secp256k1_keypair_seckey_load(ctx, sk, keypair);
  157. }
  158. if (!ret) {
  159. *pk = secp256k1_ge_const_g;
  160. if (sk != NULL) {
  161. *sk = secp256k1_scalar_one;
  162. }
  163. }
  164. return ret;
  165. }
  166. int secp256k1_keypair_create(const secp256k1_context* ctx, secp256k1_keypair *keypair, const unsigned char *seckey32) {
  167. secp256k1_scalar sk;
  168. secp256k1_ge pk;
  169. int ret = 0;
  170. VERIFY_CHECK(ctx != NULL);
  171. ARG_CHECK(keypair != NULL);
  172. memset(keypair, 0, sizeof(*keypair));
  173. ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
  174. ARG_CHECK(seckey32 != NULL);
  175. ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &sk, &pk, seckey32);
  176. secp256k1_keypair_save(keypair, &sk, &pk);
  177. secp256k1_memczero(keypair, sizeof(*keypair), !ret);
  178. secp256k1_scalar_clear(&sk);
  179. return ret;
  180. }
  181. int secp256k1_keypair_sec(const secp256k1_context* ctx, unsigned char *seckey, const secp256k1_keypair *keypair) {
  182. VERIFY_CHECK(ctx != NULL);
  183. ARG_CHECK(seckey != NULL);
  184. memset(seckey, 0, 32);
  185. ARG_CHECK(keypair != NULL);
  186. memcpy(seckey, &keypair->data[0], 32);
  187. return 1;
  188. }
  189. int secp256k1_keypair_pub(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair) {
  190. VERIFY_CHECK(ctx != NULL);
  191. ARG_CHECK(pubkey != NULL);
  192. memset(pubkey, 0, sizeof(*pubkey));
  193. ARG_CHECK(keypair != NULL);
  194. memcpy(pubkey->data, &keypair->data[32], sizeof(*pubkey));
  195. return 1;
  196. }
  197. int secp256k1_keypair_xonly_pub(const secp256k1_context* ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) {
  198. secp256k1_ge pk;
  199. int tmp;
  200. VERIFY_CHECK(ctx != NULL);
  201. ARG_CHECK(pubkey != NULL);
  202. memset(pubkey, 0, sizeof(*pubkey));
  203. ARG_CHECK(keypair != NULL);
  204. if (!secp256k1_keypair_load(ctx, NULL, &pk, keypair)) {
  205. return 0;
  206. }
  207. tmp = secp256k1_extrakeys_ge_even_y(&pk);
  208. if (pk_parity != NULL) {
  209. *pk_parity = tmp;
  210. }
  211. secp256k1_xonly_pubkey_save(pubkey, &pk);
  212. return 1;
  213. }
  214. int secp256k1_keypair_xonly_tweak_add(const secp256k1_context* ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) {
  215. secp256k1_ge pk;
  216. secp256k1_scalar sk;
  217. int y_parity;
  218. int ret;
  219. VERIFY_CHECK(ctx != NULL);
  220. ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
  221. ARG_CHECK(keypair != NULL);
  222. ARG_CHECK(tweak32 != NULL);
  223. ret = secp256k1_keypair_load(ctx, &sk, &pk, keypair);
  224. memset(keypair, 0, sizeof(*keypair));
  225. y_parity = secp256k1_extrakeys_ge_even_y(&pk);
  226. if (y_parity == 1) {
  227. secp256k1_scalar_negate(&sk, &sk);
  228. }
  229. ret &= secp256k1_ec_seckey_tweak_add_helper(&sk, tweak32);
  230. ret &= secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &pk, tweak32);
  231. secp256k1_declassify(ctx, &ret, sizeof(ret));
  232. if (ret) {
  233. secp256k1_keypair_save(keypair, &sk, &pk);
  234. }
  235. secp256k1_scalar_clear(&sk);
  236. return ret;
  237. }
  238. #endif