MCPcopy Create free account
hub / github.com/JeanLucPons/VanitySearch / IsProbablePrime

Method IsProbablePrime

Int.cpp:1147–1195  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1145}
1146
1147bool Int::IsProbablePrime() {
1148
1149 // Prime cheking (probalistic Miller-Rabin test)
1150 Int::SetupField(this);
1151 int nbBit = GetBitLength();
1152
1153 Int Q(this);
1154 Q.SubOne();
1155 Int N1(&Q);
1156 uint64_t e = 0;
1157 while(Q.IsEven()) {
1158 Q.ShiftR(1);
1159 e++;
1160 }
1161
1162 uint64_t k = 50;
1163
1164 for(uint64_t i = 0; i < k; i++) {
1165
1166 Int a;
1167 Int x;
1168 x.SetInt32(0);
1169 while(x.IsLowerOrEqual(&_ONE) || x.IsGreaterOrEqual(&N1))
1170 x.Rand(nbBit);
1171 x.ModExp(&Q);
1172 if(x.IsOne() || x.IsEqual(&N1))
1173 continue;
1174
1175 for(uint64_t j = 0; j < e - 1; j++) {
1176 x.ModSquare(&x);
1177 if(x.IsOne()) {
1178 // Composite
1179 return false;
1180 }
1181 if(x.IsEqual(&N1))
1182 break;
1183 }
1184
1185 if(x.IsEqual(&N1))
1186 continue;
1187
1188 return false;
1189
1190 }
1191
1192 // Probable prime
1193 return true;
1194
1195}
1196
1197
1198// ------------------------------------------------

Callers

nothing calls this directly

Calls 11

SubOneMethod · 0.80
IsEvenMethod · 0.80
ShiftRMethod · 0.80
SetInt32Method · 0.80
IsLowerOrEqualMethod · 0.80
IsGreaterOrEqualMethod · 0.80
RandMethod · 0.80
ModExpMethod · 0.80
IsOneMethod · 0.80
IsEqualMethod · 0.80
ModSquareMethod · 0.80

Tested by

no test coverage detected