secp256k1mod.c 3.2 KB

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