如何在qt平台上使用mingw32位编译器处理C ++中的128位变量? [英] How to deal with 128 bit variable in C++ with mingw32 bit compiler on qt platform ?

查看:545
本文介绍了如何在qt平台上使用mingw32位编译器处理C ++中的128位变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在cpp代码中被困在一个euqation中,请帮我解决它,



我正在使用,

Qt 4.8.7

编译器MinGW32

(使用升级库提升检查1.70)



我想要在其中一个代码中使用以下等式

A = g ^ a mod p; // g升至模数p。 (类似于2 ^ 5%3)= 32%3 = 2

(此等式看起来像Diffie Hellman算法的安全性)



哪里,

^(电源)

g是固定数字0x05

a是128位(16字节)randomely生成的数字,

和p是固定十六进制数128bit(16bytes)=(0xD4A283974897234CE908B3478387A3)。



我使用Qt 4.8.7

with MinGW32位编译器,





我知道这个操作似乎很长(它应该需要一些时间),但这是要求<来自客户的
,不能更改,



所以你们中任何一个人都知道如何解决这个问题,那么请让我知道。



提前谢谢。



我的尝试:



解决方案我发现哪些对我不起作用如下:

我知道了,

1可以使用__int128,但要支持那个应该使用

最新的gcc编译器或MinGW64位编译器,我现在都没用。



2我发现Qt的一个最新版本有QSslDiffieHellmanParameters类,

但我们的Qt版本再次不支持。



3我发现了一些像boost / multiprecision / cpp_int.hpp这样的库(boost 1.70 ))

确实有数据类型,如int128_t和int256_t,但是由于我们的编译器是或者其他什么,我们无法存储

128位数字,意思是

如果我这样做,

Hi all, i am stuck in one euqation in cpp code, please help me to solve it,,

I am using,
Qt 4.8.7
Compiler MinGW32
(checked with boost library boost 1.70)

I want to use below equation in one of the code
A = g^a mod p; //g raise to a modulus p. (something like 2^5 % 3) = 32%3 = 2
(This equation looks like Diffie Hellman algorithm for security)

Where,
^(power)
g is fixed number 0x05
a is 128bit(16bytes) randomely generated number,
and p is fixed hex number of 128bit(16bytes) = (0xD4A283974897234CE908B3478387A3).

I am using Qt 4.8.7
with MinGW32 bit compiler,


I know this operation seems way to long(it is supposed to take some time), but this is requirement
from client, can`t be changed,

So does any one of you have any idea how to tackle this proble, then please let me know.

Thank you in advance.

What I have tried:

Solution i found which didn`t worked for me are listed below:
I got to know,
1 one can use __int128 but to support that one should have used
latest gcc compiler or MinGW64 bit compiler, neither of that i am using now.

2 I found one latest version of Qt has QSslDiffieHellmanParameters class,
but again not supported in our Qt version.

3 I found some libraries like boost/multiprecision/cpp_int.hpp (boost 1.70))
that does have data type such as int128_t and int256_t, but due to
our compiler isssue or something else, we are not able to store
128bit number, meaning
if i do,

int128_t ptval128 = 0xAB1232423243434343BAE3453345E34B;
cout << "ptval128 = " << std::hex << ptval128 << endl;
//will print only 0xAB12324232434343;//half digits only,





4我尝试使用Bigint更有用,但是再次

5 ^(128位数)太大了,它花费数小时来计算东西,

(我等到1小时16分钟并杀死应用程序)。



4 I tried using Bigint which much more useful, but again
5^(128bit number) is way too big, it takes hours to compute things,
(I waited till 1 hour and 16 mins and kill the application).

推荐答案

首先,通过重复平方中间结果,而不是仅仅乘以基数,可以显着加快具有高整数指数的幂表达式的计算速度。这是一个示例实现,您可以适应您喜欢的整数类型:

First of all, you can significantly speed up the calculation of a power expression with a high integer exponent, by repeatedly squaring intermediate results, rather than just multiplying with the base. Here's a sample implementation that you can adapt to your prefered integer type:
#include <iostream>

using namespace std;

#define TEST_PERFORMANCE 1

#ifdef TEST_PERFORMANCE
int n_mult;
#endif
int fast_power(int base, unsigned int exponent)
{
    if (exponent == 0)
       return 1;
    int result = fast_power(base, exponent/2);
    result *= result; // result = base ^ (2 * base/2)
#ifdef TEST_PERFORMANCE
    n_mult++; // counting multiplications to get an estimate for performance
#endif
    if (exponent & 1) // is exponent odd ?
    {
        result *= base;
#ifdef TEST_PERFORMANCE
        n_mult++;
#endif
    }
    return result;
}

int main()
{
#ifdef TEST_PERFORMANCE
    n_mult = 0;
#endif
    cout << "2^10 = " << fast_power(2,10) << endl;
#ifdef TEST_PERFORMANCE
    cout << "multiplications used: " << n_mult << endl;
    n_mult = 0;
#endif
    cout << "2^17 = " << fast_power(2,17) << endl;
#ifdef TEST_PERFORMANCE
    cout << "multiplications used: " << n_mult << endl;
    n_mult = 0;
#endif
    cout << "2^30 = " << fast_power(2,30) << endl;
#ifdef TEST_PERFORMANCE
    cout << "multiplications used: " << n_mult << endl;
#endif

    return 0;
}

如果您愿意,可以将粘贴到此在线C ++编译器 [ ^ ]查看结果。如您所见,乘法的数量明显少于指数。对于128位指数,乘法的数量应该减少到不超过256.



如果有这么高的指数,可能有必要消除递归此功能将其转换为循环。但这不应该太难。



其次,你不需要计算g ^ a的总和(这将是难以存储的),而是你可以在每次乘法后插入一个模p运算,将中间结果置于p的值之上。这不会改变最终结果。

If you like, you can just paste it into this online C++ compiler[^] to see the results. As you can see, the number of multiplications are significantly less than the exponent. For a 128 bit exponent, the number of multiplications should be reduced to no more than 256.

With such a high exponent, it may be necessary to eliminate the recursion in this function and turn it into a loop instead. But that shouldn't be too hard.

Second, you don't need to calculate the total of g^a (which will be tricky to store), instead you can just insert a modulo p operation after every multiplication that puts the intermediate result above the value of p. This doesn't change the end result.


这篇关于如何在qt平台上使用mingw32位编译器处理C ++中的128位变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆