zkp_bip340.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * Copyright (c) SatoshiLabs
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <stdbool.h>
  23. #include <string.h>
  24. #include "memzero.h"
  25. #include "sha2.h"
  26. #include "zkp_context.h"
  27. #include "vendor/secp256k1-zkp/include/secp256k1.h"
  28. #include "vendor/secp256k1-zkp/include/secp256k1_extrakeys.h"
  29. #include "vendor/secp256k1-zkp/include/secp256k1_schnorrsig.h"
  30. #include "zkp_bip340.h"
  31. // Initial hash value H for SHA-256 TapTweak:
  32. const uint32_t sha256_initial_taptweak_state[8] = {
  33. 0xd129a2f3UL, 0x701c655dUL, 0x6583b6c3UL, 0xb9419727UL,
  34. 0x95f4e232UL, 0x94fd54f4UL, 0xa2ae8d85UL, 0x47ca590bUL,
  35. };
  36. // BIP340 Schnorr public key derivation
  37. // private_key_bytes has 32 bytes
  38. // public_key_bytes has 32 bytes
  39. // returns 0 on success
  40. int zkp_bip340_get_public_key(const uint8_t *private_key_bytes,
  41. uint8_t *public_key_bytes) {
  42. int result = 0;
  43. secp256k1_context *context_writable = NULL;
  44. if (result == 0) {
  45. context_writable = zkp_context_acquire_writable();
  46. if (context_writable == NULL) {
  47. result = -1;
  48. }
  49. }
  50. if (result == 0) {
  51. if (secp256k1_context_writable_randomize(context_writable) != 0) {
  52. result = -1;
  53. }
  54. }
  55. secp256k1_pubkey public_key = {0};
  56. if (result == 0) {
  57. if (secp256k1_ec_pubkey_create(context_writable, &public_key,
  58. private_key_bytes) != 1) {
  59. result = -1;
  60. }
  61. }
  62. if (context_writable) {
  63. zkp_context_release_writable();
  64. context_writable = NULL;
  65. }
  66. secp256k1_xonly_pubkey xonly_pubkey = {0};
  67. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  68. if (result == 0) {
  69. if (secp256k1_xonly_pubkey_from_pubkey(context_read_only, &xonly_pubkey,
  70. NULL, &public_key) != 1) {
  71. result = -1;
  72. }
  73. }
  74. memzero(&public_key, sizeof(public_key));
  75. if (result == 0) {
  76. if (secp256k1_xonly_pubkey_serialize(context_read_only, public_key_bytes,
  77. &xonly_pubkey) != 1) {
  78. result = -1;
  79. }
  80. }
  81. memzero(&xonly_pubkey, sizeof(xonly_pubkey));
  82. return result;
  83. }
  84. // BIP340 Schnorr signature signing
  85. // private_key_bytes has 32 bytes
  86. // digest has 32 bytes
  87. // signature_bytes has 64 bytes
  88. // auxiliary_data has 32 bytes or is NULL (32 zero bytes are used)
  89. // returns 0 on success
  90. int zkp_bip340_sign_digest(const uint8_t *private_key_bytes,
  91. const uint8_t *digest, uint8_t *signature_bytes,
  92. uint8_t *auxiliary_data) {
  93. int result = 0;
  94. secp256k1_context *context_writable = NULL;
  95. if (result == 0) {
  96. context_writable = zkp_context_acquire_writable();
  97. if (context_writable == NULL) {
  98. result = -1;
  99. }
  100. }
  101. if (result == 0) {
  102. if (secp256k1_context_writable_randomize(context_writable) != 0) {
  103. result = -1;
  104. }
  105. }
  106. secp256k1_keypair keypair = {0};
  107. if (result == 0) {
  108. if (secp256k1_keypair_create(context_writable, &keypair,
  109. private_key_bytes) != 1) {
  110. result = -1;
  111. }
  112. }
  113. if (result == 0) {
  114. if (secp256k1_context_writable_randomize(context_writable) != 0) {
  115. result = -1;
  116. }
  117. }
  118. if (result == 0) {
  119. if (secp256k1_schnorrsig_sign32(context_writable, signature_bytes, digest,
  120. &keypair, auxiliary_data) != 1) {
  121. result = -1;
  122. }
  123. }
  124. if (context_writable) {
  125. zkp_context_release_writable();
  126. context_writable = NULL;
  127. }
  128. memzero(&keypair, sizeof(keypair));
  129. return result;
  130. }
  131. // BIP340 Schnorr signature verification
  132. // public_key_bytes has 32 bytes
  133. // signature_bytes has 64 bytes
  134. // digest has 32 bytes
  135. // returns 0 if verification succeeded
  136. int zkp_bip340_verify_digest(const uint8_t *public_key_bytes,
  137. const uint8_t *signature_bytes,
  138. const uint8_t *digest) {
  139. int result = 0;
  140. secp256k1_xonly_pubkey xonly_pubkey = {0};
  141. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  142. if (result == 0) {
  143. if (secp256k1_xonly_pubkey_parse(context_read_only, &xonly_pubkey,
  144. public_key_bytes) != 1) {
  145. result = 1;
  146. }
  147. }
  148. if (result == 0) {
  149. if (secp256k1_schnorrsig_verify(context_read_only, signature_bytes, digest,
  150. 32, &xonly_pubkey) != 1) {
  151. result = 5;
  152. }
  153. }
  154. memzero(&xonly_pubkey, sizeof(xonly_pubkey));
  155. return result;
  156. }
  157. // BIP340 Schnorr public key verification
  158. // public_key_bytes has 32 bytes
  159. // returns 0 if verification succeeded
  160. int zkp_bip340_verify_publickey(const uint8_t *public_key_bytes) {
  161. int result = 0;
  162. secp256k1_xonly_pubkey xonly_pubkey = {0};
  163. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  164. if (result == 0) {
  165. if (secp256k1_xonly_pubkey_parse(context_read_only, &xonly_pubkey,
  166. public_key_bytes) != 1) {
  167. result = 1;
  168. }
  169. }
  170. memzero(&xonly_pubkey, sizeof(xonly_pubkey));
  171. return result;
  172. }
  173. // BIP340 Schnorr public key tweak
  174. // internal_public_key has 32 bytes
  175. // root_hash has 32 bytes or is empty (NULL)
  176. // output_public_key has 32 bytes
  177. // returns 0 on success
  178. int zkp_bip340_tweak_public_key(const uint8_t *internal_public_key,
  179. const uint8_t *root_hash,
  180. uint8_t *output_public_key) {
  181. int result = 0;
  182. uint8_t tweak[SHA256_DIGEST_LENGTH] = {0};
  183. if (result == 0) {
  184. SHA256_CTX ctx = {0};
  185. sha256_Init_ex(&ctx, sha256_initial_taptweak_state, 512);
  186. sha256_Update(&ctx, internal_public_key, 32);
  187. if (root_hash != NULL) {
  188. sha256_Update(&ctx, root_hash, 32);
  189. }
  190. sha256_Final(&ctx, tweak);
  191. }
  192. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  193. secp256k1_xonly_pubkey internal_pubkey = {0};
  194. if (result == 0) {
  195. if (secp256k1_xonly_pubkey_parse(context_read_only, &internal_pubkey,
  196. internal_public_key) != 1) {
  197. result = -1;
  198. }
  199. }
  200. secp256k1_pubkey output_pubkey = {0};
  201. if (result == 0) {
  202. if (secp256k1_xonly_pubkey_tweak_add(context_read_only, &output_pubkey,
  203. &internal_pubkey, tweak) != 1) {
  204. result = -1;
  205. }
  206. }
  207. memzero(tweak, sizeof(tweak));
  208. memzero(&internal_pubkey, sizeof(internal_pubkey));
  209. secp256k1_xonly_pubkey xonly_output_pubkey = {0};
  210. if (result == 0) {
  211. if (secp256k1_xonly_pubkey_from_pubkey(context_read_only,
  212. &xonly_output_pubkey, NULL,
  213. &output_pubkey) != 1) {
  214. result = -1;
  215. }
  216. }
  217. memzero(&output_pubkey, sizeof(output_pubkey));
  218. if (result == 0) {
  219. if (secp256k1_xonly_pubkey_serialize(context_read_only, output_public_key,
  220. &xonly_output_pubkey) != 1) {
  221. result = -1;
  222. }
  223. }
  224. memzero(&xonly_output_pubkey, sizeof(xonly_output_pubkey));
  225. return result;
  226. }
  227. // BIP340 Schnorr private key tweak
  228. // internal_private_key has 32 bytes
  229. // root_hash has 32 bytes or is empty (NULL)
  230. // output_private_key has 32 bytes
  231. // returns 0 on success
  232. int zkp_bip340_tweak_private_key(const uint8_t *internal_private_key,
  233. const uint8_t *root_hash,
  234. uint8_t *output_private_key) {
  235. int result = 0;
  236. secp256k1_context *context_writable = NULL;
  237. if (result == 0) {
  238. context_writable = zkp_context_acquire_writable();
  239. if (context_writable == NULL) {
  240. result = -1;
  241. }
  242. }
  243. if (result == 0) {
  244. if (secp256k1_context_writable_randomize(context_writable) != 0) {
  245. result = -1;
  246. }
  247. }
  248. secp256k1_keypair keypair = {0};
  249. if (secp256k1_keypair_create(context_writable, &keypair,
  250. internal_private_key) != 1) {
  251. result = -1;
  252. }
  253. if (context_writable) {
  254. zkp_context_release_writable();
  255. context_writable = NULL;
  256. }
  257. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  258. secp256k1_xonly_pubkey internal_xonly_pubkey = {0};
  259. if (result == 0) {
  260. if (secp256k1_keypair_xonly_pub(context_read_only, &internal_xonly_pubkey,
  261. NULL, &keypair) != 1) {
  262. result = -1;
  263. }
  264. }
  265. uint8_t internal_public_key[32] = {0};
  266. if (result == 0) {
  267. if (secp256k1_xonly_pubkey_serialize(context_read_only, internal_public_key,
  268. &internal_xonly_pubkey) != 1) {
  269. result = -1;
  270. }
  271. }
  272. memzero(&internal_xonly_pubkey, sizeof(internal_xonly_pubkey));
  273. uint8_t tweak[SHA256_DIGEST_LENGTH] = {0};
  274. if (result == 0) {
  275. SHA256_CTX ctx = {0};
  276. sha256_Init_ex(&ctx, sha256_initial_taptweak_state, 512);
  277. sha256_Update(&ctx, internal_public_key, 32);
  278. if (root_hash != NULL) {
  279. sha256_Update(&ctx, root_hash, 32);
  280. }
  281. sha256_Final(&ctx, tweak);
  282. }
  283. memzero(&internal_public_key, sizeof(internal_public_key));
  284. if (result == 0) {
  285. if (secp256k1_keypair_xonly_tweak_add(context_read_only, &keypair, tweak) !=
  286. 1) {
  287. result = -1;
  288. }
  289. }
  290. memzero(tweak, sizeof(tweak));
  291. if (result == 0) {
  292. if (secp256k1_keypair_sec(context_read_only, output_private_key,
  293. &keypair) != 1) {
  294. result = -1;
  295. }
  296. }
  297. memzero(&keypair, sizeof(keypair));
  298. return result;
  299. }