secp256k1mod.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  3. Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  4. This program is free software: you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation, either version 3 of the License, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. You should have received a copy of the GNU General Public License along with
  13. this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define PY_SSIZE_T_CLEAN
  16. #include <Python.h>
  17. #include <secp256k1.h>
  18. int privkey_check(
  19. const secp256k1_context * ctx,
  20. const unsigned char * privkey_bytes,
  21. const Py_ssize_t privkey_bytes_len,
  22. const char * desc
  23. ) {
  24. if (privkey_bytes_len != 32) {
  25. char buf[64 + strlen(desc)];
  26. sprintf(buf, "%s length not 32 bytes", desc);
  27. PyErr_SetString(PyExc_ValueError, buf);
  28. return 0;
  29. }
  30. if (secp256k1_ec_seckey_verify(ctx, privkey_bytes) != 1) {
  31. char buf[64 + strlen(desc)];
  32. sprintf(buf, "%s not in allowable range", desc);
  33. PyErr_SetString(PyExc_ValueError, buf);
  34. return 0;
  35. }
  36. return 1;
  37. }
  38. int pubkey_parse_with_check(
  39. const secp256k1_context * ctx,
  40. secp256k1_pubkey * pubkey_ptr,
  41. const unsigned char * pubkey_bytes,
  42. const Py_ssize_t pubkey_bytes_len
  43. ) {
  44. if (ctx == NULL) {
  45. PyErr_SetString(PyExc_RuntimeError, "Context initialization failed");
  46. return 0;
  47. }
  48. if (pubkey_bytes_len == 33) {
  49. if (pubkey_bytes[0] != 3 && pubkey_bytes[0] != 2) {
  50. PyErr_SetString(PyExc_ValueError, "Invalid first byte for serialized compressed public key");
  51. return 0;
  52. }
  53. } else if (pubkey_bytes_len == 65) {
  54. if (pubkey_bytes[0] != 4) {
  55. PyErr_SetString(PyExc_ValueError, "Invalid first byte for serialized uncompressed public key");
  56. return 0;
  57. }
  58. } else {
  59. PyErr_SetString(PyExc_ValueError, "Serialized public key length not 33 or 65 bytes");
  60. return 0;
  61. }
  62. /* checks for point-at-infinity (via secp256k1_pubkey_save) */
  63. if (secp256k1_ec_pubkey_parse(ctx, pubkey_ptr, pubkey_bytes, pubkey_bytes_len) != 1) {
  64. PyErr_SetString(PyExc_ValueError, "Public key could not be parsed or encodes point-at-infinity");
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. static PyObject * pubkey_gen(PyObject *self, PyObject *args) {
  70. const unsigned char * privkey_bytes;
  71. const Py_ssize_t privkey_bytes_len;
  72. const int compressed;
  73. if (!PyArg_ParseTuple(args, "y#I", &privkey_bytes, &privkey_bytes_len, &compressed)) {
  74. PyErr_SetString(PyExc_ValueError, "Unable to parse extension mod arguments");
  75. return NULL;
  76. }
  77. size_t pubkey_bytes_len = compressed == 1 ? 33 : 65;
  78. unsigned char pubkey_bytes[pubkey_bytes_len];
  79. secp256k1_pubkey pubkey;
  80. secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
  81. if (ctx == NULL) {
  82. PyErr_SetString(PyExc_RuntimeError, "Context initialization failed");
  83. return NULL;
  84. }
  85. if (!privkey_check(ctx, privkey_bytes, privkey_bytes_len, "Private key")) {
  86. return NULL;
  87. }
  88. if (secp256k1_ec_pubkey_create(ctx, &pubkey, privkey_bytes) != 1) {
  89. PyErr_SetString(PyExc_RuntimeError, "Public key creation failed");
  90. return NULL;
  91. }
  92. if (secp256k1_ec_pubkey_serialize(ctx, pubkey_bytes, &pubkey_bytes_len, &pubkey,
  93. compressed == 1 ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED) != 1) {
  94. PyErr_SetString(PyExc_RuntimeError, "Public key serialization failed");
  95. return NULL;
  96. }
  97. return Py_BuildValue("y#", pubkey_bytes, pubkey_bytes_len);
  98. }
  99. static PyObject * pubkey_tweak_add(PyObject *self, PyObject *args) {
  100. const unsigned char * pubkey_bytes;
  101. const unsigned char * tweak_bytes;
  102. const Py_ssize_t pubkey_bytes_len;
  103. const Py_ssize_t tweak_bytes_len;
  104. if (!PyArg_ParseTuple(args, "y#y#", &pubkey_bytes, &pubkey_bytes_len, &tweak_bytes, &tweak_bytes_len)) {
  105. PyErr_SetString(PyExc_ValueError, "Unable to parse extension mod arguments");
  106. return NULL;
  107. }
  108. secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
  109. secp256k1_pubkey pubkey;
  110. if (!pubkey_parse_with_check(ctx, &pubkey, pubkey_bytes, pubkey_bytes_len)) {
  111. return NULL;
  112. }
  113. if (!privkey_check(ctx, tweak_bytes, tweak_bytes_len, "Tweak")) {
  114. return NULL;
  115. }
  116. /* checks for point-at-infinity (via secp256k1_pubkey_save) */
  117. if (secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, tweak_bytes) != 1) {
  118. PyErr_SetString(PyExc_RuntimeError, "Adding public key points failed or result was point-at-infinity");
  119. return NULL;
  120. }
  121. unsigned char new_pubkey_bytes[pubkey_bytes_len];
  122. if (secp256k1_ec_pubkey_serialize(
  123. ctx,
  124. new_pubkey_bytes,
  125. (size_t*) &pubkey_bytes_len,
  126. &pubkey,
  127. pubkey_bytes_len == 33 ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED) != 1) {
  128. PyErr_SetString(PyExc_RuntimeError, "Public key serialization failed");
  129. return NULL;
  130. }
  131. return Py_BuildValue("y#", new_pubkey_bytes, pubkey_bytes_len);
  132. }
  133. static PyObject * pubkey_check(PyObject *self, PyObject *args) {
  134. const unsigned char * pubkey_bytes;
  135. const Py_ssize_t pubkey_bytes_len;
  136. if (!PyArg_ParseTuple(args, "y#", &pubkey_bytes, &pubkey_bytes_len)) {
  137. PyErr_SetString(PyExc_ValueError, "Unable to parse extension mod arguments");
  138. return NULL;
  139. }
  140. secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
  141. secp256k1_pubkey pubkey;
  142. if (!pubkey_parse_with_check(ctx, &pubkey, pubkey_bytes, pubkey_bytes_len)) {
  143. return NULL;
  144. }
  145. return Py_BuildValue("I", 1);
  146. }
  147. /* https://docs.python.org/3/howto/cporting.html */
  148. struct module_state {
  149. PyObject *error;
  150. };
  151. #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
  152. static PyMethodDef secp256k1_methods[] = {
  153. {
  154. "pubkey_gen",
  155. pubkey_gen,
  156. METH_VARARGS,
  157. "Generate a serialized pubkey from privkey bytes"
  158. },
  159. {
  160. "pubkey_tweak_add",
  161. pubkey_tweak_add,
  162. METH_VARARGS,
  163. "Add scalar bytes to a serialized pubkey, returning a serialized pubkey"
  164. },
  165. {
  166. "pubkey_check",
  167. pubkey_check,
  168. METH_VARARGS,
  169. "Check a serialized pubkey, ensuring the encoded point is not point-at-infinity"
  170. },
  171. {NULL, NULL}
  172. };
  173. static int secp256k1_traverse(PyObject *m, visitproc visit, void *arg) {
  174. Py_VISIT(GETSTATE(m)->error);
  175. return 0;
  176. }
  177. static int secp256k1_clear(PyObject *m) {
  178. Py_CLEAR(GETSTATE(m)->error);
  179. return 0;
  180. }
  181. static struct PyModuleDef moduledef = {
  182. PyModuleDef_HEAD_INIT,
  183. "secp256k1",
  184. NULL,
  185. sizeof(struct module_state),
  186. secp256k1_methods,
  187. NULL,
  188. secp256k1_traverse,
  189. secp256k1_clear,
  190. NULL
  191. };
  192. #define INITERROR return NULL
  193. PyMODINIT_FUNC PyInit_secp256k1(void) {
  194. PyObject *module = PyModule_Create(&moduledef);
  195. if (module == NULL)
  196. INITERROR;
  197. struct module_state *st = GETSTATE(module);
  198. st->error = PyErr_NewException("secp256k1.Error", NULL, NULL);
  199. if (st->error == NULL) {
  200. Py_DECREF(module);
  201. INITERROR;
  202. }
  203. return module;
  204. }