c++binary

Compliment AKS with very large number c++


We have a project that we have to implement AKS with input is a 128-bit-to-512-bit number (input is hexadecimal and I convert it into binary) in C++.

We can use C++ STL only. I would like to know if there are any binary operations or algorithms that I can refer to to complete this task?

I had tried to convert 512-bit number to int but then I realized it is impossible if using STL only. I have thought of checking whether the number is perfect power by making binary divisions by each number smaller than my input number but it takes too much time.


Solution

  • Depends on what is end purpose of using such a value, it's not necessary to treat it as integer. If it is, there are libraries which do that, but they store it internally as a static or dynamic array of bytes (and you can replicate that), and carry perations other byte by byte or in word-long blocks. A lot of vectorizing "magic" may happen there to achieve some sane performance.

    If you need some universal storage, probably std::variant<uint64_t, std::string> or std::variant<uint64_t, std::vector< >> can be a basis. Just store the result of conversion in binary, byte by byte, which is really simple if input is hexadecimal - each pair of characters in input represent a byte of actual value (keeping a possibly omitted leading zero in mind). A conversion from a huge decimal representation into its binary representation would be trickier.