0

I am trying to implement a diffie-hellman key exchange system. For my P and G value, I plan to use the one defined in the RFC5114 2.1. So I have the following code:

#Code

    #include <iostream>
    #include <boost/multiprecision/cpp_int.hpp>
    using namespace boost::multiprecision;
    using namespace std;


    int main(){
        uint1024_t P;

        P = 0xB10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371;

        cout << "P is: " << P << endl;

        cout << "Maximum value for uint1024_t is:" <<std::numeric_limits<uint1024_t>::max() << endl;
    
        return 0;
    }

#Output

    P is: 16077765716036174705
    Maximum value for uint1024_t is:179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215
    // that value is equal to 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    // which is larger than the value that i try to assign to P

#Warning warning: integer constant is too large for its type

so... ###What am I doing wrong here? And how can i fix this?

Community
  • 1
  • 1
kc12345
  • 67
  • 9

1 Answers1

1

You can construct the integer from a string:

uint1024_t P = "0xB10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371";

However this will construct the literal at runtime. If you want to have it be constructed at compile time, you can use the user-defined literals provided by boost::multiprecision:

constexpr auto P = 0xB10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371_cppui1024;

The type of an integer literal in C++ is at most (unsigned) long long int (or some larger type if the implementation supports it). In your case the literal is too large and will therefore be narrowed to that type.

See boost::multiprecision documentation part 1 and part 2.

walnut
  • 21,629
  • 4
  • 23
  • 59
  • I believe that correct syntax is uint1024_t P ("0xFFFFFFFFF"); But you are definitely right. Thank you very much for pointing me to that documentation. – kc12345 Aug 23 '19 at 20:58