Пример #1
0
bool RSA::isPrime(const ZZ& prime, long t)
{

  if (prime <= 1) 
		return false;

	// First, just try out the division by the first 2000 primes
	// Source: http://www.shoup.net/ntl/doc/tour-ex1.html
	PrimeSeq primeSequence; 
	long tempPrime;

	tempPrime = primeSequence.next();
	while(tempPrime && tempPrime < 2000) 
	{
		if((prime % tempPrime) == 0) 
			return prime == tempPrime;
		tempPrime = primeSequence.next();
	}

	// Not it's time for real prime testing
	ZZ x;
	long i;

	for(i = 0; i < t; i++) 
	{
		x = bigRandom(this->numberOfBits / 4); // random number between 0 and n-1

		if (MRTest(prime, x)) 
			return false;
	}

	return true;
}
Пример #2
0
int32_t millerRabin(uint8_t* n){
	//Most memory requirement part. mem=8*NUMLEN
#if RSADEBUG==1
	USART0_SendStr("Start MR test:\n");
#endif
	if(n[0]&1==0)
		return 0;
	uint32_t t=0;
	int32_t i=0,k;
	uint8_t temp;
	uint8_t s[NUMLEN];
	uint8_t a[NUMLEN];
	int32_t next=0;
	
	bigCopy(s,n);
	s[0]^=1;//s=s-1
	while(!s[i]){
		++i;
		t+=8;
	}	
	temp=s[i];
	while((temp&1)==0){
		++t;
		temp>>=1;
	}
	bigRShift(s,t);
	
	k=0;
	while(k<MILLER_RABIN_K){
#if RSADEBUG==1
		USART0_SendByte('.');
#endif
		n[0]|=1;
		bigRandom(a,n);		
		bigExp(a,s,n);//a=a**s mod n
		n[0]^=1;
		if(!(bigIsOne(a)||bigEqual(a,n))){
			next=0;
			for(i=0;(i<t-1)&&!next;++i){
				n[0]|=1;
				bigMul(a,a,n);
				if(bigIsOne(a)){
#if RSADEBUG==1
					USART0_SendByte(' ');
#endif
					return 0;
				}
				n[0]^=1;
				if(bigEqual(a,n))
					next=1;
			}
			if(!next){
#if RSADEBUG==1
				USART0_SendByte(' ');
#endif
				return 0;
			}
		}
		k+=2;
	}
#if RSADEBUG==1
	USART0_SendStr("\n");
#endif
	n[0]|=1;
	return 1;
}