secp256k1mod.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  3. Copyright (C)2013-2023 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. static PyObject * pubkey_gen(PyObject *self, PyObject *args) {
  39. const unsigned char * privkey_bytes;
  40. const Py_ssize_t privkey_bytes_len;
  41. const int compressed;
  42. if (!PyArg_ParseTuple(args, "y#I", &privkey_bytes, &privkey_bytes_len, &compressed)) {
  43. PyErr_SetString(PyExc_ValueError, "Unable to parse extension mod arguments");
  44. return NULL;
  45. }
  46. size_t pubkey_bytes_len = compressed == 1 ? 33 : 65;
  47. unsigned char pubkey_bytes[pubkey_bytes_len];
  48. secp256k1_pubkey pubkey;
  49. secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
  50. if (ctx == NULL) {
  51. PyErr_SetString(PyExc_RuntimeError, "Context initialization failed");
  52. return NULL;
  53. }
  54. if (!privkey_check(ctx, privkey_bytes, privkey_bytes_len, "Private key")) {
  55. return NULL;
  56. }
  57. if (secp256k1_ec_pubkey_create(ctx, &pubkey, privkey_bytes) != 1) {
  58. PyErr_SetString(PyExc_RuntimeError, "Public key creation failed");
  59. return NULL;
  60. }
  61. if (secp256k1_ec_pubkey_serialize(ctx, pubkey_bytes, &pubkey_bytes_len, &pubkey,
  62. compressed == 1 ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED) != 1) {
  63. PyErr_SetString(PyExc_RuntimeError, "Public key serialization failed");
  64. return NULL;
  65. }
  66. return Py_BuildValue("y#", pubkey_bytes, pubkey_bytes_len);
  67. }
  68. /* https://docs.python.org/3/howto/cporting.html */
  69. struct module_state {
  70. PyObject *error;
  71. };
  72. #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
  73. static PyMethodDef secp256k1_methods[] = {
  74. {
  75. "pubkey_gen",
  76. pubkey_gen,
  77. METH_VARARGS,
  78. "Generate a serialized pubkey from privkey bytes"
  79. },
  80. {NULL, NULL}
  81. };
  82. static int secp256k1_traverse(PyObject *m, visitproc visit, void *arg) {
  83. Py_VISIT(GETSTATE(m)->error);
  84. return 0;
  85. }
  86. static int secp256k1_clear(PyObject *m) {
  87. Py_CLEAR(GETSTATE(m)->error);
  88. return 0;
  89. }
  90. static struct PyModuleDef moduledef = {
  91. PyModuleDef_HEAD_INIT,
  92. "secp256k1",
  93. NULL,
  94. sizeof(struct module_state),
  95. secp256k1_methods,
  96. NULL,
  97. secp256k1_traverse,
  98. secp256k1_clear,
  99. NULL
  100. };
  101. #define INITERROR return NULL
  102. PyMODINIT_FUNC PyInit_secp256k1(void) {
  103. PyObject *module = PyModule_Create(&moduledef);
  104. if (module == NULL)
  105. INITERROR;
  106. struct module_state *st = GETSTATE(module);
  107. st->error = PyErr_NewException("secp256k1.Error", NULL, NULL);
  108. if (st->error == NULL) {
  109. Py_DECREF(module);
  110. INITERROR;
  111. }
  112. return module;
  113. }