5

I'm developing a python wrapper for C++ based cryptography scheme where the keys and ciphertext are very large numbers (up to 2048 bits).

In C++, I'm using Intel BigNumber class to store and handle such large numbers.

As Python natively supports "seemingly" infinite bits of integer through objects, I'm having trouble passing such variables as parameters via Python.

  py::class_<BigNumber>(m, "BigNumber")
      .def(py::init<unsigned int>())
      .def(py::init<int>())
      .def(py::init<BigNumber>())
      .def(py::init([](py::list data) {
        size_t length = data.size();
        unsigned int* pData = new unsigned int[length];
        for (int i = 0; i < length; i++) {
          pData[i] = data[i].cast<unsigned int>();
        }
        return std::unique_ptr<BigNumber>(new BigNumber(pData, length));
      }))
      .def(py::init([](py::array_t<unsigned int> data) {
        py::buffer_info buffer_info = data.request();

        unsigned int* pData = static_cast<unsigned int*>(buffer_info.ptr);
        std::vector<ssize_t> shape = buffer_info.shape;
        return std::unique_ptr<BigNumber>(new BigNumber(pData, shape[0]));
      }))

Above portion allows up to 32 bits single variable or a list/array of broken down large integer (each 32bit)

What if I want to take very large integers from Python as an input to fill the BigNumber class, and also what should I do to define a def_property_readonly() function that returns as a Python understandable large integer object?

Edit1: Passing large integer from Python to C++ With the help of BigNumber(const char* s) constructor, it can be done by:

      .def(py::init([](py::object obj){
        std::string s_obj = py::str(obj);
        return std::unique_ptr<BigNumber>(new BigNumber(&s_obj[0]));
      }))

Figuring out how to do reverse, probably std::string to py::object with py::cast?

Ghostx
  • 113
  • 5
  • 2
    I'm a hack. I turn big numbers into strings when I need to move data between programs and languages. – user4581301 Nov 06 '21 at 00:07
  • 2
    `C/C++` -- There is no such language. It's either C or C++. – PaulMcKenzie Nov 06 '21 at 00:08
  • I found a way to pass large integer from python to C++ using ```BigNumber(const char* s);``` constructor. Convert ```py::object obj``` to string with ```py::str(obj)``` then use the char* constructor. – Ghostx Nov 06 '21 at 00:26
  • 3
    Note that converting between decimal and binary actually has a really bad asymptotic runtime, so if you're going to go through a string representation, at least try to use hex or binary or something instead of decimal. – user2357112 Nov 06 '21 at 00:39
  • 3
    You're my kind of hack. Going with a string is simple and damn near foolproof. It's a great place to start (and stay if there are no performance concerns). Small recommendation: replace `return std::unique_ptr(new BigNumber(&s_obj[0]));` with `return std::make_unique(s_obj.c_str());`. [There's less room for error](https://stackoverflow.com/questions/22571202/differences-between-stdmake-unique-and-stdunique-ptr-with-new). – user4581301 Nov 06 '21 at 00:39
  • 2
    If you change to using `GMP` for numbers at the C level (pretty sure there are C++ wrappers), there's probably a direct way to create instances of `gmpy2.xmpz` (the mutable one) or `gmpy2.mpz` (immutable, which means you definitely have to copy - but then, you might have to anyway). Even if you don't, you could probably check how *it* does the `int`<->`mpz` conversions ... – o11c Nov 06 '21 at 02:31

1 Answers1

4

Answer for first case: Passing very large integer(>256bits) object from Python to C++ with BigNumber

Passing large integer from Python to C++ With the help of BigNumber(const char* s) constructor, it can be done by:

.def(py::init([](py::object obj){
  std::string s_obj = py::str(obj);
  return std::make_unique<BigNumber>(s_obj.c_str());
}))

However, returning to Python with a large integer object is still WIP

I tried returning to python with py::int_ as:

.def("val", [](BigNumber const& self){
  std::string s_hex;
  self.num2hex(s_hex);
  return py::int_(s_hex);
})

However this invokes the same value error when you try

>>> int("0x12345")

in Python.

ValueError: invalid literal for int() with base 10: '0x12345'

Ghostx
  • 113
  • 5