Beispiel #1
0
void read_rsa_keyfile(char* path, keys* key_ptr, int pri) {
  FILE *fp;
  fp = fopen(path, "rb");

  if (pri) {
    gmp_fscanf(fp, "%Zx.%Zx", key_ptr->m_pri_exp, key_ptr->m_n);
  } else {
    gmp_fscanf(fp, "%Zx.%Zx", key_ptr->o_pub_exp, key_ptr->o_n);
  }
}
// read hex strings from infile and write final count followed by gmp
// binary format values to output
void prep_hex_input(char *infile, char *outfile)
{
  double start = now();
  fprintf(stderr, "preprocessing input from %s\n", infile);

  FILE *in = fopen(infile, "r");
  assert(in);
  FILE *out = fopen(outfile, "wb");
  assert(out);

  int count=0;
  fwrite(&count, sizeof(count), 1, out);
  mpz_t x;
  mpz_init(x);
  for (;;) {
    int res = gmp_fscanf(in, "%Zx", x);
    if (res == EOF)
      break;
    if (res != 1) {
      fprintf(stderr, "invalid input\n");
      exit(1);
    }
    __mpz_out_raw(out, x);
    count++;
  }
  fclose(in);
  rewind(out);
  fwrite(&count, sizeof(count), 1, out);
  fclose(out);

  fprintf(stderr, "preprocessing %d elements took %0.3fs\n", count, now()-start);
}
Beispiel #3
0
/* _unix_scan(): scan noun from file.
*/
static u2_noun
_unix_scan(u2_wire wir_r,
           FILE*   fil)
{
  c3_i c = fgetc(fil);

  if ( c == '[' ) {
    return _unix_scan_cell(wir_r, fil);
  } 
  else if ( c == '%' )  {
    c3_c buf[1025];

    fscanf(fil, "%1024[a-z-]", buf);
    return u2_bn_string(wir_r, buf);
  }
  else {
    mpz_t amp;

    ungetc(c, fil);
    mpz_init(amp);
    gmp_fscanf(fil, "%Zd", amp);

    return u2_bn_mp(wir_r, amp);
  }
}
Beispiel #4
0
/**
 * Charge du contenu chiffré depuis un fichier.
 * @param fp Définit le descripteur de fichier à utiliser pour charger les données.
 * @return True si les données ont été lues correctement.
**/
bool rsaBloc::loadAsCrypted(FILE * fp)
{
	//erreur
	assert(fp!=NULL);

	//on scan l'entrée et on retourne ok si c'est bon
	return (gmp_fscanf(fp,"%Zd\n",value.get_mpz_t())==1);
}
Beispiel #5
0
int main (int argc, char *argv[]) {
   mpz_t c, m1, m2, pc, q1, q2, e, d1, d2, phi1, phi2, tmp1, tmp2,
    key1, key2;
   FILE *fp, *fp1;
   char chars[3];

   //initializations
   mpz_inits(c, m1, m2, pc, q1, q2, e, d1, d2, phi1, phi2, tmp1, tmp2,
    key1, key2, NULL);

   //Welcome!
   printf("Begin task 4 / 5...\n");
   printf("*******************************\n");
   printf("Welcome to fun with big numbers\n");
   printf("Lets do some decryption\n");
   printf("*******************************\n");

   //Get needed input from file
   fp = fopen("p4in.txt", "r");
   gmp_fscanf(fp, "%Zd\n%Zd\n%Zd\n%Zd", &key1, &key2, &pc, &c);
   gmp_printf("key1=%Zd\n\nkey2=%Zd\n\nCommon factor=%Zd\n\ncipher=%Zd\n\n",
    key1, key2, pc, c);
   fclose(fp);

   //setup calculating needed values
   mpz_cdiv_q(q1, key1, pc);
   mpz_cdiv_q(q2, key2, pc);
   mpz_set_ui(e, 65537);
   mpz_sub_ui(tmp1, pc, 1);
   mpz_sub_ui(tmp2, q1, 1);
   mpz_mul(phi1, tmp1, tmp2);
   mpz_sub_ui(tmp2, q2, 1);
   mpz_mul(phi2, tmp1, tmp2);
   mpz_invert(d1, e, phi1);
   mpz_invert(d2, e, phi2);

   //Lets try and decrypt
   decrypt(&m1, c, d1, key1);
   decrypt(&m2, c, d2, key2);

   fp = fopen("p4out.txt", "w");
   gmp_fprintf(fp, "%Zx\n%Zx\n", m1, m2);
   fclose(fp);
   mpz_clears(c, m1, m2, pc, q1, q2, e, d1, d2, phi1, phi2, tmp1, tmp2,
    key1, key2, NULL);

   fp = fopen("p4out.txt", "r");
   chars[2] = 0;
   while (fscanf(fp, "%c%c", chars, chars + 1) > 0){
     printf("%c", (int)strtol(chars, NULL, 16));
   }
   fclose(fp);

   return 0;
}
Beispiel #6
0
int main(int argc, char **argv)
{
	if (argc < 3)
	{
		printf("Usage: %s inputfile outputfile [pubkeyfile]\n", argv[0]);
		exit(0);
	}

	char *ptxtfname = argv[1];
	char *ctxtfname = argv[2];
	char *pkeyfname;

	if (argc != 4)
	{
		pkeyfname = "id_rsa.pub";
	} else {
		pkeyfname = argv[3];
	}

	FILE *fptxt = fopen(ptxtfname, "r");
	FILE *fctxt = fopen(ctxtfname, "w");
	FILE *fpkey = fopen(pkeyfname, "r");

	pubkey pkey;
	mpz_init(pkey.n);
	mpz_init(pkey.e);
	
	gmp_fscanf(fpkey, "%Zd", &(pkey.n));
	gmp_fscanf(fpkey, "%Zd", &pkey.e);
	
	int c = getc(fptxt); 
	int count = 0;
	mpz_t next; mpz_init(next);
	while (c != EOF)
	{
		encrypt_char(next, c, pkey);
		gmp_fprintf(fctxt, "%Zd\n", next);
		c = getc(fptxt);
	}
}
Beispiel #7
0
/* Read a key from the given FILE pointer. The format of a key file is
 *   d <positive integer>
 *   e <positive integer>
 *   n <positive integer>
 * The "d" line may be omitted for a public key. This is a primitive function
 * that doesn't impose any restraints on the presence of "d", "e", and "n". See
 * rsa_key_load_private and rsa_load_key_public for functions that check these
 * contraints. The return value is -1 if there was an error; 0 otherwise. */
int rsa_key_read(FILE *fp, struct rsa_key *key)
{
	mpz_t value;

	mpz_init(value);
	for (;;) {
		char c;
		mpz_t *target;
		int rc;

		rc = gmp_fscanf(fp, "%c %Zd\n", &c, value);
		if (rc == EOF)
			break;
		if (rc != 2)
			goto fail;

		switch (c) {
		case 'd':
			target = &key->d;
			break;
		case 'e':
			target = &key->e;
			break;
		case 'n':
			target = &key->n;
			break;
		default:
			/* Hmm, what variable was this supposed to be? */
			goto fail;
		}

		/* Has this variable already been assigned? */
		if (mpz_sgn(*target) > 0)
			goto fail;
		/* Make sure the value is positive. */
		if (mpz_sgn(value) <= 0)
			goto fail;

		mpz_set(*target, value);
	}

	mpz_clear(value);

	return 0;

fail:
	mpz_clear(value);
	return -1;
}
Beispiel #8
0
int main(int argc, char* argv[]) {
    bool i = false,
         o = false,
         e = false,
         d = false,
         k = false;
    int c;
    char input_path[255];
    char output_path[255];
    char key_path[255];
    FILE* input_file;
    FILE* output_file;
    FILE* key_file;
    mpz_t kn, ke, kd;
    keypair_t kp;
    while ((c = getopt(argc, argv, "i:o:edk:")) != -1) {
        switch (c) {
            case 'i':
                i = true;
                strcpy(input_path, optarg);
                break;
            case 'o':
                o = true;
                strcpy(output_path, optarg);
                break;
            case 'e':
                e = true;
                break;
            case 'd':
                d = true;
                break;
            case 'k':
                k = true;
                strcpy(key_path, optarg);
                break;
            default:
                abort();
        }
    }
    if (i && o && k) {
        if (!e && !d || e && d) {
            fprintf(stderr, "You must select EITHER e or d.\n");
            return 1;
        }
        input_file = fopen(input_path, "rb");
        output_file = fopen(output_path, "wb");
        key_file = fopen(key_path, "r");
        if (!(input_file && output_file && key_file)) {
            printf("wtf?\n");
            return 1;
        } else {
            mpz_inits(kn, ke, kd, NULL);
            gmp_fscanf(key_file, "%Zd %Zd %Zd", kn, ke, kd);
            fclose(key_file);
            kp = keypair_init_p(kn, ke, kd);
            if (e) {
                keypair_file_encrypt(kp, input_file, output_file);
            } else {
                keypair_file_decrypt(kp, input_file, output_file);
            }
            fclose(input_file);
            fclose(output_file);
            mpz_clears(kn, ke, kd, NULL);
            keypair_free(kp);
            return 0;
        }
    } else {
        fprintf(stderr, "\n### Usage:\n");
        fprintf(stderr, "%s -i file1 -o file2 -k keyfile -{e,d}\n", argv[0]);
        fprintf(stderr, "-i\tinput file\n");
        fprintf(stderr, "-o\toutput file\n");
        fprintf(stderr, "-k\tkey file\n");
        fprintf(stderr, "-e\tencrypt\n");
        fprintf(stderr, "-d\tdecrypt\n\n");
        return 1;
    }
}
int main() {
	pthread_t thread_a, thread_b; /* My threads*/
	int i;
	FILE *filePi, *fileTime;
	clock_t start, end;
	double cpu_time_used;
	mpf_set_default_prec(BITS_PER_DIGIT * 11000000);	
	
	/* Borwein Variable Initialization */   	
	for(i=0; i<2; i++)
    		for(j=0; j<2; j++)
    			mpf_init(params[i][j]);
    	
	mpf_init(real_pi);
	mpf_init(y0Aux);
	mpf_init(y0Aux2);
	mpf_init(a0Aux);
	mpf_init(a0Aux2);
	mpf_init(pi[0]);
    	mpf_init(pi[1]);	
	
	mpf_init_set_str(error, "1e-10000000", 10);

	/* Initial value setting */
	mpf_sqrt_ui(params[A][0], 2.0); /* a0 = sqrt(2)*/
	mpf_mul_ui(params[A][0], params[A][0], 4.0); /* a0 = 4 * sqrt(2) */
	mpf_ui_sub(params[A][0], 6.0, params[A][0]); /* a0 = 6 - 4 * sqrt(2) */ 
	
	mpf_sqrt_ui(params[Y][0], 2.0); /* y0 = sqrt(2) */
	mpf_sub_ui(params[Y][0], params[Y][0], 1.0); /* y0 = sqrt(2) - 1 */
	
	mpf_set_ui(pi[0], 0);
	mpf_set_ui(pi[1], 0);
	i = 1; 
	j = 1;
	iteracoes = 0;
	x = 0;

	/* Load the reals digits of pi */
	filePi = fopen("pi.txt", "r");
	gmp_fscanf(filePi, "%Ff", real_pi); 
	fclose(filePi);
	
	start = clock();

	while(1){
		/* y = ( 1 - (1 - y0 ^ 4) ^ 0.25 ) / ( 1 + ( 1 - y0 ^ 4) ^ 0.25 ) */
		mpf_pow_ui(y0Aux, params[Y][0], 4); 
		mpf_ui_sub(y0Aux, 1.0, y0Aux);
		mpf_sqrt(y0Aux, y0Aux);
                mpf_sqrt(y0Aux, y0Aux);

		mpf_add_ui(y0Aux2, y0Aux, 1.0);
		mpf_ui_sub(y0Aux, 1.0, y0Aux);
		
		mpf_div(params[Y][1], y0Aux, y0Aux2);

		/* a = a0 * ( 1 + params[Y][1] ) ^ 4 - 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */
		/* Threads creation */                
		pthread_create(&thread_a, NULL, calc_a, NULL);
        	pthread_create(&thread_b, NULL, calc_b, NULL);  		

		pthread_join(thread_a, NULL);
                pthread_join(thread_b, NULL);
		
		/* 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */	
		mpf_mul(a0Aux, a0Aux, a0Aux2);

		/*a0 * ( 1 + params[Y][1] ) ^ 4*/
                mpf_add_ui(a0Aux2, params[Y][1], 1);
                mpf_pow_ui(a0Aux2, a0Aux2, 4);
                mpf_mul(a0Aux2, params[A][0], a0Aux2);
	
		/* form the entire expression */
                mpf_sub(params[A][1], a0Aux2, a0Aux);
	
                mpf_set(params[A][0], params[A][1]);
                mpf_set(params[Y][0], params[Y][1]);

                mpf_ui_div(pi[j], 1, params[A][0]);
		gmp_printf("\nIteracao %d  | pi = %.25Ff", iteracoes, pi[j]);
		
		/* Calculate the error */
		mpf_sub(pi[(j+1)%2], real_pi, pi[j]);
		mpf_abs(pi[(j+1) % 2], pi[(j+1) % 2]);

		if(mpf_cmp(pi[(j+1)%2], error) < 0){
			printf("\n%d iteracoes para alcancar 10 milhoes de digitos de corretos.", iteracoes);			
			break;
		}

		j = (j+1) % 2;
		
		iteracoes++;
		i++;
	}

	end = clock();
    	cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

	fileTime = fopen("execution_time.txt", "w");
	fprintf(fileTime, "Execution time: %f\n", cpu_time_used);
	fclose(fileTime);
	
	/* Clean up*/
    	for(i=0; i<2; i++)
    		for(j=0; j<2; j++)
    			mpf_clear(params[i][j]);
    	
	mpf_clear(pi[0]);
    	mpf_clear(pi[1]);
	mpf_clear(real_pi);
	mpf_clear(error);

	return 0;
}
/*
 * Implementing second part of secure MP protocol. This function reads the input file containing the two encrypted products seperated by a newline, decrypts them, multiplies them and again encrypts their 
 * product and writes to the output file. If the other party has n documents in its collection, this should be called n times by the native Jav API i.e. this acts per remotely-based collection document.
 * */
int read_decrypt_mul_encrypt_write_encrypted_rand_prod( const char * input_interm_prods_file_name, const char * output_encrypt_rand_prod_file_name, const char * key_file_name)
{
	mpz_t tfidf_prod1;	//holds input randomized, encrypted tfidf vector products
	mpz_t coord_prod2;	//holds input randomized, encrypted binary vector products
	mpz_t out_enc_ran_prod;	//holds the encrypted product of tfidf_prod1, coord_prod2

	FILE *input_interm_prods_file=NULL, *output_encrypt_rand_prod_file=NULL;


	input_interm_prods_file = fopen(input_interm_prods_file_name, "r");
	output_encrypt_rand_prod_file = fopen(output_encrypt_rand_prod_file_name, "w");

	strncpy(g_key_file_name, key_file_name, sizeof(g_key_file_name));

	//Initialize
	mpz_init(tfidf_prod1);
	mpz_init(coord_prod2);

	mpz_init(out_enc_ran_prod);

	init();

	//variables are set to 0

	//init();



	//check if files are opened properly
	if (input_interm_prods_file == NULL) {
		printf("\n%s", "Error: open %s!", input_interm_prods_file_name);
		return -2;
	}

	if (output_encrypt_rand_prod_file == NULL) {
		printf("\n%s", "Error: open %s!", output_encrypt_rand_prod_file_name);
		return -2;
	}

	//Read the product values
	//File structure can be found in the Java function in which it is written i.e. 
	//<Class_name>:<function_name> :: DocSimSecApp:acceptIntermValues()
	gmp_fscanf(input_interm_prods_file, "%Zd", tfidf_prod1);
	gmp_fscanf(input_interm_prods_file, "%Zd\n", coord_prod2);

	gmp_fprintf(stderr, "TFIDF Product read = %Zd\n\n", tfidf_prod1);
	gmp_fprintf(stderr, "Co-ord Product read = %Zd\n\n", coord_prod2);

	//Decrypt both
	decrypt(tfidf_prod1);
	decrypt(coord_prod2);

	gmp_fprintf(stderr, "After decrypting, TFIDF Product read = %Zd\n\n", tfidf_prod1);
	gmp_fprintf(stderr, "After decrypting, Co-ord Product read = %Zd\n\n", coord_prod2);


	//Multiply both
	mpz_mul(out_enc_ran_prod, tfidf_prod1, coord_prod2);
	mpz_mod(out_enc_ran_prod, out_enc_ran_prod, n);
	
	gmp_fprintf(stderr, "Unencrypted Product = %Zd\n\n", out_enc_ran_prod);

	//note obtained product is not encrypted, hence, encrypting it
	encrypt_big_num(out_enc_ran_prod, out_enc_ran_prod);
	gmp_fprintf(stderr, "Encrypted Product = %Zd\n\n", out_enc_ran_prod);
	gmp_fprintf(output_encrypt_rand_prod_file, "%Zd", out_enc_ran_prod);

	fflush(output_encrypt_rand_prod_file);
	fclose(input_interm_prods_file);
	fclose(output_encrypt_rand_prod_file);


	mpz_clear(tfidf_prod1);
	mpz_clear(coord_prod2);
	mpz_clear(out_enc_ran_prod);
	clear();

	return 0;
}
/*
 * This function reads the encrypted query sent by client, computes the intermediate cosine tfidf product and cosine co-ordination factor,
 * randomizes these two values and writes them along with respective randomizing values into the output_file_name.
 * */
int read_encrypt_vec_from_file_comp_inter_sec_prod( int vsizelocal, const char * input_encr_tfidf_file_name, const char * input_encr_bin_file_name, const char * input_tfidf_vec_file_name, const char * input_bin_vec_file_name, const char * output_file_name, const char * key_file_name)
{
	int input_size = 0, i, temp, *p_tfidf_vec, *p_bin_vec;
	mpz_t *vec1;	//holds input encrypted tfidf q values
	mpz_t *vec2;	//holds input encrypted binary q values
	mpz_t cosine_result;
	mpz_t co_ord_factor;
	mpz_t random_no;
	FILE *input_encr_tfidf_file, *input_tfidf_vec_file, *input_bin_vec_file, *output_file, *input_encr_bin_file;


	vsize = vsizelocal;
	input_encr_tfidf_file = fopen(input_encr_tfidf_file_name, "r");
	input_encr_bin_file = fopen(input_encr_bin_file_name, "r");
	input_tfidf_vec_file = fopen(input_tfidf_vec_file_name, "r");
	input_bin_vec_file = fopen(input_bin_vec_file_name, "r");
	output_file = fopen(output_file_name, "w");

	strncpy(g_key_file_name, key_file_name, sizeof(g_key_file_name));

	printf("Number of vector dimensions = %d\n", vsizelocal);
	//printf("p_tfidf_vec:%p, ENOMEM:%d\n", p_tfidf_vec, (errno == ENOMEM)?1:0);



	//initialize vectors and big number variables
	//Dynamically creating the array
	vec1 = (mpz_t *)malloc(vsizelocal*sizeof(mpz_t));
	vec2 = (mpz_t *)malloc(vsizelocal*sizeof(mpz_t));
	p_tfidf_vec = (int*)malloc(vsize*sizeof(int));
	p_bin_vec = (int*)malloc(vsize*sizeof(int));
	printf("p_tfidf_vec:%p, ENOMEM:%d\n", p_tfidf_vec, (errno == ENOMEM)?1:0);

	//initialize vectors and big number variables
	for (i = 0; i < vsizelocal; i++)
		mpz_init(*(vec1+i));
	for (i = 0; i < vsizelocal; i++)
		mpz_init(*(vec2+i));

	//variables are set to 0
	mpz_init(cosine_result);
	mpz_init(co_ord_factor);
	mpz_init(random_no);

	init();

	//variables are set to 0

	//init();



	//check if files are opened properly
	if (input_encr_tfidf_file == NULL) {
		printf("\n%s", "Error: open input_encr_tfidf_file!");
		return -2;
	}

	if (input_encr_bin_file == NULL) {
		printf("\n%s", "Error: open input_encr_bin_file!");
		return -2;
	}

	if (input_tfidf_vec_file == NULL) {
		printf("\n%s", "Error: open input_tfidf_vec_file!");
		return -3;
	}

	if (input_bin_vec_file == NULL) {
		printf("\n%s", "Error: open input_bin_vec_file!");
		return -4;
	}

	if (output_file == NULL) {
		printf("\n%s", "Error: open output_file!");
		exit(1);
	}


	//fill in the first vector
	input_size = 0;
	while ( (input_size < vsizelocal) )
	{
		if ( input_size == vsizelocal - 1 )
		{
			gmp_fscanf(input_encr_tfidf_file,"%Zd", (vec1+input_size));
		}
		else
		{
			gmp_fscanf(input_encr_tfidf_file,"%Zd\n", (vec1+input_size));
		}
		gmp_printf("%d>> READ %Zd\n", input_size+1, *(vec1+input_size));

		input_size++;
	}
	if ( !( input_size == vsizelocal ) )
	{
		fprintf(stderr, "%s:%d::ERROR! TFIDF: Nothing to read OR parsing error! input_size:%d, vsizelocal:%d\n", 
				__func__, __LINE__, input_size, vsizelocal);
		return -4;
	}

	input_size = 0;
	while ( (input_size < vsizelocal) )
	{
		if ( input_size == vsizelocal - 1 )
		{
			gmp_fscanf(input_encr_bin_file,"%Zd", (vec2+input_size));
		}
		else
		{
			gmp_fscanf(input_encr_bin_file,"%Zd\n", (vec2+input_size));
		}
		gmp_printf("%d>> READ %Zd\n", input_size+1, *(vec2+input_size));

		input_size++;
	}
	if ( !( input_size == vsizelocal ) )
	{
		fprintf(stderr, "%s:%d::ERROR! Binary: Nothing to read OR parsing error! input_size:%d, vsizelocal:%d\n", 
				__func__, __LINE__, input_size, vsizelocal);
		return -4;
	}


	printf("\n");
	input_size = 0;

	//fill in the second vector
	for( fscanf(input_tfidf_vec_file,"%d", &temp); temp != EOF && input_size < vsize; 
			fscanf(input_tfidf_vec_file, "%d", &temp) ){

		printf("Non encrypted TFIDF Input::Wt.:%d\n", temp);
		*(p_tfidf_vec + input_size) = temp;
		input_size ++;
	} 

	input_size = 0;
	for( fscanf(input_bin_vec_file,"%d", &temp); temp != EOF && input_size < vsize; 
			fscanf(input_bin_vec_file, "%d", &temp) ){

		printf("Non encrypted Binary Input::Wt.:%d\n", temp);
		*(p_bin_vec + input_size) = temp;
		input_size ++;
	} 

	encrypt(cosine_result, 0);
	//compute encrypted the vec1 * p_tfidf_vec (dot product)
	for (i = 0; i < input_size; i++) {
		//compute m1 * m2
		mpz_powm_ui(big_temp, *(vec1+i), *(p_tfidf_vec+i), n_square);
		//compute m1 + m2
		mpz_mul(cosine_result, cosine_result, big_temp);
		mpz_mod(cosine_result, cosine_result, n_square);
	}

	encrypt(co_ord_factor, 0);
	//compute encrypted the vec2 * co_ord_factor (dot product)
	for (i = 0; i < input_size; i++) {
		//compute m1 * m2
		mpz_powm_ui(big_temp, *(vec2+i), *(p_bin_vec+i), n_square);
		//compute m1 + m2
		mpz_mul(co_ord_factor, co_ord_factor, big_temp);
		mpz_mod(co_ord_factor, co_ord_factor, n_square);
	}


	/*
	 * Donot decrypt here as we would not be having the CORRESPONDING private key
	 * */
	//decrypt the encrypted dot product
	//decrypt(cosine_result);

	//TODO: Remove this debug decryption. - START
	mpz_t dot_prod;
	mpz_init(dot_prod);
	mpz_set(dot_prod, cosine_result);
	decrypt(dot_prod);
	gmp_fprintf(stderr, "\n%s:%d:: Query*%s TFIDF cosine product: %Zd\n", __func__, __LINE__, input_encr_tfidf_file_name, dot_prod);

	mpz_set(dot_prod, co_ord_factor);
	decrypt(dot_prod);
	gmp_fprintf(stderr, "%s:%d:: Query*%s CO-ORD. cosine product: %Zd\n\n", __func__, __LINE__, input_encr_bin_file_name, dot_prod);
	fflush(stderr);

	mpz_clear(dot_prod);
	//TODO: Remove this debug decryption. - END

	//decrypt the encrypted co ordination factor
	//decrypt(co_ord_factor);

	/*
	 * Generate two random numbers of the modulo n_square and the add these two
	 * to the two results - cosine product and co_ord_factor. 
	 * Write these two random values one after the other
	 * and then the randomized values after them in the output file
	 * given for performing the secure multiplication
	 * protocol. All should be seperated by a newline except maybe the last one
	 * written to the file. FORMAT - output file
	 * ===START===
	 * r_1
	 * randomized cosine tfidf product
	 * r_2
	 * randomized cosine co-ord. product
	 *  ===END===
	 * */

	//Generate random number, say r_1
	get_random_r_given_modulo(random_no, n_square);
	//Write r_1 to outputfile
	mpz_out_str(output_file, 10, random_no);
	fprintf(output_file, "\n");
	//Calculate randomized cosine tfidf product, MULTIPLYING to add
	mpz_mul(cosine_result, cosine_result, random_no);
	//Compute modulus
	mpz_mod(cosine_result, cosine_result, n_square);
	//Write randomized cosine tfidf product to output file
	mpz_out_str(output_file, 10, cosine_result);
	fprintf(output_file, "\n");

	//Generate random number, say r_2
	get_random_r_given_modulo(random_no, n_square);
	//Write r_2 to outputfile
	mpz_out_str(output_file, 10, random_no);
	fprintf(output_file, "\n");
	//Calculate randomized cosine tfidf product, MULTIPLYING to add
	mpz_mul(co_ord_factor, co_ord_factor, random_no);
	//Compute modulus
	mpz_mod(co_ord_factor, co_ord_factor, n_square);
	//Write randomized cosine co_ord product to output file
	mpz_out_str(output_file, 10, co_ord_factor);

	gmp_printf("\nThus similarity of %s and %s score = %Zd written in %s\n", input_encr_tfidf_file_name, input_tfidf_vec_file_name, cosine_result, output_file_name);
#if 0
	//print the cosine_result
	if (mpz_out_str(output_file, 10, cosine_result) == 0)
		printf("ERROR: Not able to write the cosine_result!\n");

	fprintf(output_file, "\n");
#endif
	gmp_printf("\nThus co-ord. factor of %s and %s score = %Zd written in %s\n", input_encr_bin_file_name, input_bin_vec_file_name, co_ord_factor, output_file_name);
#if 0
	//print the co_ord_factor
	if (mpz_out_str(output_file, 10, co_ord_factor) == 0)
		printf("ERROR: Not able to write the co_ord_factor!\n");
#endif


	fclose(input_encr_tfidf_file);  
	fclose(input_encr_bin_file);  
	//fflush(input_tfidf_vec_file);
	fclose(input_tfidf_vec_file);
	//fflush(input_bin_vec_file);
	fclose(input_bin_vec_file);
	fflush(output_file);
	fclose(output_file);

	//release space used by big number variables
	for (i = 0; i < vsizelocal; i++)
		mpz_clear(*(vec1+i));
	for (i = 0; i < vsizelocal; i++)
		mpz_clear(*(vec2+i));


	mpz_clear(cosine_result);
	mpz_clear(co_ord_factor);
	mpz_clear(random_no);
	clear();
	free(vec1);
	free(vec2);
	free(p_tfidf_vec);
	free(p_bin_vec);

	return 0;
}