Primality Testing、Miller Rabin Algorithm

版权声明:转载请附上文章地址 https://blog.csdn.net/weixin_38134491/article/details/85008877

Primality Testing

Determine whether a given large number is prime, traditionally sieve using trial division.

Two properties of prime numbers:

  • property 1

if p is prime and a is a positive integer with a<p,

a^2 mod p =1 if and only if   a mod p =1 or a mod p =-1 mod p =p-1

  • property  2

 

Miller Rabin Algorithm

Test(n) :

  1. find integers k , q with k>0 , q odd, so that (n-1)=2^k *q
  2. select a random integer a, 1<a<n-1
  3. if a^q mod n =1 then return ('inconclusive')
  4. for j=0 to k-1 do  
  5. if (a^((2^j)*q)) mod n =n-1)         then  return("inconclusive")
  6. return ("composite")

   

example 1:

Test   n=29 

29-1 = 2^2 * 7, thus k=2, q=7

a^q mod n= 2^7 mod 29 = 12!=1!=29-1

then 12^2=144 mod 29=28= 29-1

test returns "inconclusive"(propaly prime)

example 2:

Test n=2047   a=2

2047-1 = 2046= 2^1 * 1023,  thus k=1 , q=1023

a^q mod n= 2^1023 mod 2047= 1

2^1=2

2^2= 4

2^4=16 

2^8=256

2^16=65536 = 32 (mod 2047)

2^32=1024 

2^64=512 (mod 2047)

2^128=128 (mod 2047)

2^256= 8 (mod 2047)

2^512=64 

2^1023=2^(512+256+128+64+32+16+8+4+2+1)

=64*8*128*512*1024*32*256*16*4*2 (mod 2047)

=8*256 mod 2047

=1 mod 2047

总结:  2^1023 mod 2047=  这类计算化简,不管mod 前面数有多大都要化简到2047的最小倍数然后得出其余数

猜你喜欢

转载自blog.csdn.net/weixin_38134491/article/details/85008877