secp256k1mod.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 * priv2pub(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. {"priv2pub", priv2pub, METH_VARARGS, "Generate pubkey from privkey using libsecp256k1"},
  60. {NULL, NULL}
  61. };
  62. static int secp256k1_traverse(PyObject *m, visitproc visit, void *arg) {
  63. Py_VISIT(GETSTATE(m)->error);
  64. return 0;
  65. }
  66. static int secp256k1_clear(PyObject *m) {
  67. Py_CLEAR(GETSTATE(m)->error);
  68. return 0;
  69. }
  70. static struct PyModuleDef moduledef = {
  71. PyModuleDef_HEAD_INIT,
  72. "secp256k1",
  73. NULL,
  74. sizeof(struct module_state),
  75. secp256k1_methods,
  76. NULL,
  77. secp256k1_traverse,
  78. secp256k1_clear,
  79. NULL
  80. };
  81. #define INITERROR return NULL
  82. PyMODINIT_FUNC PyInit_secp256k1(void) {
  83. PyObject *module = PyModule_Create(&moduledef);
  84. if (module == NULL)
  85. INITERROR;
  86. struct module_state *st = GETSTATE(module);
  87. st->error = PyErr_NewException("secp256k1.Error", NULL, NULL);
  88. if (st->error == NULL) {
  89. Py_DECREF(module);
  90. INITERROR;
  91. }
  92. return module;
  93. }