Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Python large integers (>256bits) to and from C++ with pybind11

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?

like image 336
Ghostx Avatar asked Nov 06 '21 00:11

Ghostx


1 Answers

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'

like image 71
Ghostx Avatar answered Oct 20 '22 10:10

Ghostx