9324bfc02d593ecb31faa926ba213035f809aa44
3 * It uses GMPxx lib and C++11.
4 * It needs the RNG file "/dev/urandom".
6 * Author: Grégory Burri
20 const uint KEY_SIZE_BITS
= 1024;
21 const uint RSA_PUBLIC_EXPONENT
= 65537;
23 bool testRsaStandard()
25 const auto& keys
= RsaStd::generateRSAKeys(RSA_PUBLIC_EXPONENT
, KEY_SIZE_BITS
);
26 const auto& kPub
= keys
.first
;
27 const auto& kPriv
= keys
.second
;
29 mpz_class message
= Rand::randSize(KEY_SIZE_BITS
/ 2);
30 mpz_class signature
= RsaStd::sign(message
, kPriv
);
32 return Rsa::verifySignature(message
, signature
, kPub
) && !Rsa::verifySignature(message
+ 1, signature
, kPub
);
37 const auto& keys
= RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT
, KEY_SIZE_BITS
);
38 const auto& kPub
= keys
.first
;
39 const auto& kPriv
= keys
.second
;
41 mpz_class message
= Rand::randSize(KEY_SIZE_BITS
/ 2);
42 mpz_class signature
= RsaCrt::sign(message
, kPriv
);
44 return Rsa::verifySignature(message
, signature
, kPub
) && !Rsa::verifySignature(message
+ 1, signature
, kPub
);
47 int timeSignRsaStd(int N
)
50 const auto& keys
= RsaStd::generateRSAKeys(RSA_PUBLIC_EXPONENT
, KEY_SIZE_BITS
);
52 for (int i
= 0; i
< N
; i
++)
54 mpz_class message
= Rand::randSize(KEY_SIZE_BITS
/ 2);
55 RsaStd::sign(message
, keys
.second
);
61 int timeSignRsaCRT(int N
)
64 const auto& keys
= RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT
, KEY_SIZE_BITS
);
66 for (int i
= 0; i
< N
; i
++)
68 mpz_class message
= Rand::randSize(KEY_SIZE_BITS
/ 2);
69 RsaCrt::sign(message
, keys
.second
);
75 void measuresRsaDurations()
78 int timeRsaStd
= timeSignRsaStd(N
);
79 int timeRsaCRT
= timeSignRsaCRT(N
);
81 cout
<< N
<< " x RSA standard: " << timeRsaStd
<< " ms" << endl
;
82 cout
<< N
<< " x RSA CRT: " << timeRsaCRT
<< " ms" << endl
;
83 cout
<< "Speedup: " << (double(timeRsaStd
) / double(timeRsaCRT
)) << endl
;
86 int main(int argc
, char** argv
)
89 for (int i
= 0; i
< argc
; i
++)
90 args
.push_back(string(argv
[i
]));
92 if (!testRsaStandard())
93 cout
<< "RSA standard failed!" << endl
;
96 cout
<< "RSA CRT failed!" << endl
;
98 measuresRsaDurations();