Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Mono.Math.Prime;
- using Mono.Math.Prime.Generator;
- using System;
- using System.Security.Cryptography;
- namespace Mono.Math
- {
- internal class BigInteger
- {
- private const UInt32 DEFAULT_LEN = 20;
- private const String WouldReturnNegVal = "Operation would return a negative value";
- private UInt32 length = 1;
- private UInt32[] data;
- internal readonly static UInt32[] smallPrimes;
- private static RandomNumberGenerator rng;
- private static RandomNumberGenerator Rng
- {
- get
- {
- if (BigInteger.rng == null)
- {
- BigInteger.rng = RandomNumberGenerator.Create();
- }
- return BigInteger.rng;
- }
- }
- static BigInteger()
- {
- BigInteger.smallPrimes = new UInt32[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987 };
- }
- public BigInteger()
- {
- unsafe
- {
- this.data = new UInt32[20];
- this.length = 20;
- }
- }
- public BigInteger(BigInteger.Sign sign, UInt32 len)
- {
- unsafe
- {
- this.data = new UInt32[len];
- this.length = len;
- }
- }
- public BigInteger(BigInteger bi)
- {
- this.data = (UInt32[])bi.data.Clone();
- this.length = bi.length;
- }
- public BigInteger(BigInteger bi, UInt32 len)
- {
- unsafe
- {
- this.data = new UInt32[len];
- for (UInt32 i = 0; i < bi.length; i++)
- {
- this.data[i] = bi.data[i];
- }
- this.length = bi.length;
- }
- }
- public BigInteger(Byte[] inData)
- {
- unsafe
- {
- this.length = (UInt32)((Int32)inData.Length >> 2);
- Int32 length = (Int32)inData.Length & 3;
- if (length != 0)
- {
- BigInteger bigInteger = this;
- bigInteger.length = bigInteger.length + 1;
- }
- this.data = new UInt32[this.length];
- Int32 num = (Int32)inData.Length - 1;
- Int32 num1 = 0;
- while (num >= 3)
- {
- this.data[num1] = (UInt32)(inData[num - 3] << 24 | inData[num - 2] << 16 | inData[num - 1] << 8 | inData[num]);
- num = num - 4;
- num1++;
- }
- switch (length)
- {
- case 1:
- {
- this.data[this.length - 1] = inData[0];
- break;
- }
- case 2:
- {
- this.data[this.length - 1] = (UInt32)(inData[0] << 8 | inData[1]);
- break;
- }
- case 3:
- {
- this.data[this.length - 1] = (UInt32)(inData[0] << 16 | inData[1] << 8 | inData[2]);
- break;
- }
- }
- this.Normalize();
- }
- }
- public BigInteger(UInt32[] inData)
- {
- unsafe
- {
- this.length = (UInt32)inData.Length;
- this.data = new UInt32[this.length];
- Int32 num = (Int32)(this.length - 1);
- Int32 num1 = 0;
- while (num >= 0)
- {
- this.data[num1] = inData[num];
- num--;
- num1++;
- }
- this.Normalize();
- }
- }
- public BigInteger(UInt32 ui)
- {
- this.data = new UInt32[] { ui };
- }
- public BigInteger(UInt64 ul)
- {
- this.data = new UInt32[] { (UInt32)ul, (UInt32)(ul >> 32) };
- this.length = 2;
- this.Normalize();
- }
- public static BigInteger Add(BigInteger bi1, BigInteger bi2)
- {
- return bi1 + bi2;
- }
- public Int32 BitCount()
- {
- unsafe
- {
- this.Normalize();
- UInt32 num = this.data[this.length - 1];
- UInt32 num1 = -2147483648;
- UInt32 num2 = 32;
- while (num2 > 0 && (num & num1) == 0)
- {
- num2--;
- num1 = num1 >> 1;
- }
- num2 = num2 + (this.length - 1 << 5);
- return (Int32)num2;
- }
- }
- public Void Clear()
- {
- for (Int32 i = 0; (Int64)i < (UInt64)this.length; i++)
- {
- this.data[i] = 0;
- }
- }
- public Void ClearBit(UInt32 bitNum)
- {
- this.SetBit(bitNum, false);
- }
- public BigInteger.Sign Compare(BigInteger bi)
- {
- return BigInteger.Kernel.Compare(this, bi);
- }
- public static BigInteger Divid(BigInteger bi, Int32 i)
- {
- return bi / i;
- }
- public static BigInteger Divid(BigInteger bi1, BigInteger bi2)
- {
- return bi1 / bi2;
- }
- public override Boolean Equals(Object o)
- {
- if (o == null)
- {
- return false;
- }
- if (o is Int32)
- {
- return ((Int32)o < 0 ? false : this == (UInt32)o);
- }
- BigInteger bigInteger = o as BigInteger;
- if (bigInteger == null)
- {
- return false;
- }
- return BigInteger.Kernel.Compare(this, bigInteger) == BigInteger.Sign.Zero;
- }
- public BigInteger GCD(BigInteger bi)
- {
- return BigInteger.Kernel.gcd(this, bi);
- }
- public static BigInteger GeneratePseudoPrime(Int32 bits)
- {
- return (new SequentialSearchPrimeGeneratorBase()).GenerateNewPrime(bits);
- }
- public static BigInteger GenerateRandom(Int32 bits, RandomNumberGenerator rng)
- {
- Int32 num = bits >> 5;
- Int32 num1 = bits & 31;
- if (num1 != 0)
- {
- num++;
- }
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, (UInt32)(num + 1));
- Byte[] numArray = new Byte[num << 2];
- rng.GetBytes(numArray);
- Buffer.BlockCopy(numArray, 0, bigInteger.data, 0, num << 2);
- if (num1 == 0)
- {
- bigInteger.data[num - 1] = bigInteger.data[num - 1] | -2147483648;
- }
- else
- {
- UInt32 num2 = (UInt32)(1 << (num1 - 1 & 31));
- bigInteger.data[num - 1] = bigInteger.data[num - 1] | num2;
- num2 = (UInt32)(-1 >> (32 - num1 & 31));
- bigInteger.data[num - 1] = bigInteger.data[num - 1] & num2;
- }
- bigInteger.Normalize();
- return bigInteger;
- }
- public static BigInteger GenerateRandom(Int32 bits)
- {
- return BigInteger.GenerateRandom(bits, BigInteger.Rng);
- }
- public Byte[] GetBytes()
- {
- if (this == 0)
- {
- return new Byte[1];
- }
- Int32 num = this.BitCount();
- Int32 num1 = num >> 3;
- if ((num & 7) != 0)
- {
- num1++;
- }
- Byte[] numArray = new Byte[num1];
- Int32 num2 = num1 & 3;
- if (num2 == 0)
- {
- num2 = 4;
- }
- Int32 num3 = 0;
- for (Int32 i = (Int32)(this.length - 1); i >= 0; i--)
- {
- UInt32 num4 = this.data[i];
- for (Int32 j = num2 - 1; j >= 0; j--)
- {
- numArray[num3 + j] = (Byte)(num4 & 255);
- num4 = num4 >> 8;
- }
- num3 = num3 + num2;
- num2 = 4;
- }
- return numArray;
- }
- public override Int32 GetHashCode()
- {
- unsafe
- {
- UInt32 num = 0;
- for (UInt32 i = 0; i < this.length; i++)
- {
- num = num ^ this.data[i];
- }
- return (Int32)num;
- }
- }
- public Void Incr2()
- {
- Int32 num = 0;
- this.data[0] = this.data[0] + 2;
- if (this.data[0] < 2)
- {
- Int32 num1 = num + 1;
- num = num1;
- this.data[num1] = this.data[num1] + 1;
- while (true)
- {
- Int32 num2 = num;
- num = num2 + 1;
- if (this.data[num2] != 0)
- {
- break;
- }
- this.data[num] = this.data[num] + 1;
- }
- if (this.length == num)
- {
- BigInteger bigInteger = this;
- bigInteger.length = bigInteger.length + 1;
- }
- }
- }
- public Boolean IsProbablePrime()
- {
- if (this <= BigInteger.smallPrimes[(Int32)BigInteger.smallPrimes.Length - 1])
- {
- for (Int32 i = 0; i < (Int32)BigInteger.smallPrimes.Length; i++)
- {
- if (this == BigInteger.smallPrimes[i])
- {
- return true;
- }
- }
- return false;
- }
- for (Int32 j = 0; j < (Int32)BigInteger.smallPrimes.Length; j++)
- {
- if ((this % BigInteger.smallPrimes[j]) == 0)
- {
- return false;
- }
- }
- return PrimalityTests.Test(this, ConfidenceFactor.Medium);
- }
- public Int32 LowestSetBit()
- {
- if (this == 0)
- {
- return -1;
- }
- Int32 num = 0;
- while (!this.TestBit(num))
- {
- num++;
- }
- return num;
- }
- public BigInteger ModInverse(BigInteger modulus)
- {
- return BigInteger.Kernel.modInverse(this, modulus);
- }
- public BigInteger ModPow(BigInteger exp, BigInteger n)
- {
- return (new BigInteger.ModulusRing(n)).Pow(this, exp);
- }
- public static Int32 Modulus(BigInteger bi, Int32 i)
- {
- return bi % i;
- }
- public static UInt32 Modulus(BigInteger bi, UInt32 ui)
- {
- return bi % ui;
- }
- public static BigInteger Modulus(BigInteger bi1, BigInteger bi2)
- {
- return bi1 % bi2;
- }
- public static BigInteger Multiply(BigInteger bi1, BigInteger bi2)
- {
- return bi1 * bi2;
- }
- public static BigInteger Multiply(BigInteger bi, Int32 i)
- {
- return bi * i;
- }
- public static BigInteger NextHighestPrime(BigInteger bi)
- {
- return (new NextPrimeFinder()).GenerateNewPrime(0, bi);
- }
- private Void Normalize()
- {
- unsafe
- {
- while (this.length > 0 && this.data[this.length - 1] == 0)
- {
- BigInteger bigInteger = this;
- bigInteger.length = bigInteger.length - 1;
- }
- if (this.length == 0)
- {
- BigInteger bigInteger1 = this;
- bigInteger1.length = bigInteger1.length + 1;
- }
- }
- }
- public static BigInteger operator +(BigInteger bi1, BigInteger bi2)
- {
- if (bi1 == 0)
- {
- return new BigInteger(bi2);
- }
- if (bi2 == 0)
- {
- return new BigInteger(bi1);
- }
- return BigInteger.Kernel.AddSameSign(bi1, bi2);
- }
- public static BigInteger operator /(BigInteger bi, Int32 i)
- {
- if (i <= 0)
- {
- throw new ArithmeticException("Operation would return a negative value");
- }
- return BigInteger.Kernel.DwordDiv(bi, (UInt32)i);
- }
- public static BigInteger operator /(BigInteger bi1, BigInteger bi2)
- {
- return BigInteger.Kernel.multiByteDivide(bi1, bi2)[0];
- }
- public static Boolean operator ==(BigInteger bi1, UInt32 ui)
- {
- if (bi1.length != 1)
- {
- bi1.Normalize();
- }
- return (bi1.length != 1 ? false : bi1.data[0] == ui);
- }
- public static Boolean operator ==(BigInteger bi1, BigInteger bi2)
- {
- if (bi1 == bi2)
- {
- return true;
- }
- if (null == bi1 || null == bi2)
- {
- return false;
- }
- return BigInteger.Kernel.Compare(bi1, bi2) == BigInteger.Sign.Zero;
- }
- public static Boolean operator >(BigInteger bi1, BigInteger bi2)
- {
- return BigInteger.Kernel.Compare(bi1, bi2) > BigInteger.Sign.Zero;
- }
- public static Boolean operator >=(BigInteger bi1, BigInteger bi2)
- {
- return BigInteger.Kernel.Compare(bi1, bi2) >= BigInteger.Sign.Zero;
- }
- public static implicit operator BigInteger(UInt32 value)
- {
- return new BigInteger(value);
- }
- public static implicit operator BigInteger(Int32 value)
- {
- if (value < 0)
- {
- throw new ArgumentOutOfRangeException("value");
- }
- return new BigInteger((UInt32)value);
- }
- public static implicit operator BigInteger(UInt64 value)
- {
- return new BigInteger(value);
- }
- public static Boolean operator !=(BigInteger bi1, UInt32 ui)
- {
- if (bi1.length != 1)
- {
- bi1.Normalize();
- }
- return (bi1.length != 1 ? 0 : (Int32)(bi1.data[0] == ui)) == 0;
- }
- public static Boolean operator !=(BigInteger bi1, BigInteger bi2)
- {
- if (bi1 == bi2)
- {
- return false;
- }
- if (null == bi1 || null == bi2)
- {
- return true;
- }
- return BigInteger.Kernel.Compare(bi1, bi2) != BigInteger.Sign.Zero;
- }
- public static BigInteger operator <<(BigInteger bi1, Int32 shiftVal)
- {
- return BigInteger.Kernel.LeftShift(bi1, shiftVal);
- }
- public static Boolean operator <(BigInteger bi1, BigInteger bi2)
- {
- return BigInteger.Kernel.Compare(bi1, bi2) < BigInteger.Sign.Zero;
- }
- public static Boolean operator <=(BigInteger bi1, BigInteger bi2)
- {
- return BigInteger.Kernel.Compare(bi1, bi2) <= BigInteger.Sign.Zero;
- }
- public static Int32 operator %(BigInteger bi, Int32 i)
- {
- if (i > 0)
- {
- return (Int32)BigInteger.Kernel.DwordMod(bi, (UInt32)i);
- }
- return (Int32)(-BigInteger.Kernel.DwordMod(bi, (UInt32)(-i)));
- }
- public static UInt32 operator %(BigInteger bi, UInt32 ui)
- {
- return BigInteger.Kernel.DwordMod(bi, ui);
- }
- public static BigInteger operator %(BigInteger bi1, BigInteger bi2)
- {
- return BigInteger.Kernel.multiByteDivide(bi1, bi2)[1];
- }
- public static BigInteger operator *(BigInteger bi1, BigInteger bi2)
- {
- if (bi1 == 0 || bi2 == 0)
- {
- return 0;
- }
- if ((Int64)((Int32)bi1.data.Length) < (UInt64)bi1.length)
- {
- throw new IndexOutOfRangeException("bi1 out of range");
- }
- if ((Int64)((Int32)bi2.data.Length) < (UInt64)bi2.length)
- {
- throw new IndexOutOfRangeException("bi2 out of range");
- }
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, bi1.length + bi2.length);
- BigInteger.Kernel.Multiply(bi1.data, 0, bi1.length, bi2.data, 0, bi2.length, bigInteger.data, 0);
- bigInteger.Normalize();
- return bigInteger;
- }
- public static BigInteger operator *(BigInteger bi, Int32 i)
- {
- if (i < 0)
- {
- throw new ArithmeticException("Operation would return a negative value");
- }
- if (i == 0)
- {
- return 0;
- }
- if (i == 1)
- {
- return new BigInteger(bi);
- }
- return BigInteger.Kernel.MultiplyByDword(bi, (UInt32)i);
- }
- public static BigInteger operator >>(BigInteger bi1, Int32 shiftVal)
- {
- return BigInteger.Kernel.RightShift(bi1, shiftVal);
- }
- public static BigInteger operator -(BigInteger bi1, BigInteger bi2)
- {
- if (bi2 == 0)
- {
- return new BigInteger(bi1);
- }
- if (bi1 == 0)
- {
- throw new ArithmeticException("Operation would return a negative value");
- }
- switch (BigInteger.Kernel.Compare(bi1, bi2))
- {
- case BigInteger.Sign.Negative:
- {
- throw new ArithmeticException("Operation would return a negative value");
- }
- case BigInteger.Sign.Zero:
- {
- return 0;
- }
- case BigInteger.Sign.Positive:
- {
- return BigInteger.Kernel.Subtract(bi1, bi2);
- }
- }
- throw new Exception();
- }
- public static BigInteger Parse(String number)
- {
- if (number == null)
- {
- throw new ArgumentNullException("number");
- }
- Int32 i = 0;
- Int32 length = number.Length;
- Boolean flag = false;
- BigInteger bigInteger = new BigInteger(0);
- if (number[i] == '+')
- {
- i++;
- }
- else if (number[i] == '-')
- {
- throw new FormatException("Operation would return a negative value");
- }
- while (i < length)
- {
- Char chr = number[i];
- if (chr == 0)
- {
- i = length;
- }
- else if (chr < '0' || chr > '9')
- {
- if (!Char.IsWhiteSpace(chr))
- {
- throw new FormatException();
- }
- for (i++; i < length; i++)
- {
- if (!Char.IsWhiteSpace(number[i]))
- {
- throw new FormatException();
- }
- }
- break;
- }
- else
- {
- bigInteger = (bigInteger * 10) + (chr - 48);
- flag = true;
- }
- i++;
- }
- if (!flag)
- {
- throw new FormatException();
- }
- return bigInteger;
- }
- public Void Randomize(RandomNumberGenerator rng)
- {
- if (this == 0)
- {
- return;
- }
- Int32 num = this.BitCount();
- Int32 num1 = num >> 5;
- Int32 num2 = num & 31;
- if (num2 != 0)
- {
- num1++;
- }
- Byte[] numArray = new Byte[num1 << 2];
- rng.GetBytes(numArray);
- Buffer.BlockCopy(numArray, 0, this.data, 0, num1 << 2);
- if (num2 == 0)
- {
- this.data[num1 - 1] = this.data[num1 - 1] | -2147483648;
- }
- else
- {
- UInt32 num3 = (UInt32)(1 << (num2 - 1 & 31));
- this.data[num1 - 1] = this.data[num1 - 1] | num3;
- num3 = (UInt32)(-1 >> (32 - num2 & 31));
- this.data[num1 - 1] = this.data[num1 - 1] & num3;
- }
- this.Normalize();
- }
- public Void Randomize()
- {
- this.Randomize(BigInteger.Rng);
- }
- public Void SetBit(UInt32 bitNum)
- {
- this.SetBit(bitNum, true);
- }
- public Void SetBit(UInt32 bitNum, Boolean value)
- {
- unsafe
- {
- UInt32 num = bitNum >> 5;
- if (num < this.length)
- {
- UInt32 num1 = 1 << (bitNum & 31 & 31);
- if (!value)
- {
- this.data[num] = this.data[num] & ~num1;
- }
- else
- {
- this.data[num] = this.data[num] | num1;
- }
- }
- }
- }
- public static BigInteger Subtract(BigInteger bi1, BigInteger bi2)
- {
- return bi1 - bi2;
- }
- public Boolean TestBit(UInt32 bitNum)
- {
- unsafe
- {
- UInt32 num = bitNum >> 5;
- UInt32 num1 = (UInt32)(1 << ((Byte)(bitNum & 31) & 31));
- return (this.data[num] & num1) != 0;
- }
- }
- public Boolean TestBit(Int32 bitNum)
- {
- unsafe
- {
- if (bitNum < 0)
- {
- throw new IndexOutOfRangeException("bitNum out of range");
- }
- UInt32 num = (UInt32)(bitNum >> 5);
- UInt32 num1 = (UInt32)(1 << ((Byte)(bitNum & 31) & 31));
- return (this.data[num] | num1) == this.data[num];
- }
- }
- public String ToString(UInt32 radix)
- {
- return this.ToString(radix, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- }
- public String ToString(UInt32 radix, String characterSet)
- {
- if ((Int64)characterSet.Length < (UInt64)radix)
- {
- throw new ArgumentException("charSet length less than radix", "characterSet");
- }
- if (radix == 1)
- {
- throw new ArgumentException("There is no such thing as radix one notation", "radix");
- }
- if (this == 0)
- {
- return "0";
- }
- if (this == 1)
- {
- return "1";
- }
- String empty = String.Empty;
- BigInteger bigInteger = new BigInteger(this);
- while (bigInteger != 0)
- {
- UInt32 num = BigInteger.Kernel.SingleByteDivideInPlace(bigInteger, radix);
- empty = String.Concat(characterSet[num], empty);
- }
- return empty;
- }
- public override String ToString()
- {
- return this.ToString(10);
- }
- private sealed class Kernel
- {
- public Kernel()
- {
- }
- public static BigInteger AddSameSign(BigInteger bi1, BigInteger bi2)
- {
- unsafe
- {
- UInt32[] numArray;
- UInt32[] numArray1;
- UInt32 num;
- UInt32 num1;
- UInt32 num2;
- UInt32 num3;
- UInt32 num4;
- UInt32 num5 = 0;
- if (bi1.length >= bi2.length)
- {
- numArray = bi1.data;
- num1 = bi1.length;
- numArray1 = bi2.data;
- num = bi2.length;
- }
- else
- {
- numArray = bi2.data;
- num1 = bi2.length;
- numArray1 = bi1.data;
- num = bi1.length;
- }
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, num1 + 1);
- UInt32[] numArray2 = bigInteger.data;
- UInt64 num6 = (UInt64)0;
- do
- {
- num6 = (UInt64)numArray[num5] + (UInt64)numArray1[num5] + num6;
- numArray2[num5] = (UInt32)num6;
- num6 = num6 >> 32;
- num2 = num5 + 1;
- num5 = num2;
- }
- while (num2 < num);
- Boolean flag = num6 != (Int64)0;
- if (flag)
- {
- if (num5 < num1)
- {
- do
- {
- UInt32 num7 = numArray[num5] + 1;
- UInt32 num8 = num7;
- numArray2[num5] = num7;
- flag = num8 == 0;
- num4 = num5 + 1;
- num5 = num4;
- }
- while (num4 < num1 && flag);
- }
- if (flag)
- {
- numArray2[num5] = 1;
- UInt32 num9 = num5 + 1;
- num5 = num9;
- bigInteger.length = num9;
- return bigInteger;
- }
- }
- if (num5 < num1)
- {
- do
- {
- numArray2[num5] = numArray[num5];
- num3 = num5 + 1;
- num5 = num3;
- }
- while (num3 < num1);
- }
- bigInteger.Normalize();
- return bigInteger;
- }
- }
- public static BigInteger.Sign Compare(BigInteger bi1, BigInteger bi2)
- {
- unsafe
- {
- UInt32 num = bi1.length;
- UInt32 num1 = bi2.length;
- while (num > 0 && bi1.data[num - 1] == 0)
- {
- num--;
- }
- while (num1 > 0 && bi2.data[num1 - 1] == 0)
- {
- num1--;
- }
- if (num == 0 && num1 == 0)
- {
- return BigInteger.Sign.Zero;
- }
- if (num < num1)
- {
- return BigInteger.Sign.Negative;
- }
- if (num > num1)
- {
- return BigInteger.Sign.Positive;
- }
- UInt32 num2 = num - 1;
- while (num2 != 0 && bi1.data[num2] == bi2.data[num2])
- {
- num2--;
- }
- if (bi1.data[num2] < bi2.data[num2])
- {
- return BigInteger.Sign.Negative;
- }
- if (bi1.data[num2] > bi2.data[num2])
- {
- return BigInteger.Sign.Positive;
- }
- return BigInteger.Sign.Zero;
- }
- }
- public static BigInteger DwordDiv(BigInteger n, UInt32 d)
- {
- unsafe
- {
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, n.length);
- UInt64 num = (UInt64)0;
- UInt32 num1 = n.length;
- while (true)
- {
- UInt32 num2 = num1;
- num1 = num2 - 1;
- if (num2 <= 0)
- {
- break;
- }
- num = num << 32;
- num = num | (UInt64)n.data[num1];
- bigInteger.data[num1] = (UInt32)(num / (UInt64)d);
- num = num % (UInt64)d;
- }
- bigInteger.Normalize();
- return bigInteger;
- }
- }
- public static BigInteger[] DwordDivMod(BigInteger n, UInt32 d)
- {
- unsafe
- {
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, n.length);
- UInt64 num = (UInt64)0;
- UInt32 num1 = n.length;
- while (true)
- {
- UInt32 num2 = num1;
- num1 = num2 - 1;
- if (num2 <= 0)
- {
- break;
- }
- num = num << 32;
- num = num | (UInt64)n.data[num1];
- bigInteger.data[num1] = (UInt32)(num / (UInt64)d);
- num = num % (UInt64)d;
- }
- bigInteger.Normalize();
- return new BigInteger[] { bigInteger, (UInt32)num };
- }
- }
- public static UInt32 DwordMod(BigInteger n, UInt32 d)
- {
- unsafe
- {
- UInt64 num = (UInt64)0;
- UInt32 num1 = n.length;
- while (true)
- {
- UInt32 num2 = num1;
- num1 = num2 - 1;
- if (num2 <= 0)
- {
- break;
- }
- num = num << 32;
- num = num | (UInt64)n.data[num1];
- num = num % (UInt64)d;
- }
- return (UInt32)num;
- }
- }
- public static BigInteger gcd(BigInteger a, BigInteger b)
- {
- BigInteger bigInteger = a;
- BigInteger bigInteger1 = b;
- BigInteger bigInteger2 = bigInteger1;
- while (bigInteger.length > 1)
- {
- bigInteger2 = bigInteger;
- bigInteger = bigInteger1 % bigInteger;
- bigInteger1 = bigInteger2;
- }
- if (bigInteger == 0)
- {
- return bigInteger2;
- }
- UInt32 num = bigInteger.data[0];
- UInt32 num1 = bigInteger1 % num;
- Int32 num2 = 0;
- while (((num1 | num) & 1) == 0)
- {
- num1 = num1 >> 1;
- num = num >> 1;
- num2++;
- }
- while (num1 != 0)
- {
- while ((num1 & 1) == 0)
- {
- num1 = num1 >> 1;
- }
- while ((num & 1) == 0)
- {
- num = num >> 1;
- }
- if (num1 < num)
- {
- num = num - num1 >> 1;
- }
- else
- {
- num1 = num1 - num >> 1;
- }
- }
- return num << (num2 & 31);
- }
- public static BigInteger LeftShift(BigInteger bi, Int32 n)
- {
- unsafe
- {
- if (n == 0)
- {
- return new BigInteger(bi, bi.length + 1);
- }
- Int32 num = n >> 5;
- n = n & 31;
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, bi.length + 1 + num);
- UInt32 num1 = 0;
- UInt32 num2 = bi.length;
- if (n == 0)
- {
- while (num1 < num2)
- {
- bigInteger.data[checked((IntPtr)((UInt64)num1 + (Int64)num))] = bi.data[num1];
- num1++;
- }
- }
- else
- {
- UInt32 num3 = 0;
- while (num1 < num2)
- {
- UInt32 num4 = bi.data[num1];
- bigInteger.data[checked((IntPtr)((UInt64)num1 + (Int64)num))] = num4 << (n & 31) | num3;
- num3 = num4 >> (32 - n & 31);
- num1++;
- }
- bigInteger.data[checked((IntPtr)((UInt64)num1 + (Int64)num))] = num3;
- }
- bigInteger.Normalize();
- return bigInteger;
- }
- }
- public static Void MinusEq(BigInteger big, BigInteger small)
- {
- unsafe
- {
- UInt32 num;
- UInt32 num1;
- UInt32[] numArray = big.data;
- UInt32[] numArray1 = small.data;
- UInt32 num2 = 0;
- UInt32 num3 = 0;
- do
- {
- UInt32 num4 = numArray1[num2];
- UInt32 num5 = num4 + num3;
- num4 = num5;
- UInt32 num6 = numArray[num2] - num4;
- UInt32 num7 = num6;
- numArray[num2] = num6;
- num3 = (UInt32)((!(num5 < num3 | num7 > ~num4) ? 0 : 1));
- num = num2 + 1;
- num2 = num;
- }
- while (num < small.length);
- if (num2 != big.length)
- {
- if (num3 == 1)
- {
- do
- {
- numArray[num2] = numArray[num2] - 1;
- num1 = num2;
- num2 = num1 + 1;
- }
- while (numArray[num1] == 0 && num2 < big.length);
- }
- }
- while (big.length > 0 && big.data[big.length - 1] == 0)
- {
- BigInteger bigInteger = big;
- bigInteger.length = bigInteger.length - 1;
- }
- if (big.length == 0)
- {
- BigInteger bigInteger1 = big;
- bigInteger1.length = bigInteger1.length + 1;
- }
- }
- }
- public static UInt32 modInverse(BigInteger bi, UInt32 modulus)
- {
- UInt32 num = modulus;
- UInt32 num1 = bi % modulus;
- UInt32 num2 = 0;
- UInt32 num3 = 1;
- while (num1 != 0)
- {
- if (num1 == 1)
- {
- return num3;
- }
- num2 = num2 + num / num1 * num3;
- num = num % num1;
- if (num != 0)
- {
- if (num == 1)
- {
- return modulus - num2;
- }
- num3 = num3 + num1 / num * num2;
- num1 = num1 % num;
- }
- else
- {
- break;
- }
- }
- return (UInt32)0;
- }
- public static BigInteger modInverse(BigInteger bi, BigInteger modulus)
- {
- if (modulus.length == 1)
- {
- return BigInteger.Kernel.modInverse(bi, modulus.data[0]);
- }
- BigInteger[] bigIntegerArray = new BigInteger[] { 0, 1 };
- BigInteger[] bigIntegerArray1 = new BigInteger[2];
- BigInteger[] bigIntegerArray2 = new BigInteger[] { 0, 0 };
- Int32 num = 0;
- BigInteger bigInteger = modulus;
- BigInteger bigInteger1 = bi;
- BigInteger.ModulusRing modulusRing = new BigInteger.ModulusRing(modulus);
- while (bigInteger1 != 0)
- {
- if (num > 1)
- {
- BigInteger bigInteger2 = modulusRing.Difference(bigIntegerArray[0], bigIntegerArray[1] * bigIntegerArray1[0]);
- bigIntegerArray[0] = bigIntegerArray[1];
- bigIntegerArray[1] = bigInteger2;
- }
- BigInteger[] bigIntegerArray3 = BigInteger.Kernel.multiByteDivide(bigInteger, bigInteger1);
- bigIntegerArray1[0] = bigIntegerArray1[1];
- bigIntegerArray1[1] = bigIntegerArray3[0];
- bigIntegerArray2[0] = bigIntegerArray2[1];
- bigIntegerArray2[1] = bigIntegerArray3[1];
- bigInteger = bigInteger1;
- bigInteger1 = bigIntegerArray3[1];
- num++;
- }
- if (bigIntegerArray2[0] != 1)
- {
- throw new ArithmeticException("No inverse!");
- }
- return modulusRing.Difference(bigIntegerArray[0], bigIntegerArray[1] * bigIntegerArray1[0]);
- }
- public static BigInteger[] multiByteDivide(BigInteger bi1, BigInteger bi2)
- {
- unsafe
- {
- if (BigInteger.Kernel.Compare(bi1, bi2) == BigInteger.Sign.Negative)
- {
- return new BigInteger[] { 0, new BigInteger(bi1) };
- }
- bi1.Normalize();
- bi2.Normalize();
- if (bi2.length == 1)
- {
- return BigInteger.Kernel.DwordDivMod(bi1, bi2.data[0]);
- }
- UInt32 num = bi1.length + 1;
- Int32 num1 = (Int32)(bi2.length + 1);
- UInt32 num2 = -2147483648;
- UInt32 num3 = bi2.data[bi2.length - 1];
- Int32 num4 = 0;
- Int32 num5 = (Int32)(bi1.length - bi2.length);
- while (num2 != 0 && (num3 & num2) == 0)
- {
- num4++;
- num2 = num2 >> 1;
- }
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, bi1.length - bi2.length + 1);
- BigInteger bigInteger1 = bi1 << num4;
- UInt32[] numArray = bigInteger1.data;
- bi2 = bi2 << num4;
- Int32 num6 = (Int32)(num - bi2.length);
- Int32 num7 = (Int32)(num - 1);
- UInt32 num8 = bi2.data[bi2.length - 1];
- UInt64 num9 = (UInt64)bi2.data[bi2.length - 2];
- while (num6 > 0)
- {
- UInt64 num10 = ((UInt64)numArray[num7] << 32) + (UInt64)numArray[num7 - 1];
- UInt64 num11 = num10 / (UInt64)num8;
- UInt64 num12 = num10 % (UInt64)num8;
- while (num11 == 4294967296L || num11 * num9 > (num12 << 32) + (UInt64)numArray[num7 - 2])
- {
- num11 = num11 - (Int64)1;
- num12 = num12 + (UInt64)num8;
- if (num12 >= 4294967296L)
- {
- break;
- }
- }
- UInt32 num13 = 0;
- Int32 num14 = num7 - num1 + 1;
- UInt64 num15 = (UInt64)0;
- UInt32 num16 = (UInt32)num11;
- do
- {
- num15 = num15 + (UInt64)bi2.data[num13] * (UInt64)num16;
- UInt32 num17 = numArray[num14];
- numArray[num14] = numArray[num14] - (UInt32)num15;
- num15 = num15 >> 32;
- if (numArray[num14] > num17)
- {
- num15 = num15 + (Int64)1;
- }
- num13++;
- num14++;
- }
- while ((UInt64)num13 < (Int64)num1);
- num14 = num7 - num1 + 1;
- num13 = 0;
- if (num15 != 0)
- {
- num16--;
- UInt64 num18 = (UInt64)0;
- do
- {
- num18 = (UInt64)numArray[num14] + (UInt64)bi2.data[num13] + num18;
- numArray[num14] = (UInt32)num18;
- num18 = num18 >> 32;
- num13++;
- num14++;
- }
- while ((UInt64)num13 < (Int64)num1);
- }
- Int32 num19 = num5;
- num5 = num19 - 1;
- bigInteger.data[num19] = num16;
- num7--;
- num6--;
- }
- bigInteger.Normalize();
- bigInteger1.Normalize();
- BigInteger[] bigIntegerArray = new BigInteger[] { bigInteger, bigInteger1 };
- if (num4 != 0)
- {
- bigIntegerArray[1] = bigIntegerArray[1] >> num4;
- }
- return bigIntegerArray;
- }
- }
- public static unsafe Void Multiply(UInt32[] x, UInt32 xOffset, UInt32 xLen, UInt32[] y, UInt32 yOffset, UInt32 yLen, UInt32[] d, UInt32 dOffset)
- {
- UInt32* numPointer;
- UInt32* numPointer1;
- UInt32* numPointer2;
- if (x == null || (Int32)x.Length == 0)
- {
- numPointer = null;
- }
- else
- {
- numPointer = &x[0];
- }
- fixed (UInt32* numPointer3 = numPointer)
- {
- if (y == null || (Int32)y.Length == 0)
- {
- numPointer1 = null;
- }
- else
- {
- numPointer1 = &y[0];
- }
- fixed (UInt32* numPointer4 = numPointer1)
- {
- if (d == null || (Int32)d.Length == 0)
- {
- numPointer2 = null;
- }
- else
- {
- numPointer2 = &d[0];
- }
- fixed (UInt32* numPointer5 = numPointer2)
- {
- UInt32* numPointer6 = numPointer3 + xOffset * 4;
- UInt32* numPointer7 = numPointer6 + xLen * 4;
- UInt32* numPointer8 = numPointer4 + yOffset * 4;
- UInt32* numPointer9 = numPointer8 + yLen * 4;
- UInt32* numPointer10 = numPointer5 + dOffset * 4;
- while (numPointer6 < numPointer7)
- {
- if (*numPointer6 != 0)
- {
- UInt64 num = (UInt64)0;
- UInt32* numPointer11 = numPointer10;
- UInt32* numPointer12 = numPointer8;
- while (numPointer12 < numPointer9)
- {
- num = num + (UInt64)(*numPointer6) * (UInt64)(*numPointer12) + (UInt64)(*numPointer11);
- *numPointer11 = (UInt32)num;
- num = num >> 32;
- numPointer12 = numPointer12 + 4;
- numPointer11 = numPointer11 + 4;
- }
- if (num != 0)
- {
- *numPointer11 = (UInt32)num;
- }
- }
- numPointer6 = numPointer6 + 4;
- numPointer10 = numPointer10 + 4;
- }
- }
- }
- }
- numPointer4 = null;
- numPointer5 = null;
- }
- public static BigInteger MultiplyByDword(BigInteger n, UInt32 f)
- {
- unsafe
- {
- UInt32 num;
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, n.length + 1);
- UInt32 num1 = 0;
- UInt64 num2 = (UInt64)0;
- do
- {
- num2 = num2 + (UInt64)n.data[num1] * (UInt64)f;
- bigInteger.data[num1] = (UInt32)num2;
- num2 = num2 >> 32;
- num = num1 + 1;
- num1 = num;
- }
- while (num < n.length);
- bigInteger.data[num1] = (UInt32)num2;
- bigInteger.Normalize();
- return bigInteger;
- }
- }
- public static unsafe Void MultiplyMod2p32pmod(UInt32[] x, Int32 xOffset, Int32 xLen, UInt32[] y, Int32 yOffest, Int32 yLen, UInt32[] d, Int32 dOffset, Int32 mod)
- {
- UInt32* numPointer;
- UInt32* numPointer1;
- UInt32* numPointer2;
- if (x == null || (Int32)x.Length == 0)
- {
- numPointer = null;
- }
- else
- {
- numPointer = &x[0];
- }
- fixed (UInt32* numPointer3 = numPointer)
- {
- if (y == null || (Int32)y.Length == 0)
- {
- numPointer1 = null;
- }
- else
- {
- numPointer1 = &y[0];
- }
- fixed (UInt32* numPointer4 = numPointer1)
- {
- if (d == null || (Int32)d.Length == 0)
- {
- numPointer2 = null;
- }
- else
- {
- numPointer2 = &d[0];
- }
- fixed (UInt32* numPointer5 = numPointer2)
- {
- UInt32* numPointer6 = numPointer3 + xOffset * 4;
- UInt32* numPointer7 = numPointer6 + xLen * 4;
- UInt32* numPointer8 = numPointer4 + yOffest * 4;
- UInt32* numPointer9 = numPointer8 + yLen * 4;
- UInt32* numPointer10 = numPointer5 + dOffset * 4;
- UInt32* numPointer11 = numPointer10 + mod * 4;
- while (numPointer6 < numPointer7)
- {
- if (*numPointer6 != 0)
- {
- UInt64 num = (UInt64)0;
- UInt32* numPointer12 = numPointer10;
- UInt32* numPointer13 = numPointer8;
- while (numPointer13 < numPointer9 && numPointer12 < numPointer11)
- {
- num = num + (UInt64)(*numPointer6) * (UInt64)(*numPointer13) + (UInt64)(*numPointer12);
- *numPointer12 = (UInt32)num;
- num = num >> 32;
- numPointer13 = numPointer13 + 4;
- numPointer12 = numPointer12 + 4;
- }
- if (num != 0 && numPointer12 < numPointer11)
- {
- *numPointer12 = (UInt32)num;
- }
- }
- numPointer6 = numPointer6 + 4;
- numPointer10 = numPointer10 + 4;
- }
- }
- }
- }
- numPointer4 = null;
- numPointer5 = null;
- }
- public static Void PlusEq(BigInteger bi1, BigInteger bi2)
- {
- unsafe
- {
- UInt32[] numArray;
- UInt32[] numArray1;
- UInt32 num;
- UInt32 num1;
- UInt32 num2;
- UInt32 num3;
- UInt32 num4;
- UInt32 num5 = 0;
- Boolean flag = false;
- if (bi1.length >= bi2.length)
- {
- numArray = bi1.data;
- num1 = bi1.length;
- numArray1 = bi2.data;
- num = bi2.length;
- }
- else
- {
- flag = true;
- numArray = bi2.data;
- num1 = bi2.length;
- numArray1 = bi1.data;
- num = bi1.length;
- }
- UInt32[] numArray2 = bi1.data;
- UInt64 num6 = (UInt64)0;
- do
- {
- num6 = num6 + (UInt64)numArray[num5] + (UInt64)numArray1[num5];
- numArray2[num5] = (UInt32)num6;
- num6 = num6 >> 32;
- num2 = num5 + 1;
- num5 = num2;
- }
- while (num2 < num);
- Boolean flag1 = num6 != (Int64)0;
- if (flag1)
- {
- if (num5 < num1)
- {
- do
- {
- UInt32 num7 = numArray[num5] + 1;
- UInt32 num8 = num7;
- numArray2[num5] = num7;
- flag1 = num8 == 0;
- num4 = num5 + 1;
- num5 = num4;
- }
- while (num4 < num1 && flag1);
- }
- if (flag1)
- {
- numArray2[num5] = 1;
- UInt32 num9 = num5 + 1;
- num5 = num9;
- bi1.length = num9;
- return;
- }
- }
- if (flag && num5 < num1 - 1)
- {
- do
- {
- numArray2[num5] = numArray[num5];
- num3 = num5 + 1;
- num5 = num3;
- }
- while (num3 < num1);
- }
- bi1.length = num1 + 1;
- bi1.Normalize();
- }
- }
- public static BigInteger RightShift(BigInteger bi, Int32 n)
- {
- unsafe
- {
- if (n == 0)
- {
- return new BigInteger(bi);
- }
- Int32 num = n >> 5;
- Int32 num1 = n & 31;
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, bi.length - num + 1);
- UInt32 length = (UInt32)((Int32)bigInteger.data.Length - 1);
- if (num1 == 0)
- {
- while (true)
- {
- UInt32 num2 = length;
- length = num2 - 1;
- if (num2 <= 0)
- {
- break;
- }
- bigInteger.data[length] = bi.data[checked((IntPtr)((UInt64)length + (Int64)num))];
- }
- }
- else
- {
- UInt32 num3 = 0;
- while (true)
- {
- UInt32 num4 = length;
- length = num4 - 1;
- if (num4 <= 0)
- {
- break;
- }
- UInt32 num5 = bi.data[checked((IntPtr)((UInt64)length + (Int64)num))];
- bigInteger.data[length] = num5 >> (n & 31) | num3;
- num3 = num5 << (32 - n & 31);
- }
- }
- bigInteger.Normalize();
- return bigInteger;
- }
- }
- public static UInt32 SingleByteDivideInPlace(BigInteger n, UInt32 d)
- {
- unsafe
- {
- UInt64 num = (UInt64)0;
- UInt32 num1 = n.length;
- while (true)
- {
- UInt32 num2 = num1;
- num1 = num2 - 1;
- if (num2 <= 0)
- {
- break;
- }
- num = num << 32;
- num = num | (UInt64)n.data[num1];
- n.data[num1] = (UInt32)(num / (UInt64)d);
- num = num % (UInt64)d;
- }
- n.Normalize();
- return (UInt32)num;
- }
- }
- public static unsafe Void SquarePositive(BigInteger bi, ref UInt32[] wkSpace)
- {
- Current member / type: System.Void Mono.Math.BigInteger/Kernel::SquarePositive(Mono.Math.BigInteger,System.UInt32[]&)
- File path: d:\steam\steamapps\common\robocraft\robocraftclient_data\managed\mscorlib.dll
- Product version: 2014.3.1021.0
- Exception in: System.Void SquarePositive(Mono.Math.BigInteger,System.UInt32[]&)
- 未将对象引用设置到对象的实例。
- 在 ..() 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Ast\Expressions\UnaryExpression.cs:行号 140
- 在 ..() 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Ast\Expressions\UnaryExpression.cs:行号 99
- 在 ..(TypeReference , Instruction ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 1876
- 在 ..(Instruction ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 1969
- 在 ..(Instruction , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Cil\InstructionDispatcher.cs:行号 243
- 在 ..Visit(Instruction ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Cil\BaseInstructionVisitor.cs:行号 1019
- 在 ..() 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 126
- 在 ..( , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 69
- 在 ..(MethodBody , ILanguage ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\DecompilationPipeline.cs:行号 83
- 在 ..( , ILanguage , MethodBody , & ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\Extensions.cs:行号 99
- 在 ..(MethodBody , ILanguage , & , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\Extensions.cs:行号 62
- 在 ..(ILanguage , MethodDefinition , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\WriterContextServices\BaseWriterContextService.cs:行号 116
- mailto: JustDecompilePublicFeedback@telerik.com
- }
- public static BigInteger Subtract(BigInteger big, BigInteger small)
- {
- unsafe
- {
- UInt32 num;
- UInt32 num1;
- UInt32 num2;
- BigInteger bigInteger = new BigInteger(BigInteger.Sign.Positive, big.length);
- UInt32[] numArray = bigInteger.data;
- UInt32[] numArray1 = big.data;
- UInt32[] numArray2 = small.data;
- UInt32 num3 = 0;
- UInt32 num4 = 0;
- do
- {
- UInt32 num5 = numArray2[num3];
- UInt32 num6 = num5 + num4;
- num5 = num6;
- UInt32 num7 = numArray1[num3] - num5;
- UInt32 num8 = num7;
- numArray[num3] = num7;
- num4 = (UInt32)((!(num6 < num4 | num8 > ~num5) ? 0 : 1));
- num = num3 + 1;
- num3 = num;
- }
- while (num < small.length);
- if (num3 != big.length)
- {
- if (num4 == 1)
- {
- do
- {
- numArray[num3] = numArray1[num3] - 1;
- num2 = num3;
- num3 = num2 + 1;
- }
- while (numArray1[num2] == 0 && num3 < big.length);
- if (num3 != big.length)
- {
- goto Label1;
- }
- bigInteger.Normalize();
- return bigInteger;
- }
- Label1:
- do
- {
- numArray[num3] = numArray1[num3];
- num1 = num3 + 1;
- num3 = num1;
- }
- while (num1 < big.length);
- }
- bigInteger.Normalize();
- return bigInteger;
- }
- }
- }
- internal sealed class ModulusRing
- {
- private BigInteger mod;
- private BigInteger constant;
- public ModulusRing(BigInteger modulus)
- {
- unsafe
- {
- this.mod = modulus;
- UInt32 num = this.mod.length << 1;
- this.constant = new BigInteger(BigInteger.Sign.Positive, num + 1);
- this.constant.data[num] = 1;
- this.constant = this.constant / this.mod;
- }
- }
- public Void BarrettReduction(BigInteger x)
- {
- unsafe
- {
- BigInteger bigInteger = this.mod;
- UInt32 num = bigInteger.length;
- UInt32 num1 = num + 1;
- UInt32 num2 = num - 1;
- if (x.length < num)
- {
- return;
- }
- if ((Int64)((Int32)x.data.Length) < (UInt64)x.length)
- {
- throw new IndexOutOfRangeException("x out of range");
- }
- BigInteger bigInteger1 = new BigInteger(BigInteger.Sign.Positive, x.length - num2 + this.constant.length);
- BigInteger.Kernel.Multiply(x.data, num2, x.length - num2, this.constant.data, 0, this.constant.length, bigInteger1.data, 0);
- x.length = (x.length <= num1 ? x.length : num1);
- x.Normalize();
- BigInteger bigInteger2 = new BigInteger(BigInteger.Sign.Positive, num1);
- BigInteger.Kernel.MultiplyMod2p32pmod(bigInteger1.data, (Int32)num1, (Int32)(bigInteger1.length - num1), bigInteger.data, 0, (Int32)bigInteger.length, bigInteger2.data, 0, (Int32)num1);
- bigInteger2.Normalize();
- if (bigInteger2 > x)
- {
- BigInteger bigInteger3 = new BigInteger(BigInteger.Sign.Positive, num1 + 1);
- bigInteger3.data[num1] = 1;
- BigInteger.Kernel.MinusEq(bigInteger3, bigInteger2);
- BigInteger.Kernel.PlusEq(x, bigInteger3);
- }
- else
- {
- BigInteger.Kernel.MinusEq(x, bigInteger2);
- }
- while (x >= bigInteger)
- {
- BigInteger.Kernel.MinusEq(x, bigInteger);
- }
- }
- }
- public BigInteger Difference(BigInteger a, BigInteger b)
- {
- BigInteger bigInteger;
- BigInteger.Sign sign = BigInteger.Kernel.Compare(a, b);
- switch (sign)
- {
- case BigInteger.Sign.Negative:
- {
- bigInteger = b - a;
- break;
- }
- case BigInteger.Sign.Zero:
- {
- return 0;
- }
- case BigInteger.Sign.Positive:
- {
- bigInteger = a - b;
- break;
- }
- default:
- {
- throw new Exception();
- }
- }
- if (bigInteger >= this.mod)
- {
- if (bigInteger.length < this.mod.length << 1)
- {
- this.BarrettReduction(bigInteger);
- }
- else
- {
- bigInteger = bigInteger % this.mod;
- }
- }
- if (sign == BigInteger.Sign.Negative)
- {
- bigInteger = this.mod - bigInteger;
- }
- return bigInteger;
- }
- public BigInteger Multiply(BigInteger a, BigInteger b)
- {
- if (a == 0 || b == 0)
- {
- return 0;
- }
- if (a > this.mod)
- {
- a = a % this.mod;
- }
- if (b > this.mod)
- {
- b = b % this.mod;
- }
- BigInteger bigInteger = a * b;
- this.BarrettReduction(bigInteger);
- return bigInteger;
- }
- public BigInteger Pow(BigInteger a, BigInteger k)
- {
- BigInteger bigInteger = new BigInteger(1);
- if (k == 0)
- {
- return bigInteger;
- }
- BigInteger bigInteger1 = a;
- if (k.TestBit(0))
- {
- bigInteger = a;
- }
- for (Int32 i = 1; i < k.BitCount(); i++)
- {
- bigInteger1 = this.Multiply(bigInteger1, bigInteger1);
- if (k.TestBit(i))
- {
- bigInteger = this.Multiply(bigInteger1, bigInteger);
- }
- }
- return bigInteger;
- }
- public BigInteger Pow(UInt32 b, BigInteger exp)
- {
- return this.Pow(new BigInteger(b), exp);
- }
- }
- internal sealed class Montgomery
- {
- private Montgomery()
- {
- }
- public static UInt32 Inverse(UInt32 n)
- {
- UInt32 num = n;
- while (true)
- {
- UInt32 num1 = n * num;
- UInt32 num2 = num1;
- if (num1 == 1)
- {
- break;
- }
- num = num * (2 - num2);
- }
- return (UInt32)(-(UInt64)num);
- }
- public static unsafe BigInteger Reduce(BigInteger n, BigInteger m, UInt32 mPrime)
- {
- Current member / type: Mono.Math.BigInteger Mono.Math.BigInteger/Montgomery::Reduce(Mono.Math.BigInteger,Mono.Math.BigInteger,System.UInt32)
- File path: d:\steam\steamapps\common\robocraft\robocraftclient_data\managed\mscorlib.dll
- Product version: 2014.3.1021.0
- Exception in: Mono.Math.BigInteger Reduce(Mono.Math.BigInteger,Mono.Math.BigInteger,System.UInt32)
- 未将对象引用设置到对象的实例。
- 在 ..() 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Ast\Expressions\UnaryExpression.cs:行号 140
- 在 ..() 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Ast\Expressions\UnaryExpression.cs:行号 99
- 在 ..(TypeReference , Instruction ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 1876
- 在 ..(Instruction ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 1969
- 在 ..(Instruction , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Cil\InstructionDispatcher.cs:行号 243
- 在 ..Visit(Instruction ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Cil\BaseInstructionVisitor.cs:行号 1019
- 在 ..() 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 126
- 在 ..( , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\ExpressionDecompilerStep.cs:行号 69
- 在 ..(MethodBody , ILanguage ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\DecompilationPipeline.cs:行号 83
- 在 ..( , ILanguage , MethodBody , & ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\Extensions.cs:行号 99
- 在 ..(MethodBody , ILanguage , & , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\Extensions.cs:行号 62
- 在 ..(ILanguage , MethodDefinition , ) 位置 c:\Builds\245\Behemoth\ReleaseBranch Production Build\Sources\Decompiler\Cecil.Decompiler\Decompiler\WriterContextServices\BaseWriterContextService.cs:行号 116
- mailto: JustDecompilePublicFeedback@telerik.com
- }
- public static BigInteger ToMont(BigInteger n, BigInteger m)
- {
- n.Normalize();
- m.Normalize();
- n = n << m.length * 32;
- n = n % m;
- return n;
- }
- }
- public enum Sign
- {
- Negative = -1,
- Zero = 0,
- Positive = 1
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement