public BigInt ChineseTheorem(BigInt r1, BigInt r2, BigInt mod1, BigInt mod2) { // Console.WriteLine("r1:" + r1.toString()); BigInt M = BigInt.multiply(mod1, mod2); BigInt m1 = BigInt.divide(M, mod1); BigInt m2 = BigInt.divide(M, mod2); BigInt y1 = new BigInt("1"); BigInt y2 = new BigInt("1"); BigInt one = new BigInt("1"); for (BigInt i = one; BigInt.less(i, mod1); i = BigInt.add(i, one)) { if (BigInt.equal(BigInt.multiplyByMod(m1, i, mod1), one)) { y1 = i; break; } } for (BigInt i = one; BigInt.less(i, mod2); i = BigInt.add(i, one)) { if (BigInt.equal(BigInt.multiplyByMod(m2, i, mod2), one)) { y2 = i; break; } } return(BigInt.mod(BigInt.add(BigInt.multiply(m1, BigInt.multiply(r1, y1)), BigInt.multiply(m2, BigInt.multiply(r2, y2))), mod)); }
public BigInt Hash(string word, BigInt mod) { BigInt sum = new BigInt("0"); for (int i = 0; i < word.Length; i++) { sum = BigInt.add(sum, BigInt.multiply(new BigInt(((int)word[i]).ToString()), BigInt.pow(new BigInt("2"), new BigInt(i.ToString())))); } return(sum); }
static void calculate() { BigInt a, b, m; int n; switch (choice) { case 1: Console.WriteLine("1 - +"); a = getNumber("First "); // BigInt.Billie(a); // Console.WriteLine((BigInt.Billie(a).toString())); b = getNumber("Second "); Console.WriteLine(a.toString() + "+" + b.toString() + "=" + BigInt.add(a, b).toString()); break; case 2: Console.WriteLine("2 - -"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "-" + b.toString() + "=" + BigInt.subtract(a, b).toString()); break; case 3: Console.WriteLine("3 - *"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "*" + b.toString() + "=" + BigInt.multiply(a, b).toString()); break; case 4: Console.WriteLine("4 - /"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "/" + b.toString() + "=" + BigInt.divide(a, b).toString()); Console.WriteLine("Ostacha: " + BigInt.subtract(a, BigInt.multiply(b, BigInt.divide(a, b))).toString()); break; case 5: Console.WriteLine("5 - ^"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "^" + b.toString() + "=" + BigInt.pow(a, b).toString()); break; case 6: Console.WriteLine("6 - [sqrt(x)]"); a = getNumber("Number >0 "); Console.WriteLine("sqrt =" + a.sqrt().toString()); break; case 7: Console.WriteLine("7 - <"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "<" + b.toString() + "=" + BigInt.less(a, b)); break; case 8: Console.WriteLine("8 - >"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + ">" + b.toString() + "=" + BigInt.greater(a, b)); break; case 9: Console.WriteLine("9 - =="); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "==" + b.toString() + "=" + BigInt.equal(a, b)); break; case 10: Console.WriteLine("10 - + (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "+" + b.toString() + "mod" + m.toString() + "=" + BigInt.addMod(a, b, m).toString()); break; case 11: Console.WriteLine("11 - - (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "-" + b.toString() + "mod" + m.toString() + "=" + BigInt.subtractMod(a, b, m).toString()); break; case 12: Console.WriteLine("12 - * (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "*" + b.toString() + "mod" + m.toString() + "=" + BigInt.multiplyByMod(a, b, m).toString()); break; case 13: Console.WriteLine("13 - /(mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "/" + b.toString() + "mod" + m.toString() + "=" + BigInt.divideMod(a, b, m).toString()); Console.WriteLine("Ostacha: " + BigInt.subtractMod(a, BigInt.multiply(b, BigInt.divide(a, b)), m).toString()); break; case 14: Console.WriteLine("14 - ^ (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "^" + b.toString() + "mod" + m.toString() + "=" + BigInt.powByMod(a, b, m).toString()); break; case 15: Console.WriteLine("15 - aiX=bi(mod pi)"); n = getUInt("Count of equations"); BigInt[] A = new BigInt[n]; BigInt[] B = new BigInt[n]; BigInt[] M = new BigInt[n]; for (int i = 0; i < n; i++) { A[i] = getNumber("A[" + i + "]"); B[i] = getNumber("B[" + i + "]"); M[i] = getNumber("P[" + i + "]"); } for (int i = 0; i < n; i++) { Console.WriteLine(A[i].toString() + "x =" + B[i].toString() + "mod" + M[i].toString()); } (BigInt, BigInt)res = BigInt.solve(A, B, M, n); Console.WriteLine("x=" + res.Item1.toString() + "mod" + res.Item2.toString()); break; case 16: Console.WriteLine("16 - ^ (mod) fast"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "^" + b.toString() + "mod" + m.toString() + "=" + BigInt.powByModFast(a, b, m).toString()); break; case 17: Console.WriteLine("17 - Miller Rabin"); a = getNumber("Number "); Console.WriteLine(BigInt.MillerRabin(a)); break; case 18: //Console.WriteLine("18 - Billie"); //Console.WriteLine("Enter the word"); //string word = Console.ReadLine(); //RSA test = new RSA(8, 1, 6); //test.Encryption(word); //Console.WriteLine(test.Decryption()); break; case 19: Console.WriteLine("Enter the word"); string word = Console.ReadLine(); long ellapledTicks = DateTime.Now.Ticks; RSA test = new RSA(8, 32, 64); test.Encryption(word); ellapledTicks = DateTime.Now.Ticks - ellapledTicks; TimeSpan elapsedSpan = new TimeSpan(ellapledTicks); Console.WriteLine(test.Decryption()); Console.WriteLine("Time: " + elapsedSpan.TotalMilliseconds); break; case 20: Console.WriteLine("19 - exit"); end = true; break; } }