Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
1, Generation principle of pseudo large prime numbers
How to generate a random large prime number?
Method 1
① Randomly select a large odd number n
② Arrange m prime numbers starting from 2 (within 2000) into an array as a tool a[i]
③ Let i=0, calculate x=n%a[i]
④ Judge that if x=0, it indicates that n is obviously a composite number, and return to step 1. If it is not equal to 0, it means that for the time being, n can be considered prime, and proceed to step 5.
⑤ Detect n% other a[i]. When i=m-1, n is regarded as a pseudo prime, and then as the result of the prime generation part.
Method 2
Perform prime detection on a large odd number. If it is not a prime number, regenerate a large odd number and test it again until a large odd number with a maximum probability of prime number is found, that is, a pseudo large prime number. In this paper, this method is used to generate pseudo large prime numbers, and the prime judgment algorithms are Fermat's small theorem judgment method and millarbin prime judgment method respectively.
Fundamentals of Mathematics
Fermat theorem: if n is an odd prime number and a is any integer (1 ≤ a ≤ n-1), then a^(n-1) ≡ 1(mod n).
Miller Rabin algorithm is a modification of Fermat algorithm. Its theoretical basis is extended from Fermat theorem.
Theoretical basis of Miller Rabin algorithm:
If n Is an odd prime number that will n-1 Expressed as 2^s*r Form of(r It's an odd number),a Yes and n Any integer that is coprime, then a^r ≡1(mod n) Or for someone j(0≤j ≤s -1, j∈Z) equation a^(2^j*r) ≡-1(mod n)Established. This theory is through a fact Fermat Derived from the theorem: n Is an odd prime number, then the equation x^2 ≡ 1 mod n only±1 Two solutions.
Namely:
2, Distinguishing method of Fermat's small theorem
1. Algorithm
if 1 ≤ a ≤ n-1 has an ≡ a (mod n), that is, an-1 mod n = 1, so n is a prime number
2. Code implementation
#include<iostream> #include<iomanip> #include<cstdlib> #include<ctime> #include<cmath> using namespace std; /*Use three rand() to generate pseudo-random numbers, and combine them to generate an odd random number as a pseudo prime number **System time is seed **And returns the generated large odd number */ unsigned int ProduceRandomOdd(){//UINT unsigned integer, and each pseudo-random number is placed in the RandomArray array time_t t;//c + + time type unsigned int RandomNumber;//Record random number do{ srand((unsigned)time(&t));//srand(seed) is used to seed the rand() function, where the system time is used //generate RandomNumber=(rand()<<17)|(rand()<<3)|(rand()); //cout<<RandomNumber<<endl; }while(RandomNumber%2==0||RandomNumber<100000000); //return return RandomNumber; } long long qmod(int a, int b, int p) { long long res = 1; long long term = a%p; while(b) { if(b&1){ res = (res*term)%p; } term = (term*term)%p; b >>= 1; } return res; } bool Fermat_prime(long long n) { int i; for(i = 0; i < 100; ++i) { if(qmod(1+rand()%(n-1),n-1, n) != 1) break; } if(i < 100) return false; else return true; } int main(){ unsigned int RandomOdd; bool flag; //Until the passing pseudo prime number is found do{ RandomOdd=ProduceRandomOdd(); //cout<<RandomOdd<<endl; flag=Fermat_prime(RandomOdd); }while(flag==false); //cout<<"=========================="<<endl; cout<<"A pseudo prime generated is:" <<RandomOdd; }
2, Millarabin prime number determination method
1. Algorithm
Miller-Rabin(n,t)
Input: an odd integer n greater than 3 and a safety parameter t greater than or equal to 1 (used to determine the number of test rounds).
Output: Returns whether n is a prime number (in the sense of probability, the general misjudgment probability is less than (1 / 2) 80).
1. Express n-1 as 2sr (where r is an odd number)
2. Perform the following operations for i cycle from 1 to t:
2.1 select a random integer a(2 ≤ a ≤ n-2)
2.2 calculation of y ← ar mod n
2.3 if y ≠ 1 and Y ≠ n-1 do the following operations, otherwise turn to 3:
2.3.1 j←1;
2.3.2 when j ≤ s-1 and y ≠ n-1 cycle, perform the following operations, otherwise skip to 2.3.3:
{calculate y ← y2 mod n;
If y=1, returns the composite number;
Otherwise, J ← j+1;}
2.3.3 if y ≠ n-1, the composite number is returned;
3. Return prime.
2. Code implementation
#include <iostream> #include <stdlib.h> #include <time.h> #include <math.h> //#include <cstddef.h> using namespace std; /*Use three rand() to generate pseudo-random numbers, and combine them to generate an odd random number as a pseudo prime number **System time is seed **And returns the generated large odd number */ unsigned int ProduceRandomOdd(){//UINT unsigned integer, and each pseudo-random number is placed in the RandomArray array time_t t;//c + + time type unsigned int RandomNumber;//Record random number do{ srand((unsigned)time(&t));//srand(seed) is used to seed the rand() function, where the system time is used //generate RandomNumber=(rand()<<17)|(rand()<<3)|(rand()); //cout<<RandomNumber<<endl; }while(RandomNumber%2==0||RandomNumber<100000000); //return return RandomNumber; } //Modular repeat square algorithm for (b^n)%m size_t repeatMod(size_t base, size_t n, size_t mod){ size_t a = 1; while(n){ if(n&1){ a=(a*base)%mod; } base=(base*base)%mod; n=n>>1; } return a; } //Miller Rabin Prime detection bool rabinmiller(size_t n, size_t k){ int s=0; int temp=n-1; //Express n-1 as (2^s)*t while ((temp&0x1)==0&&temp){ temp=temp>>1; s++; } size_t t = temp; //The misjudgment probability of judging k rounds shall not be greater than (1/4)^k while(k--){ srand((unsigned)time(0)); size_t b = rand()%(n-2)+2; //Generate a b(2 ≤ a ≤ n-2) size_t y = repeatMod(b,t,n); if (y == 1 || y == (n-1)) return true; for(int j = 1; j<=(s-1) && y != (n-1); ++j){ y = repeatMod(y,2,n); if (y == 1) return false; } if (y != (n-1)) return false; } return true; } int main(){ size_t k=80;//Perform 80 rounds of testing unsigned int RandomOdd; bool flag; //Until the passing pseudo prime number is found do{ RandomOdd=ProduceRandomOdd(); //cout<<RandomOdd<<endl; flag=rabinmiller(RandomOdd,k); }while(flag==false); //cout<<"=========================="<<endl; cout<<"A pseudo prime generated is:" <<RandomOdd; }
3, Characteristics and advantages and disadvantages of different algorithms
① Miller Rabin algorithm is the mainstream prime number testing algorithm based on probability, which plays an important role in the construction of password security system.
② By comparing various prime test algorithms and careful study of Miller Rabin algorithm, it is proved that Miller Rabin algorithm is the best choice to complete prime test when constructing password security system in computer.
③ Through the optimization of the underlying operation of Miller Rabin algorithm, better performance can be achieved than before, which is the reason why Fermat prime test is no longer useful.