internal static void Subtract(System.Security.Cryptography.BigInt a, System.Security.Cryptography.BigInt b, ref System.Security.Cryptography.BigInt c) { byte num = 0; int num2 = 0; if (a < b) { Subtract(b, a, ref c); Negate(ref c); } else { int index = 0; int size = a.Size; int num5 = 0; for (index = 0; index < size; index++) { num2 = (a.GetDigit(index) - b.GetDigit(index)) - num; num = 0; if (num2 < 0) { num2 += 0x100; num = 1; } c.SetDigit(index, (byte)(num2 & 0xff), ref num5); } c.Size = num5; } }
private static void Multiply(System.Security.Cryptography.BigInt a, int b, ref System.Security.Cryptography.BigInt c) { if (b == 0) { c.Clear(); } else { int num = 0; int num2 = 0; int size = a.Size; int num4 = 0; for (int i = 0; i < size; i++) { num2 = (b * a.GetDigit(i)) + num; num = num2 / 0x100; c.SetDigit(i, (byte)(num2 % 0x100), ref num4); } if (num != 0) { byte[] bytes = BitConverter.GetBytes(num); for (int j = 0; j < bytes.Length; j++) { c.SetDigit(size + j, bytes[j], ref num4); } } c.Size = num4; } }
internal static void Divide(System.Security.Cryptography.BigInt numerator, System.Security.Cryptography.BigInt denominator, ref System.Security.Cryptography.BigInt quotient, ref System.Security.Cryptography.BigInt remainder) { if (numerator < denominator) { quotient.Clear(); remainder.CopyFrom(numerator); } else if (numerator == denominator) { quotient.Clear(); quotient.SetDigit(0, 1); remainder.Clear(); } else { System.Security.Cryptography.BigInt a = new System.Security.Cryptography.BigInt(); a.CopyFrom(numerator); System.Security.Cryptography.BigInt num2 = new System.Security.Cryptography.BigInt(); num2.CopyFrom(denominator); uint num3 = 0; while (num2.Size < a.Size) { num2.Multiply(0x100); num3++; } if (num2 > a) { num2.Divide(0x100); num3--; } int num4 = 0; int digit = 0; int b = 0; System.Security.Cryptography.BigInt c = new System.Security.Cryptography.BigInt(); quotient.Clear(); for (int i = 0; i <= num3; i++) { num4 = (a.Size == num2.Size) ? a.GetDigit(a.Size - 1) : ((0x100 * a.GetDigit(a.Size - 1)) + a.GetDigit(a.Size - 2)); digit = num2.GetDigit(num2.Size - 1); b = num4 / digit; if (b >= 0x100) { b = 0xff; } Multiply(num2, b, ref c); while (c > a) { b--; Multiply(num2, b, ref c); } quotient.Multiply(0x100); Add(quotient, (byte)b, ref quotient); Subtract(a, c, ref a); num2.Divide(0x100); } remainder.CopyFrom(a); } }
internal static void Negate(ref System.Security.Cryptography.BigInt a) { int size = 0; for (int i = 0; i < 0x80; i++) { a.SetDigit(i, (byte)(~a.GetDigit(i) & 0xff), ref size); } for (int j = 0; j < 0x80; j++) { a.SetDigit(j, (byte)(a.GetDigit(j) + 1), ref size); if ((a.GetDigit(j) & 0xff) != 0) { break; } a.SetDigit(j, (byte)(a.GetDigit(j) & 0xff), ref size); } a.Size = size; }
internal static void Add(System.Security.Cryptography.BigInt a, byte b, ref System.Security.Cryptography.BigInt c) { byte digit = b; int num2 = 0; int size = a.Size; int num4 = 0; for (int i = 0; i < size; i++) { num2 = a.GetDigit(i) + digit; c.SetDigit(i, (byte)(num2 & 0xff), ref num4); digit = (byte)((num2 >> 8) & 0xff); } if (digit != 0) { c.SetDigit(a.Size, digit, ref num4); } c.Size = num4; }