Exemplo n.º 1
0
int main()
{
	mpz_t a,b,r;
	mpz_init(a);
	mpz_init(b);
	mpz_init(r);

	printf("Enter a:");
	gmp_scanf("%Zd",a);
	printf("Enter b:");
	gmp_scanf("%Zd",b);
	
	
	mpz_abs(a,a);
	mpz_abs(b,b);
	gcd(r,a,b);
	
	
	
	gmp_printf("GCD : %Zd \n",r);	
	mpz_clear(a);
	mpz_clear(b);
	mpz_clear(r);

}
Exemplo n.º 2
0
int main( int argc, char* argv[] ) {
    mpz_t x;

    mpz_init( x );

    gmp_scanf( "%Zd", x );

    size_t n = abs( x->_mp_size );

    mp_limb_t* t = x->_mp_d;



    for( int i = 0; i < n; i++ ) {
        if( i != 0 ) {
            printf( "+" );
        }

        gmp_printf( "%llu*(2^(64))^(%d)", t[ i ], i );
    }

    printf( "\n" );

    mpz_clear( x );

    return 0;
}
Exemplo n.º 3
0
int main() {
	mpz_t N, e, d, p, q;
	mpz_init(N); mpz_init(e); mpz_init(d); mpz_init(p); mpz_init(q); 
	int r;
	init();
	while(r = gmp_scanf("%Zd %Zd %Zd", N, e, d) == 3) {
		rsafact(N, e, d, p, q);
		gmp_printf("%Zd %Zd\n", p, q);
	}
}
int
main ()
{
  mpz_t n;
  mpz_init (n);
  printf ("Enter a number: ");
  gmp_scanf ("%Zd", &n);
  printf ("AKS says: %s\n", aks (n) ? "prime" : "composite");
  return 0;
}
Exemplo n.º 5
0
int main(int argc, char const *argv[])
{
    // PART 2 - create GCD and test it
    
    mpz_t numA, numB, gcd;

    mpz_init(numA); //mpz_set_ui(numA, 48);
    mpz_init(numB); //mpz_set_ui(numB, 18);
    mpz_init(gcd);

    gmp_printf("Input first number: ");
    gmp_scanf("%Zd", numA);
    gmp_printf("Input second number: ");
    gmp_scanf("%Zd", numB);

    // Calculate and print output
    // (they separated because euclidianGCD dirties the inputs)
    gmp_printf("GDC of %Zd and %Zd is ", numA, numB);
    euclidianGCD(&gcd, numA, numB);
    gmp_printf(" %Zd!\n", gcd);

    return 0;
}
Exemplo n.º 6
0
static void testDecrypt() {
    /*
      Input format is:
      - Number of testcases
      - For each testcase:   E  F  p  g  y  x  m  m2
    */
    int ntc, i;
    if(1!=scanf("%d\n", &ntc)) {
      printf("Input failure.\n");
      exit(1);
    }
    for(i=0;i<ntc;++i) {
      Ciphertext ct;
      PublicKey pk;
      Plaintext got;
      mpz_t x;
      Plaintext m1, m2;
      mpz_init(ct.E);
      mpz_init(ct.F);
      mpz_init(pk.p);
      mpz_init(pk.g);
      mpz_init(pk.y);
      mpz_init(x);
      mpz_init(m1.m);
      mpz_init(m2.m);
      if(8!=gmp_scanf("%Zd %Zd %Zd %Zd %Zd %Zd %Zd %Zd\n", ct.E, ct.F, pk.p, pk.g, pk.y, x, m1.m, m2.m)) {
        printf("Input failure %d.\n", i);
        exit(1);
      }

      got = decrypt(ct, pk);
      if(mpz_cmp(got.m, m1.m)!=0) {
        gmp_printf("\nTestcase %d  Wrong answer: expected %Zd got:\n%Zd\n", i, m1.m, got.m);
        exit(0);
      } else {
        printf("%d.", i);
      }
      mpz_clear(got.m);
      mpz_clear(ct.E);
      mpz_clear(ct.F);
      mpz_clear(pk.p);
      mpz_clear(pk.g);
      mpz_clear(pk.y);
      mpz_clear(x);
      mpz_clear(m1.m);
      mpz_clear(m2.m);
    }
}
int
main (void)
{
  mpz_t n, r;

  mpz_inits (n, r, NULL);
  while (gmp_scanf ("%Zd", n) > 0)
    {
      introot (r, n);
      gmp_printf ("%Zd : %Zd\n", n, r);
      fflush (stdout);
    }
  mpz_clears (n, r, NULL);

  return 0;
}
Exemplo n.º 8
0
int main()
{
	char function;

  mpf_t number1, number2, output;

  mpf_init_set_ui(number1, 0);
  mpf_init_set_ui(number2, 0);
  mpf_init_set_ui(output, 0);

  while(true)
  {
      printf("enter a calculation using A +,-,*,/ B format.\n");
      gmp_scanf("%Ff%c%Ff", number1, &function, number2);

      if (function == '+')
      {
        mpf_add(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }

      if (function == '-')
      {
        mpf_sub(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }

      if (function == 'x' || function == '*')
      {
        mpf_mul(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }

      if (function == '/')
      {
        mpf_div(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }
  }

  mpf_clear(number1);
  mpf_clear(number2);
  mpf_clear(output);
}
/**
 * If the -gmp option is specied then the GMP implementation is used.
 */
int main (int argc, char** argv) {
  int use_gmp_solution = FALSE;
  if (argc > 1 && strcmp(argv[1], "-gmp") == 0) {
    use_gmp_solution = TRUE;
  }

  mpz_t n;
  mpz_init(n);

  while (!feof(stdin)) {
    gmp_scanf("%Zd", &n);
    if(feof(stdin)) {
      break;
    }
    int prime = use_gmp_solution ? mpz_probab_prime_p(n, DEFAULT_K) : miller_rabin_is_prime(n, DEFAULT_K);
    gmp_printf("%d\n", prime);
  }
  mpz_clear(n);
  return 0;
}
/**
 * Debug statements will be used if the -d option is used.
 */
int main(int argc, char** argv) {
  if (argc > 1 && strcmp(argv[1], "-d") == 0) {
    aks_debug = 1;
  }

  mpz_t n;
  mpz_init(n);

  while (!feof(stdin)) {
    gmp_scanf("%Zd", &n);
    if(feof(stdin)) {
      break;
    }
    int prime = aks_is_prime(n);
    gmp_printf("%d\n", prime);
  }

  mpz_clear(n);
  mpfr_free_cache();
  return 0;
}
Exemplo n.º 11
0
int main()
{
                                        // VARIABLES DECLARATION
        //Clées Générées
    mpz_t clee_privee;      mpz_init(clee_privee);
    mpz_t clee_publique;    mpz_init(clee_publique);
    mpz_t clee_modulo;      mpz_init(clee_modulo);


        // Auto GMP + test  prima
    mpz_t __n;      mpz_init(__n);
    mpz_t __max;    mpz_init(__max);
    mpz_t __two;    mpz_init_set_ui(__two, 2);
    mpz_t __p;      mpz_init(__p);
    mpz_t __q;      mpz_init(__q);

    gmp_randstate_t __rand_state;
    gmp_randinit_default(__rand_state);
    gmp_randseed_ui (__rand_state, time(NULL));

        // Manu GMP
    mpz_t _p, _q;   mpz_init (_p); mpz_init (_q);
	mpz_t _n;       mpz_init (_n);
	mpz_t _phi;     mpz_init(_phi);
	mpz_t _e,_d;    mpz_init(_e); mpz_init(_d);
	mpz_t _temp1;    mpz_init(_temp1);
	mpz_t _temp2;   mpz_init(_temp2);

        //manu Small
	int p,q;
	int n;
	int phi;
	int e,d;

        // In / Out
    char s_temp[1000]="";
    char s_in[1000]  = "ref.txt";
    char s_out[1000] = "out.txt";
    FILE * f_in ;
    FILE * f_out;

        // Some vars
    int longeur_nombres=100;
    int clees_generees=0;
    int i,j;
 	char c = ' ';
	int menu;
	int continuer =1;
	int base_cryptage=10;
	char separator='\n';

    while (continuer ==1 )
    {
        if (clees_generees==1) {printf("\n\n\n\nLes clees ont ete generees");}
        else  {printf("\n\n\nLes clees n'ont  pas encore ete generees");}
            //Menu
        printf("\n\t-> 1: generer des cles RSA manuellement (petits nombres ; sans GMP).");
        printf("\n\t-> 2: generer des cles RSA manuellement (grands nombres ; avec GMP).");
        printf("\n\t-> 3: generer des cles RSA automatiquement (avec GMP ).");
        printf("\n\t-> 8: Entrer manuellement les clees.");
        printf("\n\t-> 9: Charger les clees a partir d'un fichier.");

        if (clees_generees==1)
        {
            printf("\n\n4: Cripter un fichier.");
            printf("\n5: Decripter un fichier.");
            printf("\n10: Enregistrer les clees dans un fichier");
        }
        printf("\n\n6: Test de primalite ( Miller Rabin ).");
        printf("\n\n0: Quitter.");

        printf("\n\nChoix : ");
        scanf("%d",&menu);

		switch (menu)
		{
			case 0: // quiter
				continuer=0;
			break;

			case 1: // setup RSA
				printf("\nEntrez deux nombres premiers p et q, : \n"); // On entre deux nbres premiers p, q
				do { printf("\tp = ");           scanf("%d",&p); } while( premier(p)!= 1 );
				do { printf("\tp = %d, q = ",p); scanf("%d",&q); } while( q==p || premier(q)!= 1 );

				n = p*q;		    // on calcul la première partie de la clé publique
				phi=(p-1)*(q-1);	// on calcule l'indicatrice d'euler de n

				printf("\tphi(%d) = %d\n",n,phi);
				printf("\nEntrez un nombre e, tel que Pgcd(%d,e) = 0 :\n",phi);
				do { printf("\te = "); scanf("%d",&e);printf("\n"); d= inverseModulo(e,phi); } while( d==0 );
				printf("\n\td = %d , car (%d * %d = 1 mod %d )\n",d,d,e,phi);

                while (d<0) {d+=n;}
				printf("\n\tCle Publique : { %d, %d }",e,n);
				printf("\n\tCle Privee   : { %d, %d }\n\n",d,n);

				mpz_set_ui(clee_privee,d);
				mpz_set_ui(clee_publique,e);
				mpz_set_ui(clee_modulo,n);
				clees_generees=1;
			break;

			case 2: // setup RSA
				printf("\nEntrez deux nombres premiers p et q, : \n"); // On entre deux nbres premiers p, q
				do { printf("\tp = ");                gmp_scanf("%Zd",_p);  } while( mpz_premier(_p)!= 1 );
				do { gmp_printf("\tp = %Zd, q = ",_p); gmp_scanf("%Zd",_q); } while( mpz_cmp(_q,_p)==0 || mpz_premier(_q)!= 1 );

				mpz_mul(_n,_p,_q);              // on calcul la première partie de la clé publique

				mpz_sub_ui(_temp1,_p,1);
				mpz_sub_ui(_temp2,_q,1);
				mpz_mul(_phi,_temp1,_temp2);    // on calcule l'indicatrice d'euler de n

				gmp_printf("\tphi(%Zd) = %Zd\n",_n,_phi);
				gmp_printf("\nEntrez un nombre e, tel que Pgcd(%Zd,e) = 0 :\n",_phi);
                do { printf("\te = "); gmp_scanf("%Zd",_e);printf("\n"); mpz_inverseModulo(&_d,_e,_phi); } while( mpz_cmp_ui(_d,0)==0 );

				gmp_printf("%Zd",_e);
				gmp_printf("%Zd",_phi);

				gmp_printf("\n\td = %Zd , car (%Zd * %Zd = 1 mod %Zd )\n",_d,_d,_e,_phi);

				gmp_printf("\n\tCle Publique : { %Zd, %Zd }",_e,_n);
				gmp_printf("\n\tCle Privee   : { %Zd, %Zd }\n\n",_d,_n);

                mpz_set(clee_privee,_d);
				mpz_set(clee_publique,_e);
				mpz_set(clee_modulo,_n);
				clees_generees=1;
			break;

            case 8:
                printf("\n\n\t  * ENTREZ LES CLEES *\n");
                printf("\n\tClee publique = "); gmp_scanf("%Zd",clee_publique);
                printf("\tClee privee = ");   gmp_scanf("%Zd",clee_privee);
                printf("\tClee Modulo = ");   gmp_scanf("%Zd",clee_modulo);
                printf("\n\n");
                clees_generees=1;

			break;

            case 9: // Charger les clees a partir d'un fichier
                {
                    j=0;
                    do
                    {
                        printf("\n\n\t\t * CHARGER *\n( clee publique / privee / modulo)\n ");

                        printf("\n\t1 : Input  : %s",s_in);
                        printf("\n\t2 : separateur input  : ");
                            if (separator == '\n'){printf("retour a la ligne");}
                            if (separator == '\t'){printf("tabulation");}
                            if (separator == ' '){printf("espace");}
                        printf("\n\n\t0 : Charger !!! :");

                        scanf("%d",&menu);
                        if (menu == 1) {printf("\n\tnouveau fichier : "); scanf("%s",s_in);}
                        if (menu == 2)
                        {
                                printf("\n\tnouveau caractere de separation :");
                                printf("\n\t\t1 : retour a la ligne");
                                printf("\n\t\t2 : tabulation ");
                                printf("\n\t\t3 : espace ");
                                printf("\n\n\t\t0 : annuler ");
                                scanf("%d",&menu);
                                if (menu==1) { separator='\n';}
                                if (menu==2) { separator='\t';}
                                if (menu==3) { separator=' ';}
                        }

                    }while (menu!=0);

                    f_in = fopen(s_in,"r");

                    c = '0';
                    while(c != EOF)
                        {
                            i=0;
                            c = '0';
                            while(c != EOF && c!= separator)
                            {
                                c = fgetc(f_in);
                                if (c != EOF && c!= separator)
                                {
                                    s_temp[i]=c;
                                    i++;
                                }
                            }
                            if (c!=EOF)
                            {
                                s_temp[i]='\0';
                                if (j==0) {mpz_set_str(clee_publique,s_temp,10); }
                                if (j==1) {mpz_set_str(clee_privee  ,s_temp,10); }
                                if (j==2) {mpz_set_str(clee_modulo  ,s_temp,10); }
                                j++;
                            }
                        }

                    fclose(f_in);
                    printf("\nappuyez sur une touche pour continuer");
                    clees_generees=1;
                    getchar(); getchar();
                    j=0;
                }

			break;


			case 3: // auto crypte rabin
                mpz_mul_2exp(__max, __two, longeur_nombres );
                printf("\nGeneration de 2 nombres premiers p et q, : \n");
                do
                {
                    mpz_urandomm(__p, __rand_state, __max);

                    // a remplacer par une boucle propre sur les 100 plus petits premiers par exemple
                    if (mpz_even_p(__p)) continue;
                    if (mpz_fdiv_ui(__p, 3) == 0) continue;
                    if (mpz_fdiv_ui(__p, 5) == 0) continue;
                    if (mpz_fdiv_ui(__p, 7) == 0) continue;
                } while (miller_rabin(__p, __rand_state, 100) == 0);
                do
                {
                    mpz_urandomm(__q, __rand_state, __max);

                    // a remplacer par une boucle propre sur les 100 plus petits premiers par exemple
                    if (mpz_even_p(__q)) continue;
                    if (mpz_fdiv_ui(__q, 3) == 0) continue;
                    if (mpz_fdiv_ui(__q, 5) == 0) continue;
                    if (mpz_fdiv_ui(__q, 7) == 0) continue;
                } while (miller_rabin(__q, __rand_state, 100) == 0);
                do
                {
                    mpz_urandomm(_e, __rand_state, __max);

                    // a remplacer par une boucle propre sur les 100 plus petits premiers par exemple
                    if (mpz_even_p(_e)) continue;
                    if (mpz_fdiv_ui(_e, 3) == 0) continue;
                    if (mpz_fdiv_ui(_e, 5) == 0) continue;
                    if (mpz_fdiv_ui(_e, 7) == 0) continue;
                } while (miller_rabin(_e, __rand_state, 100) == 0);
                gmp_printf("\n\tp = %Zd,\n\n\tq = %Zd",__p,__q);

                mpz_mul(_n,__p,__q);            // on calcul la première partie de la clé publique
				mpz_sub_ui(_temp1,__p,1);
				mpz_sub_ui(_temp2,__q,1);
				mpz_mul(_phi,_temp1,_temp2);    // on calcule l'indicatrice d'euler de n

				gmp_printf("\n\n\tphi(%Zd) \n\t= %Zd\n",_n,_phi);
				mpz_inverseModulo(&_d,_e,_phi);

				gmp_printf("\n\tCle Publique : \n\t{ %Zd,\n\t%Zd }",_e,_n);
				gmp_printf("\n\n\tCle Privee   : \n\t{ %Zd,\n\t%Zd }\n\n",_d,_n);

                mpz_set(clee_privee,_d);
				mpz_set(clee_publique,_e);
				mpz_set(clee_modulo,_n);
				clees_generees=1;
            break;

            case 4: // crypte
                if (clees_generees==1)
                {
                    do
                    {
                        printf("\n\n\t * CRYPTAGE *\n");

                        printf("\n\t1 : Input  : %s",s_in);
                        printf("\n\t2 : Output : %s",s_out);
                        printf("\n\t3 : Base   : %d",base_cryptage);
                        printf("\n\t4 : separateur : ");
                            if (separator == '\n'){printf("retour a la ligne");}
                            if (separator == '\t'){printf("tabulation");}
                            if (separator == ' '){printf("espace");}
                        printf("\n\n\t0 : CRYPTER !!! :");

                        scanf("%d",&menu);
                        if (menu == 1) {printf("\n\tnouveau fichier : "); scanf("%s",s_in);}
                        if (menu == 2) {printf("\n\tnouveau fichier : "); scanf("%s",s_out);}
                        if (menu == 3) {printf("\n\tnouvelle base ( entre 2 et 36) : "); scanf("%d",&base_cryptage);
                                        if (base_cryptage<2 || base_cryptage>36) {base_cryptage=10;}}
                        if (menu == 4)
                        {
                                printf("\n\tnouveau caractere de separation :");
                                printf("\n\t\t1 : retour a la ligne");
                                printf("\n\t\t2 : tabulation ");
                                printf("\n\t\t3 : espace ");
                                printf("\n\n\t\t0 : annuler ");
                                scanf("%d",&menu);
                                if (menu==1) { separator='\n';}
                                if (menu==2) { separator='\t';}
                                if (menu==3) { separator=' ';}
                        }
                    }while (menu!=0);

                    f_in = fopen(s_in,"r");
                    f_out = fopen(s_out,"w+");

                    c = ' ';
                    while(c != EOF)
                    {
                        c = fgetc(f_in);
                        if (c!=EOF)
                        {
                            mpz_set_ui(_temp1,(unsigned int)c);
                            mpz_powm(_temp1,_temp1,clee_publique,clee_modulo);
                            mpz_out_str (f_out, base_cryptage, _temp1 );
                            fputc(separator,f_out);
                        }
                    }

                    fclose(f_in);
                    fclose(f_out);
                    printf("\nappuyez sur une touche pour continuer");
                    getchar(); getchar();
                }
                else
                {
                    printf("\nVous devez d'abord generer un set de Clees");
                    printf("\nappuyez sur une touche pour continuer");
                    getchar(); getchar();
                }
			break;

			case 5: // Décrypte
                if (clees_generees==1)
                {
                    do
                    {
                        printf("\n\n\t * DECRYPTAGE *\n");

                        printf("\n\t1 : Input  : %s",s_in);
                        printf("\n\t2 : Output : %s",s_out);
                        printf("\n\t3 : Base   : %d",base_cryptage);
                        printf("\n\t4 : separateur input  : ");
                            if (separator == '\n'){printf("retour a la ligne");}
                            if (separator == '\t'){printf("tabulation");}
                            if (separator == ' '){printf("espace");}
                        printf("\n\n\t0 : DECRYPTER !!! :");

                        scanf("%d",&menu);
                        if (menu == 1) {printf("\n\tnouveau fichier : "); scanf("%s",s_in);}
                        if (menu == 2) {printf("\n\tnouveau fichier : "); scanf("%s",s_out);}
                        if (menu == 3) {printf("\n\tnouvelle base ( entre 2 et 36) : "); scanf("%d",&base_cryptage);
                                        if (base_cryptage<2 || base_cryptage>36) {base_cryptage=10;}}
                        if (menu == 4)
                        {
                                printf("\n\tnouveau caractere de separation :");
                                printf("\n\t\t1 : retour a la ligne");
                                printf("\n\t\t2 : tabulation ");
                                printf("\n\t\t3 : espace ");
                                printf("\n\n\t\t0 : annuler ");
                                scanf("%d",&menu);
                                if (menu==1) { separator='\n';}
                                if (menu==2) { separator='\t';}
                                if (menu==3) { separator=' ';}
                        }

                    }while (menu!=0);

                    f_in = fopen(s_in,"r");
                    f_out = fopen(s_out,"w+");

                    c = '0';
                    while(c != EOF)
                        {
                            i=0;
                            c = '0';
                            while(c != EOF && c!= separator)
                            {
                                c = fgetc(f_in);
                                if (c != EOF && c!= separator)
                                {
                                    s_temp[i]=c;
                                    i++;
                                }
                            }
                            if (c!=EOF)
                            {
                                s_temp[i]='\0';
                                mpz_set_str(_temp1,s_temp,base_cryptage);
                                mpz_powm(_temp1,_temp1,clee_privee,clee_modulo);
                                fputc((char)(mpz_get_ui(_temp1)),f_out);
                            }
                        }

                    fclose(f_in);
                    fclose(f_out);
                    printf("\nappuyez sur une touche pour continuer");
                    getchar(); getchar();
                }
                else
                {
                    printf("\nVous devez d'abord generer un set de Clees");
                    printf("\nappuyez sur une touche pour continuer");
                    getchar(); getchar();
                }
			break;

			case 10: // enregistre les clees
                if (clees_generees==1)
                {
                    do
                    {
                        printf("\n\n\t * SAUVER  *\n");

                        printf("\n\t1 : Output : %s",s_out);
                        printf("\n\t2 : separateur : ");
                            if (separator == '\n'){printf("retour a la ligne");}
                            if (separator == '\t'){printf("tabulation");}
                            if (separator == ' '){printf("espace");}
                        printf("\n\n\t0 : CRYPTER !!! :");

                        scanf("%d",&menu);
                        if (menu == 1) {printf("\n\tnouveau fichier : "); scanf("%s",s_out);}
                        if (menu == 2)
                        {
                                printf("\n\tnouveau caractere de separation :");
                                printf("\n\t\t1 : retour a la ligne");
                                printf("\n\t\t2 : tabulation ");
                                printf("\n\t\t3 : espace ");
                                printf("\n\n\t\t0 : annuler ");
                                scanf("%d",&menu);
                                if (menu==1) { separator='\n';}
                                if (menu==2) { separator='\t';}
                                if (menu==3) { separator=' ';}
                        }
                    }while (menu!=0);

                    f_out = fopen(s_out,"w+");

                    mpz_out_str (f_out, 10, clee_publique );
                    fputc(separator,f_out);

                    mpz_out_str (f_out, 10, clee_privee );
                    putc(separator,f_out);

                    mpz_out_str (f_out, 10, clee_modulo );
                    fputc(separator,f_out);

                    fclose(f_out);
                    printf("\nappuyez sur une touche pour continuer");
                    getchar(); getchar();
                }
                else
                {
                    printf("\nVous devez d'abord generer un set de Clees");
                    printf("\nappuyez sur une touche pour continuer");
                    getchar(); getchar();
                }
			break;

            case 6: // Test de primalité
                printf("Entrez un nombre, et nous vous dirrons s'il est premier ou pas :\n\t n = ");
                gmp_scanf("%Zd",__n);
                if (miller_rabin(__n, __rand_state, 100) == 1)
                        { printf("\n PROBABLEMENT, ce nombre est probablement premier ."); }
                else    { printf("\n NON, ce nombre n'est pas premier !"); }
			break;
			default : printf("  -> Choix non reconnu !"); break;
		}
    }

	return 0;
}
Exemplo n.º 12
0
main (int argc, char *argv[]) {
  mpz_t n, c, x1, x2, product, t, g, t1, t2;
  unsigned long max, range, terms, j, ans;

  mpz_init (n);
  mpz_init (c);
  mpz_init (x1);
  mpz_init (x2);
  mpz_init (product);
  mpz_init (t);
  mpz_init (g);
  mpz_init (t1);
  mpz_init (t2);

initialize:
  mpz_set_str (n, argv[1], 10);
  gmp_printf("n = %Zd\n", n);
  mpz_set_str (c, argv[2], 10);
  max = atoi(argv[3]);

restart:
  mpz_set_ui (x1, 2);
  mpz_add_ui (x2, c, 4);
  range = 1;
  mpz_set_ui (product, 1);
  terms = 0;

compute_diff:
  while ( terms <= max ) {
    for (j=1; j<=range; j++) {
      mpz_mul (t, x2, x2);
      mpz_add (t, t, c);
      mpz_fdiv_r ( x2, t, n );
      mpz_sub (t, x1, x2);
      mpz_mul (t, product, t);
      mpz_fdiv_r (product, t, n);
      terms++;
      if ((terms%10) == 0) {
        mpz_set (t1, n);
        mpz_set (t2, product);
        gcd (g, t1, t2);
        if (mpz_cmp_ui (g, 1) > 0) {
          gmp_printf("g = %Zd\n",g);
          goto terminate;
        }
        mpz_set_ui (product, 1);
      }
    }

reset:
    mpz_set (x1, x2);
    range = 2*range;
    for (j=1; j<=range; j++) {
      mpz_mul (t, x2, x2);
      mpz_add (t, t, c);
      mpz_fdiv_r (x2, t, n);
    }
  }

  gmp_printf("n = %Zd --- no factor found for c = %Zd!!!\n", n, c);
  printf("Enter 1 to input new c, 0 to stop\n");
  scanf("%d",&ans);
  if (ans != 1) goto terminate;
  printf("Enter new c value:");
  gmp_scanf("%Zd",&c);
  goto restart;

terminate:
  mpz_clear (g);
  mpz_clear (t);
  mpz_clear (product);
  mpz_clear (x2);
  mpz_clear (x1);
  mpz_clear (c);
  mpz_clear (n);
  mpz_clear (t1);
  mpz_clear (t2);
  exit (0);

}
Exemplo n.º 13
0
int main(){

	mpz_t p , q , N ,phi_N,e,d,M,C,res;
	mpz_inits(p,q,N,phi_N,e,d,M,C,res,NULL);

	gmp_randstate_t state ;
	gmp_randinit_mt(state);

 //Manual input of p and q
     //int repeats=5,primecheck=0;   
	// printf("Enter p \n");
	// gmp_scanf("%Zd",&p);
	// //mpz_inp_str(p,stdin,10);
	// primecheck = mpz_probab_prime_p(p,repeats);
	// if(primecheck != 2)
	// {printf(" p is not prime . Error code : %d \n",primecheck);exit(1);
 //    }
	// printf("Enter q \n");
	// gmp_scanf("%Zd",&q);
	// //mpz_inp_str(q,stdin,10);
	// primecheck = mpz_probab_prime_p(q,repeats);
	// if(primecheck!= 2)
	// {printf(" q is not prime . Error code : %d \n",primecheck);exit(1);
 //    }
    

    // Randomly generating p and q
	//b - bits uradomb ( 0- 2^n -1 ) urandom  (0 - n-1)
	mpz_urandomb(p,state,512);  
	mpz_nextprime(p,p);
	gmp_printf("p : %Zd \n",p );

	mpz_urandomb(q,state,512);
	mpz_nextprime(q,q);
	gmp_printf("q : %Zd \n",q );

	printf("Enter M \n");
	gmp_scanf("%Zd",&M);
	

	mpz_mul(N,p,q);
	gmp_printf("N : %Zd \n",N );

    mpz_sub_ui(p,p,1);
    mpz_sub_ui(q,q,1);
	mpz_mul(phi_N,p,q);
	gmp_printf("phi_N : %Zd \n",phi_N );


	// mpz_urandomm(e,state,phi_N);
	// mpz_nextprime(e,e);
	
	
	mpz_set_ui(e,2);
     mpz_gcd(res,e,phi_N);
	 while( mpz_cmp_ui(res,1) != 0 ){
	 	  mpz_add_ui(e,e,1);
	 	  mpz_gcd(res,e,phi_N);
                   
	 }


	gmp_printf("e : %Zd \n",e);

	gmp_printf("\n\nPublic key : { %Zd , %Zd \n }",e,N);

	mpz_invert(d,e,phi_N);

	gmp_printf("\n\nd : %Zd \n",d);
	gmp_printf("\n\nPrivate key : { %Zd , %Zd \n }",d,N);

	mpz_powm (C , M , e , N );

	gmp_printf("\n\nC : %Zd \n",C );

	mpz_powm (M , C , d, N );

	gmp_printf("\n\nM : %Zd \n",M );




}