Esempio n. 1
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);
}
Esempio n. 2
0
void test_encryt_decrypt() {
	printf("ENCRYPT/DECRYPT\n");
	int i, j, m0, m1;
	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 (i = 0; i < KEYRUNS; i++) {
		fhe_keygen(pk, sk);
		
		for (j = 0; j < RUNS; j++) {
			fhe_encrypt(c0, pk, 0);
			m0 = fhe_decrypt(c0, sk);
			fhe_encrypt(c1, pk, 1);
			m1 = fhe_decrypt(c1, sk);
			
			assert(m0 == 0);
			assert(m1 == 1);
			printf(".");
			fflush(stdout);
		}
		printf("\n");
	}
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);
	mpz_clear(c0);
	mpz_clear(c1);
	printf("PASSED.\n");
}
Esempio n. 3
0
void mesure_test_matrix_prod(){
	/*
	int m, n, p, q, c, d, k ;
  	
	int first[10][10], second[10][10];
 	mpz_t first_enc[10][10];
	mpz_t second_enc[10][10];
	mpz_t multiply_enc[10][10];
	*/
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);
	struct timeval start, end;	
    	long mtime, seconds, useconds;    
	//gettimeofday(&start, NULL);  
	for(int i= 4; i <7; i++){
		gettimeofday(&start, NULL);
		test_matrix_prod( 4*i,4* i, 4*i,4* i,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("size : %d , time : %ld ms \n",4*i, mtime );
	}
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);
}
Esempio n. 4
0
void mesure_sum_integers(){
	//KeyGen
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);

	struct timeval start, end;	
	long mtime, seconds, useconds; 
   
	unsigned a = 0, b= 1;
	for(int i= 0; i < 30; i++){
		a= a*2; b = (2 * b )/2;
		for(int k= 1; k < 100; k++){
			a= a + k ; b = b + 3 * k ;

			gettimeofday(&start, NULL);   

			test_sum_integers(a , b, 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("time: %ld ms \n", mtime );
		}
	}
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);

}
Esempio n. 5
0
void
test_fully_homomorphic()
{
	printf("FULLY HOMOMORPHIC (with recrypt)\n");
	
	int 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 (int 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);
		//fhe_encrypt(c1, pk, 1);
		//printf("\nadd-chain: ");
		//for (int 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 (int 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");
}
Esempio n. 6
0
void essai(){
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);
	mpz_t c;
	mpz_init(c);
	mpz_set_ui(c, 1);
	int m= fhe_decrypt(c, sk);
	printf("m= %d\n",m);
	mpz_clear(c);
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);
}
Esempio n. 7
0
void
test_fulladd()
{
	printf("FULLADD\n");
	mpz_t c0, c1;
	mpz_t sum, carry;
	
	mpz_init(c0);
	mpz_init(c1);
	mpz_init(sum);
	mpz_init(carry);
	
	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);
		
		fhe_encrypt(c0, pk, 0);
		fhe_encrypt(c1, pk, 1);
		
		ASSERT_FULLADD(c0,c0,c0,0,0);
		ASSERT_FULLADD(c1,c0,c0,1,0);
		ASSERT_FULLADD(c0,c1,c0,1,0);
		ASSERT_FULLADD(c1,c1,c0,0,1);
		ASSERT_FULLADD(c0,c0,c1,1,0);
		ASSERT_FULLADD(c1,c0,c1,0,1);
		ASSERT_FULLADD(c0,c1,c1,0,1);
		ASSERT_FULLADD(c1,c1,c1,1,1);
		printf(".");
		fflush(stdout);
	}
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);
	mpz_clear(sum);
	mpz_clear(carry);
	mpz_clear(c0);
	mpz_clear(c1);
	printf(" PASSED.\n");
}
Esempio n. 8
0
void test_keygen(){
	struct timeval start, end;	
    	long mtime, seconds, useconds;    
	//clock_t  START_eval;
	//double T_Elapsed4;
	//START_eval = clock();
 	gettimeofday(&start, NULL);    
	fhe_pk_t pk;
	fhe_sk_t sk;
	fhe_pk_init(pk);
	fhe_sk_init(sk);
	fhe_keygen(pk, sk);
	fhe_pk_clear(pk);
	fhe_sk_clear(sk);
	//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 KeyGen : %ld ms \n", mtime );
}
Esempio n. 9
0
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");
}
Esempio n. 10
0
        int main(int argc, char **argv)
        {
                int org=0;
                char *line;
                int i,j;
                int sourceline=0;

                char skout[500],pkout[500];

                //printf("argc=%d\n",argc);

                if(argc!=3)
                {
                        printf("usage: %s <in> <out>\n",argv[0]);
                        exit(0);
                }

                sprintf(skout,"%s.hcrypt_sk",argv[2]);
                sprintf(pkout,"%s.hcrypt_pk",argv[2]);

                fhe_pk_init(pk);
                fhe_sk_init(sk);

               // fhe_pk_loadkey(pk,argv[3]);
                //fhe_pk_print(pk);

                puts("keygen");
                fhe_keygen(pk,sk);
                fhe_pk_store(pk,pkout);
                fhe_sk_store(sk,skout);

                printf("wrote secret key: %s\n",skout);
                printf("wrote public key: %s\n",pkout);

                symbols=NULL;
                alloc=&symbols;

                FILE *r=NULL;
                FILE *w=NULL;
                //BufferedReader r=new BufferedReader(new FileReader("/tmp/test.asm"));
                r=fopen(argv[1],"r");
                if(r==NULL)
                {
                        puts("source error");
                        return 8;
                }
                //BufferedWriter w=new BufferedWriter(new FileWriter("/tmp/test.obj"));
                w=fopen(argv[2],"w");
                if(w==NULL)
                {
                        puts("target error");
                        return 8;
                }

                //System.out.println("PASS 1");
                puts("pass 1");

                line=(char*)malloc(1024);

                //HashMap<String,Integer> labels=new HashMap<String,Integer>();

                while(!feof(r))
                {
                        char *token, *tokens[100];
                        //String label=null;
                        char *label;
                        int numtokens;
                        //String line=r.readLine();
                        ++sourceline;
                        fgets(line,100,r);


                        if(*line==0)
                                break;

                        if(strstr(line,"INITAC")||strstr(line,"INITPC"))
                                continue;

                        //StringTokenizer st=new StringTokenizer(line," \t\n;");

                        numtokens=0;
                        token=strtok(line," \n\t");
                        while(token!=NULL)
                        {
                                tokens[numtokens]=token;
                                token=strtok(NULL," \n\t");
                                ++numtokens;
                        }

                        label=tokens[0];

                        if(numtokens==0)
                                continue;

                        if(numtokens==2)
                        {
                                if(!(*line==' '||*line=='\t'))
                                {
                                        //++label;
                                        //labels.put(label, new Integer(org));
                                        //printf("put symbol %s=%d\n",label,org);
                                        putsymbol(label,org,1,sourceline);
                                }
                        }
                        else if(numtokens>2)
                        {
                                //++label;
                                //labels.put(label, new Integer(org));
                                //printf("put symbol %s=%d\n",label,org);
                                putsymbol(label,org,1,sourceline);
                        }
                        org++;
                }
                //printlist(symbols);
                printf(" %d symbols\n %d command words\n",numsymbols,org);


                //XXX
                //System.out.println("PASS 2");
                puts("pass 2");
                rewind(r);

                sourceline=0;
                //r=new BufferedReader(new FileReader("test.asm"));

                while(!feof(r))
                {
                        char *token, *tokens[100];
                        //String label=null;
                        char *label,*opcode,*operand;
                        int numtokens,symboladdress,cmd;
                        char line0;
                        struct node_t *search;
                        //String label=null;

                        ++sourceline;
                        fgets(line,100,r);
                        if(feof(r))
                                break;

                        if(*line==0)
                                break;

                        line0=*line;
                        numtokens=0;
                        token=strtok(line," \n\t");
                        while(token!=NULL)
                        {
                                tokens[numtokens]=token;
                                token=strtok(NULL," \n\t");
                                ++numtokens;
                        }

                        //StringTokenizer st=new StringTokenizer(line," \n\t;");
                        if(numtokens==0)
                                continue;

                        if(!strcmp(tokens[0],"INITAC"))
                        {
                                search=findsymbol(symbols,tokens[1]);

                                if(search!=NULL)
                                {
                                        symboladdress=search->i;
                                }
                                else
                                {
                                        symboladdress=atoi(tokens[1]);

                                        if(symboladdress==0&&*tokens[1]!='0')
                                        {
                                                ok=0;
                                                printf("undef'd symbol %s in line %d\n",tokens[1],sourceline);
                                        }
                                }
                                for(i=1;i<256;i*=2)
                                {
                                        if(symboladdress&i)
                                                fhe_encrypt(cipher,pk,1);
                                        else
                                                fhe_encrypt(cipher,pk,0);

                                        gmp_fprintf(w,"%Zd\n",cipher);
                                }
                                continue;
                        }

                        if(!strcmp(tokens[0],"INITPC"))
                        {
                                search=findsymbol(symbols,tokens[1]);

                                if(search!=NULL)
                                {
                                        symboladdress=search->i;
                                }
                                else
                                {
                                        symboladdress=atoi(tokens[1]);

                                        if(symboladdress==0&&*tokens[1]!='0')
                                        {
                                                ok=0;
                                                printf("undef'd symbol %s in line %d\n",tokens[1],sourceline);
                                        }
                                }

                                for(i=1;i<256;i*=2)
                                {
                                        if(symboladdress&i)
                                                fhe_encrypt(cipher,pk,1);
                                        else
                                                fhe_encrypt(cipher,pk,0);

                                        gmp_fprintf(w,"%Zd\n",cipher);
                                }

                                continue;
                        }



                        label="";
                        opcode="";
                        operand="";

                        if(line0==' '||line0=='\t')
                        {
                                opcode=tokens[0];
                                if(numtokens==2)
                                        operand=tokens[1];
                        }
                        else
                        {
                                label=tokens[0];
                                opcode=tokens[1];
                                if(numtokens>2)
                                        operand=tokens[2];
                        }

                        cmd=0;

                        if(!strcmp(opcode,".BD"))
                                cmd=100;
                        if(!strcmp(opcode,"STa"))
                                cmd=15;
                        if(!strcmp(opcode,"L"))
                                cmd=14;
                        if(!strcmp(opcode,"ROL"))
                                cmd=13;
                        if(!strcmp(opcode,"ROR"))
                                cmd=12;
                        if(!strncmp(opcode,"ADD",3))
                                cmd=11;
                        if(!strcmp(opcode,"CLC"))
                                cmd=10;
                        if(!strcmp(opcode,"SEC"))
                                cmd=9;
                        if(!strncmp(opcode,"XOR",3))
                                cmd=8;
                        if(!strncmp(opcode,"AND",3))
                                cmd=7;
                        if(!strncmp(opcode,"OR",2))
                                cmd=6;
                        if(!strcmp(opcode,"BEQ"))
                                cmd=5;
                        if(!strcmp(opcode,"J"))
                                cmd=4;
                        if(!strcmp(opcode,"La"))
                                cmd=3;
                        if(!strcmp(opcode,"BMI"))
                                cmd=2;
                        if(!strncmp(opcode,"CMP",3))
                                cmd=1;

                        if(opcode[strlen(opcode)-1]=='a')
                                cmd+=16;

                        if(*operand==0)
                                operand="0";

                        search=findsymbol(symbols,operand);
                        if(search!=NULL)
                        {
                                symboladdress=search->i;
                        }
                        else
                        {
                                symboladdress=atoi(operand);

                                if(symboladdress==0&&*operand!='0'&&cmd<100)
                                {
                                        ok=0;
                                        printf("undef'd symbol %s in line %d\n",operand,sourceline);
                                }
                        }

                        //printf("%s,%s,%s (%d)\n",*label?label:"-",opcode,operand,symboladdress);

                        //System.out.println(label+","+opcode+","+operand+"("+lookup+")");

                        //int[] operation=null;



                        /*
                        operation=Function.encode(5, cmd);

                        int[] argument=Function.encode(8, Integer.parseInt(lookup!=null?lookup:operand));

                        for(int i=0;i<8;i++)
                        {
                                System.out.print(hex(argument[i])+" ");
                                w.write(hex(argument[i])+" ");
                        }
                        for(int i=0;i<5;i++)
                        {
                                System.out.print(hex(operation[i])+" ");
                                w.write(hex(operation[i])+" ");
                        }
                        System.out.println("\t\t"+line);
                        w.write("\n");
                        */
                        //printf("%d %d -> ",cmd,symboladdress);

                        if(cmd<100)
                        {
                                for(i=1;i<256;i*=2)
                                {
                                        if(symboladdress&i)
                                                fhe_encrypt(cipher,pk,1);
                                        else
                                                fhe_encrypt(cipher,pk,0);

                                        gmp_fprintf(w,"%Zd\n",cipher);
                                }

                                for(i=1;i<32;i*=2)
                                {
                                        if(cmd&i)
                                                fhe_encrypt(cipher,pk,1);
                                        else
                                                fhe_encrypt(cipher,pk,0);

                                        gmp_fprintf(w,"%Zd\n",cipher);
                                }
                        }
                        else
                        {
                                if(cmd==100) //.BD binary data
                                {
                                        if(strlen(operand)!=13)
                                        {
                                                printf("binary dataword length mismatch in line %d\n",sourceline);
                                        }
                                        for(i=0;i<13;i++)
                                        {
                                                if(operand[i]!='0'&&operand[i]!='1')
                                                {
                                                        printf("binary dataword syntax error in line %d\n",sourceline);
                                                }
                                                if(operand[i]=='0')
                                                        fhe_encrypt(cipher,pk,0);
                                                else
                                                        fhe_encrypt(cipher,pk,1);

                                                gmp_fprintf(w,"%Zd\n",cipher);
                                        }
                                }
                        }
                        //printf(".\n");
                }

                printf("---- reference table start ----\n");
                printlist(symbols);
                printf("---- reference table end   ----\n");
                //XXX
                fclose(w);
                fclose(r);
                freelist(symbols);
                fhe_pk_clear(pk);
                mpz_clear(cipher);

                printf("%s\n",ok?"ok":"errors");

                return 0;
        }
Esempio n. 11
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);			


}
Esempio n. 12
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);


}
Esempio n. 13
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);
}
Esempio n. 14
0
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);
}
Esempio n. 15
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);
	
}