void pollard(mpz_t x_1, mpz_t x_2, mpz_t d, mpz_t n, int init, int c, int counter) { if(mpz_divisible_ui_p(n, 2)) { mpz_set_ui(d, 2); return; } mpz_set_ui(x_1, init); NEXT_VALUE(x_1, x_1, n, c); NEXT_VALUE(x_2, x_1, n, c); mpz_set_ui(d, 1); while (mpz_cmp_ui(d, 1) == 0) { NEXT_VALUE(x_1, x_1, n, c); NEXT_VALUE(x_2, x_2, n, c); NEXT_VALUE(x_2, x_2, n, c); mpz_sub(d, x_2, x_1); mpz_abs(d, d); mpz_gcd(d, d, n); if (counter < 0) { mpz_set_ui(d, 0); return; } else { --counter; } } if(mpz_cmp(d, n) == 0) { return pollard(x_1, x_2, d, n, primes[RAND], primes[RAND], counter); } }
// Precondition: factors is an empty map, n is a positive integer // Postcondition: factors[p] is the exponent of p in prime factorization of n void fact(llui n){ if(!isPrime(n)){ llui fac = pollard(n); fact(n/fac); fact(fac); }else{ map<llui,int>::iterator it; it = factors.find(n); if(it != factors.end()){ (*it).second++; }else{ factors[n] = 1; } } }
int main() { long long int fac1; long long int other_factor,after_e,g1; long long int p, q, n; int flag; long long int t; double start, end, time; printf("Enter the prime number\n"); scanf("%lld",&p); flag = IsPrime(p); if( flag == 0) { printf("wrong input"); exit(0); } printf("Enter another prime number\n"); scanf("%lld",&q); flag = IsPrime(q); if(flag ==0 || p==q) { printf("wrong input"); exit(0); } n= p*q; t = (p-1)*(q-1); //decrypt(after_e); g1 = gcd( p, q); fac1 = pollard(n); printf("Factor is : %lld", fac1); other_factor = n/ fac1; after_e = (fac1-1)*(other_factor-1); start = clock(); end = clock(); time = (double) (end-start)/CLOCKS_PER_SEC; printf("\nTime taken = %f\n", time); return 0; }
int main (int argc, char **argv) { int id, p; long long int *h_primearray,*in; long long int bound= BOUND; long long int n = (PRIME1*PRIME2); long long int h_highestPrime , res; double start, end; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &id); MPI_Comm_size(MPI_COMM_WORLD, &p); MPI_Barrier(MPI_COMM_WORLD); start=MPI_Wtime(); if(id==0) { h_highestPrime = prime(bound, &h_primearray); } MPI_Bcast(&bound, 1, MPI_LONG_LONG,0, MPI_COMM_WORLD); if( bound%p != 0) { printf("unevenly distributed \n"); MPI_Finalize(); return 0; } in = (long long int *) malloc((bound/p) *sizeof(long long int)); MPI_Scatter(h_primearray,(bound/p), MPI_LONG_LONG, in, (bound/p), MPI_LONG_LONG, 0, MPI_COMM_WORLD); res = pollard(in,bound,p,n); if( res> 1 && res!=n) { printf("res= %d\n",res); end=MPI_Wtime(); } MPI_Finalize(); if(id==0) printf("Total time taken =%f\n",end-start); free(h_primearray); free(in); fflush(stdout); return 0; }
int main(){ long long a, b, n; clock_t t; float current; for(int i=0; i<10; i++) { n = rand() % 100000000000 + 1000000000; /*a = rand() % n; b = rand() % n;*/ a=2; b=2; std::cout<<std::endl<<"********************* POLLARD *********************"<<std::endl; t = clock(); pollard(a, b, 1, n); current = ((float)clock() - t); std::cout << std::endl<<"Pollard time: "<<current<<" ms"<<std::endl; std::cout<<std::endl<<"********************* TRIAL DIVISION *********************"<<std::endl; t = clock(); trial_division2(n); current = ((float)clock() - t); std::cout << " | Trial division time: "<<current<<" ms"<<std::endl; } }
int main() { int i; FILE *fp; big K,rid,id,w,a,b,n,q1; miracl *mip=mirsys(200,256); for (i=0;i<NPRIMES;i++) { pp[i]=mirvar(0); rem[i]=mirvar(0); } w=mirvar(0); n=mirvar(0); a=mirvar(0); b=mirvar(0); p=mirvar(0); p1=mirvar(0); q1=mirvar(0); K=mirvar(0); lim1=mirvar(0); lim2=mirvar(0); id=mirvar(0); rid=mirvar(0); order=mirvar(0); printf("Enter ID= "); innum(rid,stdin); getprime("trap1.dat"); copy(p,n); getprime("trap2.dat"); multiply(n,p,n); printf("\ncomposite =\n"); cotnum(n,stdout); premult(rid,256,id); while (jack(id,n)!=1) { /* bad identity - id=256*rid+i */ printf("No Discrete Log. for this ID -- incrementing\n"); incr(id,1,id); } getprime("trap1.dat"); copy(p1,q1); pollard(id,b); getprime("trap2.dat"); pollard(id,a); xgcd(p1,q1,K,K,K); subtract(b,a,w); mad(w,K,w,q1,q1,w); if(size(w)<0) add_r(w,q1,w); subdiv(w,2,w); multiply(w,p1,w); add_r(w,a,w); fp=fopen("secret.dat","w"); otnum(rid,fp); cotnum(w,fp); cotnum(n,fp); fclose(fp); printf("\nDiscrete log (secret key) \n"); cotnum(w,stdout); powltr(PROOT,w,n,id); subdiv(id,256,id); otstr(id,mip->IOBUFF); printf("Check Identity= %s\n",mip->IOBUFF); return 0; }
for (int i = 0, step = 2; i < MAXI && (g == 1 || g == n); i++) { if (i == step) { y = x; step *= 2; } x = (mul(x, x, n) + add) % n; //cerr << x << endl; if (x > y) d = x - y; else d = y - x; g = __gcd(n, d); } if (g != 1 && g != n) return g; return 0; } ... ll n; cin >> n; ll d = max(pollard(n, 1), pollard(n, n - 1)); if (d == 0) cout << "IMPOSSIBLE"; else cout << d << ' ' << n / d;