示例#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);
}
示例#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");
}
示例#3
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");
}
示例#4
0
void test_aIsGreater(mpz_t res, fmpz_poly_t polya, fmpz_poly_t polyb, fhe_pk_t pk, int nbits){

	mpz_t a_k;
	mpz_t b_k;
	mpz_t tmp1;
	mpz_t anot;
	mpz_t bnot;
	mpz_t tmp2;
	mpz_t count;

	mpz_init(a_k);
	mpz_init(b_k);
	mpz_init(tmp1);
	mpz_init(tmp2);
	mpz_init(anot);
	mpz_init(bnot);
	mpz_init(count);	

	mpz_t aIsGreater;
	mpz_init(aIsGreater);

	fhe_encrypt(aIsGreater, pk, 0);	
	fhe_encrypt(count, pk, 1);

	for(long k=nbits-1;k>=0;k--){

		fmpz_poly_get_coeff_mpz(a_k, polya,k);	
		fmpz_poly_get_coeff_mpz(b_k, polyb,k);
		
		not(anot, a_k, pk);
		not(bnot, b_k, pk);	

		fhe_mul(tmp1, a_k, b_k, pk);
		fhe_mul(tmp2, anot, bnot,pk); 
		or(tmp1, tmp2, tmp1, pk); //Ek

		fhe_mul(tmp2, a_k, bnot, pk);
		fhe_mul(tmp2, tmp2, count, pk);		
		
		fhe_mul(count, count, tmp1,pk);		
		or(aIsGreater,aIsGreater, tmp2,pk);		
	}
	mpz_set(res,aIsGreater);
	mpz_clear(anot);
	mpz_clear(bnot);
	mpz_clear(count);
	mpz_clear(a_k);
	mpz_clear(b_k);
	mpz_clear(tmp1);
	mpz_clear(tmp2);
	mpz_clear(aIsGreater);
}
示例#5
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);
}
示例#6
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");
}
示例#7
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");
}
示例#8
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);
}
示例#9
0
void ALU_addadr(fhe_int_t *a,fhe_int_t *b,fhe_int_t *res,fhe_pk_t pk)
{
	fhe_int_t t[2];
	fhe_int_t c;
	int i;

	fhe_int_init(t[0]);
	fhe_int_init(t[1]);
	fhe_int_init(c);

	fhe_encrypt(c->v,pk,0);

	for(i=0;i<Memory_ARRAY_COLS;i++)
	{
		Function_fa(a[i], b[i], c,t,pk);
		fhe_int_set(res[i],t[0]);
		fhe_int_set(c,t[1]);
	}
	fhe_int_clear(t[0]);
	fhe_int_clear(t[1]);
	fhe_int_clear(c);
}
示例#10
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);
	
}
示例#11
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;
        }
示例#12
0
void Memory_access2(fhe_int_t *a,fhe_int_t *reg,fhe_int_t rw,fhe_int_t *b1,fhe_pk_t pk,fhe_sk_t sk)
{
	int row;
	int mask;
	int m;
	int i;
	int i2;
	int j;
	fhe_int_t b2[Memory_WORD_SIZE];


	if(!inited)
	{
		inited=1;

		fhe_int_init(rowsel);
		fhe_int_init(nam);
		fhe_int_init(nam2);
		fhe_int_init(nrw);
		fhe_int_init(read);
		fhe_int_init(write);
		fhe_int_init(nsel);
		fhe_int_init(ZERO);
		fhe_encrypt(ZERO->v,pk,0);
		for(i=0;i<Memory_ARRAY_ROWS;i++)
			fhe_int_init(r[i]);
	}
//	for(i=0;i<Memory_WORD_SIZE;i++)
//		fhe_int_init(b2[i]);

//	puts("memory access 1");fflush(stdout);
	//this loop generates a positive row signal for the *one* selected row
//	if(fhe_decrypt(rw->v,sk)==1)
//		puts("  read request");
//	else
//		puts("  write request");

	for(i=0;i<13;i++)
	{
		fhe_int_init(b2[i]);
		fhe_int_set(b2[i],ZERO);
	}

	Function_not(nrw,rw,pk);

	for(row=0;row<4/*Memory_ARRAY_ROWS*/;row++)
	{
		/* SCHRITT 1:
		 *
		 * fuer jede row ein select-signal generieren
		 *
		 * dieser abschnitt implementiert die AND-logik eines
		 * selektor-schaltkreises; das vielstellige AND wird
		 * durch viele zweistellige ANDs umgesetzt:
		 * AND(a,b,c,d) = AND(AND(AND(a,b),c),d)
		 *
		 * es wird durch die binaeren darstellungen der row-nummer
		 * geritten und mit der angelegten adresse a[0..COLS] "verglichen":
		 *
		 * row[1]=row[1000]= a[0] & !a[1] & !a[2] & !a[3]
		 * row[5]=row[1010]= a[0] & !a[1] &  a[2] & !a[3]
		 *
		 * am ende enthaelt nur eine row die 1 (sofern mit einer gueltigen
		 * adresse zugegriffen wird)
		 *
		 */

		mask=4;
		//das "innere" AND von hand...
		Function_not(nam,a[0],pk);
		Function_not(nam2,a[1],pk);
		Function_and2(r[row],(row&1)>0?a[0]:nam,(row&2)>0?a[1]:nam2,pk);

		// ...die weiteren ANDs werden "aufaddiert"
		for(m=2;m<Memory_ARRAY_COLS;m++,mask<<=1)
		{
			Function_not(nam,a[m],pk);
			Function_and2(r[row],r[row],(row&mask)>0?a[m]:nam,pk);
		}
		Function_not(nam,r[row],pk);

		/*
		 * SCHRITT 2:
		 *
		 * speicherzelle mit select-signal verknuepfen; dieser abschnitt
		 * implementiert den zweiten teil der AND-logik
		 *
		 */
		for(m=0;m<Memory_WORD_SIZE;m++)
		{
			Function_and2(read,Memory_cellarray[row][m],r[row],pk);
			Function_and2(write,b1[m],r[row],pk);

			/*
			 * SCHRITT 3:
			 *
			 * refresh der speicherzellen des data-compartments
			 * (alter wert wenn row-select=0, sonst neuer wert aus register)
			 */
			if(m<8)
			{
				//set memory cell
				Function_and3(read,Memory_cellarray[row][m],r[row],rw,pk);
				Function_and3(write,b1[m],r[row],nrw,pk);
				Function_and2(nsel,Memory_cellarray[row][m],nam,pk);
				Function_or3(Memory_cellarray[row][m],read,write,nsel,pk);
			}
			/*
			 * SCHRITT 4:
			 *
			 * register schreiben; dies ist die OR-logik des selektor-
			 * schaltkreises
			 */
			Function_or3(b2[m],b2[m],read,write,pk);
		}
	}

	for(m=0;m<13;m++)
	{
		fhe_int_set(b1[m],b2[m]);
		fhe_int_clear(b2[m]);
	}

	return;
}
示例#13
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);			


}
示例#14
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);


}
示例#15
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);
}
示例#16
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); 
}
示例#17
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");
   		 }
  	}	



}
示例#18
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);
}