Number theory algorithm--Summary of basic algorithm of information security Mathematics (Python version)

Source: Internet
Author: User

/*author:wsnpyo Update Date:2014-11-16
Algorithm: Fast power/fermat, Solovay_stassen, miller-rabin primality test/EXGCD non-recursive version/Chinese remainder theorem */ImportRandomdefQuickpower (A, N, p):#Fast Power AlgorithmTMP =a ret= 1 while(N >0):if(n&1): Ret= (RET * tmp)%P tmp= (TMP * tmp)%P N>>=1returnretdefJacobi (N, m):#Calc Jacobi (n/m)n = n%mifn = =0:return0 Jacobi2= 1if not(n&1):#if n is an even number, calculate JACOBI2 = Jacobi (2/m) ^ (s) where n = 2^s*t T is oddK = ( -1) * * (((m**2-1)//8) &1) while not(n&1): Jacobi2*=k N>>= 1ifn = = 1: returnJacobi2returnJACOBI2 * ( -1) * * (((m-1)//2* (n-1)//2) * &AMP;1 (JacobiN, N)defEXGCD (R0, R1):#Calc Ax+by = gcd (A, B) return xx0, y0 = 1, 0 x1, y1= 0, 1x, y=R0, R1 R= r0%R1 Q= R0//R1 whiler:x, y= X0-q * x1, Y0-q *y1 x0, y0=x1, y1 x1, y1=x, y r0=R1 R1=R R= r0%R1 Q= R0//R1returnxdefFermat (x, T):#Fermat primality judgment ifX < 2: returnFalseifX <= 3: returnTrueifx%2 = = 0orx%3 = =0:returnFalse forIinchRange (T): Ran= Random.randint (2, X-2)#randomly take an integer [2, X-2] ifQuickpower (ran, x-1, x)! = 1: returnFalsereturnTruedefSolovay_stassen (x, T):#Solovay_stassen primality judgment ifX < 2: returnFalseifX <= 3: returnTrueifx%2 = = 0orx%3 = =0:returnFalse forIinchRange (T):#random selection of t integersRan = Random.randint (2, X-2) R= Quickpower (ran, (x-1)//2, X)ifR! = 1 andr! = X-1: returnFalseifr = = X-1: R=-1ifr! =Jacobi (ran, x):returnFalsereturnTruedefMillerrabin (x, ran):#x-1 = 2^s*ttx = X-1S2= tx& (~tx+1)#remove the last binary that starts with 1, which is 2^sR = Quickpower (ran, tx//s2, x)ifr = = 1orr = =TX:returnTrue whileS2>1:#from 2^s to 2^1 cycle s timesR = (r*r)%xifr = = 1: returnFalseifr = =TX:returnTrue S2>>= 1returnFalsedefMillerrabin_init (x, T):#Miller-rabin primality judgment ifX < 2: returnFalseifX <= 3: returnTrueifx%2 = = 0orx%3 = =0:returnFalse forIinchRange (T):#random selection of t integersRan = Random.randint (2, X-2) if notMillerrabin (x, ran):returnFalsereturnTruedefCRT (b, M, N):#Calc x = b[]% m[]M = 1 forIinchrange (N): M*=M[i] ans=0 forIinchrange (n): ans+ = b[i] * M//m[i] * EXGCD (m//M[i], m[i])returnAns%m

The above as a half semester to learn number theory a summary, perhaps later difficult to learn the system of number theory. Slightly sad Eedle

--thanks to the teacher of Information Security Mathematics Foundation

Number theory algorithm--Summary of basic algorithm of information security Mathematics (Python version)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.