| 1145 | } |
| 1146 | |
| 1147 | bool 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 | // ------------------------------------------------ |
nothing calls this directly
no test coverage detected