PROBABILISTICALLY TESTING FOR PRIMALITY

(ref: Rivest/Shamir/Adleman Comm. ACM Feb. 1978, p.124)

To test if an odd N is prime:

	for loop LeftArrow 1 to k do
		randomly select b from the set {1..N-1}
		if  GCD(b,N)==1  and  J(b,N) equivalent b(N-1)/2(mod N)
		then	continue looping
		else	return  not prime
	return  prime with probability > 1-2-k

To calculate the value of J(x,y):

	if  x==1		then	return  1
	else if  x is even	then	return  J( x/2,y ) * (-1)(y2-1)/8
	else				return  J( y(mod x),x ) * (-1)(x-1)(y-1)/4

To calculate the value of b(N-1)/2(mod N):

	x LeftArrow (N-1)/2
	y LeftArrow b
	a LeftArrow 1
	while x > 0  do
		if x is odd then a LeftArrow ay(mod N)
		y LeftArrow y2(mod N)
		x LeftArrow floor(x/2)
	return  a


Last modified: Feb 25, 2011