Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

301
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda