public void TestMethod1() { Elgamal elgamal = new Elgamal(); NumberGenerator ng = new NumberGenerator(); ElgamalDecryptor decryptor = new ElgamalDecryptor(); int[] one = { 1 }; elgamal.P = NumberConverter.IntToBits(547); elgamal.G = NumberConverter.IntToBits(9); elgamal.A = NumberConverter.IntToBits(23); elgamal.M = NumberConverter.IntToBits(100); elgamal.K = NumberConverter.IntToBits(125); elgamal.B = elgamal.GenerateB(); Assert.AreEqual("81", NumberConverter.BitsArraystoString(elgamal.B).ToString()); elgamal.Kprim = elgamal.GenerateKprim(); Assert.AreEqual("83", NumberConverter.BitsArraystoString(elgamal.Kprim).ToString()); elgamal.R = elgamal.GenerateR(); Assert.AreEqual("304", NumberConverter.BitsArraystoString(elgamal.R).ToString()); elgamal.S = ng.GetS(elgamal.Kprim, elgamal.M, elgamal.A, elgamal.R, MathOperations.BitsSubstraction(elgamal.P, one)); Assert.AreEqual("172", NumberConverter.BitsArraystoString(elgamal.S).ToString()); Assert.AreEqual("81", NumberConverter.BitsArraystoString(decryptor.GetX2(elgamal)).ToString()); Assert.AreEqual("304", NumberConverter.BitsArraystoString(decryptor.GetBR(elgamal)).ToString()); Assert.AreEqual("182", NumberConverter.BitsArraystoString(decryptor.GetRS(elgamal)).ToString()); Assert.AreEqual("81", NumberConverter.BitsArraystoString(decryptor.GetX1(elgamal, decryptor.GetBR(elgamal), decryptor.GetRS(elgamal))).ToString()); Assert.AreEqual(true, decryptor.IsCorrect(elgamal)); }
public void Equal() { Assert.AreEqual(0, MathOperations.HigherThan(NumberConverter.IntToBits(200), NumberConverter.IntToBits(200))); Assert.AreEqual(0, MathOperations.HigherThan(NumberConverter.IntToBits(231), NumberConverter.IntToBits(231))); Assert.AreEqual(0, MathOperations.HigherThan(NumberConverter.IntToBits(2500), NumberConverter.IntToBits(2500))); Assert.AreEqual(0, MathOperations.HigherThan(NumberConverter.IntToBits(12458), NumberConverter.IntToBits(12458))); }
public void BIsHigher() { Assert.AreEqual(-1, MathOperations.HigherThan(NumberConverter.IntToBits(231), NumberConverter.IntToBits(232))); Assert.AreEqual(-1, MathOperations.HigherThan(NumberConverter.IntToBits(231), NumberConverter.IntToBits(232))); Assert.AreEqual(-1, MathOperations.HigherThan(NumberConverter.IntToBits(2), NumberConverter.IntToBits(2500))); Assert.AreEqual(-1, MathOperations.HigherThan(NumberConverter.IntToBits(12458), NumberConverter.IntToBits(2738514))); }
public void Modulo() { Assert.AreEqual(1, NumberConverter.BitsToInt(MathOperations.Modulo(NumberConverter.IntToBits(7), NumberConverter.IntToBits(2)))); Assert.AreEqual(2, NumberConverter.BitsToInt(MathOperations.Modulo(NumberConverter.IntToBits(200), NumberConverter.IntToBits(3)))); Assert.AreEqual(1, NumberConverter.BitsToInt(MathOperations.Modulo(NumberConverter.IntToBits(742), NumberConverter.IntToBits(13)))); Assert.AreEqual(4, NumberConverter.BitsToInt(MathOperations.Modulo(NumberConverter.IntToBits(789), NumberConverter.IntToBits(5)))); Assert.AreEqual(172, NumberConverter.BitsToInt(MathOperations.Modulo(NumberConverter.IntToBits(7888896), NumberConverter.IntToBits(874)))); Assert.AreEqual(21, NumberConverter.BitsToInt(MathOperations.Modulo(NumberConverter.IntToBits(4523106), NumberConverter.IntToBits(1743)))); }
public void IsPrime() { int[] tmp = { 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1 }; Array.Reverse(tmp); Assert.AreEqual(false, NumberGenerator.IsPrime(NumberConverter.IntToBits(18))); Assert.AreEqual(false, NumberGenerator.IsPrime(NumberConverter.IntToBits(123))); Assert.AreEqual(true, NumberGenerator.IsPrime(NumberConverter.IntToBits(324239))); Assert.AreEqual(true, NumberGenerator.IsPrime(NumberConverter.IntToBits(324251))); Assert.AreEqual(false, NumberGenerator.IsPrime(NumberConverter.IntToBits(324250))); Assert.AreEqual(true, NumberGenerator.IsPrime(tmp)); }
public void GCD() { int[] tmp = { 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0 }; Array.Reverse(tmp); Assert.AreEqual(1, NumberConverter.BitsToInt(MathOperations.GCD(NumberConverter.IntToBits(7), NumberConverter.IntToBits(5)))); Assert.AreEqual(7, NumberConverter.BitsToInt(MathOperations.GCD(NumberConverter.IntToBits(175), NumberConverter.IntToBits(7)))); Assert.AreEqual(7, NumberConverter.BitsToInt(MathOperations.GCD(NumberConverter.IntToBits(7), NumberConverter.IntToBits(175)))); Assert.AreEqual(1, NumberConverter.BitsToInt(MathOperations.GCD(NumberConverter.IntToBits(1236589), NumberConverter.IntToBits(2567)))); Assert.AreEqual(1, NumberConverter.BitsToInt(MathOperations.GCD(NumberConverter.IntToBits(6519413), NumberConverter.IntToBits(122)))); Assert.AreEqual(1, NumberConverter.BitsToInt(MathOperations.GCD(NumberConverter.IntToBits(6519413), NumberConverter.IntToBits(12)))); Assert.AreEqual(9, NumberConverter.BitsToInt(MathOperations.GCD(NumberConverter.IntToBits(123432534), NumberConverter.IntToBits(12123)))); }
public void Multiply() { Assert.AreEqual(20, NumberConverter.BitsToInt(MathOperations.BitsMultiplier(NumberConverter.IntToBits(2), NumberConverter.IntToBits(10)))); Assert.AreEqual(200, NumberConverter.BitsToInt(MathOperations.BitsMultiplier(NumberConverter.IntToBits(2), NumberConverter.IntToBits(100)))); Assert.AreEqual(4914, NumberConverter.BitsToInt(MathOperations.BitsMultiplier(NumberConverter.IntToBits(63), NumberConverter.IntToBits(78)))); Assert.AreEqual(978750, NumberConverter.BitsToInt(MathOperations.BitsMultiplier(NumberConverter.IntToBits(1250), NumberConverter.IntToBits(783)))); Assert.AreEqual(1000000, NumberConverter.BitsToInt(MathOperations.BitsMultiplier(NumberConverter.IntToBits(1000), NumberConverter.IntToBits(1000)))); Assert.AreEqual(473859433, NumberConverter.BitsToInt(MathOperations.BitsMultiplier(NumberConverter.IntToBits(5413), NumberConverter.IntToBits(87541)))); }
public void Substract() { Assert.AreEqual(2, NumberConverter.BitsToInt(MathOperations.BitsSubstraction(NumberConverter.IntToBits(32), NumberConverter.IntToBits(30)))); Assert.AreEqual(20, NumberConverter.BitsToInt(MathOperations.BitsSubstraction(NumberConverter.IntToBits(84), NumberConverter.IntToBits(64)))); Assert.AreEqual(200, NumberConverter.BitsToInt(MathOperations.BitsSubstraction(NumberConverter.IntToBits(876), NumberConverter.IntToBits(676)))); Assert.AreEqual(123, NumberConverter.BitsToInt(MathOperations.BitsSubstraction(NumberConverter.IntToBits(723), NumberConverter.IntToBits(600)))); Assert.AreEqual(8756, NumberConverter.BitsToInt(MathOperations.BitsSubstraction(NumberConverter.IntToBits(100000), NumberConverter.IntToBits(91244)))); }
public void InversePow() { Assert.AreEqual(3, NumberConverter.BitsToInt(MathOperations.InversePow(NumberConverter.IntToBits(5), NumberConverter.IntToBits(7)))); }
public void Addition() { Assert.AreEqual(20, NumberConverter.BitsToInt(MathOperations.BitsAddition(NumberConverter.IntToBits(10), NumberConverter.IntToBits(10)))); //Assert.AreEqual(123, NumberConverter.BitsToInt(MathOperations.BitsAddition(NumberConverter.IntToBits(10), NumberConverter.IntToBits(113)))); //Assert.AreEqual(201234, NumberConverter.BitsToInt(MathOperations.BitsAddition(NumberConverter.IntToBits(1789), NumberConverter.IntToBits(199445)))); }