X-Git-Url: http://git.euphorik.ch/?p=crypto_lab3.git;a=blobdiff_plain;f=src%2Fmain.cpp;h=9324bfc02d593ecb31faa926ba213035f809aa44;hp=ac75df1f1a7ab658ee3163590a67a779f13553cf;hb=91989c2627abc2cdf511f17169e4f862dc55e838;hpb=7975d02c6c1ee679a236087e86955c086f1a9a8e diff --git a/src/main.cpp b/src/main.cpp index ac75df1..9324bfc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,30 +11,91 @@ using namespace std; #include +#include "Utils.h" +#include "Rand.h" #include "Rsa.h" +#include "RsaStd.h" #include "RsaCrt.h" const uint KEY_SIZE_BITS = 1024; const uint RSA_PUBLIC_EXPONENT = 65537; +bool testRsaStandard() +{ + const auto& keys = RsaStd::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS); + const auto& kPub = keys.first; + const auto& kPriv = keys.second; + + mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2); + mpz_class signature = RsaStd::sign(message, kPriv); + + return Rsa::verifySignature(message, signature, kPub) && !Rsa::verifySignature(message + 1, signature, kPub); +} + +bool testRsaCrt() +{ + const auto& keys = RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS); + const auto& kPub = keys.first; + const auto& kPriv = keys.second; + + mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2); + mpz_class signature = RsaCrt::sign(message, kPriv); + + return Rsa::verifySignature(message, signature, kPub) && !Rsa::verifySignature(message + 1, signature, kPub); +} + +int timeSignRsaStd(int N) +{ + Timer timer; + const auto& keys = RsaStd::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS); + + for (int i = 0; i < N; i++) + { + mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2); + RsaStd::sign(message, keys.second); + } + + return timer.ms(); +} + +int timeSignRsaCRT(int N) +{ + Timer timer; + const auto& keys = RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS); + + for (int i = 0; i < N; i++) + { + mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2); + RsaCrt::sign(message, keys.second); + } + + return timer.ms(); +} + +void measuresRsaDurations() +{ + const int N = 10000; + int timeRsaStd = timeSignRsaStd(N); + int timeRsaCRT = timeSignRsaCRT(N); + + cout << N << " x RSA standard: " << timeRsaStd << " ms" << endl; + cout << N << " x RSA CRT: " << timeRsaCRT << " ms" << endl; + cout << "Speedup: " << (double(timeRsaStd) / double(timeRsaCRT)) << endl; +} + int main(int argc, char** argv) { vector args; for (int i = 0; i < argc; i++) args.push_back(string(argv[i])); - const auto& keys = Rsa::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS); - const auto& kPub = keys.first; - const auto& kPriv = keys.second; - - mpz_class message(42); - mpz_class signature = Rsa::sign(message, kPriv); + if (!testRsaStandard()) + cout << "RSA standard failed!" << endl; - mpz_class message2(42); - cout << "verify: " << Rsa::verifySignature(message2, signature, kPub) << endl; + if (!testRsaCrt()) + cout << "RSA CRT failed!" << endl; - mpz_class message3(43); - cout << "verify: " << Rsa::verifySignature(message3, signature, kPub) << endl; + measuresRsaDurations(); return 0; }