加载RFC5114中定义的1024bit P和G值到boost uint1024

Load 1024bit P and G value defined in the RFC5114 into boost uint1024

我正在尝试实施 diffie-hellman 密钥交换系统。对于我的P和G值,我打算使用RFC5114 2.1中定义的那个。所以我有以下代码:

#代码

    #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;
    }

#输出

    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

#警告 警告:整数常量对其类型而言太大

所以... ###我在这里做错了什么?我该如何解决这个问题?

您可以从字符串构造整数:

uint1024_t P = "0xB10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371";

然而,这将在运行时构造文字。如果你想在编译时构造它,你可以使用 boost::multiprecision:

提供的用户定义文字
constexpr auto P = 0xB10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371_cppui1024;

C++ 中整数文字的类型最多为 (unsigned) long long int(如果实现支持,则为更大的类型)。在您的情况下,文字太大,因此将缩小为该类型。

请参阅 boost::multiprecision 文档 part 1 and part 2