Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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'

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!