private static void ComputeBenchmarks(TestSummation.Summation2 sigma) { Util.Timer t = new Util.Timer(false); t.Tick("sigma=" + sigma); double value = sigma.Compute(); t.Tick("compute=" + value); NUnit.Framework.Assert.AreEqual(value, sigma.Compute_modular()); t.Tick("compute_modular"); NUnit.Framework.Assert.AreEqual(value, sigma.Compute_montgomery()); t.Tick("compute_montgomery"); NUnit.Framework.Assert.AreEqual(value, sigma.Compute_montgomery2()); t.Tick("compute_montgomery2"); NUnit.Framework.Assert.AreEqual(value, sigma.Compute_modBigInteger()); t.Tick("compute_modBigInteger"); NUnit.Framework.Assert.AreEqual(value, sigma.Compute_modPow()); t.Tick("compute_modPow"); }
internal static void SquareBenchmarks() { Util.Timer t = new Util.Timer(false); t.Tick("squareBenchmarks(), MAX_SQRT=" + Modular.MaxSqrtLong); long[][][] rn = GenerateRN(1000, 1000); t.Tick("generateRN"); for (int i = 0; i < rn.Length; i++) { long n = rn[i][0][0]; for (int j = 1; j < rn[i].Length; j++) { long r = rn[i][j][0]; long answer = rn[i][j][1]; long s = Square_slow(r, n); if (s != answer) { NUnit.Framework.Assert.AreEqual("r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("square_slow"); for (int i_1 = 0; i_1 < rn.Length; i_1++) { long n = rn[i_1][0][0]; long r2p64 = (unchecked ((long)(0x4000000000000000L)) % n) << 1; if (r2p64 >= n) { r2p64 -= n; } for (int j = 1; j < rn[i_1].Length; j++) { long r = rn[i_1][j][0]; long answer = rn[i_1][j][1]; long s = Square(r, n, r2p64); if (s != answer) { NUnit.Framework.Assert.AreEqual("r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("square"); for (int i_2 = 0; i_2 < rn.Length; i_2++) { long n = rn[i_2][0][0]; BigInteger N = BigInteger.ValueOf(n); for (int j = 1; j < rn[i_2].Length; j++) { long r = rn[i_2][j][0]; long answer = rn[i_2][j][1]; BigInteger R = BigInteger.ValueOf(r); long s = R.Multiply(R).Mod(N); if (s != answer) { NUnit.Framework.Assert.AreEqual("r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("R.multiply(R).mod(N)"); for (int i_3 = 0; i_3 < rn.Length; i_3++) { long n = rn[i_3][0][0]; BigInteger N = BigInteger.ValueOf(n); for (int j = 1; j < rn[i_3].Length; j++) { long r = rn[i_3][j][0]; long answer = rn[i_3][j][1]; BigInteger R = BigInteger.ValueOf(r); long s = R.ModPow(Two, N); if (s != answer) { NUnit.Framework.Assert.AreEqual("r=" + r + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("R.modPow(TWO, N)"); }
internal static void ModBenchmarks() { Util.Timer t = new Util.Timer(false); t.Tick("modBenchmarks()"); long[][][] en = GenerateEN(10000, 10); t.Tick("generateEN"); for (int i = 0; i < en.Length; i++) { long n = en[i][0][0]; for (int j = 1; j < en[i].Length; j++) { long e = en[i][j][0]; long answer = en[i][j][1]; long s = Modular.Mod(e, n); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("Modular.mod"); TestModular.Montgomery2 m2 = new TestModular.Montgomery2(); for (int i_1 = 0; i_1 < en.Length; i_1++) { long n = en[i_1][0][0]; m2.Set(n); for (int j = 1; j < en[i_1].Length; j++) { long e = en[i_1][j][0]; long answer = en[i_1][j][1]; long s = m2.Mod(e); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("montgomery.mod"); for (int i_2 = 0; i_2 < en.Length; i_2++) { long n = en[i_2][0][0]; m2.Set(n); for (int j = 1; j < en[i_2].Length; j++) { long e = en[i_2][j][0]; long answer = en[i_2][j][1]; long s = m2.Mod2(e); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("montgomery.mod2"); for (int i_3 = 0; i_3 < en.Length; i_3++) { long n = en[i_3][0][0]; BigInteger N = BigInteger.ValueOf(n); for (int j = 1; j < en[i_3].Length; j++) { long e = en[i_3][j][0]; long answer = en[i_3][j][1]; long s = Two.ModPow(BigInteger.ValueOf(e), N); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("BigInteger.modPow(e, n)"); }
private static void ComputePi(Util.Timer t, long b) { t.Tick(Util.Pi2string(ComputePi(b), Bit2terms(b))); }