Add measures and tests.
[crypto_lab3.git] / src / main.cpp
index 9324bfc..03d1173 100644 (file)
@@ -6,81 +6,20 @@
   * Author: GrĂ©gory Burri
   */
 
+#include <vector>
 #include <iostream>
 using namespace std;
 
 #include <gmpxx.h>
 
-#include "Utils.h"
-#include "Rand.h"
-#include "Rsa.h"
-#include "RsaStd.h"
-#include "RsaCrt.h"
+#include "Tests.h"
 
 const uint KEY_SIZE_BITS = 1024;
 const uint RSA_PUBLIC_EXPONENT = 65537;
 
-bool testRsaStandard()
+void printUsage(const string& progName)
 {
-   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;
+   cout << "Usage: " << progName << " [tests|time-measures]" << endl;
 }
 
 int main(int argc, char** argv)
@@ -89,13 +28,12 @@ int main(int argc, char** argv)
    for (int i = 0; i < argc; i++)
       args.push_back(string(argv[i]));
 
-   if (!testRsaStandard())
-      cout << "RSA standard failed!" << endl;
-
-   if (!testRsaCrt())
-      cout << "RSA CRT failed!" << endl;
-
-   measuresRsaDurations();
+   if (args.size() >= 2 && args[1] == "tests")
+      Tests(KEY_SIZE_BITS, RSA_PUBLIC_EXPONENT).runTests();
+   else if (args.size() >= 2 && args[1] == "time-measures")
+      Tests(KEY_SIZE_BITS, RSA_PUBLIC_EXPONENT).runTimeMeasures();
+   else
+      printUsage(args[0]);
 
    return 0;
 }