/// <summary> /// Loop until a probable prime is found. /// Based on the Miller Rabin primility test with 64 loops to be deterministic /// </summary> /// <param name="prime"></param> /// <returns>Prime number</returns> private BigInteger GetPrime(BigInteger prime) { int count = 1; BigInteger two = new BigInteger(2); while (true) { if (MillerRabin.IsProbablePrime(prime, 64)) { Console.WriteLine("Number of rounds: {0}", count); return(prime); } prime += two; count++; } }
private void init() { millerRabbin = new MillerRabin(); random = new Random(); p = GetSimpleNum(length); q = GetSimpleNum(length); eyler = (p - 1) * (q - 1); N = p * q; montRed = new MontgomeryReducer(N); expSqr = new ExponentiationSquaring(); //byte[] arr = N.ToByteArray(); e = GetOpenExponent(2 * length / 3); d = MathMethods.GCD(e, eyler); }
private void btnTestPrimality_Click(object sender, EventArgs e) { if (IsBusy) { return; } List <BigInteger> inputLines = GetInputNumbers(); int searchDepth = SearchDepth; DateTime startTime = DateTime.Now; List <Task <bool> > taskList = new List <Task <bool> >(); foreach (BigInteger number in inputLines) { Task <bool> newTask = Task.Run(() => MillerRabin.IsProbablyPrime(number, searchDepth)); taskList.Add(newTask); } IsBusy = true; tbOutput.Clear(); // Thread to gather results new Thread(() => { //List<Task<bool>> taskList = new List<Task<bool>>(taskLis); int counter = 1; foreach (Task <bool> tsk in taskList) { bool result = tsk.Result; WriteOutputTextboxLine(string.Format("#{0}: {1}", counter.ToString().PadRight(3, ' '), result.ToString())); counter++; } this.Invoke(new MethodInvoker(() => IsBusy = false)); }).Start(); }
public void TestForMediumNotPromalityNumbers() { Assert.IsFalse(MillerRabin.IsSimplyNumber(221)); Assert.IsFalse(MillerRabin.IsSimplyNumber(421321)); Assert.IsFalse(MillerRabin.IsSimplyNumber(781323)); }
public void TestForLargeNotPromalityNumbers() { Assert.IsFalse(MillerRabin.IsSimplyNumber(2114782963)); Assert.IsFalse(MillerRabin.IsSimplyNumber(1114781963)); }
public void TestForSmallNotPromalityNumbers() { Assert.IsFalse(MillerRabin.IsSimplyNumber(4)); Assert.IsFalse(MillerRabin.IsSimplyNumber(15)); Assert.IsFalse(MillerRabin.IsSimplyNumber(21)); }
public void TestForLargePromalityNumbers() { Assert.IsTrue(MillerRabin.IsSimplyNumber(1046527)); Assert.IsTrue(MillerRabin.IsSimplyNumber(1073676287)); }
public void TestForMediumPromalityNumbers() { Assert.IsTrue(MillerRabin.IsSimplyNumber(45137)); Assert.IsTrue(MillerRabin.IsSimplyNumber(46147)); Assert.IsTrue(MillerRabin.IsSimplyNumber(60373)); }
public void TestForSmallPromalityNumbers() { Assert.IsTrue(MillerRabin.IsSimplyNumber(1)); Assert.IsTrue(MillerRabin.IsSimplyNumber(3)); Assert.IsTrue(MillerRabin.IsSimplyNumber(5)); }
static void Main(string[] args) { // Initialize Standard configuration IOrderFinder of = new QuantumOrderFinder(); IPrimeChecker pc = new MillerRabin(); int n = args.Length; bool saveFile = false; string logfile = ""; // Read Arguments from the command line for (int i = 0; i < n; i++) { if (args[i] == "-of") { if (i == n - 1) { Console.WriteLine("Error: Argument -of must be followed by the name of the OrderFinder."); return; } string nofn = args[i + 1]; i++; if (nofn.ToLower() == "naive") { of = new NaiveOrderFinder(); } if (nofn.ToLower() == "babygiant") { of = new BabyGiantOrderFinder(); } if (nofn.ToLower() == "quantum") { of = new QuantumOrderFinder(); } } if (args[i] == "-pc") { if (i == n - 1) { Console.WriteLine("Error: Argument -pc must be followed by the name of the PrimeChecker."); return; } string nofn = args[i + 1]; i++; if (nofn.ToLower() == "naive") { pc = new NaivePrimeChecker(); } if (nofn.ToLower() == "millerrabin") { if (i == n - 1) { Console.WriteLine("Error: MillerRabin must be followed by the number of iterations."); return; } string vl = args[i + 1]; i++; int it; if (!int.TryParse(vl, out it)) { Console.WriteLine("Error: MillerRabin must be followed by the number of iterations."); return; } pc = new MillerRabin(it); } } if (args[i] == "-h") { Console.WriteLine(help_text); return; } if (args[i] == "-f") { if (i == n - 1) { Console.WriteLine("Error: -f Must be followed by valid file name"); return; } string fn = args[i + 1]; i++; saveFile = true; logfile = Path.GetFullPath(fn); } } // Initialize the Timer PerformanceTimer.Init(pc, of); // Factorize each number in the input: // For each line, while not at end of line string st; while ((st = Console.ReadLine()) != null) { List <string> vals = st.Split(' ').ToList(); List <long> inp = new List <long>(); // Try to convert each word on this line to a number foreach (string s in vals) { long r = 0; if (!long.TryParse(s, out r)) { Console.WriteLine("Error: Conversion to long failed"); return; } inp.Add(r); } // Measure the time for each of these numbers foreach (long v in inp) { TimeSpan ts = PerformanceTimer.Measure(v); string message = "Factorized " + v.ToString() + " in " + ts.Days + "d " + ts.Hours + "h " + ts.Minutes + "m " + ts.Seconds + "s " + ts.Milliseconds + "ms."; if (saveFile) { var sw = new StreamWriter(logfile, true); sw.WriteLine(message); sw.Close(); } Console.WriteLine(message); } } }