Advertisement
Guest User

Untitled

a guest
May 29th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <random>
  4. #include <thread>
  5. #include <atomic>
  6. #include <cstdint>
  7. #include "Header.h"
  8. #include <vector>
  9. #include <omp.h>
  10. #include "ttmath-0.9.3\ttmath\ttmath.h"
  11. using namespace ttmath;
  12. using namespace std;
  13. UInt<512> GenerateRandom ( size_t _sizeInBits, bool _testForPrimility )
  14. {
  15.     PRIMES;
  16.     _sizeInBits = ( _sizeInBits % 32 ) ? _sizeInBits + ( _sizeInBits % 32 ) : _sizeInBits;
  17.     UInt<512> random_number ( 0 );
  18.     random_device rand_dev;
  19.     uniform_int_distribution<uint32_t> engine ( UINT32_MAX/2 ,UINT32_MAX );
  20.     while ( true )
  21.     {
  22.         random_number = 0;
  23.         for ( auto i = 0; i < _sizeInBits; i+=32 )
  24.         {
  25.             UInt<512> temp = engine ( rand_dev );
  26.             temp <<= i;
  27.             random_number += temp;
  28.         }
  29.         if ( _testForPrimility )
  30.         {
  31.             bool bFlag = true;
  32.             for ( auto & i : primes )
  33.             {
  34.                 if ( random_number % i == 0 )
  35.                 {
  36.                     bFlag = false;
  37.                     break;
  38.                 }
  39.             }
  40.             if ( bFlag )
  41.             {
  42.                 return random_number;
  43.             }
  44.         }
  45.         else
  46.         {
  47.             return random_number;
  48.         }
  49.     }
  50. }
  51.  
  52. bool MillerRabinTest ( UInt<512> m )
  53. {
  54.     random_device rand_dev;
  55.     uniform_int_distribution<uint32_t> engine ( 2 ,UINT32_MAX );
  56.     UInt<512> t = m - 1;
  57.     UInt<512> s = 0;
  58.     UInt<512> x = 0;
  59.     UInt<512> r1 = 2;
  60.     UInt<512> r2 = m - 2;
  61.     UInt<512> a;
  62.     while ( t != 0 && ( t % 2 == 0 ) )
  63.     {
  64.         s++;
  65.         t /= 2;
  66.     }
  67.     UInt<512> r = 1;
  68.     for ( UInt<512> i = 0; i < r; i++ )
  69.     {
  70.         a = r1 + UInt<512> ( engine ( rand_dev ) ) % ( r2 - r1 );
  71.         a.PowMod ( t ,m );
  72.         x = a;
  73.         if( x == 1 || x == m - 1 )
  74.         {
  75.             continue;
  76.         }
  77.         for ( UInt<512> j = 0; j < s - 1; j++ )
  78.         {
  79.             x.PowMod ( 2 ,m );
  80.             if ( x == 1 )
  81.             {
  82.                 return false;
  83.             }
  84.             if ( x == m - 1 )
  85.             {
  86.                 break;
  87.             }
  88.         }
  89.         if ( x == m - 1 )
  90.         {
  91.             continue;
  92.         }
  93.         return false;
  94.     }
  95.     return true;
  96. }
  97.  
  98. UInt<512> PrimitiveRoot ( UInt<512> _prime )
  99. {
  100.     vector<UInt<512>> fact;
  101.     UInt<512> phi = _prime - 1 ,n = phi;
  102.     for ( UInt<512> i = 2; i*i <= n; ++i )
  103.     {
  104.         if ( n % i == 0 )
  105.         {
  106.             fact.push_back ( i );
  107.             while ( n % i == 0 )
  108.             {
  109.                 n /= i;
  110.             }
  111.         }
  112.         i += ( i > 10 ) ? 2 : 0;
  113.     }
  114.     if ( n > 1 )
  115.     {
  116.         fact.push_back ( n );
  117.     }
  118.    
  119.     cout << "Factorization done\n";
  120.     for (UInt<512> res = 2; res <= _prime; ++res )
  121.     {
  122.         bool ok = true;
  123.         for ( size_t i = 0; i < fact.size ( ) && ok; ++i )
  124.         {
  125.             UInt<512> temp = res;
  126.             temp.PowMod ( phi / fact[ i ] ,_prime );
  127.             ok = ( temp != 1 ) ? true : false;
  128.         }
  129.         if ( ok )
  130.         {
  131.             return res;
  132.         }
  133.     }
  134.     return -1;
  135. }
  136. UInt<512> GeneratePrime ( size_t _sizeInBits )
  137. {
  138.     UInt<512> Prime[ 4 ];
  139.     cout << "Mode - " << _sizeInBits << "bit - generation prime";
  140.     atomic<int> id_done ( -1 );
  141.     vector<thread> threads;
  142.     auto PrimalityThread = [ &id_done ,&Prime ,&_sizeInBits ] ( size_t thread_id )-> void
  143.     {
  144.         while ( true )
  145.         {
  146.             if ( id_done != -1 )
  147.             {
  148.                 break;
  149.             }
  150.             Prime[ thread_id ] = GenerateRandom ( _sizeInBits, true );
  151.             cout << ".";
  152.             if ( ( id_done == -1 ) && MillerRabinTest ( Prime[ thread_id ] ) )
  153.             {
  154.                 id_done = thread_id;
  155.                 cout << "done ( thread "<< id_done << " )\n";
  156.                 break;
  157.             }
  158.         }
  159.     };
  160.     for ( int i = 0; i < ((_sizeInBits <= 128 ) ? 1 : 4); i++ )
  161.     {
  162.         threads.push_back ( thread(PrimalityThread, i ) );
  163.     }
  164.     for ( auto &i : threads )
  165.     {
  166.         i.join ( );
  167.     }
  168.     return Prime[ id_done ];
  169. }
  170. void AliceBob ( int key )
  171. {
  172.     UInt<512> PublicPrime = GeneratePrime ( key ); // p
  173.     UInt<512> AliceSecretKey = GenerateRandom ( key ,false ); // a
  174.     UInt<512> BobSecretKey = GenerateRandom ( key ,false );  // b
  175.     cout << "Public Random Prime ( p ): " << PublicPrime << endl;
  176.     UInt<512> PrimeRoot = PrimitiveRoot ( PublicPrime );
  177.     cout << "Primitive root of public prime ( g ): " << PrimeRoot << endl;
  178.     cout << "Alice secret key (a) : " << AliceSecretKey << endl;
  179.     cout << "Bob secret key (b) : " << BobSecretKey << endl;
  180.     UInt<512> temp = PrimeRoot;
  181.     temp.PowMod ( AliceSecretKey ,PublicPrime );
  182.     UInt<512> AlicePublicKey = temp;
  183.     temp = PrimeRoot;
  184.     temp.PowMod ( BobSecretKey ,PublicPrime );
  185.     UInt<512> BobPublicKey = temp;
  186.     cout << "Alice public key (A) : " << AlicePublicKey << endl;
  187.     cout << "Bob public key (B) : " << BobPublicKey << endl;
  188.     UInt<512> AliceValide = BobPublicKey;
  189.     UInt<512> BobValide = AlicePublicKey;
  190.     AliceValide.PowMod ( AliceSecretKey ,PublicPrime );
  191.     BobValide.PowMod ( BobSecretKey ,PublicPrime );
  192.     if ( AliceValide == BobValide )
  193.     {
  194.         cout << "\n\nALICE <-> BOB \nValidation successful!\n" << AliceValide << " = " << BobValide << endl;
  195.     }
  196.     else
  197.     {
  198.         cout << "Validation failed!\n" << AliceValide << " != " << BobValide << endl;
  199.     }
  200. }
  201. void AliceMaloryBob ( int key )
  202. {
  203.     cout << "\n\nATTACK MAN IN THE MIDDLE " << endl;
  204.     UInt<512> PublicPrime = GeneratePrime ( key ); // p
  205.     UInt<512> AliceSecretKey = GenerateRandom ( key ,false ); // a
  206.     UInt<512> BobSecretKey = GenerateRandom ( key ,false );  // b
  207.     UInt<512> MalorySecretKey = GenerateRandom ( key ,false );
  208.     cout << "Public Random Prime ( p ): " << PublicPrime << endl;
  209.     UInt<512> PrimeRoot = PrimitiveRoot ( PublicPrime );
  210.     cout << "Primitive root of public prime ( g ): " << PrimeRoot << endl;
  211.     cout << "Alice secret key (a) : " << AliceSecretKey << endl;
  212.     cout << "Bob secret key (b) : " << BobSecretKey << endl;
  213.     cout << "Malory secret key (c) : " << MalorySecretKey << endl;
  214.     UInt<512> temp = PrimeRoot;
  215.     temp.PowMod ( AliceSecretKey ,PublicPrime );
  216.     UInt<512> AlicePublicKey = temp;
  217.     temp = PrimeRoot;
  218.     temp.PowMod ( BobSecretKey ,PublicPrime );
  219.     UInt<512> BobPublicKey = temp;
  220.     temp = PrimeRoot;
  221.     temp.PowMod ( MalorySecretKey ,PublicPrime );
  222.     UInt<512> MaloryPublicKey = temp;
  223.     cout << "Alice public key (A) : " << AlicePublicKey << endl;
  224.     cout << "Bob public key (B) : " << BobPublicKey << endl;
  225.     cout << "Malory public key (C) : " << MaloryPublicKey << endl;
  226.     UInt<512> AliceValide = MaloryPublicKey;
  227.     UInt<512> BobValide = MaloryPublicKey;
  228.     UInt<512> MaloryBobValide = BobPublicKey;
  229.     UInt<512> MaloryAliceValide = AlicePublicKey;
  230.     AliceValide.PowMod ( AliceSecretKey ,PublicPrime );
  231.     BobValide.PowMod ( BobSecretKey ,PublicPrime );
  232.     MaloryAliceValide.PowMod ( MalorySecretKey ,PublicPrime );
  233.     MaloryBobValide.PowMod ( MalorySecretKey ,PublicPrime );
  234.     cout << "Alice - Bob " << AliceValide << "\t" << BobValide << endl;
  235.     cout << "Alice - Malory " << AliceValide << "\t" << MaloryAliceValide << endl;
  236.     cout << "Malory - Bob " << MaloryBobValide <<"\t" << BobValide << endl;
  237.     if ( AliceValide == BobValide )
  238.     {
  239.         cout << "\n\nALICE <-> BOB \nValidation successful!\n" << AliceValide << " = " << BobValide << endl;
  240.     }
  241.     else
  242.     {
  243.         cout << "\n\nALICE <-> BOB \nValidation failed!\n" << AliceValide << " = " << BobValide << endl;
  244.     }
  245. }
  246. int main ( )
  247. {
  248.     int key;
  249.     cout << "Enter size of key : ";
  250.     cin >> key;
  251.     AliceBob ( key );
  252.     AliceMaloryBob ( key );
  253.     system ( "pause" );
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement