Advertisement
Guest User

blummicali

a guest
Jan 22nd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include "Blum.hpp"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4.  
  5. using namespace std;
  6.  
  7. int losowe()                                                                    //Generator wartosci x0
  8. {
  9.     int a;
  10.     random_device device;
  11.     mt19937 generator(device());
  12.     uniform_int_distribution<int> distribution(65, 122);
  13.     a = distribution(generator);
  14.     if (a > 90 && a < 97) { a = losowe(); } // eliminuje tutaj znaki inne niż litery
  15.     return a;
  16. }
  17.  
  18. //HugeInt power( HugeInt x, HugeInt y, HugeInt p)
  19. //{
  20. //    HugeInt res = HugeInt("1"); /* wynik */
  21. //
  22. //    x = x % p;
  23. //    while (y > 0)
  24. //    {
  25. //        if (y%2 == 1) {
  26. //            res *= x;
  27. //            res = res % p;
  28. //        }
  29. //        x *= x;
  30. //        x = x % p;
  31. //        y /= 2;
  32. //    }
  33. //    return res;
  34. //}
  35. HugeInt modpow(HugeInt base, HugeInt exp, HugeInt modulus) {
  36.     base = base % modulus;
  37.     HugeInt result = 1;
  38.     while (exp > 0) {
  39.         if (exp % 2 == 1) result = (result * base) % modulus;
  40.         base = (base * base) % modulus;
  41.         exp /= 2;
  42.     }
  43.     return result;
  44. }
  45.  
  46. vector<bool> BlumMicali( int dlugosc)
  47. {
  48.     srand( time( NULL ) );
  49.     HugeInt a = HugeInt("12360923874510234561092345619023465019234561092345601234965123495619234659123465901234650923456019432560923456109234650059360928365127965012397561230489751209571627389519659185234750129875104928502");
  50.     HugeInt p = HugeInt("97512978125816728651326745927562561375169519415679951917625138884594562974219756961687292466758265957415675231498987423728564198147423285187561498415673281280265418907820596742182062749517488941522");
  51.  
  52.     a = HugeInt("23456190234650192345610923456012349651234956192346591234659012346509234560194325609232345619023465019234561092345601234965123495619234659123465901234650923456019432560923");
  53.     p = HugeInt("99324320234650192345610923456012349651234956192346591234659012346509234560194325609232345619023465019234561092345601234965123495619234659123465901234650923456019432560923");
  54.  
  55.     HugeInt x0 = losowe();
  56.     cout << x0 ;
  57.     HugeInt x;
  58.  
  59.     vector<bool> klucz;
  60.  
  61.     for( int i=0; i < dlugosc * 8; i++)
  62.     {
  63.         x = modpow(a, x0, p);
  64.         //cout << x << '\t';
  65.         if( x > (p-1)/2 ) klucz.push_back(1);
  66.         else klucz.push_back(0);
  67.         x0 = x;
  68.     }
  69.     return klucz;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement