Sometimes my encrypt code is going well, but sometimes it got buffer overflow or did'nt get the right answer when I try to parallel it. I don't know why. This is my code:
#ifdef _OPENMP #pragma omp parallel shared(result) //#pragma omp parallel for shared(M,C) private(x) schedule(static) default(shared) #endif do { mpz_powm(MN, M, N, pub.n); /* MN = M^N mod n */ mpz_mul(result, result, MN); /* result = result * MN */ mpz_mod(result, result, pub.n); /* result = result mod n */ } while(!tmp); unsigned int i, j; unsigned int core = (unsigned int) omp_get_num_procs(); for(i = 1; i < core; i++) { if(mpz_get_ui(pub.e) % core == i) { for(j = 1; j <= i; j++) { mpz_mul(result, result, M); /* result = result * M */ mpz_mod(result, result, pub.n); /* result = result mod n */ } } } mpz_mod(C, result, pub.n); /* C = result mod n */
mpz_powmin parallel, but the followingmpz_mulwritesresultin which case you have to synchronize the processes on write.tmpnot defined and certainly not updated in thedo...while(!tmp);loop 2) in a (for instance) 4 core CPU, this line:for(i = 1; i < core; i++)will only cover 3 of the cores