Example #1
0
void Algorithms::GeneratePrime(longz number, unsigned int numBits, unsigned int randomaiser)
{
    printf("Start\n");
    unsigned int i;
    bool isComposite;
    char *prime_str = new char[numBits + 1];

    longz buf;
    longz_init(buf);

    srand(randomaiser);
    prime_str[0] = '1';
    printf("before cycle\n");
    char *data1 = new char[1000];
    char *data2 = new char[1000];

    while(true)
    {
        prime_str[0] = '1';
        for(i = 1; i < numBits - 1; i++)
            prime_str[i] = (int)(2.0 * rand() / (RAND_MAX + 1.0)) + '0';
        prime_str[numBits - 1] = '1';
        prime_str[numBits] = '\0';

        longz_set_str(number, prime_str, 2);

        printf("\nprime_str = %s\n", prime_str);
        for(isComposite = false, i = 0; i <= maxPrimeIndex; i++)
        {
            if(longz_cmp_ui(number,shortPrimes[i]) <= 0)
                break;
            longz_get_str(data1, 16, buf);
            longz_get_str(data2, 16, number);
            longz_mod_ui(buf, number, shortPrimes[i]);
            if(longz_cmp_ui(buf,(unsigned long int)0) == 0)
            {
                isComposite = true;
                break;
            }
        }
        if(isComposite)
            continue;
            if(MillerRabin(number, numBits, NUMBERTESTS))
                break;
    }
    printf("end\n");
    longz_clear(buf);
    free(prime_str);
}
Example #2
0
void Algorithms::GeneratePrime(mpz_t number, unsigned int numBits, unsigned int randomaiser)
{
    unsigned int i;
    bool isComposite;
    char *prime_str = new char[numBits + 1];

    mpz_t buf;
    mpz_init(buf);

    srand(randomaiser);
    prime_str[0] = '1';

    while(true)
    {

        for(i = 1; i < numBits - 1; i++)
            prime_str[i] = (int)(2.0 * rand() / (RAND_MAX + 1.0)) + '0';
        prime_str[numBits - 1] = '1';
        prime_str[numBits] = '\0';
        mpz_set_str(number,prime_str,2);
        for(isComposite = false, i = 0; i <= maxPrimeIndex; i++)
        {
            if(mpz_cmp_ui(number,shortPrimes[i]) <= 0)//////////////////
                break;
            mpz_mod_ui(buf, number, shortPrimes[i]);/////////////////////
            if(mpz_cmp_ui(buf,(unsigned long int)0) == 0)////////////////////
            {
                isComposite = true;
                break;
            }
        }
        if(isComposite)
            continue;
            if(MillerRabin(number, numBits, 10))
                break;
    }

    mpz_clear(buf);
    free(prime_str);
}
Example #3
0
/*
 * Procedure qui calcule les facteurs premiers d'un nombre sur 64 bits 
 */
int get_prime_factors(uint64_t n, uint64_t* dest)
{
	uint64_t i,j,racine;
	int k=0;
	int dictInd=-1;
	int miller=0;
	int found=0;
	uint64_t nombre=n;
	if(n==1)
	{
		dest[k++]=1;
		found=1;
	}
	if((dictInd = RecupererFacteurs(n))!=-1)
		found=1;
	if(!found)
	{
		racine = sqrt(n);
		if(MillerRabin(n,17))
			dest[k++]=n;
		else
		{
			for(j=0;j<index1;++j)
			{
				if(n%primes[j]==0)
				{
					while(n%primes[j]==0)
					{
						n/=primes[j];
						dest[k++]=primes[j];
					}
					if(MillerRabin(n,17))
					{
						dest[k++]=n;
						miller=1;
						break;
					}
				}
			}
			i=N+1;
			j=0;
			racine = sqrt(n);
			if(!miller)
			{
				for(;i<=racine;i+=tab[j++],j%=index2)
				{
					if(n%i==0)
					{
						while(n%i==0)
						{
							n/=i;
							dest[k++]=i;
						}
						racine = sqrt(n);
						if(MillerRabin(n,17))
						{
							dest[k++]=n;
							miller=1;
							break;
						}
					}
				}
				if(n!=1 && !miller)
					dest[k++]=n;
			}
		}
	}
	if(!found)
		AjouterFacteurs(nombre,dest,k);
	else
	{
		unsigned int i;
		int sz=dictionnaire[dictInd].taille;
		for(i=0;i<sz;++i)
		{
			dest[k++]=dictionnaire[dictInd].facteurs[i];
		}
		AjouterFacteurs(nombre,dest,k);
	}
	return k;
}