public void SimpleAndNeg() { IntX int1 = new IntX(8); Assert.IsTrue(int1 << 0 == int1 >> 0 && int1 << 0 == 8); Assert.IsTrue(int1 << 32 == int1 >> -32 && int1 << 32 == new IntX(new uint[] { 0, 8 }, false)); }
public void Complex() { IntX int1 = new IntX("0x0080808080808080"); Assert.IsTrue((int1 << 4).ToString(16) == "808080808080800"); Assert.IsTrue(int1 >> 36 == 0x80808); }
public void Add0IntX() { IntX int1 = new IntX(3); Assert.IsTrue(int1 + 0 == 3); Assert.IsTrue(int1 + new IntX() == 3); }
public static IEnumerable <IntX> Fibonacci(int range) { int i = 0; IntX very = 0; yield return(very); ++i; IntX old = 1; yield return(old); ++i; IntX fib = 0; while (i < range) { fib = very + old; yield return(fib); ++i; very = old; old = fib; } }
public void AddSub() { IntX int1 = new IntX(2); IntX int2 = new IntX(-2); Assert.IsTrue(int1 + int2 == 0); }
public void Add2IntXNeg() { IntX int1 = new IntX(-3); IntX int2 = new IntX(-5); Assert.IsTrue(int1 + int2 == -8); }
public void Add2IntX() { IntX int1 = new IntX(3); IntX int2 = new IntX(5); Assert.IsTrue(int1 + int2 == 8); }
//Extended GCD implementation. Taken from: //http://amir-shenodua.blogspot.com/2012/06/extended-gcd-algorithm-extended.html //(Easier to copy than to figure out how to implement this in C#) public static IntX[] Extended_GCD(IntX a, IntX b) { IntX[] result = new IntX[3]; if (a < b) //if a less than b, switch them { IntX temp = a; a = b; b = temp; } IntX r = b; IntX q = 0; IntX x0 = 1; IntX y0 = 0; IntX x1 = 0; IntX y1 = 1; IntX x = 0, y = 0; while (r > 1) { r = a % b; q = a / b; x = x0 - q * x1; y = y0 - q * y1; x0 = x1; y0 = y1; x1 = x; y1 = y; a = b; b = r; } result[0] = r; result[1] = x; result[2] = y; return(result); }
public void Add2BigIntXC3() { IntX int1 = new IntX(new uint[] { uint.MaxValue, uint.MaxValue }, false); IntX int2 = new IntX(new uint[] { 1, 1 }, false); IntX int3 = new IntX(new uint[] { 0, 1, 1 }, false); Assert.IsTrue(int1 + int2 == int3); }
public void Zero() { IntX int1 = new IntX(); Assert.IsTrue(int1 << 100 == 0); Assert.IsTrue(int1 >> 100 == 0); }
//Old GCD algorithm. public static IntX gcd(IntX a, IntX b) { if(b == 0) { return a; }else { return gcd(b, a % b); } }
public void Sub2BigIntXC4() { IntX int1 = new IntX(new uint[] { uint.MaxValue, uint.MaxValue, 1, 1 }, false); IntX int2 = new IntX(new uint[] { 1, 1 }, false); IntX int3 = new IntX(new uint[] { 0, 1, 2, 1 }, false); Assert.IsTrue(int1 == int3 - int2); }
public void Simple() { IntX int1 = new IntX(7); IntX int2 = new IntX(8); Assert.IsTrue(int2 > int1); }
public void Add0IntXNeg() { IntX int1 = new IntX(-3); Assert.IsTrue(int1 + 0 == -3); Assert.IsTrue(int1 + new IntX() == -3); Assert.IsTrue(new IntX(0) + new IntX(-1) == -1); }
public void ConvertToLong() { long n = 1234567890123456789; IntX intX = n; Assert.AreEqual(n, (long)intX); n = -n; intX = n; Assert.AreEqual(n, (long)intX); n = 0; intX = n; Assert.AreEqual(n, (long)intX); uint un = 1234567890; n = (long)(un | (ulong)un << 32); intX = new IntX(new uint[] { un, un, un, un, un }, false); Assert.AreEqual(n, (long)intX); intX = new IntX(new uint[] { un, un, un, un, un }, true); Assert.AreEqual(-n, (long)intX); int ni = 1234567890; n = ni; intX = ni; Assert.AreEqual(n, (long)intX); }
public override string ToString() { IntX afterPoint = null; IntX beforePoint = IntX.DivideModulo(_integerPart, _scale, out afterPoint, DivideMode.AutoNewton); return(beforePoint.ToString() + "." + afterPoint.ToString()); }
public void Add2BigIntX() { IntX int1 = new IntX(new uint[] { 1, 2, 3 }, false); IntX int2 = new IntX(new uint[] { 3, 4, 5 }, false); IntX int3 = new IntX(new uint[] { 4, 6, 8 }, false); Assert.IsTrue(int1 + int2 == int3); }
/// <summary> /// Compares 2 <see cref="IntX" /> objects. /// Returns "-2" if any argument is null, "-1" if <paramref name="int1" /> < <paramref name="int2" />, /// "0" if equal and "1" if >. /// </summary> /// <param name="int1">First big integer.</param> /// <param name="int2">Second big integer.</param> /// <param name="throwNullException">Raises or not <see cref="NullReferenceException" />.</param> /// <returns>Comparsion result.</returns> /// <exception cref="ArgumentNullException"><paramref name="int1" /> or <paramref name="int2" /> is a null reference and <paramref name="throwNullException" /> is set to true.</exception> static public int Cmp(IntX int1, IntX int2, bool throwNullException) { // If one of the operands is null, throw exception or return -2 bool isNull1 = ReferenceEquals(int1, null); bool isNull2 = ReferenceEquals(int2, null); if (isNull1 || isNull2) { if (throwNullException) { throw new ArgumentNullException(isNull1 ? "int1" : "int2", Strings.CantBeNullCmp); } else { return(isNull1 && isNull2 ? 0 : -2); } } // Compare sign if (int1._negative && !int2._negative) { return(-1); } if (!int1._negative && int2._negative) { return(1); } // Compare presentation return(DigitOpHelper.Cmp(int1._digits, int1._length, int2._digits, int2._length) * (int1._negative ? -1 : 1)); }
public void Equals2IntX() { IntX int1 = new IntX(8); IntX int2 = new IntX(8); Assert.IsTrue(int1.Equals(int2)); }
//--------------------------------------------------------------------------- //Fast exponentiation function. Consulted Wikipedia for this. //Performs exponentiation by squaring. Fewer operations needed than //traditional exponentiation function. public static IntX power(IntX x, IntX n) { if (n < 0) { x = 1 / x; n = n * -1; } if (n == 0) { return(1); } IntX y = 1; while (n > 1) { if ((n % 2) == 0) { x = x * x; n = n / 2; } else { y = x * y; x = x * x; n = (n - 1) / 2; } } return(x * y); }
public void Equals2IntXOp() { IntX int1 = new IntX(8); IntX int2 = new IntX(8); Assert.IsTrue(int1 == int2); }
//Reads in a file and decrypts its contents. public static IntX[] decryptfromFile(string path, IntX key_e) { int lineCount = File.ReadLines(path).Count(); IntX[] fromFile = new IntX[lineCount]; int count = 0; foreach (string line in File.ReadLines(path)) { fromFile[count] = stringToInt(line); count++; } IntX key_n = fromFile[0]; IntX[] output = new IntX[fromFile.Length - 2]; int outindex = 0; for (int i = 2; i < fromFile.Length; i++) { output[outindex] = fromFile[i]; outindex++; } IntX[] dec = decrypt(output, key_n, key_e); return(dec); }
public void MinusMinus() { IntX intX = 77; Assert.IsTrue(intX-- == 77); Assert.IsTrue(--intX == 75); }
public void PlusPlus() { IntX intX = 77; Assert.IsTrue(intX++ == 77); Assert.IsTrue(++intX == 79); }
public void Big() { IntX int1 = new IntX(new uint[] { 1, 1 }, false); IntX int2 = new IntX(new uint[] { 1, 1 }, false); IntX intRes = new IntX(new uint[] { 1, 2, 1 }, false); Assert.AreEqual(int1 * int2, intRes); }
/// <summary> /// Multiplies two big integers. /// </summary> /// <param name="int1">First big integer.</param> /// <param name="int2">Second big integer.</param> /// <returns>Resulting big integer.</returns> /// <exception cref="ArgumentNullException"><paramref name="int1" /> or <paramref name="int2" /> is a null reference.</exception> /// <exception cref="ArgumentException"><paramref name="int1" /> or <paramref name="int2" /> is too big for multiply operation.</exception> public virtual IntX Multiply(IntX int1, IntX int2) { // Exceptions if (ReferenceEquals(int1, null)) { throw new ArgumentNullException("int1", Strings.CantBeNull); } else if (ReferenceEquals(int2, null)) { throw new ArgumentNullException("int2", Strings.CantBeNull); } // Special behavior for zero cases if (int1._length == 0 || int2._length == 0) return new IntX(); // Get new big integer length and check it ulong newLength = (ulong)int1._length + int2._length; if (newLength >> 32 != 0) { throw new ArgumentException(Strings.IntegerTooBig); } // Create resulting big int IntX newInt = new IntX((uint)newLength, int1._negative ^ int2._negative); // Perform actual digits multiplication newInt._length = Multiply(int1._digits, int1._length, int2._digits, int2._length, newInt._digits); // Normalization may be needed newInt.TryNormalize(); return newInt; }
public static IntX MulInverse(IntX eq, IntX modulo) { eq = Mod(eq, modulo); Stack <IntX> collect = new Stack <IntX>(); IntX v = modulo; // Copy modulo IntX m; while ((m = v % eq) != 0) { collect.Push(-v / eq /*-(m.l_div)*/); v = eq; eq = m; } if (collect.Count == 0) { return(1); } v = 1; m = collect.Pop(); while (collect.Count > 0) { eq = m; m = v + (m * collect.Pop()); v = eq; } return(Mod(m, modulo)); }
public void EqualValues() { IntX int1 = new IntX(new uint[] { 1, 2, 3 }, true); IntX int2 = new IntX(new uint[] { 1, 2, 3 }, true); Assert.IsFalse(int2 > int1); }
public EllipticDiffieHellman(EllipticCurve curve, CurvePoint generator, IntX order, byte[] priv = null) { this.curve = curve; this.generator = generator; // Generate private key if (priv == null) { byte[] max = order.ToArray(); do { byte[] p1 = new byte[5 /*rand.Next(max.Length) + 1*/]; rand.GetBytes(p1); if (p1.Length == max.Length) { p1[p1.Length - 1] %= max[max.Length - 1]; } else { p1[p1.Length - 1] &= 127; } this.priv = DHHelper.FromArray(p1); } while (this.priv < 2); } else { this.priv = DHHelper.FromArray(priv); } // Generate public key pub = curve.Multiply(generator, this.priv); }
public void Simple() { IntX intX = new IntX(-1); Assert.IsTrue(IntX.Pow(intX, 17) == -1); Assert.IsTrue(IntX.Pow(intX, 18) == 1); }
public void Sub2BigIntX() { IntX int1 = new IntX(new uint[] { 1, 2, 3 }, false); IntX int2 = new IntX(new uint[] { 3, 4, 5 }, false); IntX int3 = new IntX(new uint[] { 2, 2, 2 }, true); Assert.IsTrue(int1 - int2 == int3); }
// // void Simplify() { if (Numberator == 0) { Denominator = 1; return; } IntX a = Numberator; IntX b = Denominator; IntX r = b % a; while (r != 0) { b = a; a = r; r = b % a; } Numberator /= (IntX)a; Denominator /= a; if (Denominator < 0) { Numberator = -Numberator; Denominator = -Denominator; } }
public void Big() { IntX int1 = new IntX(new uint[] {0, 0, 0x80000000U, 0x7fffffffU}, false); IntX int2 = new IntX(new uint[] {1, 0, 0x80000000U}, false); IntX intM = new IntX(new uint[] {2, 0xffffffffU, 0x7fffffffU}, false); Assert.IsTrue(int1 % int2 == intM); }
public void Simple() { IntX int1 = new IntX(8); int1 *= int1; int1.Normalize(); Assert.IsTrue(int1 == 64); }
public void Big3() { IntX int1 = new IntX(new uint[] { uint.MaxValue, uint.MaxValue }, false); IntX int2 = new IntX(new uint[] { uint.MaxValue, uint.MaxValue }, false); IntX intRes = new IntX(new uint[] { 1, 0, uint.MaxValue - 1, uint.MaxValue }, false); Assert.IsTrue(int1 * int2 == intRes); }
public void Neg() { IntX int1 = new IntX(-10); IntX int2 = new IntX(-2); Assert.IsTrue(int1 < int2); }
public void BigFail() { IntX int1 = new IntX(new uint[] { 1, 2 }, false); IntX int2 = new IntX(new uint[] { 1, 2, 3 }, true); Assert.IsFalse(int2 > int1); }
private static IntX Trim(IntX value, int size) { IntX rem = new IntX(1) << size; switch (DesignContext.Instance.FixPoint.OverflowMode) { case EOverflowMode.Wrap: value = IntX.Modulo(value, rem, DivideMode.AutoNewton); if (value < 0) { value += rem; } break; case EOverflowMode.Saturate: if (value > rem) { value = rem; } else if (value < 0) { value = 0; } break; case EOverflowMode.Fail: if (value > rem || value < 0) { throw new ArgumentException(); } break; } return(value); }
public void Big() { IntX int1 = new IntX(new uint[] { 1, 2 }, false); IntX int2 = new IntX(new uint[] { 1, 2, 3 }, true); Assert.IsTrue(int1 > int2); }
public void Zero() { IntX intX = 0; Assert.AreEqual(intX, +intX); Assert.AreEqual(intX, -intX); }
/// <summary> /// Compares <see cref="IntX" /> object to int. /// Returns "-1" if <paramref name="int1" /> < <paramref name="int2" />, "0" if equal and "1" if >. /// </summary> /// <param name="int1">First big integer.</param> /// <param name="int2">Second integer.</param> /// <returns>Comparsion result.</returns> static public int Cmp(IntX int1, int int2) { // Special processing for zero if (int2 == 0) { return(int1._length == 0 ? 0 : (int1._negative ? -1 : 1)); } if (int1._length == 0) { return(int2 > 0 ? -1 : 1); } // Compare presentation if (int1._length > 1) { return(int1._negative ? -1 : 1); } uint digit2; bool negative2; DigitHelper.ToUInt32WithSign(int2, out digit2, out negative2); // Compare sign if (int1._negative && !negative2) { return(-1); } if (!int1._negative && negative2) { return(1); } return(int1._digits[0] == digit2 ? 0 : (int1._digits[0] < digit2 ^ negative2 ? -1 : 1)); }
public void ShouldBeFalseForZero() { IntX value = new IntX(); bool result = value.IsOdd; Assert.That(result, Is.False); }
//Generates p, q, and n. Returns an array of p, q, and n in that order. public static IntX[] genN() { IntX[] output = new IntX[3]; output[0] = genRandPrime(); output[1] = genRandPrime(); output[2] = output[0] * output[1]; return output; }
public void Performance() { IntX intX = new IntX(new uint[] { 0, 1 }, false); IntX intX2 = intX; for (int i = 0; i < 1000; ++i) { intX2 *= intX; } }
private void GenerateE() { if (this.phi != null) { this.e = MathAlgs.GenerateCoprime(this.phi, this.bitsize / 2); this.d = MathAlgs.GenerateInverse(this.e, this.phi); } }
public void Big2() { IntX int1 = new IntX(new uint[] { 1, 1 }, false); IntX int2 = new IntX(new uint[] { 2 }, false); IntX intRes = new IntX(new uint[] { 2, 2 }, false); Assert.AreEqual(intRes, int1 * int2); Assert.AreEqual(intRes, int2 * int1); }
public void ShouldBeFalseForEvenNumber() { IntX value = new IntX(42); bool result = value.IsOdd; Assert.That(result, Is.False); }
public void ShouldOnesComplementZero() { IntX value = new IntX(); IntX result = ~value; Assert.That(result, Is.EqualTo(0)); }
public void ShouldBeTrueForOddNumber() { IntX value = new IntX(57); bool result = value.IsOdd; Assert.That(result, Is.True); }
public void Sub0IntXNeg() { IntX int1 = new IntX(-3); Assert.IsTrue(int1 - 0 == -3); Assert.IsTrue(0 - int1 == 3); Assert.IsTrue(int1 - new IntX() == -3); Assert.IsTrue(new IntX() - int1 == 3); }
public void ShouldOnesComplementBigIntX() { IntX value = new IntX(new uint[] { 3, 5, uint.MaxValue }, false); IntX result = ~value; Assert.That(result, Is.EqualTo(new IntX(new uint[] { ~(uint)3, ~(uint)5 }, true))); }
public void ShouldOnesComplementNegativeIntX() { IntX value = new IntX(-11); IntX result = ~value; Assert.That(result, Is.EqualTo(~(uint)11)); }
public void ShouldBitwiseOrTwoBigIntX() { IntX int1 = new IntX(new uint[] { 3, 5, uint.MaxValue }, false); IntX int2 = new IntX(new uint[] { 1, 8 }, false); IntX result = int1 | int2; Assert.That(result, Is.EqualTo(new IntX(new uint[] { 3, 13, uint.MaxValue }, false))); }
public void ShouldBitwiseOrTwoIntX() { IntX int1 = new IntX(3); IntX int2 = new IntX(5); IntX result = int1 | int2; Assert.That(result, Is.EqualTo(7)); }
/// <summary> /// Returns string representation of <see cref="IntX" /> object in given base. /// </summary> /// <param name="intX">Big integer to convert.</param> /// <param name="numberBase">Base of system in which to do output.</param> /// <param name="alphabet">Alphabet which contains chars used to represent big integer, char position is coresponding digit value.</param> /// <returns>Object string representation.</returns> /// <exception cref="ArgumentException"><paramref name="numberBase" /> is less then 2 or <paramref name="intX" /> is too big to fit in string.</exception> public virtual string ToString(IntX intX, uint numberBase, char[] alphabet) { // Test base if (numberBase < 2 || numberBase > 65536) { throw new ArgumentException(Strings.ToStringSmallBase, "numberBase"); } // Special processing for zero values if (intX._length == 0) return "0"; // Calculate output array length uint outputLength = (uint)System.Math.Ceiling(Constants.DigitBaseLog / System.Math.Log(numberBase) * intX._length); // Define length coefficient for string builder bool isBigBase = numberBase > alphabet.Length; uint lengthCoef = isBigBase ? (uint)System.Math.Ceiling(System.Math.Log10(numberBase)) + 2U : 1U; // Determine maximal possible length of string ulong maxBuilderLength = (ulong)outputLength * lengthCoef + 1UL; if (maxBuilderLength > int.MaxValue) { // This big integer can't be transformed to string throw new ArgumentException(Strings.IntegerTooBig, "intX"); } // Transform digits into another base uint[] outputArray = ToString(intX._digits, intX._length, numberBase, ref outputLength); // Output everything to the string builder StringBuilder outputBuilder = new StringBuilder((int)(outputLength * lengthCoef + 1)); // Maybe append minus sign if (intX._negative) { outputBuilder.Append(Constants.DigitsMinusChar); } // Output all digits for (uint i = outputLength - 1; i < outputLength; --i) { if (!isBigBase) { // Output char-by-char for bases up to covered by alphabet outputBuilder.Append(alphabet[(int)outputArray[i]]); } else { // Output digits in bracets for bigger bases outputBuilder.Append(Constants.DigitOpeningBracet); outputBuilder.Append(outputArray[i].ToString()); outputBuilder.Append(Constants.DigitClosingBracet); } } return outputBuilder.ToString(); }
public void ShouldBitwiseOrTwoNegativeIntX() { IntX int1 = new IntX(-3); IntX int2 = new IntX(-5); IntX result = int1 | int2; Assert.That(result, Is.EqualTo(-7)); }
public void ShouldBitwiseAndTwoNegativeIntX() { IntX int1 = new IntX(-11); IntX int2 = new IntX(-13); IntX result = int1 & int2; Assert.That(result, Is.EqualTo(-9)); }
public void ShouldBitwiseAndTwoIntX() { IntX int1 = new IntX(11); IntX int2 = new IntX(13); IntX result = int1 & int2; Assert.That(result, Is.EqualTo(9)); }
public void ShouldBitwiseAndTwoBigIntX() { IntX int1 = new IntX(new uint[] { 11, 6, uint.MaxValue, uint.MaxValue }, false); IntX int2 = new IntX(new uint[] { 13, 3, 0 }, false); IntX result = int1 & int2; Assert.That(result, Is.EqualTo(new IntX(new uint[] { 9, 2 }, false))); }
public void ShouldBitwiseAndIntXAndZero() { IntX int1 = new IntX(11); IntX int2 = new IntX(); IntX result = int1 & int2; Assert.That(result, Is.EqualTo(0)); }