secp256k1mod.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. static PyObject * pubkey_gen(PyObject *self, PyObject *args) {
  19. const unsigned char * privkey;
  20. const int klen;
  21. const int compressed;
  22. if (!PyArg_ParseTuple(args, "y#I", &privkey, &klen, &compressed)) {
  23. PyErr_SetString(PyExc_ValueError, "Unable to parse extension mod arguments");
  24. return NULL;
  25. }
  26. if (klen != 32) {
  27. PyErr_SetString(PyExc_ValueError, "Private key length not 32 bytes");
  28. return NULL;
  29. }
  30. secp256k1_pubkey pubkey;
  31. size_t pubkeyclen = compressed == 1 ? 33 : 65;
  32. unsigned char pubkeyc[pubkeyclen];
  33. static secp256k1_context *ctx = NULL;
  34. if (ctx == NULL) {
  35. /* puts ("Initializing context"); */
  36. ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
  37. }
  38. if (ctx == NULL) {
  39. PyErr_SetString(PyExc_RuntimeError, "Context initialization failed");
  40. return NULL;
  41. }
  42. if (secp256k1_ec_pubkey_create(ctx, &pubkey, privkey) != 1) {
  43. PyErr_SetString(PyExc_RuntimeError, "Public key creation failed");
  44. return NULL;
  45. }
  46. if (secp256k1_ec_pubkey_serialize(ctx, pubkeyc, &pubkeyclen, &pubkey,
  47. compressed == 1 ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED) != 1) {
  48. PyErr_SetString(PyExc_RuntimeError, "Public key serialization failed");
  49. return NULL;
  50. }
  51. return Py_BuildValue("y#", pubkeyc,pubkeyclen);
  52. }
  53. /* https://docs.python.org/3/howto/cporting.html */
  54. struct module_state {
  55. PyObject *error;
  56. };
  57. #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
  58. static PyMethodDef secp256k1_methods[] = {
  59. {
  60. "pubkey_gen",
  61. pubkey_gen,
  62. METH_VARARGS,
  63. "Generate a serialized pubkey from privkey bytes"
  64. },
  65. {NULL, NULL}
  66. };
  67. static int secp256k1_traverse(PyObject *m, visitproc visit, void *arg) {
  68. Py_VISIT(GETSTATE(m)->error);
  69. return 0;
  70. }
  71. static int secp256k1_clear(PyObject *m) {
  72. Py_CLEAR(GETSTATE(m)->error);
  73. return 0;
  74. }
  75. static struct PyModuleDef moduledef = {
  76. PyModuleDef_HEAD_INIT,
  77. "secp256k1",
  78. NULL,
  79. sizeof(struct module_state),
  80. secp256k1_methods,
  81. NULL,
  82. secp256k1_traverse,
  83. secp256k1_clear,
  84. NULL
  85. };
  86. #define INITERROR return NULL
  87. PyMODINIT_FUNC PyInit_secp256k1(void) {
  88. PyObject *module = PyModule_Create(&moduledef);
  89. if (module == NULL)
  90. INITERROR;
  91. struct module_state *st = GETSTATE(module);
  92. st->error = PyErr_NewException("secp256k1.Error", NULL, NULL);
  93. if (st->error == NULL) {
  94. Py_DECREF(module);
  95. INITERROR;
  96. }
  97. return module;
  98. }