Advertisement
Guest User

Untitled

a guest
Jan 26th, 2013
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <mpir.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. int compare(const void* a, const void* b){
  7.     return ( *(uint64_t*)a - *(uint64_t*)b );
  8. }
  9.  
  10. bool find_in_array(uint64_t* arr, size_t len, uint64_t x){
  11.     size_t first = 0;
  12.     size_t last = len;
  13.  
  14.     size_t mid;
  15.     if(last == 0)
  16.         return false;
  17.     if(arr[0] > x)
  18.         return false;
  19.     if (arr[len - 1] < x)
  20.         return false;
  21.     while (first < last){
  22.         mid = first + (last - first) / 2;
  23.         if (x <= arr[mid]){
  24.             last = mid;
  25.         }else{
  26.             first = mid + 1;
  27.         }
  28.     }
  29.     if(arr[last] == x)
  30.         return true;
  31.     return false;
  32. }
  33.  
  34. int main(){
  35.         mpz_t a, b, m;
  36.         mpz_init_set_str(a,"6511709995802996252529428210009069141516455100602512742258547866292839663070281213795462953359084389612327175129512390242393774885156190907043183764572298",10);
  37.         mpz_init_set_str(b,"4016751268155181878780762958485595786864539552267966173503519872828650364102948010668074636189225260885467109630266705699778224615794326630829490560742794",10);
  38.         mpz_init_set_str(m,"9913033868557133667485171584043308894354447793802229576147965224579616862439637051468618269920939124273132778910108308880842351094700094598897767100778663",10);
  39.         const unsigned long long  n = 1048576;
  40.  
  41.         uint64_t* mem = (uint64_t*)calloc(n+1, sizeof(uint64_t));
  42.        
  43.  
  44.         uint64_t* memp = mem;
  45.         mpz_t tmp; mpz_init_set_ui(tmp,1);
  46.         mpz_t tmp2; mpz_init(tmp2);
  47.         mpz_powm_ui(tmp,a,n,m);
  48.         for(uint64_t p = 0; p <= n ;++p){
  49.             mpz_mul(tmp,tmp,a);
  50.             mpz_mod(tmp,tmp,m);
  51.             mpz_export(memp,NULL,0,sizeof(uint64_t),0,0,tmp);
  52.             memp++;
  53.         }
  54.         printf("OK!\n");
  55.         qsort(mem,n,sizeof(uint64_t),compare);
  56.         printf("OK2!\n");
  57.         uint64_t elem = 0;
  58.         mpz_set(tmp, b);
  59.         for(uint64_t q = 0; q!=UINT64_MAX ; ++q){
  60.             //mpz_powm_ui(tmp, a, q, m);
  61.             mpz_mul(tmp,tmp,a);
  62.             mpz_mod(tmp, tmp, m);
  63.             mpz_export(&elem,NULL,0,sizeof(uint64_t),0,0,tmp);
  64.             if(find_in_array(mem,n,elem)){
  65.                 printf("OK2!\n");
  66.                 break;
  67.             }
  68.         }
  69.         free(mem);
  70.         return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement