Пример #1
0
void bitonicMergeDown(fmpz_poly_t *poly_nums, int  nbits, int n,  int lo , int  high, fhe_sk_t sk, fhe_pk_t pk){
	if(high==0) return;

	//////////// SWAP //////////////////

	mpz_t * max;
	mpz_t * min;
	max = malloc(sizeof(mpz_t) * nbits);
	min = malloc(sizeof(mpz_t) * nbits);
	for(int i=0;i<nbits;i++){
		mpz_init(max[i]);
		mpz_init(min[i]);
	}


	for(int i = 0; i<high;i++){
		min_max(min, max, poly_nums[i+lo], poly_nums[i+lo+high], pk, nbits);		
		for(int k=0;k<nbits;k++){
			fmpz_poly_set_coeff_mpz(poly_nums[i+lo+high], k, min[k]);
			fmpz_poly_set_coeff_mpz(poly_nums[i+lo],k, max[k]);
		}
			
	}

	bitonicMergeDown(poly_nums, nbits, n, lo, high/2,  sk, pk);
	bitonicMergeDown(poly_nums, nbits, n, lo+high, high/2,  sk, pk);
	for(int k=0;k<nbits;k++){
		mpz_clear(max[k]);
		mpz_clear(min[k]);
	}
	free(max);
	free(min);	
}
Пример #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);
}
Пример #3
0
void OddEvenMerge(fmpz_poly_t * poly_nums, int n, int nbits, int lo, int high, int r, fhe_pk_t pk){

	//printf("Inside OddEven Merge");		
	mpz_t * max;
	mpz_t * min;
	max = malloc(sizeof(mpz_t) * nbits);
	min = malloc(sizeof(mpz_t) * nbits);
	for(int i=0;i<nbits;i++){
		mpz_init(max[i]);
		mpz_init(min[i]);
	}

	
	int m = 2*r;
	
	if(m<high){
		OddEvenMerge(poly_nums, n, nbits, lo, high, m, pk); // even subsequence
		OddEvenMerge(poly_nums, n, nbits, lo+r, high, m, pk); //odd subsequence	
	for(int i=lo+r; i+r<lo+high; i+=m){
			min_max(min, max, poly_nums[i], poly_nums[i+r], pk, nbits);
			for(int k=0;k<nbits;k++){
				fmpz_poly_set_coeff_mpz(poly_nums[i], k, min[k]);
				fmpz_poly_set_coeff_mpz(poly_nums[i+r],k, max[k]);
			}
		


		}

	}	
	else {
		min_max(min, max, poly_nums[lo], poly_nums[lo+r], pk, nbits);		
		for(int k=0;k<nbits;k++){
			fmpz_poly_set_coeff_mpz(poly_nums[lo], k, min[k]);
			fmpz_poly_set_coeff_mpz(poly_nums[lo+r],k, max[k]);
		}


		
	}

	for(int k=0;k<nbits;k++){
		mpz_clear(max[k]);
		mpz_clear(min[k]);
	}
	free(max);
	free(min);
	
	
}
Пример #4
0
void fmpz_poly_disc_gauss_rounding(fmpz_poly_t rop, const fmpq_poly_t x, const mpfr_t r_f, gmp_randstate_t randstate) {
  mpfr_t xi;  mpfr_init2(xi, mpfr_get_prec(r_f));
  mpf_t xi_f; mpf_init2(xi_f, mpfr_get_prec(r_f));
  mpq_t xi_q; mpq_init(xi_q);
  mpz_t s_z;  mpz_init(s_z);

  const long n = fmpq_poly_length(x);
  const size_t tau = (ceil(2*sqrt(log2((double)n))) > 3) ? ceil(2*sqrt(log2((double)n))) : 3;

  fmpz_poly_zero(rop);

  for(int i=0; i<n; i++) {
    fmpq_poly_get_coeff_mpq(xi_q, x, i);
    mpf_set_q(xi_f, xi_q);
    mpfr_set_f(xi, xi_f, MPFR_RNDN);

    dgs_disc_gauss_mp_t *D = dgs_disc_gauss_mp_init(r_f, xi, tau, DGS_DISC_GAUSS_UNIFORM_ONLINE);
    D->call(s_z, D, randstate);
    dgs_disc_gauss_mp_clear(D);

    fmpz_poly_set_coeff_mpz(rop, i, s_z);
  }
  mpz_clear(s_z);
  mpq_clear(xi_q);
  mpf_clear(xi_f);
  mpfr_clear(xi);
}
Пример #5
0
void sum32_ui(fmpz_poly_t s, unsigned a , unsigned b, fhe_pk_t pk  , fhe_sk_t sk){
	unsigned aux1 = a, aux2 = b;
	int i=0;
	mpz_t c1, c2;
	mpz_init(c1); mpz_init(c2); 
	fmpz_poly_t poly_a, poly_b;
	fmpz_poly_init(poly_a); fmpz_poly_init(poly_b);
	//
	//printf("aux1[i] -----> %i\t", aux1 % 2);
	//printf("aux2[i] -----> %i\n", aux2 % 2);

	fhe_encrypt(c1, pk, aux1 % 2);
	fhe_encrypt(c2, pk, aux2 % 2);
	fmpz_poly_set_coeff_mpz( poly_a , i , c1 );
	fmpz_poly_set_coeff_mpz( poly_b , i , c2 );
	aux1 = aux1 >> 1;
	aux2 = aux2 >> 1;
	//
	while( aux1 != 0 || aux2 != 0){		
		//printf("aux1[i] -----> %i\t", aux1 % 2);
		//printf("aux2[i] -----> %i\n", aux2 % 2);
		fhe_encrypt(c1, pk, aux1 % 2);
		fhe_encrypt(c2, pk, aux2 % 2);
		i++;
		fmpz_poly_set_coeff_mpz (poly_a , i , c1 );
		fmpz_poly_set_coeff_mpz (poly_b , i , c2 );
		aux1 = aux1 >> 1;	
		aux2 = aux2 >> 1;	
	}
	//
	test_sum_mpz_t(s, poly_a, poly_b, pk , sk );
	printf("nb de bits %d ", i+1);
	//
	mpz_clear(c1); mpz_clear(c2); 
	fmpz_poly_clear(poly_a); fmpz_poly_clear(poly_b);
}
Пример #6
0
void
fmpz_poly_rand_coeff_even(fmpz_poly_t poly, int n, ulong length, gmp_randstate_t* state)
{
	mpz_t c, exp_length_half;
	mpz_init(c);
	mpz_init(exp_length_half);
	
	mpz_ui_pow_ui(exp_length_half, 2, length-1);
	
	for (int i = 1; i<n; i++) {
		mpz_urandomb(c, *state, length);
		mpz_sub(c, c, exp_length_half);
		mpz_mul_ui(c, c, 2);
		fmpz_poly_set_coeff_mpz(poly, i, c);
	}
	
	mpz_clear(c);
	mpz_clear(exp_length_half);
}
Пример #7
0
int dgsl_rot_mp_call_identity(fmpz_poly_t rop,  const dgsl_rot_mp_t *self, gmp_randstate_t state) {
  assert(rop); assert(self);

  const long n = self->n;
  mpz_t  tmp_g;  mpz_init(tmp_g);

  fmpz_poly_zero(rop);
  fmpz_poly_realloc(rop, n);

  for(long i=0; i<n; i++) {
    self->D[0]->call(tmp_g, self->D[0], state);
    fmpz_poly_set_coeff_mpz(rop, i, tmp_g);
  }
  _fmpz_poly_set_length(rop, n);
  _fmpz_poly_normalise(rop);

  mpz_clear(tmp_g);

  return 0;
}
Пример #8
0
void test_bitonic_sort(){ // Sorts a bitonic array of 2^n elements
	int n = 32;// Nombre d'entiers à trier
	int nbits = 8 ;// Size in number of bits
	int *list = malloc(sizeof(int)*n); // List d'entiers à trier
	
	fmpz_poly_t * poly_nums;
	poly_nums = malloc(sizeof(fmpz_poly_t) * n);
	
	int aux ; 	
	int a ;

	int i;

	mpz_t c0;
	mpz_t tmp;	
	mpz_init(tmp);

	int d ;
		
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);
	
	mpz_init(c0);
	struct timeval start, end;
	long mtime, seconds, useconds;    
	for(int obs=0;obs<3;obs++){
	////////////// Encryption of the bit sequennces //////////
		srand(time(NULL));
		int mod = (int)pow(2, nbits);
		printf("The array is \n");

		for(int t=0;t<n;t++){
			list[t]= rand()%mod;
			printf("%d ", list[t]);
			
		}
		printf("\n");
		nbits = 0;
		for(int k = 0; k < n ; k++){
			i=0;
			fmpz_poly_init(poly_nums[k]);
			a= list[k];
			aux=a;
			fhe_encrypt(c0, pk, a % 2);
			fmpz_poly_set_coeff_mpz( poly_nums[k] , i , c0 );
			aux = aux >> 1;
			do {
		//printf("--------->%i\n", aux % 2);
				fhe_encrypt(c0, pk, aux % 2);
				i++;
				fmpz_poly_set_coeff_mpz ( poly_nums[k] , i , c0 );
				aux = aux >> 1;
			}while(aux!= 0);
			if(i+1>nbits) nbits=i+1;
		}


	////////////////// Bitonic Sorting : sort from the index 0 till n //////////////
		clock_t  START_eval = clock();
		gettimeofday(&start, NULL);

		bitonicSortUp(poly_nums, nbits, n, 0, n, sk, pk);
		
		double T_Elapsed4 = (double) (clock () - START_eval);
		//T_Elapsed4/=CLOCKS_PER_SEC;
		//printf(" Evaluation took %f clock/sec \n ", T_Elapsed4);
					
		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("%d \t %d \t %f \t %ld \n",n, nbits, T_Elapsed4, mtime);	
		printf("After Bitonic sort \n");
		for (i=0;i<n;i++){
			aux = 0;
			d=0;
			for (int k=nbits-1;k>=0;k--){
				fmpz_poly_get_coeff_mpz(tmp, poly_nums[i],k);
				d = fhe_decrypt(tmp, sk);
				aux = (aux*2)+d;
			} 
		printf("%d ", aux);
		
	}
	printf("\n");


	}
	
	////////////// Decryption /////////////////////


/*		for(i=0;i<n;i++){
		printf(" \n The %d'th number is \n", i);
		for(k=0;k<nbits;k++){
			fmpz_poly_get_coeff_mpz(tmp, poly_nums[i],k);
			printf(" %i ", fhe_decrypt(tmp, sk));
		}
	
	}
	
	printf("\n");	
	mpz_t tmp;	
	mpz_init(tmp);

	int d ;
	printf("\n After Bitonic sort \n");
	for (i=0;i<n;i++){
		aux = 0;
		d=0;
		for (int k=nbits-1;k>=0;k--){
			fmpz_poly_get_coeff_mpz(tmp, poly_nums[i],k);
			d = fhe_decrypt(tmp, sk);
			aux = (aux*2)+d;
		} 
		printf(" %d ", aux);
		
	}
*/	
	printf("\n");
	for(int k=0;k<n;k++)
		fmpz_poly_clear( poly_nums[k]);
	free(poly_nums);
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);	
	mpz_clear(c0);
	mpz_clear(tmp);
	free(list);			


}
Пример #9
0
void test_min_max(){
	
	clock_t START_init = clock();

	struct timeval start, end;
	
    	long mtime, seconds, useconds;    

   	 gettimeofday(&start, NULL);
    	
   
 	
	////////////////  Initialization ////////////////

	unsigned a ,b, aux1, aux2;

	a=2; b=5;  

	printf("a = %d et b = %d\n", a, b);
	aux1 = a ; aux2=b;
	int i = 0;
//	unsigned a =30050183, b= 504195648;
//	unsigned aux1, aux2;


	int nbits;   // Number of bits in the binary representation of the integers

	mpz_t c0, c1;
	fmpz_poly_t poly_c1;
	fmpz_poly_t poly_c2;	
	
	mpz_init(c0);
	mpz_init(c1);

	fmpz_poly_init(poly_c1);
	fmpz_poly_init(poly_c2);

		
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	
	double T_Elapsed1 = (double) ( clock () - START_init ); 
	printf(" Initialization of the variables etc took %f clocks / sec \n ", T_Elapsed1);

	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 Init : %ld milliseconds\n", mtime);
  	
	////////////////////// Initialization Ends ////////////////////
	

	//////////////////////// Key Generation /////////
	
	clock_t  START_keygen = clock();
	gettimeofday(&start, NULL);


	fhe_keygen(pk, sk);

	double T_Elapsed2 = (double) (clock () - START_keygen);	
	printf(" KeyGen took %f clocks/sec \n", T_Elapsed2);
	
	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 milliseconds\n", mtime);
  		
	////////////////////// Key Generation Ends /////////////
	
	////// Encryption of the bit sequences ////////////////

	clock_t  START_enc = clock();
	gettimeofday(&start, NULL);

	fhe_encrypt(c0, pk, a % 2);
	fhe_encrypt(c1, pk, b % 2);

	fmpz_poly_set_coeff_mpz( poly_c1 , i , c0 );
	fmpz_poly_set_coeff_mpz( poly_c2, i, c1 );

	aux1 = aux1 >> 1;
	
	aux2 = aux2 >> 1;
	
	do {
		//printf("--------->%i\n", aux % 2);
		fhe_encrypt(c0, pk, aux1 % 2);
		fhe_encrypt(c1, pk, aux2 %2);
		i++;
		fmpz_poly_set_coeff_mpz ( poly_c1 , i , c0 );
		fmpz_poly_set_coeff_mpz (poly_c2, i, c1);
		aux1 = aux1 >> 1;
		aux2 = aux2 >> 1;

	}while(aux1 != 0 || aux2 !=0);

	nbits=i+1;

	printf("Maximum number of bits is %d", nbits);
	
	double T_Elapsed3 = (double) (clock () - START_enc);
	printf(" Encryption took %f clocks/sec \n ", T_Elapsed3);	
	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 Encryption  : %ld milliseconds\n", mtime);
  	/////////////////// Encryption Ends /////////////



	/////////// Evaluation ////////////////////
	clock_t  START_eval = clock();
	gettimeofday(&start, NULL);

	mpz_t * max;
	mpz_t * min;
	max = malloc(sizeof(mpz_t) * nbits);
	min = malloc(sizeof(mpz_t) * nbits);
	for(i=0;i<nbits;i++){
		mpz_init(max[i]);
		mpz_init(min[i]);
	}


	/////////// Evaluation ////////////////////
	//nbits= i +1;
	//fmpz_poly_t max;
	//fmpz_poly_t min;
	//mpz_t * max;
	//mpz_t * min;
	//fmpz_poly_init(max);
	//fmpz_poly_init(min);
	//max = malloc(sizeof(mpz_t) * nbits);
	//min = malloc(sizeof(mpz_t) * nbits);
	//for(i=0;i<nbits;i++){
	//	mpz_init(max[i]);
	//	mpz_init(min[i]);
	//}

	
	mpz_t a_k;
	mpz_t b_k;
	mpz_t tmp;

	mpz_init(a_k);
	mpz_init(b_k);
	mpz_init(tmp);
	
	mpz_t aIsGreater;
	mpz_init(aIsGreater);

	
	min_max(min, max, poly_c1, poly_c2, pk, nbits);
	
	double T_Elapsed4 = (double) (clock () - START_eval);
	printf(" Evaluation took %f clock/sec \n ", T_Elapsed4);
					
	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 Evaluation : %ld milliseconds\n", mtime);
  	


	//////////////// Evaluation Ends ////////////////

/*
		fmpz_poly_set_coeff_mpz(max , k , tmp) ;
		//mpz_set(max[k],tmp);

		fmpz_poly_get_coeff_mpz(a_k, poly_c1,k);	
		fmpz_poly_get_coeff_mpz(b_k, poly_c2,k);
			
		fhe_mul(b_k, b_k, aIsGreater,pk);
		not(tmp, aIsGreater,pk);
		fhe_mul(a_k, a_k, tmp,pk);
		or(tmp, a_k,b_k, pk);

		fmpz_poly_set_coeff_mpz(min , k , tmp) ;
		//mpz_set(min[k],tmp);		
			
	}*/



	///////////////////// Decryption /////////////////
	

	clock_t  START_dec = clock();
	gettimeofday(&start, NULL);

	aux1= 0; aux2= 0;

	unsigned d; int k;
	for(k=nbits-1; k>=0 ;k--){
		d =  fhe_decrypt(max[k],sk);
		aux1= (aux1 * 2) + d;
		
	}

	printf("le max est: %d \n", aux1);

	for(k=nbits-1;k>=0 ;k--){
		d= fhe_decrypt(min[k],sk);
		aux2= (aux2 * 2) +d;
	}
	printf("le min est: %d\n", aux2);
	
	double T_Elapsed5 = (double) (clock () - START_dec);
	printf(" Decryption took  %f clocks/sec \n ", T_Elapsed5);
	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 Decryption : %ld milliseconds\n", mtime);
  	
	//////////////////////// Decryption Ends /////////////
	
	
	for(k=0;k<nbits;k++){
		mpz_clear(max[k]);
		mpz_clear(min[k]);
	}
	

	free(max);
	free(min);
	fmpz_poly_clear( poly_c1 );
	fmpz_poly_clear( poly_c2 ); 
	
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);	
	mpz_clear(c0);
	mpz_clear(c1);
	mpz_clear(a_k);
	mpz_clear(b_k); 
	mpz_clear(tmp);

	mpz_clear(aIsGreater);


}
Пример #10
0
void test_majority_bit(){
	
	unsigned aux1, aux2;
	int i, nbits ;

	mpz_t c0;
	mpz_t c1;
	fmpz_poly_t poly_c1;
	fmpz_poly_t poly_c2;

	mpz_t tmp;
	mpz_init(tmp);

	fmpz_poly_t tmp1_poly;
	fmpz_poly_init(tmp1_poly);
	fmpz_poly_t tmp2_poly;
	fmpz_poly_init(tmp2_poly);

	mpz_t tmp1 ;
	mpz_init(tmp1);
	mpz_t tmp2 ;
	mpz_init(tmp2);	

	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);
	
	mpz_init(c0);
	mpz_init(c1);
	fmpz_poly_init(poly_c1);
	fmpz_poly_init(poly_c2);
	struct timeval start, end;	
    	long mtime, seconds, useconds;    
	unsigned a = 1300000000;
	printf("a: %d , ", a);
	gettimeofday(&start, NULL);   
	aux1= a ; 
	i=0;
	////// Encryption of the bit sequences ////////////////
	//printf("aux1[i] -----> %i\n", aux1 % 2);
	fhe_encrypt(c0, pk, a % 2);
	fmpz_poly_set_coeff_mpz( poly_c1 , i , c0 );
	i++;		
	aux1 = aux1 >> 1;
	do {		
		//printf("aux1[i] -----> %i\n", aux1 % 2);
		fhe_encrypt(c0, pk, aux1 % 2);
		fmpz_poly_set_coeff_mpz (poly_c1 , i , c0 );
		i++;
		aux1 = aux1 >> 1;	
	}while(aux1 != 0 );
	////////////////////////////////////////

	nbits=i;
	aux2=nbits/2;
	
	////// Encryption of the bit sequences ////////////////
	//printf("aux2[i] -----> %i\n", aux2 % 2);
	fhe_encrypt(c1, pk, nbits % 2);
	i=0;
	fmpz_poly_set_coeff_mpz(poly_c2, i, c1);		
	aux2 = aux2 >> 1;
	do {		
		//	printf("aux2[i]------>%i\n", aux2 % 2);
		fhe_encrypt(c1, pk, aux2 %2);
		i++;
		fmpz_poly_set_coeff_mpz (poly_c2, i, c1);
		aux2 = aux2 >> 1;	
	}while(aux2!=0 );
	////////////////////////////////////////	

	////////////////// Evaluation ///////////////////////	

	fhe_encrypt(tmp1, pk, 0);
	fmpz_poly_set_coeff_mpz(tmp1_poly,0,tmp1);
	//fmpz_poly_set_coeff_mpz(tmp2_poly,0,tmp1);
	//fhe_encrypt(tmp1,pk,1);
	//fmpz_poly_set_coeff_mpz(tmp2_poly,1,tmp1);
	//test_sum_mpz_t(tmp1_poly, tmp2_poly, tmp1_poly,pk,sk);
	
	
	for(long k = 0 ; k<nbits; k++){		
		fmpz_poly_get_coeff_mpz(tmp2, poly_c1, k);
		fmpz_poly_set_coeff_mpz(tmp2_poly,0,tmp2);

		//printf("\n tmp2_poly \n");
		//for (int j=fmpz_poly_degree(tmp2_poly);j>=0;j--){
		//	fmpz_poly_get_coeff_mpz(tmp1,tmp2_poly,j);
		//	printf("%i*", fhe_decrypt(tmp1,sk));
		//}
		//printf("\n tmp1_poly \n");
		//for (int j=fmpz_poly_degree(tmp1_poly);j>=0;j--){
		//	fmpz_poly_get_coeff_mpz(tmp1,tmp1_poly,j);
		//	printf("%i.", fhe_decrypt(tmp1,sk));
		//}		

		test_sum_mpz_t(tmp1_poly, tmp1_poly, tmp2_poly,pk, sk);

		//printf("\n sum_poly \n");
		//for (int j=fmpz_poly_degree(tmp1_poly);j>=0;j--){
		//fmpz_poly_get_coeff_mpz(tmp1,tmp1_poly,j);
		//printf("%i-", fhe_decrypt(tmp1,sk));
		//}			
	}
	
	//printf(" \n Printing the sum of n bits \n");

	/*  for(int k=fmpz_poly_degree(tmp1_poly);k>=0;k--){
	    fmpz_poly_get_coeff_mpz(tmp1,tmp1_poly,k);
	    printf(" %i ",fhe_decrypt(tmp1,sk));
	    }
	    printf(" \n");
	*/
	//// Calling isGreaterfunction //////////

	test_aIsGreater(tmp1,tmp1_poly, poly_c2, pk, fmpz_poly_degree(tmp1_poly)+1);

	//printf("The majority bit in %d is %i", a, fhe_decrypt(tmp1, sk) ) ;
		
	//printf("\n");
	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("nbits: %d , temps: %ld \n",nbits,  mtime );

	fmpz_poly_clear(poly_c1);
	fmpz_poly_clear(poly_c2);
	mpz_clear(c1);
	fmpz_poly_clear(tmp1_poly);
	fmpz_poly_clear(tmp2_poly);
	mpz_clear(tmp1);
	mpz_clear(tmp2);
	fhe_pk_clear(pk);
	mpz_clear(c0);
	fhe_sk_clear(sk);
	mpz_clear(tmp);
}
Пример #11
0
void test_sum_mpz_t(fmpz_poly_t sum_result, fmpz_poly_t poly_c1, fmpz_poly_t poly_c2, fhe_pk_t pk , fhe_sk_t sk ){	
	/////////// Evaluation ////////////////////
	int nbits,ninf, n1, n2;
	n1=fmpz_poly_degree(poly_c1)+1 ;
	n2=fmpz_poly_degree(poly_c2)+1 ;
	if(n1 > n2){
		nbits = n1; ninf = n2;
	} else {
		nbits = n2; ninf = n1;
	}
	//printf("nbits of the sum would be %d", nbits);
		
	/*mpz_t * sum_result;
	sum_result = malloc(sizeof(mpz_t) * (nbits+1));
	for(i=0;i<nbits+1;i++)
	mpz_init(sum_result[i]);*/

	mpz_t tmp;
	mpz_init(tmp);

	//printf("\n In the sum function : The input polynomials are : \n");
/*
	for (int j=fmpz_poly_degree(poly_c1);j>=0;j--){
		fmpz_poly_get_coeff_mpz(tmp,poly_c1,j);
		printf(" %i ", fhe_decrypt(tmp,sk));
	}
	printf(" AND \n");

	for (int j=fmpz_poly_degree(poly_c2);j>=0;j--){
		fmpz_poly_get_coeff_mpz(tmp,poly_c2,j);
		printf(" %i ", fhe_decrypt(tmp,sk));
	}
*/	

	mpz_t a_k, b_k, c_in, c_out, aux;
	mpz_init(a_k);	mpz_init(b_k);	mpz_init(c_in);	
	mpz_init(c_out); mpz_init(aux);
	fhe_encrypt(c_in, pk, 0);fhe_encrypt(aux, pk, 0);
	//fhe_encrypt(c_out, pk, 0);

	int k;
	for(k=0;k<ninf;k++){
		fmpz_poly_get_coeff_mpz(a_k, poly_c1,k);	
		fmpz_poly_get_coeff_mpz(b_k, poly_c2,k);
//		printf("for k = %d,  a_k is %i ", k, fhe_decrypt(a_k,sk));
//		printf(" b_k is %i  \n ", fhe_decrypt(b_k, sk));
		//printf("b's bit is %i \n", fhe_decrypt(b_k, sk));
		fhe_fulladd(tmp, c_out, a_k, b_k, c_in, pk);
	//	fmpz_poly_get_coeff_mpz(a_k, sum_result, k);
//		printf("sum at %d th loop is %i \n", k, fhe_decrypt(tmp, sk));
//		printf("Carry at %d th loop is %i\n",k,fhe_decrypt(c_out, sk));
		fmpz_poly_set_coeff_mpz(sum_result,k,tmp);
		mpz_set(c_in, c_out);					
	}
	if(n1 < n2){
		for(k=ninf;k<nbits;k++){
			fmpz_poly_get_coeff_mpz(b_k, poly_c2,k);

			fhe_fulladd(tmp, c_out, aux, b_k, c_in, pk);
		
			fmpz_poly_set_coeff_mpz(sum_result,k,tmp);
			mpz_set(c_in, c_out);					
		}
	} else {
		for(k=ninf;k<nbits;k++){
			fmpz_poly_get_coeff_mpz(a_k, poly_c1,k);	

			fhe_fulladd(tmp, c_out, a_k, aux, c_in, pk);

			fmpz_poly_set_coeff_mpz(sum_result,k,tmp);
			mpz_set(c_in, c_out);	
		}
	}
	fmpz_poly_set_coeff_mpz(sum_result, nbits, c_out);
	
	//////////////// Decryption ///////////////////////////
	//printf("\n The sum is \n");

	//for (k=fmpz_poly_degree(sum_result);k>=0;k--){
	//	fmpz_poly_get_coeff_mpz(tmp, sum_result, k);
		//printf(" %i ",fhe_decrypt(tmp,sk));
	//} 
	//printf("\n Exiting Sum function \n");
	//for(k=0;k<nbits+1;k++)
	//	mpz_clear(sum_result[k]);

	//free(sum_result);
	//fmpz_poly_clear( poly_c1 );
	//fmpz_poly_clear( poly_c2 ); 
	
	
	mpz_clear(c_in); mpz_clear(c_out); mpz_clear(tmp);
	mpz_clear(a_k); mpz_clear(b_k); mpz_clear(aux); 
}
Пример #12
0
void test_insertion_sort(){   // Generate n random numbers of nbits each and sort them
	int n = 32;// Nombre d'entiers à trier
	int nbits = 4; // Size in number of bits
	int *list = malloc(sizeof(int)*n); // List d'entiers à trier
	//printf("\n");

	mpz_t * max;
	mpz_t * min;
	max = malloc(sizeof(mpz_t) * nbits);
	min = malloc(sizeof(mpz_t) * nbits);
	for(int i=0;i<nbits;i++){
		mpz_init(max[i]);
		mpz_init(min[i]);
	}
	
	mpz_t tmp;	
	mpz_init(tmp);
	
	struct timeval start, end;
	long mtime, seconds, useconds;    
	

	fmpz_poly_t * poly_nums;
	poly_nums = malloc(sizeof(fmpz_poly_t) * n);
	
	int aux ; 	
	int a ;

	int i;
	int k;
	mpz_t c0;
	
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);
	
	mpz_init(c0);
	
	for(int obser = 0 ; obser < 3 ;obser++){	
		srand(time(NULL));
		int mod = (int)pow(2, nbits);
		//printf(" The array is \n");
	
		for(int t=0;t<n;t++){
			list[t]= rand()%mod;
		//printf("%d ", list[t]);
		}
	
	nbits=0;
	////////////// Encryption of the bit sequennces //////////
		for(int k = 0; k < n ; k++){
			i=0;
			fmpz_poly_init(poly_nums[k]);
			a= list[k];
			aux=a;
			fhe_encrypt(c0, pk, a % 2);
			fmpz_poly_set_coeff_mpz( poly_nums[k] , i , c0 );
			aux = aux >> 1;
			do {
		//printf("--------->%i\n", aux % 2);
				fhe_encrypt(c0, pk, aux % 2);
				i++;
				fmpz_poly_set_coeff_mpz ( poly_nums[k] , i , c0 );
				aux = aux >> 1;
			}while(aux!= 0);
			if(i+1>nbits) nbits = i+1;
		}

	//printf("Number of bits in this case is %d \n ", nbits);
int k;	


	/////////// Evaluation ////////////////////
	
	
	
		
	clock_t  START_eval = clock();
	gettimeofday(&start, NULL);

	
	i=n-2;
	
	while(i>=0){
		for(int j=0;j<=i;j++){
			min_max(min, max, poly_nums[j], poly_nums[j+1],pk, nbits);
				
	
			for(k=0;k<nbits;k++){
				fmpz_poly_set_coeff_mpz(poly_nums[j], k, min[k]);
				fmpz_poly_set_coeff_mpz(poly_nums[j+1],k, max[k]);
			}
		
		}
		i--;
	
	}

	double T_Elapsed4 = (double) (clock () - START_eval);
	//printf(" Evaluation took %f clock/sec \n ", T_Elapsed4);
					
	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("%d \t %d \t %f \t %ld \n",n, nbits, T_Elapsed4, mtime);
  }
	
	//////////////// Decryption ///////////////////////////
/*	
	int d ;
	printf("\n After insertion sort \n");
	for (i=0;i<n;i++){
		aux = 0;
		d=0;
		for (k=nbits-1;k>=0;k--){
			fmpz_poly_get_coeff_mpz(tmp, poly_nums[i],k);
			d = fhe_decrypt(tmp, sk);
			aux = (aux*2)+d;
		} 
		printf(" %d ", aux);
		
	}
	
	printf("\n");
*/
	for(k=0;k<nbits;k++){
		mpz_clear(max[k]);
		mpz_clear(min[k]);
	}
	free(max);
	free(min);	
	for(k=0;k<n;k++)
		fmpz_poly_clear( poly_nums[k]);
	
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);	
	mpz_clear(c0);
	mpz_clear(tmp);
	free(list);
	
}