Example #1
0
void or(mpz_t res, mpz_t a, mpz_t b, fhe_pk_t pk){
	mpz_t aux1, aux2;
	mpz_init(aux1);
	mpz_init(aux2);
	fhe_add(aux1, a, b, pk);
	fhe_mul(aux2, a, b, pk);
	fhe_add(res, aux1, aux2, pk);
	mpz_clear(aux1);
	mpz_clear(aux2);
}
Example #2
0
void test_xor_bits(){
	unsigned m, aux;
	mpz_t c0, c1;
	fmpz_poly_t poly_c;
	mpz_init(c0);
	mpz_init(c1);
	fmpz_poly_init(poly_c);
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);
	int j, nbits;	

	struct timeval start, end;	
	long mtime, seconds, useconds;    
	//clock_t  START_eval;
	//double T_Elapsed4;

	m= 2147483648	;
	for(int i=0; i< 250; i++){
		m= m + 2;
		j=0;
		//printf("--------->%i\n", m % 2);
		fhe_encrypt(c0, pk, m % 2);
		fmpz_poly_set_coeff_mpz ( poly_c , 0 , c0 );
		aux = m;	
		aux = aux >> 1;
		do {
			//	printf("--------->%i\n", aux % 2);
			fhe_encrypt(c0, pk, aux % 2);
			aux = aux >> 1;
			j++;
			fmpz_poly_set_coeff_mpz ( poly_c , j , c0 );
			//fhe_add(c0, c0, c1, pk);
		}while(aux != 0);
		nbits= j+1;
		//START_eval = clock();
		gettimeofday(&start, NULL);    
		mpz_set(c1, c0);
		while(j>0){
			j--;
			fmpz_poly_get_coeff_mpz ( c0 , poly_c , j );
			fhe_add(c1, c0, c1, 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("Elapsed time in KeyGen : %ld ms \n", mtime );

		aux = fhe_decrypt(c1, sk);
		printf("xor de %u: %d bits, runtime: %ld ms\n", m, nbits, mtime);
	}
	mpz_clear(c0);
	mpz_clear(c1);
	fmpz_poly_clear(poly_c);
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);
}
Example #3
0
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");
}
Example #4
0
void not(mpz_t res, mpz_t a, fhe_pk_t pk){
	mpz_t aux, c1;
	mpz_init(aux);
	mpz_init(c1);
	fhe_encrypt(c1, pk, 1);
	fhe_add(res, a, c1, pk);
	mpz_clear(aux);
	mpz_clear(c1);
}
Example #5
0
void test_matrix_prod(int m, int n, int p, int q,fhe_pk_t pk,fhe_sk_t sk){
	
	int c, d, k ;
  	
	int first[30][30], second[30][30];
 	mpz_t first_enc[30][30];
	mpz_t second_enc[30][30];
	mpz_t multiply_enc[30][30];
	

	mpz_t sum;
	mpz_init(sum);
	mpz_t tmp;
	mpz_init(tmp);
	
	/*

	printf("Enter the number of rows and columns of first matrix\n");
  	scanf("%d%d", &m, &n);
	printf("Enter the elements of first matrix\n");
 
 	for ( c = 0 ; c < m ; c++ ){
   		for ( d = 0 ; d < n ; d++ ){
     			scanf("%d", &first[c][d]);
			mpz_init(first_enc[c][d]);
			fhe_encrypt(first_enc[c][d], pk, first[c][d]);
			
		}
 	}

	
 	 printf("Enter the number of rows and columns of second matrix\n");
 	 scanf("%d%d", &p, &q);
	*/
  	if ( n != p )
 	   	printf("Matrices with entered orders can't be multiplied with each other.\n");
	else{
			
   		printf("Enter the elements of second matrix\n");
 		fhe_encrypt(sum, pk, 0);	 
		for ( c = 0 ; c < p ; c++ ){
      			for ( d = 0 ; d < q ; d++ ){
       				scanf("%d", &second[c][d]);
 				mpz_init(second_enc[c][d]);
				fhe_encrypt(second_enc[c][d], pk, second[c][d]);
			}
		}


   		 for ( c = 0 ; c < m ; c++ ){
      			for ( d = 0 ; d < q ; d++ ){
      			 	 for ( k = 0 ; k < p ; k++ ){
					fhe_mul(tmp, first_enc[c][k], second_enc[k][d], pk);
					fhe_add(sum, sum, tmp, pk);
         				
       				 }
 	
        			mpz_init(multiply_enc[c][d]);
				mpz_set(multiply_enc[c][d],sum);
        			fhe_encrypt(sum, pk,0);
     			 }
    		}
 
 	   	printf("Product of entered matrices:-\n");
 
   		 for ( c = 0 ; c < m ; c++ ) {
      			for ( d = 0 ; d < q ; d++ )
        			printf("%d\t", fhe_decrypt(multiply_enc[c][d], sk));
 
      			printf("\n");
   		 }
  	}	



}