void test_fully_homomorphic() { printf("FULLY HOMOMORPHIC (with recrypt)\n"); int i, j, m; mpz_t c0, c1, temp; mpz_init(c0); mpz_init(c1); mpz_init(temp); fhe_pk_t pk; fhe_sk_t sk; fhe_pk_init(pk); fhe_sk_init(sk); for (i = 0; i < KEYRUNS; i++) { mpz_t c0, c1; mpz_init(c0); mpz_init(c1); fhe_pk_t pk; fhe_sk_t sk; fhe_pk_init(pk); fhe_sk_init(sk); fhe_keygen(pk, sk); fhe_encrypt(c0, pk, 0); printf("\nadd-chain: "); for (j = 0; j < RUNS*RUNS; j++) { fhe_add(c0, c0, c0, pk); fhe_recrypt(c0, pk); m = fhe_decrypt(c0, sk); printf("%i", m); fflush(stdout); } fhe_encrypt(c1, pk, 1); printf("\nmul-chain: "); for (j = 0; j < RUNS*RUNS; j++) { fhe_mul(c1, c1, c1, pk); fhe_recrypt(c1, pk); m = fhe_decrypt(c1, sk); printf("%i", m); fflush(stdout); } printf("\n"); } fhe_pk_clear(pk); fhe_sk_clear(sk); mpz_clear(c0); mpz_clear(c1); mpz_clear(temp); printf("PASSED.\n"); }
void test_recrypt() { printf("RECRYPT\n"); mpz_t c0, c1; mpz_init(c0); mpz_init(c1); fhe_pk_t pk; fhe_sk_t sk; fhe_pk_init(pk); fhe_sk_init(sk); for (int i = 0; i < KEYRUNS; i++) { fhe_keygen(pk, sk); for (int j = 0; j < RUNS; j++) { fhe_encrypt(c0, pk, 0); fhe_encrypt(c1, pk, 1); fhe_recrypt(c0, pk); assert(fhe_decrypt(c0, sk) == 0); fhe_recrypt(c1, pk); assert(fhe_decrypt(c1, sk) == 1); printf("."); fflush(stdout); } printf("\n"); } fhe_pk_clear(pk); fhe_sk_clear(sk); mpz_clear(c0); mpz_clear(c1); printf("PASSED.\n"); }
void mesure_encrypt_decrypt(){ fhe_pk_t pk; fhe_sk_t sk; mpz_t c, aux1, aux2; int m; mpz_init(c); mpz_init(aux1); mpz_init(aux2); mpz_set_ui(aux2, 0); fhe_pk_init(pk); fhe_sk_init(sk); struct timeval start, end; long mtime, seconds, useconds; gettimeofday(&start, NULL); fhe_keygen(pk, sk); gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; //gmp_printf("p: %Zd \n", pk->p); //gmp_printf("alpha: %Zd \n", pk->alpha); //gmp_printf("B: %Zd \n", sk->B); //printf("N: %d , log(p): %d , Mu: %d , Nu: %d , keygen: %ld ",N,mpz_sizeinbase( pk->p, 2), MU, LOG_NU, mtime ); gettimeofday(&start, NULL); for(int i=0; i < 100; i++){ fhe_encrypt(c, pk, 1); } gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; printf("encrypt: %ld ms ", mtime ); gettimeofday(&start, NULL); /* for(int i=0; i < 100; i++){ fhe_fulladd(c, aux1,c,c,aux2, pk); }*/ gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; printf("add : %ld ms ", mtime ); gettimeofday(&start, NULL); for(int i=0; i < 100; i++){ fhe_mul(c,c, c, pk); printf("\nCheck decryption depth %d \n",fhe_decrypt(c,sk)); } gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; printf("mul : %ld ms ", mtime ); gettimeofday(&start, NULL); for(int i=0; i < 100; i++){ fhe_recrypt(c, pk); } gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; printf("recrypt : %ld ms ", mtime ); gettimeofday(&start, NULL); for(int i=0; i < 100; i++){ fhe_decrypt(c, sk); } //printf("m : %d ",m); gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; printf("decrypt : %ld ms ", mtime ); gettimeofday(&start, NULL); test_sum_integers(2000 , 49, pk, sk); gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; printf("sum: %ld ms \n", mtime ); printf("N: %d , log(p): %d , Mu: %d , Nu: %d , keygen: %ld ",N,mpz_sizeinbase( pk->p, 2), MU, LOG_NU, mtime ); mpz_clear(c); mpz_clear(aux1); mpz_clear(aux2); fhe_pk_clear(pk); fhe_sk_clear(sk); }