| | | 1 | | using System; |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | using Renci.SshNet.Security.Org.BouncyCastle.Security; |
| | | 9 | | using Renci.SshNet.Security.Org.BouncyCastle.Utilities; |
| | | 10 | | |
| | | 11 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Math |
| | | 12 | | { |
| | | 13 | | #if FEATURE_BINARY_SERIALIZATION |
| | | 14 | | [Serializable] |
| | | 15 | | #endif |
| | | 16 | | internal class BigInteger |
| | | 17 | | { |
| | | 18 | | // The first few odd primes |
| | | 19 | | /* |
| | | 20 | | 3 5 7 11 13 17 19 23 29 |
| | | 21 | | 31 37 41 43 47 53 59 61 67 71 |
| | | 22 | | 73 79 83 89 97 101 103 107 109 113 |
| | | 23 | | 127 131 137 139 149 151 157 163 167 173 |
| | | 24 | | 179 181 191 193 197 199 211 223 227 229 |
| | | 25 | | 233 239 241 251 257 263 269 271 277 281 |
| | | 26 | | 283 293 307 311 313 317 331 337 347 349 |
| | | 27 | | 353 359 367 373 379 383 389 397 401 409 |
| | | 28 | | 419 421 431 433 439 443 449 457 461 463 |
| | | 29 | | 467 479 487 491 499 503 509 521 523 541 |
| | | 30 | | 547 557 563 569 571 577 587 593 599 601 |
| | | 31 | | 607 613 617 619 631 641 643 647 653 659 |
| | | 32 | | 661 673 677 683 691 701 709 719 727 733 |
| | | 33 | | 739 743 751 757 761 769 773 787 797 809 |
| | | 34 | | 811 821 823 827 829 839 853 857 859 863 |
| | | 35 | | 877 881 883 887 907 911 919 929 937 941 |
| | | 36 | | 947 953 967 971 977 983 991 997 1009 |
| | | 37 | | 1013 1019 1021 1031 1033 1039 1049 1051 |
| | | 38 | | 1061 1063 1069 1087 1091 1093 1097 1103 |
| | | 39 | | 1109 1117 1123 1129 1151 1153 1163 1171 |
| | | 40 | | 1181 1187 1193 1201 1213 1217 1223 1229 |
| | | 41 | | 1231 1237 1249 1259 1277 1279 1283 1289 |
| | | 42 | | */ |
| | | 43 | | |
| | | 44 | | // Each list has a product < 2^31 |
| | 1 | 45 | | internal static readonly int[][] primeLists = new int[][] |
| | 1 | 46 | | { |
| | 1 | 47 | | new int[]{ 3, 5, 7, 11, 13, 17, 19, 23 }, |
| | 1 | 48 | | new int[]{ 29, 31, 37, 41, 43 }, |
| | 1 | 49 | | new int[]{ 47, 53, 59, 61, 67 }, |
| | 1 | 50 | | new int[]{ 71, 73, 79, 83 }, |
| | 1 | 51 | | new int[]{ 89, 97, 101, 103 }, |
| | 1 | 52 | | |
| | 1 | 53 | | new int[]{ 107, 109, 113, 127 }, |
| | 1 | 54 | | new int[]{ 131, 137, 139, 149 }, |
| | 1 | 55 | | new int[]{ 151, 157, 163, 167 }, |
| | 1 | 56 | | new int[]{ 173, 179, 181, 191 }, |
| | 1 | 57 | | new int[]{ 193, 197, 199, 211 }, |
| | 1 | 58 | | |
| | 1 | 59 | | new int[]{ 223, 227, 229 }, |
| | 1 | 60 | | new int[]{ 233, 239, 241 }, |
| | 1 | 61 | | new int[]{ 251, 257, 263 }, |
| | 1 | 62 | | new int[]{ 269, 271, 277 }, |
| | 1 | 63 | | new int[]{ 281, 283, 293 }, |
| | 1 | 64 | | |
| | 1 | 65 | | new int[]{ 307, 311, 313 }, |
| | 1 | 66 | | new int[]{ 317, 331, 337 }, |
| | 1 | 67 | | new int[]{ 347, 349, 353 }, |
| | 1 | 68 | | new int[]{ 359, 367, 373 }, |
| | 1 | 69 | | new int[]{ 379, 383, 389 }, |
| | 1 | 70 | | |
| | 1 | 71 | | new int[]{ 397, 401, 409 }, |
| | 1 | 72 | | new int[]{ 419, 421, 431 }, |
| | 1 | 73 | | new int[]{ 433, 439, 443 }, |
| | 1 | 74 | | new int[]{ 449, 457, 461 }, |
| | 1 | 75 | | new int[]{ 463, 467, 479 }, |
| | 1 | 76 | | |
| | 1 | 77 | | new int[]{ 487, 491, 499 }, |
| | 1 | 78 | | new int[]{ 503, 509, 521 }, |
| | 1 | 79 | | new int[]{ 523, 541, 547 }, |
| | 1 | 80 | | new int[]{ 557, 563, 569 }, |
| | 1 | 81 | | new int[]{ 571, 577, 587 }, |
| | 1 | 82 | | |
| | 1 | 83 | | new int[]{ 593, 599, 601 }, |
| | 1 | 84 | | new int[]{ 607, 613, 617 }, |
| | 1 | 85 | | new int[]{ 619, 631, 641 }, |
| | 1 | 86 | | new int[]{ 643, 647, 653 }, |
| | 1 | 87 | | new int[]{ 659, 661, 673 }, |
| | 1 | 88 | | |
| | 1 | 89 | | new int[]{ 677, 683, 691 }, |
| | 1 | 90 | | new int[]{ 701, 709, 719 }, |
| | 1 | 91 | | new int[]{ 727, 733, 739 }, |
| | 1 | 92 | | new int[]{ 743, 751, 757 }, |
| | 1 | 93 | | new int[]{ 761, 769, 773 }, |
| | 1 | 94 | | |
| | 1 | 95 | | new int[]{ 787, 797, 809 }, |
| | 1 | 96 | | new int[]{ 811, 821, 823 }, |
| | 1 | 97 | | new int[]{ 827, 829, 839 }, |
| | 1 | 98 | | new int[]{ 853, 857, 859 }, |
| | 1 | 99 | | new int[]{ 863, 877, 881 }, |
| | 1 | 100 | | |
| | 1 | 101 | | new int[]{ 883, 887, 907 }, |
| | 1 | 102 | | new int[]{ 911, 919, 929 }, |
| | 1 | 103 | | new int[]{ 937, 941, 947 }, |
| | 1 | 104 | | new int[]{ 953, 967, 971 }, |
| | 1 | 105 | | new int[]{ 977, 983, 991 }, |
| | 1 | 106 | | |
| | 1 | 107 | | new int[]{ 997, 1009, 1013 }, |
| | 1 | 108 | | new int[]{ 1019, 1021, 1031 }, |
| | 1 | 109 | | new int[]{ 1033, 1039, 1049 }, |
| | 1 | 110 | | new int[]{ 1051, 1061, 1063 }, |
| | 1 | 111 | | new int[]{ 1069, 1087, 1091 }, |
| | 1 | 112 | | |
| | 1 | 113 | | new int[]{ 1093, 1097, 1103 }, |
| | 1 | 114 | | new int[]{ 1109, 1117, 1123 }, |
| | 1 | 115 | | new int[]{ 1129, 1151, 1153 }, |
| | 1 | 116 | | new int[]{ 1163, 1171, 1181 }, |
| | 1 | 117 | | new int[]{ 1187, 1193, 1201 }, |
| | 1 | 118 | | |
| | 1 | 119 | | new int[]{ 1213, 1217, 1223 }, |
| | 1 | 120 | | new int[]{ 1229, 1231, 1237 }, |
| | 1 | 121 | | new int[]{ 1249, 1259, 1277 }, |
| | 1 | 122 | | new int[]{ 1279, 1283, 1289 }, |
| | 1 | 123 | | }; |
| | | 124 | | |
| | | 125 | | internal static readonly int[] primeProducts; |
| | | 126 | | |
| | | 127 | | private const long IMASK = 0xFFFFFFFFL; |
| | | 128 | | private const ulong UIMASK = 0xFFFFFFFFUL; |
| | | 129 | | |
| | 1 | 130 | | private static readonly int[] ZeroMagnitude = new int[0]; |
| | 1 | 131 | | private static readonly byte[] ZeroEncoding = new byte[0]; |
| | | 132 | | |
| | 1 | 133 | | private static readonly BigInteger[] SMALL_CONSTANTS = new BigInteger[17]; |
| | | 134 | | public static readonly BigInteger Zero; |
| | | 135 | | public static readonly BigInteger One; |
| | | 136 | | public static readonly BigInteger Two; |
| | | 137 | | public static readonly BigInteger Three; |
| | | 138 | | public static readonly BigInteger Ten; |
| | | 139 | | |
| | | 140 | | //private readonly static byte[] BitCountTable = |
| | | 141 | | //{ |
| | | 142 | | // 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, |
| | | 143 | | // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| | | 144 | | // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| | | 145 | | // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| | | 146 | | // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| | | 147 | | // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| | | 148 | | // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| | | 149 | | // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| | | 150 | | // 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, |
| | | 151 | | // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| | | 152 | | // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| | | 153 | | // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| | | 154 | | // 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, |
| | | 155 | | // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| | | 156 | | // 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, |
| | | 157 | | // 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 |
| | | 158 | | //}; |
| | | 159 | | |
| | 1 | 160 | | private readonly static byte[] BitLengthTable = |
| | 1 | 161 | | { |
| | 1 | 162 | | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
| | 1 | 163 | | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| | 1 | 164 | | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| | 1 | 165 | | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| | 1 | 166 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| | 1 | 167 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| | 1 | 168 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| | 1 | 169 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| | 1 | 170 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| | 1 | 171 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| | 1 | 172 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| | 1 | 173 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| | 1 | 174 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| | 1 | 175 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| | 1 | 176 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| | 1 | 177 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 |
| | 1 | 178 | | }; |
| | | 179 | | |
| | | 180 | | // TODO Parse radix-2 64 bits at a time and radix-8 63 bits at a time |
| | | 181 | | private const int chunk2 = 1, chunk8 = 1, chunk10 = 19, chunk16 = 16; |
| | | 182 | | private static readonly BigInteger radix2, radix2E, radix8, radix8E, radix10, radix10E, radix16, radix16E; |
| | | 183 | | |
| | 1 | 184 | | private static readonly SecureRandom RandomSource = new SecureRandom(); |
| | | 185 | | |
| | | 186 | | /* |
| | | 187 | | * These are the threshold bit-lengths (of an exponent) where we increase the window size. |
| | | 188 | | * They are calculated according to the expected savings in multiplications. |
| | | 189 | | * Some squares will also be saved on average, but we offset these against the extra storage costs. |
| | | 190 | | */ |
| | 1 | 191 | | private static readonly int[] ExpWindowThresholds = { 7, 25, 81, 241, 673, 1793, 4609, Int32.MaxValue }; |
| | | 192 | | |
| | | 193 | | private const int BitsPerByte = 8; |
| | | 194 | | private const int BitsPerInt = 32; |
| | | 195 | | private const int BytesPerInt = 4; |
| | | 196 | | |
| | | 197 | | static BigInteger() |
| | 1 | 198 | | { |
| | 1 | 199 | | Zero = new BigInteger(0, ZeroMagnitude, false); |
| | 2 | 200 | | Zero.nBits = 0; Zero.nBitLength = 0; |
| | | 201 | | |
| | 1 | 202 | | SMALL_CONSTANTS[0] = Zero; |
| | 34 | 203 | | for (uint i = 1; i < SMALL_CONSTANTS.Length; ++i) |
| | 16 | 204 | | { |
| | 16 | 205 | | SMALL_CONSTANTS[i] = CreateUValueOf(i); |
| | 16 | 206 | | } |
| | | 207 | | |
| | 1 | 208 | | One = SMALL_CONSTANTS[1]; |
| | 1 | 209 | | Two = SMALL_CONSTANTS[2]; |
| | 1 | 210 | | Three = SMALL_CONSTANTS[3]; |
| | 1 | 211 | | Ten = SMALL_CONSTANTS[10]; |
| | | 212 | | |
| | 1 | 213 | | radix2 = ValueOf(2); |
| | 1 | 214 | | radix2E = radix2.Pow(chunk2); |
| | | 215 | | |
| | 1 | 216 | | radix8 = ValueOf(8); |
| | 1 | 217 | | radix8E = radix8.Pow(chunk8); |
| | | 218 | | |
| | 1 | 219 | | radix10 = ValueOf(10); |
| | 1 | 220 | | radix10E = radix10.Pow(chunk10); |
| | | 221 | | |
| | 1 | 222 | | radix16 = ValueOf(16); |
| | 1 | 223 | | radix16E = radix16.Pow(chunk16); |
| | | 224 | | |
| | 1 | 225 | | primeProducts = new int[primeLists.Length]; |
| | | 226 | | |
| | 130 | 227 | | for (int i = 0; i < primeLists.Length; ++i) |
| | 64 | 228 | | { |
| | 64 | 229 | | int[] primeList = primeLists[i]; |
| | 64 | 230 | | int product = primeList[0]; |
| | 416 | 231 | | for (int j = 1; j < primeList.Length; ++j) |
| | 144 | 232 | | { |
| | 144 | 233 | | product *= primeList[j]; |
| | 144 | 234 | | } |
| | 64 | 235 | | primeProducts[i] = product; |
| | 64 | 236 | | } |
| | 1 | 237 | | } |
| | | 238 | | |
| | | 239 | | private int[] magnitude; // array of ints with [0] being the most significant |
| | | 240 | | private int sign; // -1 means -ve; +1 means +ve; 0 means 0; |
| | 579136 | 241 | | private int nBits = -1; // cache BitCount() value |
| | 579136 | 242 | | private int nBitLength = -1; // cache BitLength() value |
| | 579136 | 243 | | private int mQuote = 0; // -m^(-1) mod b, b = 2^32 (see Montgomery mult.), 0 when uninitialised |
| | | 244 | | |
| | | 245 | | private static int GetByteLength( |
| | | 246 | | int nBits) |
| | 438 | 247 | | { |
| | 438 | 248 | | return (nBits + BitsPerByte - 1) / BitsPerByte; |
| | 438 | 249 | | } |
| | | 250 | | |
| | | 251 | | internal static BigInteger Arbitrary(int sizeInBits) |
| | 0 | 252 | | { |
| | 0 | 253 | | return new BigInteger(sizeInBits, RandomSource); |
| | 0 | 254 | | } |
| | | 255 | | |
| | 577876 | 256 | | private BigInteger( |
| | 577876 | 257 | | int signum, |
| | 577876 | 258 | | int[] mag, |
| | 577876 | 259 | | bool checkMag) |
| | 577876 | 260 | | { |
| | 577876 | 261 | | if (checkMag) |
| | 343812 | 262 | | { |
| | 343812 | 263 | | int i = 0; |
| | 455650 | 264 | | while (i < mag.Length && mag[i] == 0) |
| | 111838 | 265 | | { |
| | 111838 | 266 | | ++i; |
| | 111838 | 267 | | } |
| | | 268 | | |
| | 343812 | 269 | | if (i == mag.Length) |
| | 0 | 270 | | { |
| | 0 | 271 | | this.sign = 0; |
| | 0 | 272 | | this.magnitude = ZeroMagnitude; |
| | 0 | 273 | | } |
| | | 274 | | else |
| | 343812 | 275 | | { |
| | 343812 | 276 | | this.sign = signum; |
| | | 277 | | |
| | 343812 | 278 | | if (i == 0) |
| | 247602 | 279 | | { |
| | 247602 | 280 | | this.magnitude = mag; |
| | 247602 | 281 | | } |
| | | 282 | | else |
| | 96210 | 283 | | { |
| | | 284 | | // strip leading 0 words |
| | 96210 | 285 | | this.magnitude = new int[mag.Length - i]; |
| | 96210 | 286 | | Array.Copy(mag, i, this.magnitude, 0, this.magnitude.Length); |
| | 96210 | 287 | | } |
| | 343812 | 288 | | } |
| | 343812 | 289 | | } |
| | | 290 | | else |
| | 234064 | 291 | | { |
| | 234064 | 292 | | this.sign = signum; |
| | 234064 | 293 | | this.magnitude = mag; |
| | 234064 | 294 | | } |
| | 577876 | 295 | | } |
| | | 296 | | |
| | | 297 | | public BigInteger( |
| | | 298 | | string value) |
| | 0 | 299 | | : this(value, 10) |
| | 0 | 300 | | { |
| | 0 | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | public BigInteger( |
| | 0 | 304 | | string str, |
| | 0 | 305 | | int radix) |
| | 0 | 306 | | { |
| | 0 | 307 | | if (str.Length == 0) |
| | 0 | 308 | | throw new FormatException("Zero length BigInteger"); |
| | | 309 | | |
| | | 310 | | NumberStyles style; |
| | | 311 | | int chunk; |
| | | 312 | | BigInteger r; |
| | | 313 | | BigInteger rE; |
| | | 314 | | |
| | 0 | 315 | | switch (radix) |
| | | 316 | | { |
| | | 317 | | case 2: |
| | | 318 | | // Is there anyway to restrict to binary digits? |
| | 0 | 319 | | style = NumberStyles.Integer; |
| | 0 | 320 | | chunk = chunk2; |
| | 0 | 321 | | r = radix2; |
| | 0 | 322 | | rE = radix2E; |
| | 0 | 323 | | break; |
| | | 324 | | case 8: |
| | | 325 | | // Is there anyway to restrict to octal digits? |
| | 0 | 326 | | style = NumberStyles.Integer; |
| | 0 | 327 | | chunk = chunk8; |
| | 0 | 328 | | r = radix8; |
| | 0 | 329 | | rE = radix8E; |
| | 0 | 330 | | break; |
| | | 331 | | case 10: |
| | | 332 | | // This style seems to handle spaces and minus sign already (our processing redundant?) |
| | 0 | 333 | | style = NumberStyles.Integer; |
| | 0 | 334 | | chunk = chunk10; |
| | 0 | 335 | | r = radix10; |
| | 0 | 336 | | rE = radix10E; |
| | 0 | 337 | | break; |
| | | 338 | | case 16: |
| | | 339 | | // TODO Should this be HexNumber? |
| | 0 | 340 | | style = NumberStyles.AllowHexSpecifier; |
| | 0 | 341 | | chunk = chunk16; |
| | 0 | 342 | | r = radix16; |
| | 0 | 343 | | rE = radix16E; |
| | 0 | 344 | | break; |
| | | 345 | | default: |
| | 0 | 346 | | throw new FormatException("Only bases 2, 8, 10, or 16 allowed"); |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | |
| | 0 | 350 | | int index = 0; |
| | 0 | 351 | | sign = 1; |
| | | 352 | | |
| | 0 | 353 | | if (str[0] == '-') |
| | 0 | 354 | | { |
| | 0 | 355 | | if (str.Length == 1) |
| | 0 | 356 | | throw new FormatException("Zero length BigInteger"); |
| | | 357 | | |
| | 0 | 358 | | sign = -1; |
| | 0 | 359 | | index = 1; |
| | 0 | 360 | | } |
| | | 361 | | |
| | | 362 | | // strip leading zeros from the string str |
| | 0 | 363 | | while (index < str.Length && Int32.Parse(str[index].ToString(), style) == 0) |
| | 0 | 364 | | { |
| | 0 | 365 | | index++; |
| | 0 | 366 | | } |
| | | 367 | | |
| | 0 | 368 | | if (index >= str.Length) |
| | 0 | 369 | | { |
| | | 370 | | // zero value - we're done |
| | 0 | 371 | | sign = 0; |
| | 0 | 372 | | magnitude = ZeroMagnitude; |
| | 0 | 373 | | return; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | ////// |
| | | 377 | | // could we work out the max number of ints required to store |
| | | 378 | | // str.Length digits in the given base, then allocate that |
| | | 379 | | // storage in one hit?, then Generate the magnitude in one hit too? |
| | | 380 | | ////// |
| | | 381 | | |
| | 0 | 382 | | BigInteger b = Zero; |
| | | 383 | | |
| | | 384 | | |
| | 0 | 385 | | int next = index + chunk; |
| | | 386 | | |
| | 0 | 387 | | if (next <= str.Length) |
| | 0 | 388 | | { |
| | | 389 | | do |
| | 0 | 390 | | { |
| | 0 | 391 | | string s = str.Substring(index, chunk); |
| | 0 | 392 | | ulong i = ulong.Parse(s, style); |
| | 0 | 393 | | BigInteger bi = CreateUValueOf(i); |
| | | 394 | | |
| | 0 | 395 | | switch (radix) |
| | | 396 | | { |
| | | 397 | | case 2: |
| | | 398 | | // TODO Need this because we are parsing in radix 10 above |
| | 0 | 399 | | if (i >= 2) |
| | 0 | 400 | | throw new FormatException("Bad character in radix 2 string: " + s); |
| | | 401 | | |
| | | 402 | | // TODO Parse 64 bits at a time |
| | 0 | 403 | | b = b.ShiftLeft(1); |
| | 0 | 404 | | break; |
| | | 405 | | case 8: |
| | | 406 | | // TODO Need this because we are parsing in radix 10 above |
| | 0 | 407 | | if (i >= 8) |
| | 0 | 408 | | throw new FormatException("Bad character in radix 8 string: " + s); |
| | | 409 | | |
| | | 410 | | // TODO Parse 63 bits at a time |
| | 0 | 411 | | b = b.ShiftLeft(3); |
| | 0 | 412 | | break; |
| | | 413 | | case 16: |
| | 0 | 414 | | b = b.ShiftLeft(64); |
| | 0 | 415 | | break; |
| | | 416 | | default: |
| | 0 | 417 | | b = b.Multiply(rE); |
| | 0 | 418 | | break; |
| | | 419 | | } |
| | | 420 | | |
| | 0 | 421 | | b = b.Add(bi); |
| | | 422 | | |
| | 0 | 423 | | index = next; |
| | 0 | 424 | | next += chunk; |
| | 0 | 425 | | } |
| | 0 | 426 | | while (next <= str.Length); |
| | 0 | 427 | | } |
| | | 428 | | |
| | 0 | 429 | | if (index < str.Length) |
| | 0 | 430 | | { |
| | 0 | 431 | | string s = str.Substring(index); |
| | 0 | 432 | | ulong i = ulong.Parse(s, style); |
| | 0 | 433 | | BigInteger bi = CreateUValueOf(i); |
| | | 434 | | |
| | 0 | 435 | | if (b.sign > 0) |
| | 0 | 436 | | { |
| | 0 | 437 | | if (radix == 2) |
| | 0 | 438 | | { |
| | | 439 | | // NB: Can't reach here since we are parsing one char at a time |
| | 0 | 440 | | Debug.Assert(false); |
| | | 441 | | |
| | | 442 | | // TODO Parse all bits at once |
| | | 443 | | // b = b.ShiftLeft(s.Length); |
| | 0 | 444 | | } |
| | 0 | 445 | | else if (radix == 8) |
| | 0 | 446 | | { |
| | | 447 | | // NB: Can't reach here since we are parsing one char at a time |
| | 0 | 448 | | Debug.Assert(false); |
| | | 449 | | |
| | | 450 | | // TODO Parse all bits at once |
| | | 451 | | // b = b.ShiftLeft(s.Length * 3); |
| | 0 | 452 | | } |
| | 0 | 453 | | else if (radix == 16) |
| | 0 | 454 | | { |
| | 0 | 455 | | b = b.ShiftLeft(s.Length << 2); |
| | 0 | 456 | | } |
| | | 457 | | else |
| | 0 | 458 | | { |
| | 0 | 459 | | b = b.Multiply(r.Pow(s.Length)); |
| | 0 | 460 | | } |
| | | 461 | | |
| | 0 | 462 | | b = b.Add(bi); |
| | 0 | 463 | | } |
| | | 464 | | else |
| | 0 | 465 | | { |
| | 0 | 466 | | b = bi; |
| | 0 | 467 | | } |
| | 0 | 468 | | } |
| | | 469 | | |
| | | 470 | | // Note: This is the previous (slower) algorithm |
| | | 471 | | // while (index < value.Length) |
| | | 472 | | // { |
| | | 473 | | // char c = value[index]; |
| | | 474 | | // string s = c.ToString(); |
| | | 475 | | // int i = Int32.Parse(s, style); |
| | | 476 | | // |
| | | 477 | | // b = b.Multiply(r).Add(ValueOf(i)); |
| | | 478 | | // index++; |
| | | 479 | | // } |
| | | 480 | | |
| | 0 | 481 | | magnitude = b.magnitude; |
| | 0 | 482 | | } |
| | | 483 | | |
| | | 484 | | public BigInteger( |
| | | 485 | | byte[] bytes) |
| | 0 | 486 | | : this(bytes, 0, bytes.Length) |
| | 0 | 487 | | { |
| | 0 | 488 | | } |
| | | 489 | | |
| | 0 | 490 | | public BigInteger( |
| | 0 | 491 | | byte[] bytes, |
| | 0 | 492 | | int offset, |
| | 0 | 493 | | int length) |
| | 0 | 494 | | { |
| | 0 | 495 | | if (length == 0) |
| | 0 | 496 | | throw new FormatException("Zero length BigInteger"); |
| | | 497 | | |
| | | 498 | | // TODO Move this processing into MakeMagnitude (provide sign argument) |
| | 0 | 499 | | if ((sbyte)bytes[offset] < 0) |
| | 0 | 500 | | { |
| | 0 | 501 | | this.sign = -1; |
| | | 502 | | |
| | 0 | 503 | | int end = offset + length; |
| | | 504 | | |
| | | 505 | | int iBval; |
| | | 506 | | // strip leading sign bytes |
| | 0 | 507 | | for (iBval = offset; iBval < end && ((sbyte)bytes[iBval] == -1); iBval++) |
| | 0 | 508 | | { |
| | 0 | 509 | | } |
| | | 510 | | |
| | 0 | 511 | | if (iBval >= end) |
| | 0 | 512 | | { |
| | 0 | 513 | | this.magnitude = One.magnitude; |
| | 0 | 514 | | } |
| | | 515 | | else |
| | 0 | 516 | | { |
| | 0 | 517 | | int numBytes = end - iBval; |
| | 0 | 518 | | byte[] inverse = new byte[numBytes]; |
| | | 519 | | |
| | 0 | 520 | | int index = 0; |
| | 0 | 521 | | while (index < numBytes) |
| | 0 | 522 | | { |
| | 0 | 523 | | inverse[index++] = (byte)~bytes[iBval++]; |
| | 0 | 524 | | } |
| | | 525 | | |
| | 0 | 526 | | Debug.Assert(iBval == end); |
| | | 527 | | |
| | 0 | 528 | | while (inverse[--index] == byte.MaxValue) |
| | 0 | 529 | | { |
| | 0 | 530 | | inverse[index] = byte.MinValue; |
| | 0 | 531 | | } |
| | | 532 | | |
| | 0 | 533 | | inverse[index]++; |
| | | 534 | | |
| | 0 | 535 | | this.magnitude = MakeMagnitude(inverse, 0, inverse.Length); |
| | 0 | 536 | | } |
| | 0 | 537 | | } |
| | | 538 | | else |
| | 0 | 539 | | { |
| | | 540 | | // strip leading zero bytes and return magnitude bytes |
| | 0 | 541 | | this.magnitude = MakeMagnitude(bytes, offset, length); |
| | 0 | 542 | | this.sign = this.magnitude.Length > 0 ? 1 : 0; |
| | 0 | 543 | | } |
| | 0 | 544 | | } |
| | | 545 | | |
| | | 546 | | private static int[] MakeMagnitude( |
| | | 547 | | byte[] bytes, |
| | | 548 | | int offset, |
| | | 549 | | int length) |
| | 1260 | 550 | | { |
| | 1260 | 551 | | int end = offset + length; |
| | | 552 | | |
| | | 553 | | // strip leading zeros |
| | | 554 | | int firstSignificant; |
| | 2798 | 555 | | for (firstSignificant = offset; firstSignificant < end |
| | 1816 | 556 | | && bytes[firstSignificant] == 0; firstSignificant++) |
| | 278 | 557 | | { |
| | 278 | 558 | | } |
| | | 559 | | |
| | 1260 | 560 | | if (firstSignificant >= end) |
| | 0 | 561 | | { |
| | 0 | 562 | | return ZeroMagnitude; |
| | | 563 | | } |
| | | 564 | | |
| | 1260 | 565 | | int nInts = (end - firstSignificant + 3) / BytesPerInt; |
| | 1260 | 566 | | int bCount = (end - firstSignificant) % BytesPerInt; |
| | 1260 | 567 | | if (bCount == 0) |
| | 702 | 568 | | { |
| | 702 | 569 | | bCount = BytesPerInt; |
| | 702 | 570 | | } |
| | | 571 | | |
| | 1260 | 572 | | if (nInts < 1) |
| | 0 | 573 | | { |
| | 0 | 574 | | return ZeroMagnitude; |
| | | 575 | | } |
| | | 576 | | |
| | 1260 | 577 | | int[] mag = new int[nInts]; |
| | | 578 | | |
| | 1260 | 579 | | int v = 0; |
| | 1260 | 580 | | int magnitudeIndex = 0; |
| | 133632 | 581 | | for (int i = firstSignificant; i < end; ++i) |
| | 65556 | 582 | | { |
| | 65556 | 583 | | v <<= 8; |
| | 65556 | 584 | | v |= bytes[i] & 0xff; |
| | 65556 | 585 | | bCount--; |
| | 65556 | 586 | | if (bCount <= 0) |
| | 16730 | 587 | | { |
| | 16730 | 588 | | mag[magnitudeIndex] = v; |
| | 16730 | 589 | | magnitudeIndex++; |
| | 16730 | 590 | | bCount = BytesPerInt; |
| | 16730 | 591 | | v = 0; |
| | 16730 | 592 | | } |
| | 65556 | 593 | | } |
| | | 594 | | |
| | 1260 | 595 | | if (magnitudeIndex < mag.Length) |
| | 0 | 596 | | { |
| | 0 | 597 | | mag[magnitudeIndex] = v; |
| | 0 | 598 | | } |
| | | 599 | | |
| | 1260 | 600 | | return mag; |
| | 1260 | 601 | | } |
| | | 602 | | |
| | | 603 | | public BigInteger( |
| | | 604 | | int sign, |
| | | 605 | | byte[] bytes) |
| | 1227 | 606 | | : this(sign, bytes, 0, bytes.Length) |
| | 1227 | 607 | | { |
| | 1227 | 608 | | } |
| | | 609 | | |
| | 1251 | 610 | | public BigInteger( |
| | 1251 | 611 | | int sign, |
| | 1251 | 612 | | byte[] bytes, |
| | 1251 | 613 | | int offset, |
| | 1251 | 614 | | int length) |
| | 1251 | 615 | | { |
| | 1251 | 616 | | if (sign < -1 || sign > 1) |
| | 0 | 617 | | throw new FormatException("Invalid sign value"); |
| | | 618 | | |
| | 1251 | 619 | | if (sign == 0) |
| | 0 | 620 | | { |
| | 0 | 621 | | this.sign = 0; |
| | 0 | 622 | | this.magnitude = ZeroMagnitude; |
| | 0 | 623 | | } |
| | | 624 | | else |
| | 1251 | 625 | | { |
| | | 626 | | // copy bytes |
| | 1251 | 627 | | this.magnitude = MakeMagnitude(bytes, offset, length); |
| | 1251 | 628 | | this.sign = this.magnitude.Length < 1 ? 0 : sign; |
| | 1251 | 629 | | } |
| | 1251 | 630 | | } |
| | | 631 | | |
| | 9 | 632 | | public BigInteger( |
| | 9 | 633 | | int sizeInBits, |
| | 9 | 634 | | Random random) |
| | 9 | 635 | | { |
| | 9 | 636 | | if (sizeInBits < 0) |
| | 0 | 637 | | throw new ArgumentException("sizeInBits must be non-negative"); |
| | | 638 | | |
| | 9 | 639 | | this.nBits = -1; |
| | 9 | 640 | | this.nBitLength = -1; |
| | | 641 | | |
| | 9 | 642 | | if (sizeInBits == 0) |
| | 0 | 643 | | { |
| | 0 | 644 | | this.sign = 0; |
| | 0 | 645 | | this.magnitude = ZeroMagnitude; |
| | 0 | 646 | | return; |
| | | 647 | | } |
| | | 648 | | |
| | 9 | 649 | | int nBytes = GetByteLength(sizeInBits); |
| | 9 | 650 | | byte[] b = new byte[nBytes]; |
| | | 651 | | #pragma warning disable CA5394 // Do not use insecure randomness |
| | 9 | 652 | | random.NextBytes(b); |
| | | 653 | | #pragma warning restore CA5394 // Do not use insecure randomness |
| | | 654 | | |
| | | 655 | | // strip off any excess bits in the MSB |
| | 9 | 656 | | int xBits = BitsPerByte * nBytes - sizeInBits; |
| | 9 | 657 | | b[0] &= (byte)(255U >> xBits); |
| | | 658 | | |
| | 9 | 659 | | this.magnitude = MakeMagnitude(b, 0, b.Length); |
| | 9 | 660 | | this.sign = this.magnitude.Length < 1 ? 0 : 1; |
| | 9 | 661 | | } |
| | | 662 | | |
| | | 663 | | #pragma warning disable CA5394 // Do not use insecure randomness |
| | 0 | 664 | | public BigInteger( |
| | 0 | 665 | | int bitLength, |
| | 0 | 666 | | int certainty, |
| | 0 | 667 | | Random random) |
| | 0 | 668 | | { |
| | 0 | 669 | | if (bitLength < 2) |
| | 0 | 670 | | throw new ArithmeticException("bitLength < 2"); |
| | | 671 | | |
| | 0 | 672 | | this.sign = 1; |
| | 0 | 673 | | this.nBitLength = bitLength; |
| | | 674 | | |
| | 0 | 675 | | if (bitLength == 2) |
| | 0 | 676 | | { |
| | 0 | 677 | | this.magnitude = random.Next(2) == 0 |
| | 0 | 678 | | ? Two.magnitude |
| | 0 | 679 | | : Three.magnitude; |
| | 0 | 680 | | return; |
| | | 681 | | } |
| | | 682 | | |
| | 0 | 683 | | int nBytes = GetByteLength(bitLength); |
| | 0 | 684 | | byte[] b = new byte[nBytes]; |
| | | 685 | | |
| | 0 | 686 | | int xBits = BitsPerByte * nBytes - bitLength; |
| | 0 | 687 | | byte mask = (byte)(255U >> xBits); |
| | 0 | 688 | | byte lead = (byte)(1 << (7 - xBits)); |
| | | 689 | | |
| | | 690 | | for (;;) |
| | 0 | 691 | | { |
| | 0 | 692 | | random.NextBytes(b); |
| | | 693 | | |
| | | 694 | | // strip off any excess bits in the MSB |
| | 0 | 695 | | b[0] &= mask; |
| | | 696 | | |
| | | 697 | | // ensure the leading bit is 1 (to meet the strength requirement) |
| | 0 | 698 | | b[0] |= lead; |
| | | 699 | | |
| | | 700 | | // ensure the trailing bit is 1 (i.e. must be odd) |
| | 0 | 701 | | b[nBytes - 1] |= 1; |
| | | 702 | | |
| | 0 | 703 | | this.magnitude = MakeMagnitude(b, 0, b.Length); |
| | 0 | 704 | | this.nBits = -1; |
| | 0 | 705 | | this.mQuote = 0; |
| | | 706 | | |
| | 0 | 707 | | if (certainty < 1) |
| | 0 | 708 | | break; |
| | | 709 | | |
| | 0 | 710 | | if (CheckProbablePrime(certainty, random, true)) |
| | 0 | 711 | | break; |
| | | 712 | | |
| | 0 | 713 | | for (int j = 1; j < (magnitude.Length - 1); ++j) |
| | 0 | 714 | | { |
| | 0 | 715 | | this.magnitude[j] ^= random.Next(); |
| | | 716 | | |
| | 0 | 717 | | if (CheckProbablePrime(certainty, random, true)) |
| | 0 | 718 | | return; |
| | 0 | 719 | | } |
| | 0 | 720 | | } |
| | 0 | 721 | | } |
| | | 722 | | #pragma warning restore CA5394 // Do not use insecure randomness |
| | | 723 | | |
| | | 724 | | public BigInteger Abs() |
| | 91565 | 725 | | { |
| | 91565 | 726 | | return sign >= 0 ? this : Negate(); |
| | 91565 | 727 | | } |
| | | 728 | | |
| | | 729 | | /** |
| | | 730 | | * return a = a + b - b preserved. |
| | | 731 | | */ |
| | | 732 | | private static int[] AddMagnitudes( |
| | | 733 | | int[] a, |
| | | 734 | | int[] b) |
| | 106094 | 735 | | { |
| | 106094 | 736 | | int tI = a.Length - 1; |
| | 106094 | 737 | | int vI = b.Length - 1; |
| | 106094 | 738 | | long m = 0; |
| | | 739 | | |
| | 1480935 | 740 | | while (vI >= 0) |
| | 1374841 | 741 | | { |
| | 1374841 | 742 | | m += ((long)(uint)a[tI] + (long)(uint)b[vI--]); |
| | 1374841 | 743 | | a[tI--] = (int)m; |
| | 1374841 | 744 | | m = (long)((ulong)m >> 32); |
| | 1374841 | 745 | | } |
| | | 746 | | |
| | 106094 | 747 | | if (m != 0) |
| | 26918 | 748 | | { |
| | 26918 | 749 | | while (tI >= 0 && ++a[tI--] == 0) |
| | 0 | 750 | | { |
| | 0 | 751 | | } |
| | 26918 | 752 | | } |
| | | 753 | | |
| | 106094 | 754 | | return a; |
| | 106094 | 755 | | } |
| | | 756 | | |
| | | 757 | | public BigInteger Add( |
| | | 758 | | BigInteger value) |
| | 116182 | 759 | | { |
| | 116182 | 760 | | if (this.sign == 0) |
| | 0 | 761 | | return value; |
| | | 762 | | |
| | 116182 | 763 | | if (this.sign != value.sign) |
| | 10248 | 764 | | { |
| | 10248 | 765 | | if (value.sign == 0) |
| | 0 | 766 | | return this; |
| | | 767 | | |
| | 10248 | 768 | | if (value.sign < 0) |
| | 0 | 769 | | return Subtract(value.Negate()); |
| | | 770 | | |
| | 10248 | 771 | | return value.Subtract(Negate()); |
| | | 772 | | } |
| | | 773 | | |
| | 105934 | 774 | | return AddToMagnitude(value.magnitude); |
| | 116182 | 775 | | } |
| | | 776 | | |
| | | 777 | | private BigInteger AddToMagnitude( |
| | | 778 | | int[] magToAdd) |
| | 105934 | 779 | | { |
| | | 780 | | int[] big, small; |
| | 105934 | 781 | | if (this.magnitude.Length < magToAdd.Length) |
| | 20305 | 782 | | { |
| | 20305 | 783 | | big = magToAdd; |
| | 20305 | 784 | | small = this.magnitude; |
| | 20305 | 785 | | } |
| | | 786 | | else |
| | 85629 | 787 | | { |
| | 85629 | 788 | | big = this.magnitude; |
| | 85629 | 789 | | small = magToAdd; |
| | 85629 | 790 | | } |
| | | 791 | | |
| | | 792 | | // Conservatively avoid over-allocation when no overflow possible |
| | 105934 | 793 | | uint limit = uint.MaxValue; |
| | 105934 | 794 | | if (big.Length == small.Length) |
| | 66304 | 795 | | limit -= (uint) small[0]; |
| | | 796 | | |
| | 105934 | 797 | | bool possibleOverflow = (uint) big[0] >= limit; |
| | | 798 | | |
| | | 799 | | int[] bigCopy; |
| | 105934 | 800 | | if (possibleOverflow) |
| | 11601 | 801 | | { |
| | 11601 | 802 | | bigCopy = new int[big.Length + 1]; |
| | 11601 | 803 | | big.CopyTo(bigCopy, 1); |
| | 11601 | 804 | | } |
| | | 805 | | else |
| | 94333 | 806 | | { |
| | 94333 | 807 | | bigCopy = (int[]) big.Clone(); |
| | 94333 | 808 | | } |
| | | 809 | | |
| | 105934 | 810 | | bigCopy = AddMagnitudes(bigCopy, small); |
| | | 811 | | |
| | 105934 | 812 | | return new BigInteger(this.sign, bigCopy, possibleOverflow); |
| | 105934 | 813 | | } |
| | | 814 | | |
| | | 815 | | public BigInteger And( |
| | | 816 | | BigInteger value) |
| | 0 | 817 | | { |
| | 0 | 818 | | if (this.sign == 0 || value.sign == 0) |
| | 0 | 819 | | { |
| | 0 | 820 | | return Zero; |
| | | 821 | | } |
| | | 822 | | |
| | 0 | 823 | | int[] aMag = this.sign > 0 |
| | 0 | 824 | | ? this.magnitude |
| | 0 | 825 | | : Add(One).magnitude; |
| | | 826 | | |
| | 0 | 827 | | int[] bMag = value.sign > 0 |
| | 0 | 828 | | ? value.magnitude |
| | 0 | 829 | | : value.Add(One).magnitude; |
| | | 830 | | |
| | 0 | 831 | | bool resultNeg = sign < 0 && value.sign < 0; |
| | 0 | 832 | | int resultLength = System.Math.Max(aMag.Length, bMag.Length); |
| | 0 | 833 | | int[] resultMag = new int[resultLength]; |
| | | 834 | | |
| | 0 | 835 | | int aStart = resultMag.Length - aMag.Length; |
| | 0 | 836 | | int bStart = resultMag.Length - bMag.Length; |
| | | 837 | | |
| | 0 | 838 | | for (int i = 0; i < resultMag.Length; ++i) |
| | 0 | 839 | | { |
| | 0 | 840 | | int aWord = i >= aStart ? aMag[i - aStart] : 0; |
| | 0 | 841 | | int bWord = i >= bStart ? bMag[i - bStart] : 0; |
| | | 842 | | |
| | 0 | 843 | | if (this.sign < 0) |
| | 0 | 844 | | { |
| | 0 | 845 | | aWord = ~aWord; |
| | 0 | 846 | | } |
| | | 847 | | |
| | 0 | 848 | | if (value.sign < 0) |
| | 0 | 849 | | { |
| | 0 | 850 | | bWord = ~bWord; |
| | 0 | 851 | | } |
| | | 852 | | |
| | 0 | 853 | | resultMag[i] = aWord & bWord; |
| | | 854 | | |
| | 0 | 855 | | if (resultNeg) |
| | 0 | 856 | | { |
| | 0 | 857 | | resultMag[i] = ~resultMag[i]; |
| | 0 | 858 | | } |
| | 0 | 859 | | } |
| | | 860 | | |
| | 0 | 861 | | BigInteger result = new BigInteger(1, resultMag, true); |
| | | 862 | | |
| | | 863 | | // TODO Optimise this case |
| | 0 | 864 | | if (resultNeg) |
| | 0 | 865 | | { |
| | 0 | 866 | | result = result.Not(); |
| | 0 | 867 | | } |
| | | 868 | | |
| | 0 | 869 | | return result; |
| | 0 | 870 | | } |
| | | 871 | | |
| | | 872 | | public BigInteger AndNot( |
| | | 873 | | BigInteger val) |
| | 0 | 874 | | { |
| | 0 | 875 | | return And(val.Not()); |
| | 0 | 876 | | } |
| | | 877 | | |
| | | 878 | | public int BitCount |
| | | 879 | | { |
| | | 880 | | get |
| | 9 | 881 | | { |
| | 9 | 882 | | if (nBits == -1) |
| | 9 | 883 | | { |
| | 9 | 884 | | if (sign < 0) |
| | 0 | 885 | | { |
| | | 886 | | // TODO Optimise this case |
| | 0 | 887 | | nBits = Not().BitCount; |
| | 0 | 888 | | } |
| | | 889 | | else |
| | 9 | 890 | | { |
| | 9 | 891 | | int sum = 0; |
| | 246 | 892 | | for (int i = 0; i < magnitude.Length; ++i) |
| | 114 | 893 | | { |
| | 114 | 894 | | sum += BitCnt(magnitude[i]); |
| | 114 | 895 | | } |
| | 9 | 896 | | nBits = sum; |
| | 9 | 897 | | } |
| | 9 | 898 | | } |
| | | 899 | | |
| | 9 | 900 | | return nBits; |
| | 9 | 901 | | } |
| | | 902 | | } |
| | | 903 | | |
| | | 904 | | public static int BitCnt(int i) |
| | 114 | 905 | | { |
| | 114 | 906 | | uint u = (uint)i; |
| | 114 | 907 | | u = u - ((u >> 1) & 0x55555555); |
| | 114 | 908 | | u = (u & 0x33333333) + ((u >> 2) & 0x33333333); |
| | 114 | 909 | | u = (u + (u >> 4)) & 0x0f0f0f0f; |
| | 114 | 910 | | u += (u >> 8); |
| | 114 | 911 | | u += (u >> 16); |
| | 114 | 912 | | u &= 0x3f; |
| | 114 | 913 | | return (int)u; |
| | 114 | 914 | | } |
| | | 915 | | |
| | | 916 | | private static int CalcBitLength(int sign, int indx, int[] mag) |
| | 142382 | 917 | | { |
| | | 918 | | for (;;) |
| | 142382 | 919 | | { |
| | 142382 | 920 | | if (indx >= mag.Length) |
| | 0 | 921 | | return 0; |
| | | 922 | | |
| | 142382 | 923 | | if (mag[indx] != 0) |
| | 142382 | 924 | | break; |
| | | 925 | | |
| | 0 | 926 | | ++indx; |
| | 0 | 927 | | } |
| | | 928 | | |
| | | 929 | | // bit length for everything after the first int |
| | 142382 | 930 | | int bitLength = 32 * ((mag.Length - indx) - 1); |
| | | 931 | | |
| | | 932 | | // and determine bitlength of first int |
| | 142382 | 933 | | int firstMag = mag[indx]; |
| | 142382 | 934 | | bitLength += BitLen(firstMag); |
| | | 935 | | |
| | | 936 | | // Check for negative powers of two |
| | 142382 | 937 | | if (sign < 0 && ((firstMag & -firstMag) == firstMag)) |
| | 0 | 938 | | { |
| | | 939 | | do |
| | 0 | 940 | | { |
| | 0 | 941 | | if (++indx >= mag.Length) |
| | 0 | 942 | | { |
| | 0 | 943 | | --bitLength; |
| | 0 | 944 | | break; |
| | | 945 | | } |
| | 0 | 946 | | } |
| | 0 | 947 | | while (mag[indx] == 0); |
| | 0 | 948 | | } |
| | | 949 | | |
| | 142382 | 950 | | return bitLength; |
| | 142382 | 951 | | } |
| | | 952 | | |
| | | 953 | | public int BitLength |
| | | 954 | | { |
| | | 955 | | get |
| | 461106 | 956 | | { |
| | 461106 | 957 | | if (nBitLength == -1) |
| | 142380 | 958 | | { |
| | 142380 | 959 | | nBitLength = sign == 0 |
| | 142380 | 960 | | ? 0 |
| | 142380 | 961 | | : CalcBitLength(sign, 0, magnitude); |
| | 142380 | 962 | | } |
| | | 963 | | |
| | 461106 | 964 | | return nBitLength; |
| | 461106 | 965 | | } |
| | | 966 | | } |
| | | 967 | | |
| | | 968 | | // |
| | | 969 | | // BitLen(value) is the number of bits in value. |
| | | 970 | | // |
| | | 971 | | internal static int BitLen(int w) |
| | 142542 | 972 | | { |
| | 142542 | 973 | | uint v = (uint)w; |
| | 142542 | 974 | | uint t = v >> 24; |
| | 142542 | 975 | | if (t != 0) |
| | 84433 | 976 | | return 24 + BitLengthTable[t]; |
| | 58109 | 977 | | t = v >> 16; |
| | 58109 | 978 | | if (t != 0) |
| | 14307 | 979 | | return 16 + BitLengthTable[t]; |
| | 43802 | 980 | | t = v >> 8; |
| | 43802 | 981 | | if (t != 0) |
| | 33600 | 982 | | return 8 + BitLengthTable[t]; |
| | 10202 | 983 | | return BitLengthTable[v]; |
| | 142542 | 984 | | } |
| | | 985 | | |
| | | 986 | | private bool QuickPow2Check() |
| | 321615 | 987 | | { |
| | 321615 | 988 | | return sign > 0 && nBits == 1; |
| | 321615 | 989 | | } |
| | | 990 | | |
| | | 991 | | public int CompareTo( |
| | | 992 | | object obj) |
| | 0 | 993 | | { |
| | 0 | 994 | | return CompareTo((BigInteger)obj); |
| | 0 | 995 | | } |
| | | 996 | | |
| | | 997 | | /** |
| | | 998 | | * unsigned comparison on two arrays - note the arrays may |
| | | 999 | | * start with leading zeros. |
| | | 1000 | | */ |
| | | 1001 | | private static int CompareTo( |
| | | 1002 | | int xIndx, |
| | | 1003 | | int[] x, |
| | | 1004 | | int yIndx, |
| | | 1005 | | int[] y) |
| | 0 | 1006 | | { |
| | 0 | 1007 | | while (xIndx != x.Length && x[xIndx] == 0) |
| | 0 | 1008 | | { |
| | 0 | 1009 | | xIndx++; |
| | 0 | 1010 | | } |
| | | 1011 | | |
| | 0 | 1012 | | while (yIndx != y.Length && y[yIndx] == 0) |
| | 0 | 1013 | | { |
| | 0 | 1014 | | yIndx++; |
| | 0 | 1015 | | } |
| | | 1016 | | |
| | 0 | 1017 | | return CompareNoLeadingZeroes(xIndx, x, yIndx, y); |
| | 0 | 1018 | | } |
| | | 1019 | | |
| | | 1020 | | private static int CompareNoLeadingZeroes( |
| | | 1021 | | int xIndx, |
| | | 1022 | | int[] x, |
| | | 1023 | | int yIndx, |
| | | 1024 | | int[] y) |
| | 404654 | 1025 | | { |
| | 404654 | 1026 | | int diff = (x.Length - y.Length) - (xIndx - yIndx); |
| | | 1027 | | |
| | 404654 | 1028 | | if (diff != 0) |
| | 117271 | 1029 | | { |
| | 117271 | 1030 | | return diff < 0 ? -1 : 1; |
| | | 1031 | | } |
| | | 1032 | | |
| | | 1033 | | // lengths of magnitudes the same, test the magnitude values |
| | | 1034 | | |
| | 311270 | 1035 | | while (xIndx < x.Length) |
| | 311270 | 1036 | | { |
| | 311270 | 1037 | | uint v1 = (uint)x[xIndx++]; |
| | 311270 | 1038 | | uint v2 = (uint)y[yIndx++]; |
| | | 1039 | | |
| | 311270 | 1040 | | if (v1 != v2) |
| | 287383 | 1041 | | return v1 < v2 ? -1 : 1; |
| | 23887 | 1042 | | } |
| | | 1043 | | |
| | 0 | 1044 | | return 0; |
| | 404654 | 1045 | | } |
| | | 1046 | | |
| | | 1047 | | public int CompareTo( |
| | | 1048 | | BigInteger value) |
| | 235413 | 1049 | | { |
| | 235413 | 1050 | | return sign < value.sign ? -1 |
| | 235413 | 1051 | | : sign > value.sign ? 1 |
| | 235413 | 1052 | | : sign == 0 ? 0 |
| | 235413 | 1053 | | : sign * CompareNoLeadingZeroes(0, magnitude, 0, value.magnitude); |
| | 235413 | 1054 | | } |
| | | 1055 | | |
| | | 1056 | | /** |
| | | 1057 | | * return z = x / y - done in place (z value preserved, x contains the |
| | | 1058 | | * remainder) |
| | | 1059 | | */ |
| | | 1060 | | private int[] Divide( |
| | | 1061 | | int[] x, |
| | | 1062 | | int[] y) |
| | 1 | 1063 | | { |
| | 1 | 1064 | | int xStart = 0; |
| | 1 | 1065 | | while (xStart < x.Length && x[xStart] == 0) |
| | 0 | 1066 | | { |
| | 0 | 1067 | | ++xStart; |
| | 0 | 1068 | | } |
| | | 1069 | | |
| | 1 | 1070 | | int yStart = 0; |
| | 1 | 1071 | | while (yStart < y.Length && y[yStart] == 0) |
| | 0 | 1072 | | { |
| | 0 | 1073 | | ++yStart; |
| | 0 | 1074 | | } |
| | | 1075 | | |
| | 1 | 1076 | | Debug.Assert(yStart < y.Length); |
| | | 1077 | | |
| | 1 | 1078 | | int xyCmp = CompareNoLeadingZeroes(xStart, x, yStart, y); |
| | | 1079 | | int[] count; |
| | | 1080 | | |
| | 1 | 1081 | | if (xyCmp > 0) |
| | 1 | 1082 | | { |
| | 1 | 1083 | | int yBitLength = CalcBitLength(1, yStart, y); |
| | 1 | 1084 | | int xBitLength = CalcBitLength(1, xStart, x); |
| | 1 | 1085 | | int shift = xBitLength - yBitLength; |
| | | 1086 | | |
| | | 1087 | | int[] iCount; |
| | 1 | 1088 | | int iCountStart = 0; |
| | | 1089 | | |
| | | 1090 | | int[] c; |
| | 1 | 1091 | | int cStart = 0; |
| | 1 | 1092 | | int cBitLength = yBitLength; |
| | 1 | 1093 | | if (shift > 0) |
| | 1 | 1094 | | { |
| | | 1095 | | // iCount = ShiftLeft(One.magnitude, shift); |
| | 1 | 1096 | | iCount = new int[(shift >> 5) + 1]; |
| | 1 | 1097 | | iCount[0] = 1 << (shift % 32); |
| | | 1098 | | |
| | 1 | 1099 | | c = ShiftLeft(y, shift); |
| | 1 | 1100 | | cBitLength += shift; |
| | 1 | 1101 | | } |
| | | 1102 | | else |
| | 0 | 1103 | | { |
| | 0 | 1104 | | iCount = new int[] { 1 }; |
| | | 1105 | | |
| | 0 | 1106 | | int len = y.Length - yStart; |
| | 0 | 1107 | | c = new int[len]; |
| | 0 | 1108 | | Array.Copy(y, yStart, c, 0, len); |
| | 0 | 1109 | | } |
| | | 1110 | | |
| | 1 | 1111 | | count = new int[iCount.Length]; |
| | | 1112 | | |
| | | 1113 | | for (;;) |
| | 166 | 1114 | | { |
| | 166 | 1115 | | if (cBitLength < xBitLength |
| | 166 | 1116 | | || CompareNoLeadingZeroes(xStart, x, cStart, c) >= 0) |
| | 160 | 1117 | | { |
| | 160 | 1118 | | Subtract(xStart, x, cStart, c); |
| | 160 | 1119 | | AddMagnitudes(count, iCount); |
| | | 1120 | | |
| | 169 | 1121 | | while (x[xStart] == 0) |
| | 9 | 1122 | | { |
| | 9 | 1123 | | if (++xStart == x.Length) |
| | 0 | 1124 | | return count; |
| | 9 | 1125 | | } |
| | | 1126 | | |
| | | 1127 | | //xBitLength = CalcBitLength(xStart, x); |
| | 160 | 1128 | | xBitLength = 32 * (x.Length - xStart - 1) + BitLen(x[xStart]); |
| | | 1129 | | |
| | 160 | 1130 | | if (xBitLength <= yBitLength) |
| | 1 | 1131 | | { |
| | 1 | 1132 | | if (xBitLength < yBitLength) |
| | 1 | 1133 | | return count; |
| | | 1134 | | |
| | 0 | 1135 | | xyCmp = CompareNoLeadingZeroes(xStart, x, yStart, y); |
| | | 1136 | | |
| | 0 | 1137 | | if (xyCmp <= 0) |
| | 0 | 1138 | | break; |
| | 0 | 1139 | | } |
| | 159 | 1140 | | } |
| | | 1141 | | |
| | 165 | 1142 | | shift = cBitLength - xBitLength; |
| | | 1143 | | |
| | | 1144 | | // NB: The case where c[cStart] is 1-bit is harmless |
| | 165 | 1145 | | if (shift == 1) |
| | 4 | 1146 | | { |
| | 4 | 1147 | | uint firstC = (uint) c[cStart] >> 1; |
| | 4 | 1148 | | uint firstX = (uint) x[xStart]; |
| | 4 | 1149 | | if (firstC > firstX) |
| | 0 | 1150 | | ++shift; |
| | 4 | 1151 | | } |
| | | 1152 | | |
| | 165 | 1153 | | if (shift < 2) |
| | 163 | 1154 | | { |
| | 163 | 1155 | | ShiftRightOneInPlace(cStart, c); |
| | 163 | 1156 | | --cBitLength; |
| | 163 | 1157 | | ShiftRightOneInPlace(iCountStart, iCount); |
| | 163 | 1158 | | } |
| | | 1159 | | else |
| | 2 | 1160 | | { |
| | 2 | 1161 | | ShiftRightInPlace(cStart, c, shift); |
| | 2 | 1162 | | cBitLength -= shift; |
| | 2 | 1163 | | ShiftRightInPlace(iCountStart, iCount, shift); |
| | 2 | 1164 | | } |
| | | 1165 | | |
| | | 1166 | | //cStart = c.Length - ((cBitLength + 31) / 32); |
| | 174 | 1167 | | while (c[cStart] == 0) |
| | 9 | 1168 | | { |
| | 9 | 1169 | | ++cStart; |
| | 9 | 1170 | | } |
| | | 1171 | | |
| | 173 | 1172 | | while (iCount[iCountStart] == 0) |
| | 8 | 1173 | | { |
| | 8 | 1174 | | ++iCountStart; |
| | 8 | 1175 | | } |
| | 165 | 1176 | | } |
| | 0 | 1177 | | } |
| | | 1178 | | else |
| | 0 | 1179 | | { |
| | 0 | 1180 | | count = new int[1]; |
| | 0 | 1181 | | } |
| | | 1182 | | |
| | 0 | 1183 | | if (xyCmp == 0) |
| | 0 | 1184 | | { |
| | 0 | 1185 | | AddMagnitudes(count, One.magnitude); |
| | 0 | 1186 | | Array.Clear(x, xStart, x.Length - xStart); |
| | 0 | 1187 | | } |
| | | 1188 | | |
| | 0 | 1189 | | return count; |
| | 1 | 1190 | | } |
| | | 1191 | | |
| | | 1192 | | public BigInteger Divide( |
| | | 1193 | | BigInteger val) |
| | 1 | 1194 | | { |
| | 1 | 1195 | | if (val.sign == 0) |
| | 0 | 1196 | | throw new ArithmeticException("Division by zero error"); |
| | | 1197 | | |
| | 1 | 1198 | | if (sign == 0) |
| | 0 | 1199 | | return Zero; |
| | | 1200 | | |
| | 1 | 1201 | | if (val.QuickPow2Check()) // val is power of two |
| | 0 | 1202 | | { |
| | 0 | 1203 | | BigInteger result = this.Abs().ShiftRight(val.Abs().BitLength - 1); |
| | 0 | 1204 | | return val.sign == this.sign ? result : result.Negate(); |
| | | 1205 | | } |
| | | 1206 | | |
| | 1 | 1207 | | int[] mag = (int[]) this.magnitude.Clone(); |
| | | 1208 | | |
| | 1 | 1209 | | return new BigInteger(this.sign * val.sign, Divide(mag, val.magnitude), true); |
| | 1 | 1210 | | } |
| | | 1211 | | |
| | | 1212 | | public BigInteger[] DivideAndRemainder( |
| | | 1213 | | BigInteger val) |
| | 0 | 1214 | | { |
| | 0 | 1215 | | if (val.sign == 0) |
| | 0 | 1216 | | throw new ArithmeticException("Division by zero error"); |
| | | 1217 | | |
| | 0 | 1218 | | BigInteger[] biggies = new BigInteger[2]; |
| | | 1219 | | |
| | 0 | 1220 | | if (sign == 0) |
| | 0 | 1221 | | { |
| | 0 | 1222 | | biggies[0] = Zero; |
| | 0 | 1223 | | biggies[1] = Zero; |
| | 0 | 1224 | | } |
| | 0 | 1225 | | else if (val.QuickPow2Check()) // val is power of two |
| | 0 | 1226 | | { |
| | 0 | 1227 | | int e = val.Abs().BitLength - 1; |
| | 0 | 1228 | | BigInteger quotient = this.Abs().ShiftRight(e); |
| | 0 | 1229 | | int[] remainder = this.LastNBits(e); |
| | | 1230 | | |
| | 0 | 1231 | | biggies[0] = val.sign == this.sign ? quotient : quotient.Negate(); |
| | 0 | 1232 | | biggies[1] = new BigInteger(this.sign, remainder, true); |
| | 0 | 1233 | | } |
| | | 1234 | | else |
| | 0 | 1235 | | { |
| | 0 | 1236 | | int[] remainder = (int[]) this.magnitude.Clone(); |
| | 0 | 1237 | | int[] quotient = Divide(remainder, val.magnitude); |
| | | 1238 | | |
| | 0 | 1239 | | biggies[0] = new BigInteger(this.sign * val.sign, quotient, true); |
| | 0 | 1240 | | biggies[1] = new BigInteger(this.sign, remainder, true); |
| | 0 | 1241 | | } |
| | | 1242 | | |
| | 0 | 1243 | | return biggies; |
| | 0 | 1244 | | } |
| | | 1245 | | |
| | | 1246 | | public override bool Equals( |
| | | 1247 | | object obj) |
| | 47171 | 1248 | | { |
| | 47171 | 1249 | | if (obj == this) |
| | 30 | 1250 | | return true; |
| | | 1251 | | |
| | 47141 | 1252 | | BigInteger biggie = obj as BigInteger; |
| | 47141 | 1253 | | if (biggie == null) |
| | 0 | 1254 | | return false; |
| | | 1255 | | |
| | 47141 | 1256 | | return sign == biggie.sign && IsEqualMagnitude(biggie); |
| | 47171 | 1257 | | } |
| | | 1258 | | |
| | | 1259 | | private bool IsEqualMagnitude(BigInteger x) |
| | 47141 | 1260 | | { |
| | 47141 | 1261 | | int[] xMag = x.magnitude; |
| | 47141 | 1262 | | if (magnitude.Length != x.magnitude.Length) |
| | 21104 | 1263 | | return false; |
| | 105236 | 1264 | | for (int i = 0; i < magnitude.Length; i++) |
| | 26581 | 1265 | | { |
| | 26581 | 1266 | | if (magnitude[i] != x.magnitude[i]) |
| | 0 | 1267 | | return false; |
| | 26581 | 1268 | | } |
| | 26037 | 1269 | | return true; |
| | 47141 | 1270 | | } |
| | | 1271 | | |
| | | 1272 | | public BigInteger Gcd( |
| | | 1273 | | BigInteger value) |
| | 0 | 1274 | | { |
| | 0 | 1275 | | if (value.sign == 0) |
| | 0 | 1276 | | return Abs(); |
| | | 1277 | | |
| | 0 | 1278 | | if (sign == 0) |
| | 0 | 1279 | | return value.Abs(); |
| | | 1280 | | |
| | | 1281 | | BigInteger r; |
| | 0 | 1282 | | BigInteger u = this; |
| | 0 | 1283 | | BigInteger v = value; |
| | | 1284 | | |
| | 0 | 1285 | | while (v.sign != 0) |
| | 0 | 1286 | | { |
| | 0 | 1287 | | r = u.Mod(v); |
| | 0 | 1288 | | u = v; |
| | 0 | 1289 | | v = r; |
| | 0 | 1290 | | } |
| | | 1291 | | |
| | 0 | 1292 | | return u; |
| | 0 | 1293 | | } |
| | | 1294 | | |
| | | 1295 | | public override int GetHashCode() |
| | 0 | 1296 | | { |
| | 0 | 1297 | | int hc = magnitude.Length; |
| | 0 | 1298 | | if (magnitude.Length > 0) |
| | 0 | 1299 | | { |
| | 0 | 1300 | | hc ^= magnitude[0]; |
| | | 1301 | | |
| | 0 | 1302 | | if (magnitude.Length > 1) |
| | 0 | 1303 | | { |
| | 0 | 1304 | | hc ^= magnitude[magnitude.Length - 1]; |
| | 0 | 1305 | | } |
| | 0 | 1306 | | } |
| | | 1307 | | |
| | 0 | 1308 | | return sign < 0 ? ~hc : hc; |
| | 0 | 1309 | | } |
| | | 1310 | | |
| | | 1311 | | // TODO Make public? |
| | | 1312 | | private BigInteger Inc() |
| | 0 | 1313 | | { |
| | 0 | 1314 | | if (this.sign == 0) |
| | 0 | 1315 | | return One; |
| | | 1316 | | |
| | 0 | 1317 | | if (this.sign < 0) |
| | 0 | 1318 | | return new BigInteger(-1, doSubBigLil(this.magnitude, One.magnitude), true); |
| | | 1319 | | |
| | 0 | 1320 | | return AddToMagnitude(One.magnitude); |
| | 0 | 1321 | | } |
| | | 1322 | | |
| | | 1323 | | public int IntValue |
| | | 1324 | | { |
| | | 1325 | | get |
| | 1446 | 1326 | | { |
| | 1446 | 1327 | | if (sign == 0) |
| | 2 | 1328 | | return 0; |
| | | 1329 | | |
| | 1444 | 1330 | | int n = magnitude.Length; |
| | | 1331 | | |
| | 1444 | 1332 | | int v = magnitude[n - 1]; |
| | | 1333 | | |
| | 1444 | 1334 | | return sign < 0 ? -v : v; |
| | 1446 | 1335 | | } |
| | | 1336 | | } |
| | | 1337 | | |
| | | 1338 | | /** |
| | | 1339 | | * return whether or not a BigInteger is probably prime with a |
| | | 1340 | | * probability of 1 - (1/2)**certainty. |
| | | 1341 | | * <p>From Knuth Vol 2, pg 395.</p> |
| | | 1342 | | */ |
| | | 1343 | | public bool IsProbablePrime(int certainty) |
| | 0 | 1344 | | { |
| | 0 | 1345 | | return IsProbablePrime(certainty, false); |
| | 0 | 1346 | | } |
| | | 1347 | | |
| | | 1348 | | internal bool IsProbablePrime(int certainty, bool randomlySelected) |
| | 0 | 1349 | | { |
| | 0 | 1350 | | if (certainty <= 0) |
| | 0 | 1351 | | return true; |
| | | 1352 | | |
| | 0 | 1353 | | BigInteger n = Abs(); |
| | | 1354 | | |
| | 0 | 1355 | | if (!n.TestBit(0)) |
| | 0 | 1356 | | return n.Equals(Two); |
| | | 1357 | | |
| | 0 | 1358 | | if (n.Equals(One)) |
| | 0 | 1359 | | return false; |
| | | 1360 | | |
| | 0 | 1361 | | return n.CheckProbablePrime(certainty, RandomSource, randomlySelected); |
| | 0 | 1362 | | } |
| | | 1363 | | |
| | | 1364 | | private bool CheckProbablePrime(int certainty, Random random, bool randomlySelected) |
| | 0 | 1365 | | { |
| | 0 | 1366 | | Debug.Assert(certainty > 0); |
| | 0 | 1367 | | Debug.Assert(CompareTo(Two) > 0); |
| | 0 | 1368 | | Debug.Assert(TestBit(0)); |
| | | 1369 | | |
| | | 1370 | | |
| | | 1371 | | // Try to reduce the penalty for really small numbers |
| | 0 | 1372 | | int numLists = System.Math.Min(BitLength - 1, primeLists.Length); |
| | | 1373 | | |
| | 0 | 1374 | | for (int i = 0; i < numLists; ++i) |
| | 0 | 1375 | | { |
| | 0 | 1376 | | int test = Remainder(primeProducts[i]); |
| | | 1377 | | |
| | 0 | 1378 | | int[] primeList = primeLists[i]; |
| | 0 | 1379 | | for (int j = 0; j < primeList.Length; ++j) |
| | 0 | 1380 | | { |
| | 0 | 1381 | | int prime = primeList[j]; |
| | 0 | 1382 | | int qRem = test % prime; |
| | 0 | 1383 | | if (qRem == 0) |
| | 0 | 1384 | | { |
| | | 1385 | | // We may find small numbers in the list |
| | 0 | 1386 | | return BitLength < 16 && IntValue == prime; |
| | | 1387 | | } |
| | 0 | 1388 | | } |
| | 0 | 1389 | | } |
| | | 1390 | | |
| | | 1391 | | |
| | | 1392 | | // TODO Special case for < 10^16 (RabinMiller fixed list) |
| | | 1393 | | // if (BitLength < 30) |
| | | 1394 | | // { |
| | | 1395 | | // RabinMiller against 2, 3, 5, 7, 11, 13, 23 is sufficient |
| | | 1396 | | // } |
| | | 1397 | | |
| | | 1398 | | |
| | | 1399 | | // TODO Is it worth trying to create a hybrid of these two? |
| | 0 | 1400 | | return RabinMillerTest(certainty, random, randomlySelected); |
| | | 1401 | | // return SolovayStrassenTest(certainty, random); |
| | | 1402 | | |
| | | 1403 | | // bool rbTest = RabinMillerTest(certainty, random); |
| | | 1404 | | // bool ssTest = SolovayStrassenTest(certainty, random); |
| | | 1405 | | // |
| | | 1406 | | // Debug.Assert(rbTest == ssTest); |
| | | 1407 | | // |
| | | 1408 | | // return rbTest; |
| | 0 | 1409 | | } |
| | | 1410 | | |
| | | 1411 | | public bool RabinMillerTest(int certainty, Random random) |
| | 0 | 1412 | | { |
| | 0 | 1413 | | return RabinMillerTest(certainty, random, false); |
| | 0 | 1414 | | } |
| | | 1415 | | |
| | | 1416 | | internal bool RabinMillerTest(int certainty, Random random, bool randomlySelected) |
| | 0 | 1417 | | { |
| | 0 | 1418 | | int bits = BitLength; |
| | | 1419 | | |
| | 0 | 1420 | | Debug.Assert(certainty > 0); |
| | 0 | 1421 | | Debug.Assert(bits > 2); |
| | 0 | 1422 | | Debug.Assert(TestBit(0)); |
| | | 1423 | | |
| | 0 | 1424 | | int iterations = ((certainty - 1) / 2) + 1; |
| | 0 | 1425 | | if (randomlySelected) |
| | 0 | 1426 | | { |
| | 0 | 1427 | | int itersFor100Cert = bits >= 1024 ? 4 |
| | 0 | 1428 | | : bits >= 512 ? 8 |
| | 0 | 1429 | | : bits >= 256 ? 16 |
| | 0 | 1430 | | : 50; |
| | | 1431 | | |
| | 0 | 1432 | | if (certainty < 100) |
| | 0 | 1433 | | { |
| | 0 | 1434 | | iterations = System.Math.Min(itersFor100Cert, iterations); |
| | 0 | 1435 | | } |
| | | 1436 | | else |
| | 0 | 1437 | | { |
| | 0 | 1438 | | iterations -= 50; |
| | 0 | 1439 | | iterations += itersFor100Cert; |
| | 0 | 1440 | | } |
| | 0 | 1441 | | } |
| | | 1442 | | |
| | | 1443 | | // let n = 1 + d . 2^s |
| | 0 | 1444 | | BigInteger n = this; |
| | 0 | 1445 | | int s = n.GetLowestSetBitMaskFirst(-1 << 1); |
| | 0 | 1446 | | Debug.Assert(s >= 1); |
| | 0 | 1447 | | BigInteger r = n.ShiftRight(s); |
| | | 1448 | | |
| | | 1449 | | // NOTE: Avoid conversion to/from Montgomery form and check for R/-R as result instead |
| | | 1450 | | |
| | 0 | 1451 | | BigInteger montRadix = One.ShiftLeft(32 * n.magnitude.Length).Remainder(n); |
| | 0 | 1452 | | BigInteger minusMontRadix = n.Subtract(montRadix); |
| | | 1453 | | |
| | | 1454 | | do |
| | 0 | 1455 | | { |
| | | 1456 | | BigInteger a; |
| | | 1457 | | do |
| | 0 | 1458 | | { |
| | 0 | 1459 | | a = new BigInteger(n.BitLength, random); |
| | 0 | 1460 | | } |
| | 0 | 1461 | | while (a.sign == 0 || a.CompareTo(n) >= 0 |
| | 0 | 1462 | | || a.IsEqualMagnitude(montRadix) || a.IsEqualMagnitude(minusMontRadix)); |
| | | 1463 | | |
| | 0 | 1464 | | BigInteger y = ModPowMonty(a, r, n, false); |
| | | 1465 | | |
| | 0 | 1466 | | if (!y.Equals(montRadix)) |
| | 0 | 1467 | | { |
| | 0 | 1468 | | int j = 0; |
| | 0 | 1469 | | while (!y.Equals(minusMontRadix)) |
| | 0 | 1470 | | { |
| | 0 | 1471 | | if (++j == s) |
| | 0 | 1472 | | return false; |
| | | 1473 | | |
| | 0 | 1474 | | y = ModPowMonty(y, Two, n, false); |
| | | 1475 | | |
| | 0 | 1476 | | if (y.Equals(montRadix)) |
| | 0 | 1477 | | return false; |
| | 0 | 1478 | | } |
| | 0 | 1479 | | } |
| | 0 | 1480 | | } |
| | 0 | 1481 | | while (--iterations > 0); |
| | | 1482 | | |
| | 0 | 1483 | | return true; |
| | 0 | 1484 | | } |
| | | 1485 | | |
| | | 1486 | | // private bool SolovayStrassenTest( |
| | | 1487 | | // int certainty, |
| | | 1488 | | // Random random) |
| | | 1489 | | // { |
| | | 1490 | | // Debug.Assert(certainty > 0); |
| | | 1491 | | // Debug.Assert(CompareTo(Two) > 0); |
| | | 1492 | | // Debug.Assert(TestBit(0)); |
| | | 1493 | | // |
| | | 1494 | | // BigInteger n = this; |
| | | 1495 | | // BigInteger nMinusOne = n.Subtract(One); |
| | | 1496 | | // BigInteger e = nMinusOne.ShiftRight(1); |
| | | 1497 | | // |
| | | 1498 | | // do |
| | | 1499 | | // { |
| | | 1500 | | // BigInteger a; |
| | | 1501 | | // do |
| | | 1502 | | // { |
| | | 1503 | | // a = new BigInteger(nBitLength, random); |
| | | 1504 | | // } |
| | | 1505 | | // // NB: Spec says 0 < x < n, but 1 is trivial |
| | | 1506 | | // while (a.CompareTo(One) <= 0 || a.CompareTo(n) >= 0); |
| | | 1507 | | // |
| | | 1508 | | // |
| | | 1509 | | // // TODO Check this is redundant given the way Jacobi() works? |
| | | 1510 | | //// if (!a.Gcd(n).Equals(One)) |
| | | 1511 | | //// return false; |
| | | 1512 | | // |
| | | 1513 | | // int x = Jacobi(a, n); |
| | | 1514 | | // |
| | | 1515 | | // if (x == 0) |
| | | 1516 | | // return false; |
| | | 1517 | | // |
| | | 1518 | | // BigInteger check = a.ModPow(e, n); |
| | | 1519 | | // |
| | | 1520 | | // if (x == 1 && !check.Equals(One)) |
| | | 1521 | | // return false; |
| | | 1522 | | // |
| | | 1523 | | // if (x == -1 && !check.Equals(nMinusOne)) |
| | | 1524 | | // return false; |
| | | 1525 | | // |
| | | 1526 | | // --certainty; |
| | | 1527 | | // } |
| | | 1528 | | // while (certainty > 0); |
| | | 1529 | | // |
| | | 1530 | | // return true; |
| | | 1531 | | // } |
| | | 1532 | | // |
| | | 1533 | | // private static int Jacobi( |
| | | 1534 | | // BigInteger a, |
| | | 1535 | | // BigInteger b) |
| | | 1536 | | // { |
| | | 1537 | | // Debug.Assert(a.sign >= 0); |
| | | 1538 | | // Debug.Assert(b.sign > 0); |
| | | 1539 | | // Debug.Assert(b.TestBit(0)); |
| | | 1540 | | // Debug.Assert(a.CompareTo(b) < 0); |
| | | 1541 | | // |
| | | 1542 | | // int totalS = 1; |
| | | 1543 | | // for (;;) |
| | | 1544 | | // { |
| | | 1545 | | // if (a.sign == 0) |
| | | 1546 | | // return 0; |
| | | 1547 | | // |
| | | 1548 | | // if (a.Equals(One)) |
| | | 1549 | | // break; |
| | | 1550 | | // |
| | | 1551 | | // int e = a.GetLowestSetBit(); |
| | | 1552 | | // |
| | | 1553 | | // int bLsw = b.magnitude[b.magnitude.Length - 1]; |
| | | 1554 | | // if ((e & 1) != 0 && ((bLsw & 7) == 3 || (bLsw & 7) == 5)) |
| | | 1555 | | // totalS = -totalS; |
| | | 1556 | | // |
| | | 1557 | | // // TODO Confirm this is faster than later a1.Equals(One) test |
| | | 1558 | | // if (a.BitLength == e + 1) |
| | | 1559 | | // break; |
| | | 1560 | | // BigInteger a1 = a.ShiftRight(e); |
| | | 1561 | | //// if (a1.Equals(One)) |
| | | 1562 | | //// break; |
| | | 1563 | | // |
| | | 1564 | | // int a1Lsw = a1.magnitude[a1.magnitude.Length - 1]; |
| | | 1565 | | // if ((bLsw & 3) == 3 && (a1Lsw & 3) == 3) |
| | | 1566 | | // totalS = -totalS; |
| | | 1567 | | // |
| | | 1568 | | //// a = b.Mod(a1); |
| | | 1569 | | // a = b.Remainder(a1); |
| | | 1570 | | // b = a1; |
| | | 1571 | | // } |
| | | 1572 | | // return totalS; |
| | | 1573 | | // } |
| | | 1574 | | |
| | | 1575 | | public long LongValue |
| | | 1576 | | { |
| | | 1577 | | get |
| | 3 | 1578 | | { |
| | 3 | 1579 | | if (sign == 0) |
| | 0 | 1580 | | return 0; |
| | | 1581 | | |
| | 3 | 1582 | | int n = magnitude.Length; |
| | | 1583 | | |
| | 3 | 1584 | | long v = magnitude[n - 1] & IMASK; |
| | 3 | 1585 | | if (n > 1) |
| | 3 | 1586 | | { |
| | 3 | 1587 | | v |= (magnitude[n - 2] & IMASK) << 32; |
| | 3 | 1588 | | } |
| | | 1589 | | |
| | 3 | 1590 | | return sign < 0 ? -v : v; |
| | 3 | 1591 | | } |
| | | 1592 | | } |
| | | 1593 | | |
| | | 1594 | | public BigInteger Max( |
| | | 1595 | | BigInteger value) |
| | 0 | 1596 | | { |
| | 0 | 1597 | | return CompareTo(value) > 0 ? this : value; |
| | 0 | 1598 | | } |
| | | 1599 | | |
| | | 1600 | | public BigInteger Min( |
| | | 1601 | | BigInteger value) |
| | 0 | 1602 | | { |
| | 0 | 1603 | | return CompareTo(value) < 0 ? this : value; |
| | 0 | 1604 | | } |
| | | 1605 | | |
| | | 1606 | | public BigInteger Mod( |
| | | 1607 | | BigInteger m) |
| | 9 | 1608 | | { |
| | 9 | 1609 | | if (m.sign < 1) |
| | 0 | 1610 | | throw new ArithmeticException("Modulus must be positive"); |
| | | 1611 | | |
| | 9 | 1612 | | BigInteger biggie = Remainder(m); |
| | | 1613 | | |
| | 9 | 1614 | | return (biggie.sign >= 0 ? biggie : biggie.Add(m)); |
| | 9 | 1615 | | } |
| | | 1616 | | |
| | | 1617 | | public BigInteger ModInverse( |
| | | 1618 | | BigInteger m) |
| | 0 | 1619 | | { |
| | 0 | 1620 | | if (m.sign < 1) |
| | 0 | 1621 | | throw new ArithmeticException("Modulus must be positive"); |
| | | 1622 | | |
| | | 1623 | | // TODO Too slow at the moment |
| | | 1624 | | // // "Fast Key Exchange with Elliptic Curve Systems" R.Schoeppel |
| | | 1625 | | // if (m.TestBit(0)) |
| | | 1626 | | // { |
| | | 1627 | | // //The Almost Inverse Algorithm |
| | | 1628 | | // int k = 0; |
| | | 1629 | | // BigInteger B = One, C = Zero, F = this, G = m, tmp; |
| | | 1630 | | // |
| | | 1631 | | // for (;;) |
| | | 1632 | | // { |
| | | 1633 | | // // While F is even, do F=F/u, C=C*u, k=k+1. |
| | | 1634 | | // int zeroes = F.GetLowestSetBit(); |
| | | 1635 | | // if (zeroes > 0) |
| | | 1636 | | // { |
| | | 1637 | | // F = F.ShiftRight(zeroes); |
| | | 1638 | | // C = C.ShiftLeft(zeroes); |
| | | 1639 | | // k += zeroes; |
| | | 1640 | | // } |
| | | 1641 | | // |
| | | 1642 | | // // If F = 1, then return B,k. |
| | | 1643 | | // if (F.Equals(One)) |
| | | 1644 | | // { |
| | | 1645 | | // BigInteger half = m.Add(One).ShiftRight(1); |
| | | 1646 | | // BigInteger halfK = half.ModPow(BigInteger.ValueOf(k), m); |
| | | 1647 | | // return B.Multiply(halfK).Mod(m); |
| | | 1648 | | // } |
| | | 1649 | | // |
| | | 1650 | | // if (F.CompareTo(G) < 0) |
| | | 1651 | | // { |
| | | 1652 | | // tmp = G; G = F; F = tmp; |
| | | 1653 | | // tmp = B; B = C; C = tmp; |
| | | 1654 | | // } |
| | | 1655 | | // |
| | | 1656 | | // F = F.Add(G); |
| | | 1657 | | // B = B.Add(C); |
| | | 1658 | | // } |
| | | 1659 | | // } |
| | | 1660 | | |
| | 0 | 1661 | | if (m.QuickPow2Check()) |
| | 0 | 1662 | | { |
| | 0 | 1663 | | return ModInversePow2(m); |
| | | 1664 | | } |
| | | 1665 | | |
| | 0 | 1666 | | BigInteger d = this.Remainder(m); |
| | | 1667 | | BigInteger x; |
| | 0 | 1668 | | BigInteger gcd = ExtEuclid(d, m, out x); |
| | | 1669 | | |
| | 0 | 1670 | | if (!gcd.Equals(One)) |
| | 0 | 1671 | | throw new ArithmeticException("Numbers not relatively prime."); |
| | | 1672 | | |
| | 0 | 1673 | | if (x.sign < 0) |
| | 0 | 1674 | | { |
| | 0 | 1675 | | x = x.Add(m); |
| | 0 | 1676 | | } |
| | | 1677 | | |
| | 0 | 1678 | | return x; |
| | 0 | 1679 | | } |
| | | 1680 | | |
| | | 1681 | | private BigInteger ModInversePow2(BigInteger m) |
| | 0 | 1682 | | { |
| | 0 | 1683 | | Debug.Assert(m.SignValue > 0); |
| | 0 | 1684 | | Debug.Assert(m.BitCount == 1); |
| | | 1685 | | |
| | 0 | 1686 | | if (!TestBit(0)) |
| | 0 | 1687 | | { |
| | 0 | 1688 | | throw new ArithmeticException("Numbers not relatively prime."); |
| | | 1689 | | } |
| | | 1690 | | |
| | 0 | 1691 | | int pow = m.BitLength - 1; |
| | | 1692 | | |
| | 0 | 1693 | | long inv64 = ModInverse64(LongValue); |
| | 0 | 1694 | | if (pow < 64) |
| | 0 | 1695 | | { |
| | 0 | 1696 | | inv64 &= ((1L << pow) - 1); |
| | 0 | 1697 | | } |
| | | 1698 | | |
| | 0 | 1699 | | BigInteger x = BigInteger.ValueOf(inv64); |
| | | 1700 | | |
| | 0 | 1701 | | if (pow > 64) |
| | 0 | 1702 | | { |
| | 0 | 1703 | | BigInteger d = this.Remainder(m); |
| | 0 | 1704 | | int bitsCorrect = 64; |
| | | 1705 | | |
| | | 1706 | | do |
| | 0 | 1707 | | { |
| | 0 | 1708 | | BigInteger t = x.Multiply(d).Remainder(m); |
| | 0 | 1709 | | x = x.Multiply(Two.Subtract(t)).Remainder(m); |
| | 0 | 1710 | | bitsCorrect <<= 1; |
| | 0 | 1711 | | } |
| | 0 | 1712 | | while (bitsCorrect < pow); |
| | 0 | 1713 | | } |
| | | 1714 | | |
| | 0 | 1715 | | if (x.sign < 0) |
| | 0 | 1716 | | { |
| | 0 | 1717 | | x = x.Add(m); |
| | 0 | 1718 | | } |
| | | 1719 | | |
| | 0 | 1720 | | return x; |
| | 0 | 1721 | | } |
| | | 1722 | | |
| | | 1723 | | private static int ModInverse32(int d) |
| | 0 | 1724 | | { |
| | | 1725 | | // Newton's method with initial estimate "correct to 4 bits" |
| | 0 | 1726 | | Debug.Assert((d & 1) != 0); |
| | 0 | 1727 | | int x = d + (((d + 1) & 4) << 1); // d.x == 1 mod 2**4 |
| | 0 | 1728 | | Debug.Assert(((d * x) & 15) == 1); |
| | 0 | 1729 | | x *= 2 - d * x; // d.x == 1 mod 2**8 |
| | 0 | 1730 | | x *= 2 - d * x; // d.x == 1 mod 2**16 |
| | 0 | 1731 | | x *= 2 - d * x; // d.x == 1 mod 2**32 |
| | 0 | 1732 | | Debug.Assert(d * x == 1); |
| | 0 | 1733 | | return x; |
| | 0 | 1734 | | } |
| | | 1735 | | |
| | | 1736 | | private static long ModInverse64(long d) |
| | 0 | 1737 | | { |
| | | 1738 | | // Newton's method with initial estimate "correct to 4 bits" |
| | 0 | 1739 | | Debug.Assert((d & 1L) != 0); |
| | 0 | 1740 | | long x = d + (((d + 1L) & 4L) << 1); // d.x == 1 mod 2**4 |
| | 0 | 1741 | | Debug.Assert(((d * x) & 15L) == 1L); |
| | 0 | 1742 | | x *= 2 - d * x; // d.x == 1 mod 2**8 |
| | 0 | 1743 | | x *= 2 - d * x; // d.x == 1 mod 2**16 |
| | 0 | 1744 | | x *= 2 - d * x; // d.x == 1 mod 2**32 |
| | 0 | 1745 | | x *= 2 - d * x; // d.x == 1 mod 2**64 |
| | 0 | 1746 | | Debug.Assert(d * x == 1L); |
| | 0 | 1747 | | return x; |
| | 0 | 1748 | | } |
| | | 1749 | | |
| | | 1750 | | /** |
| | | 1751 | | * Calculate the numbers u1, u2, and u3 such that: |
| | | 1752 | | * |
| | | 1753 | | * u1 * a + u2 * b = u3 |
| | | 1754 | | * |
| | | 1755 | | * where u3 is the greatest common divider of a and b. |
| | | 1756 | | * a and b using the extended Euclid algorithm (refer p. 323 |
| | | 1757 | | * of The Art of Computer Programming vol 2, 2nd ed). |
| | | 1758 | | * This also seems to have the side effect of calculating |
| | | 1759 | | * some form of multiplicative inverse. |
| | | 1760 | | * |
| | | 1761 | | * @param a First number to calculate gcd for |
| | | 1762 | | * @param b Second number to calculate gcd for |
| | | 1763 | | * @param u1Out the return object for the u1 value |
| | | 1764 | | * @return The greatest common divisor of a and b |
| | | 1765 | | */ |
| | | 1766 | | private static BigInteger ExtEuclid(BigInteger a, BigInteger b, out BigInteger u1Out) |
| | 0 | 1767 | | { |
| | 0 | 1768 | | BigInteger u1 = One, v1 = Zero; |
| | 0 | 1769 | | BigInteger u3 = a, v3 = b; |
| | | 1770 | | |
| | 0 | 1771 | | if (v3.sign > 0) |
| | 0 | 1772 | | { |
| | | 1773 | | for (;;) |
| | 0 | 1774 | | { |
| | 0 | 1775 | | BigInteger[] q = u3.DivideAndRemainder(v3); |
| | 0 | 1776 | | u3 = v3; |
| | 0 | 1777 | | v3 = q[1]; |
| | | 1778 | | |
| | 0 | 1779 | | BigInteger oldU1 = u1; |
| | 0 | 1780 | | u1 = v1; |
| | | 1781 | | |
| | 0 | 1782 | | if (v3.sign <= 0) |
| | 0 | 1783 | | break; |
| | | 1784 | | |
| | 0 | 1785 | | v1 = oldU1.Subtract(v1.Multiply(q[0])); |
| | 0 | 1786 | | } |
| | 0 | 1787 | | } |
| | | 1788 | | |
| | 0 | 1789 | | u1Out = u1; |
| | | 1790 | | |
| | 0 | 1791 | | return u3; |
| | 0 | 1792 | | } |
| | | 1793 | | |
| | | 1794 | | private static void ZeroOut( |
| | | 1795 | | int[] x) |
| | 0 | 1796 | | { |
| | 0 | 1797 | | Array.Clear(x, 0, x.Length); |
| | 0 | 1798 | | } |
| | | 1799 | | |
| | | 1800 | | public BigInteger ModPow(BigInteger e, BigInteger m) |
| | 0 | 1801 | | { |
| | 0 | 1802 | | if (m.sign < 1) |
| | 0 | 1803 | | throw new ArithmeticException("Modulus must be positive"); |
| | | 1804 | | |
| | 0 | 1805 | | if (m.Equals(One)) |
| | 0 | 1806 | | return Zero; |
| | | 1807 | | |
| | 0 | 1808 | | if (e.sign == 0) |
| | 0 | 1809 | | return One; |
| | | 1810 | | |
| | 0 | 1811 | | if (sign == 0) |
| | 0 | 1812 | | return Zero; |
| | | 1813 | | |
| | 0 | 1814 | | bool negExp = e.sign < 0; |
| | 0 | 1815 | | if (negExp) |
| | 0 | 1816 | | e = e.Negate(); |
| | | 1817 | | |
| | 0 | 1818 | | BigInteger result = this.Mod(m); |
| | 0 | 1819 | | if (!e.Equals(One)) |
| | 0 | 1820 | | { |
| | 0 | 1821 | | if ((m.magnitude[m.magnitude.Length - 1] & 1) == 0) |
| | 0 | 1822 | | { |
| | 0 | 1823 | | result = ModPowBarrett(result, e, m); |
| | 0 | 1824 | | } |
| | | 1825 | | else |
| | 0 | 1826 | | { |
| | 0 | 1827 | | result = ModPowMonty(result, e, m, true); |
| | 0 | 1828 | | } |
| | 0 | 1829 | | } |
| | | 1830 | | |
| | 0 | 1831 | | if (negExp) |
| | 0 | 1832 | | result = result.ModInverse(m); |
| | | 1833 | | |
| | 0 | 1834 | | return result; |
| | 0 | 1835 | | } |
| | | 1836 | | |
| | | 1837 | | private static BigInteger ModPowBarrett(BigInteger b, BigInteger e, BigInteger m) |
| | 0 | 1838 | | { |
| | 0 | 1839 | | int k = m.magnitude.Length; |
| | 0 | 1840 | | BigInteger mr = One.ShiftLeft((k + 1) << 5); |
| | 0 | 1841 | | BigInteger yu = One.ShiftLeft(k << 6).Divide(m); |
| | | 1842 | | |
| | | 1843 | | // Sliding window from MSW to LSW |
| | 0 | 1844 | | int extraBits = 0, expLength = e.BitLength; |
| | 0 | 1845 | | while (expLength > ExpWindowThresholds[extraBits]) |
| | 0 | 1846 | | { |
| | 0 | 1847 | | ++extraBits; |
| | 0 | 1848 | | } |
| | | 1849 | | |
| | 0 | 1850 | | int numPowers = 1 << extraBits; |
| | 0 | 1851 | | BigInteger[] oddPowers = new BigInteger[numPowers]; |
| | 0 | 1852 | | oddPowers[0] = b; |
| | | 1853 | | |
| | 0 | 1854 | | BigInteger b2 = ReduceBarrett(b.Square(), m, mr, yu); |
| | | 1855 | | |
| | 0 | 1856 | | for (int i = 1; i < numPowers; ++i) |
| | 0 | 1857 | | { |
| | 0 | 1858 | | oddPowers[i] = ReduceBarrett(oddPowers[i - 1].Multiply(b2), m, mr, yu); |
| | 0 | 1859 | | } |
| | | 1860 | | |
| | 0 | 1861 | | int[] windowList = GetWindowList(e.magnitude, extraBits); |
| | 0 | 1862 | | Debug.Assert(windowList.Length > 0); |
| | | 1863 | | |
| | 0 | 1864 | | int window = windowList[0]; |
| | 0 | 1865 | | int mult = window & 0xFF, lastZeroes = window >> 8; |
| | | 1866 | | |
| | | 1867 | | BigInteger y; |
| | 0 | 1868 | | if (mult == 1) |
| | 0 | 1869 | | { |
| | 0 | 1870 | | y = b2; |
| | 0 | 1871 | | --lastZeroes; |
| | 0 | 1872 | | } |
| | | 1873 | | else |
| | 0 | 1874 | | { |
| | 0 | 1875 | | y = oddPowers[mult >> 1]; |
| | 0 | 1876 | | } |
| | | 1877 | | |
| | 0 | 1878 | | int windowPos = 1; |
| | 0 | 1879 | | while ((window = windowList[windowPos++]) != -1) |
| | 0 | 1880 | | { |
| | 0 | 1881 | | mult = window & 0xFF; |
| | | 1882 | | |
| | 0 | 1883 | | int bits = lastZeroes + BitLengthTable[mult]; |
| | 0 | 1884 | | for (int j = 0; j < bits; ++j) |
| | 0 | 1885 | | { |
| | 0 | 1886 | | y = ReduceBarrett(y.Square(), m, mr, yu); |
| | 0 | 1887 | | } |
| | | 1888 | | |
| | 0 | 1889 | | y = ReduceBarrett(y.Multiply(oddPowers[mult >> 1]), m, mr, yu); |
| | | 1890 | | |
| | 0 | 1891 | | lastZeroes = window >> 8; |
| | 0 | 1892 | | } |
| | | 1893 | | |
| | 0 | 1894 | | for (int i = 0; i < lastZeroes; ++i) |
| | 0 | 1895 | | { |
| | 0 | 1896 | | y = ReduceBarrett(y.Square(), m, mr, yu); |
| | 0 | 1897 | | } |
| | | 1898 | | |
| | 0 | 1899 | | return y; |
| | 0 | 1900 | | } |
| | | 1901 | | |
| | | 1902 | | private static BigInteger ReduceBarrett(BigInteger x, BigInteger m, BigInteger mr, BigInteger yu) |
| | 0 | 1903 | | { |
| | 0 | 1904 | | int xLen = x.BitLength, mLen = m.BitLength; |
| | 0 | 1905 | | if (xLen < mLen) |
| | 0 | 1906 | | return x; |
| | | 1907 | | |
| | 0 | 1908 | | if (xLen - mLen > 1) |
| | 0 | 1909 | | { |
| | 0 | 1910 | | int k = m.magnitude.Length; |
| | | 1911 | | |
| | 0 | 1912 | | BigInteger q1 = x.DivideWords(k - 1); |
| | 0 | 1913 | | BigInteger q2 = q1.Multiply(yu); // TODO Only need partial multiplication here |
| | 0 | 1914 | | BigInteger q3 = q2.DivideWords(k + 1); |
| | | 1915 | | |
| | 0 | 1916 | | BigInteger r1 = x.RemainderWords(k + 1); |
| | 0 | 1917 | | BigInteger r2 = q3.Multiply(m); // TODO Only need partial multiplication here |
| | 0 | 1918 | | BigInteger r3 = r2.RemainderWords(k + 1); |
| | | 1919 | | |
| | 0 | 1920 | | x = r1.Subtract(r3); |
| | 0 | 1921 | | if (x.sign < 0) |
| | 0 | 1922 | | { |
| | 0 | 1923 | | x = x.Add(mr); |
| | 0 | 1924 | | } |
| | 0 | 1925 | | } |
| | | 1926 | | |
| | 0 | 1927 | | while (x.CompareTo(m) >= 0) |
| | 0 | 1928 | | { |
| | 0 | 1929 | | x = x.Subtract(m); |
| | 0 | 1930 | | } |
| | | 1931 | | |
| | 0 | 1932 | | return x; |
| | 0 | 1933 | | } |
| | | 1934 | | |
| | | 1935 | | private static BigInteger ModPowMonty(BigInteger b, BigInteger e, BigInteger m, bool convert) |
| | 0 | 1936 | | { |
| | 0 | 1937 | | int n = m.magnitude.Length; |
| | 0 | 1938 | | int powR = 32 * n; |
| | 0 | 1939 | | bool smallMontyModulus = m.BitLength + 2 <= powR; |
| | 0 | 1940 | | uint mDash = (uint)m.GetMQuote(); |
| | | 1941 | | |
| | | 1942 | | // tmp = this * R mod m |
| | 0 | 1943 | | if (convert) |
| | 0 | 1944 | | { |
| | 0 | 1945 | | b = b.ShiftLeft(powR).Remainder(m); |
| | 0 | 1946 | | } |
| | | 1947 | | |
| | 0 | 1948 | | int[] yAccum = new int[n + 1]; |
| | | 1949 | | |
| | 0 | 1950 | | int[] zVal = b.magnitude; |
| | 0 | 1951 | | Debug.Assert(zVal.Length <= n); |
| | 0 | 1952 | | if (zVal.Length < n) |
| | 0 | 1953 | | { |
| | 0 | 1954 | | int[] tmp = new int[n]; |
| | 0 | 1955 | | zVal.CopyTo(tmp, n - zVal.Length); |
| | 0 | 1956 | | zVal = tmp; |
| | 0 | 1957 | | } |
| | | 1958 | | |
| | | 1959 | | // Sliding window from MSW to LSW |
| | | 1960 | | |
| | 0 | 1961 | | int extraBits = 0; |
| | | 1962 | | |
| | | 1963 | | // Filter the common case of small RSA exponents with few bits set |
| | 0 | 1964 | | if (e.magnitude.Length > 1 || e.BitCount > 2) |
| | 0 | 1965 | | { |
| | 0 | 1966 | | int expLength = e.BitLength; |
| | 0 | 1967 | | while (expLength > ExpWindowThresholds[extraBits]) |
| | 0 | 1968 | | { |
| | 0 | 1969 | | ++extraBits; |
| | 0 | 1970 | | } |
| | 0 | 1971 | | } |
| | | 1972 | | |
| | 0 | 1973 | | int numPowers = 1 << extraBits; |
| | 0 | 1974 | | int[][] oddPowers = new int[numPowers][]; |
| | 0 | 1975 | | oddPowers[0] = zVal; |
| | | 1976 | | |
| | 0 | 1977 | | int[] zSquared = Arrays.Clone(zVal); |
| | 0 | 1978 | | SquareMonty(yAccum, zSquared, m.magnitude, mDash, smallMontyModulus); |
| | | 1979 | | |
| | 0 | 1980 | | for (int i = 1; i < numPowers; ++i) |
| | 0 | 1981 | | { |
| | 0 | 1982 | | oddPowers[i] = Arrays.Clone(oddPowers[i - 1]); |
| | 0 | 1983 | | MultiplyMonty(yAccum, oddPowers[i], zSquared, m.magnitude, mDash, smallMontyModulus); |
| | 0 | 1984 | | } |
| | | 1985 | | |
| | 0 | 1986 | | int[] windowList = GetWindowList(e.magnitude, extraBits); |
| | 0 | 1987 | | Debug.Assert(windowList.Length > 1); |
| | | 1988 | | |
| | 0 | 1989 | | int window = windowList[0]; |
| | 0 | 1990 | | int mult = window & 0xFF, lastZeroes = window >> 8; |
| | | 1991 | | |
| | | 1992 | | int[] yVal; |
| | 0 | 1993 | | if (mult == 1) |
| | 0 | 1994 | | { |
| | 0 | 1995 | | yVal = zSquared; |
| | 0 | 1996 | | --lastZeroes; |
| | 0 | 1997 | | } |
| | | 1998 | | else |
| | 0 | 1999 | | { |
| | 0 | 2000 | | yVal = Arrays.Clone(oddPowers[mult >> 1]); |
| | 0 | 2001 | | } |
| | | 2002 | | |
| | 0 | 2003 | | int windowPos = 1; |
| | 0 | 2004 | | while ((window = windowList[windowPos++]) != -1) |
| | 0 | 2005 | | { |
| | 0 | 2006 | | mult = window & 0xFF; |
| | | 2007 | | |
| | 0 | 2008 | | int bits = lastZeroes + BitLengthTable[mult]; |
| | 0 | 2009 | | for (int j = 0; j < bits; ++j) |
| | 0 | 2010 | | { |
| | 0 | 2011 | | SquareMonty(yAccum, yVal, m.magnitude, mDash, smallMontyModulus); |
| | 0 | 2012 | | } |
| | | 2013 | | |
| | 0 | 2014 | | MultiplyMonty(yAccum, yVal, oddPowers[mult >> 1], m.magnitude, mDash, smallMontyModulus); |
| | | 2015 | | |
| | 0 | 2016 | | lastZeroes = window >> 8; |
| | 0 | 2017 | | } |
| | | 2018 | | |
| | 0 | 2019 | | for (int i = 0; i < lastZeroes; ++i) |
| | 0 | 2020 | | { |
| | 0 | 2021 | | SquareMonty(yAccum, yVal, m.magnitude, mDash, smallMontyModulus); |
| | 0 | 2022 | | } |
| | | 2023 | | |
| | 0 | 2024 | | if (convert) |
| | 0 | 2025 | | { |
| | | 2026 | | // Return y * R^(-1) mod m |
| | 0 | 2027 | | MontgomeryReduce(yVal, m.magnitude, mDash); |
| | 0 | 2028 | | } |
| | 0 | 2029 | | else if (smallMontyModulus && CompareTo(0, yVal, 0, m.magnitude) >= 0) |
| | 0 | 2030 | | { |
| | 0 | 2031 | | Subtract(0, yVal, 0, m.magnitude); |
| | 0 | 2032 | | } |
| | | 2033 | | |
| | 0 | 2034 | | return new BigInteger(1, yVal, true); |
| | 0 | 2035 | | } |
| | | 2036 | | |
| | | 2037 | | private static int[] GetWindowList(int[] mag, int extraBits) |
| | 0 | 2038 | | { |
| | 0 | 2039 | | int v = mag[0]; |
| | 0 | 2040 | | Debug.Assert(v != 0); |
| | | 2041 | | |
| | 0 | 2042 | | int leadingBits = BitLen(v); |
| | | 2043 | | |
| | 0 | 2044 | | int resultSize = (((mag.Length - 1) << 5) + leadingBits) / (1 + extraBits) + 2; |
| | 0 | 2045 | | int[] result = new int[resultSize]; |
| | 0 | 2046 | | int resultPos = 0; |
| | | 2047 | | |
| | 0 | 2048 | | int bitPos = 33 - leadingBits; |
| | 0 | 2049 | | v <<= bitPos; |
| | | 2050 | | |
| | 0 | 2051 | | int mult = 1, multLimit = 1 << extraBits; |
| | 0 | 2052 | | int zeroes = 0; |
| | | 2053 | | |
| | 0 | 2054 | | int i = 0; |
| | | 2055 | | for (; ; ) |
| | 0 | 2056 | | { |
| | 0 | 2057 | | for (; bitPos < 32; ++bitPos) |
| | 0 | 2058 | | { |
| | 0 | 2059 | | if (mult < multLimit) |
| | 0 | 2060 | | { |
| | 0 | 2061 | | mult = (mult << 1) | (int)((uint)v >> 31); |
| | 0 | 2062 | | } |
| | 0 | 2063 | | else if (v < 0) |
| | 0 | 2064 | | { |
| | 0 | 2065 | | result[resultPos++] = CreateWindowEntry(mult, zeroes); |
| | 0 | 2066 | | mult = 1; |
| | 0 | 2067 | | zeroes = 0; |
| | 0 | 2068 | | } |
| | | 2069 | | else |
| | 0 | 2070 | | { |
| | 0 | 2071 | | ++zeroes; |
| | 0 | 2072 | | } |
| | | 2073 | | |
| | 0 | 2074 | | v <<= 1; |
| | 0 | 2075 | | } |
| | | 2076 | | |
| | 0 | 2077 | | if (++i == mag.Length) |
| | 0 | 2078 | | { |
| | 0 | 2079 | | result[resultPos++] = CreateWindowEntry(mult, zeroes); |
| | 0 | 2080 | | break; |
| | | 2081 | | } |
| | | 2082 | | |
| | 0 | 2083 | | v = mag[i]; |
| | 0 | 2084 | | bitPos = 0; |
| | 0 | 2085 | | } |
| | | 2086 | | |
| | 0 | 2087 | | result[resultPos] = -1; |
| | 0 | 2088 | | return result; |
| | 0 | 2089 | | } |
| | | 2090 | | |
| | | 2091 | | private static int CreateWindowEntry(int mult, int zeroes) |
| | 0 | 2092 | | { |
| | 0 | 2093 | | while ((mult & 1) == 0) |
| | 0 | 2094 | | { |
| | 0 | 2095 | | mult >>= 1; |
| | 0 | 2096 | | ++zeroes; |
| | 0 | 2097 | | } |
| | | 2098 | | |
| | 0 | 2099 | | return mult | (zeroes << 8); |
| | 0 | 2100 | | } |
| | | 2101 | | |
| | | 2102 | | /** |
| | | 2103 | | * return w with w = x * x - w is assumed to have enough space. |
| | | 2104 | | */ |
| | | 2105 | | private static int[] Square( |
| | | 2106 | | int[] w, |
| | | 2107 | | int[] x) |
| | 22376 | 2108 | | { |
| | | 2109 | | // Note: this method allows w to be only (2 * x.Length - 1) words if result will fit |
| | | 2110 | | // if (w.Length != 2 * x.Length) |
| | | 2111 | | // throw new ArgumentException("no I don't think so..."); |
| | | 2112 | | |
| | | 2113 | | ulong c; |
| | | 2114 | | |
| | 22376 | 2115 | | int wBase = w.Length - 1; |
| | | 2116 | | |
| | 594250 | 2117 | | for (int i = x.Length - 1; i > 0; --i) |
| | 274749 | 2118 | | { |
| | 274749 | 2119 | | ulong v = (uint)x[i]; |
| | | 2120 | | |
| | 274749 | 2121 | | c = v * v + (uint)w[wBase]; |
| | 274749 | 2122 | | w[wBase] = (int)c; |
| | 274749 | 2123 | | c >>= 32; |
| | | 2124 | | |
| | 4489214 | 2125 | | for (int j = i - 1; j >= 0; --j) |
| | 1969858 | 2126 | | { |
| | 1969858 | 2127 | | ulong prod = v * (uint)x[j]; |
| | | 2128 | | |
| | 1969858 | 2129 | | c += ((uint)w[--wBase] & UIMASK) + ((uint)prod << 1); |
| | 1969858 | 2130 | | w[wBase] = (int)c; |
| | 1969858 | 2131 | | c = (c >> 32) + (prod >> 31); |
| | 1969858 | 2132 | | } |
| | | 2133 | | |
| | 274749 | 2134 | | c += (uint)w[--wBase]; |
| | 274749 | 2135 | | w[wBase] = (int)c; |
| | | 2136 | | |
| | 274749 | 2137 | | if (--wBase >= 0) |
| | 264936 | 2138 | | { |
| | 264936 | 2139 | | w[wBase] = (int)(c >> 32); |
| | 264936 | 2140 | | } |
| | | 2141 | | else |
| | 9813 | 2142 | | { |
| | 9813 | 2143 | | Debug.Assert((c >> 32) == 0); |
| | 9813 | 2144 | | } |
| | | 2145 | | |
| | 274749 | 2146 | | wBase += i; |
| | 274749 | 2147 | | } |
| | | 2148 | | |
| | 22376 | 2149 | | c = (uint)x[0]; |
| | | 2150 | | |
| | 22376 | 2151 | | c = c * c + (uint)w[wBase]; |
| | 22376 | 2152 | | w[wBase] = (int)c; |
| | | 2153 | | |
| | 22376 | 2154 | | if (--wBase >= 0) |
| | 12560 | 2155 | | { |
| | 12560 | 2156 | | w[wBase] += (int)(c >> 32); |
| | 12560 | 2157 | | } |
| | | 2158 | | else |
| | 9816 | 2159 | | { |
| | 9816 | 2160 | | Debug.Assert((c >> 32) == 0); |
| | 9816 | 2161 | | } |
| | | 2162 | | |
| | 22376 | 2163 | | return w; |
| | 22376 | 2164 | | } |
| | | 2165 | | |
| | | 2166 | | /** |
| | | 2167 | | * return x with x = y * z - x is assumed to have enough space. |
| | | 2168 | | */ |
| | | 2169 | | private static int[] Multiply(int[] x, int[] y, int[] z) |
| | 104171 | 2170 | | { |
| | 104171 | 2171 | | int i = z.Length; |
| | | 2172 | | |
| | 104171 | 2173 | | if (i < 1) |
| | 0 | 2174 | | return x; |
| | | 2175 | | |
| | 104171 | 2176 | | int xBase = x.Length - y.Length; |
| | | 2177 | | |
| | | 2178 | | do |
| | 913238 | 2179 | | { |
| | 913238 | 2180 | | long a = z[--i] & IMASK; |
| | 913238 | 2181 | | long val = 0; |
| | | 2182 | | |
| | 913238 | 2183 | | if (a != 0) |
| | 834030 | 2184 | | { |
| | 21366850 | 2185 | | for (int j = y.Length - 1; j >= 0; j--) |
| | 9849395 | 2186 | | { |
| | 9849395 | 2187 | | val += a * (y[j] & IMASK) + (x[xBase + j] & IMASK); |
| | | 2188 | | |
| | 9849395 | 2189 | | x[xBase + j] = (int)val; |
| | | 2190 | | |
| | 9849395 | 2191 | | val = (long)((ulong)val >> 32); |
| | 9849395 | 2192 | | } |
| | 834030 | 2193 | | } |
| | | 2194 | | |
| | 913238 | 2195 | | --xBase; |
| | | 2196 | | |
| | 913238 | 2197 | | if (xBase >= 0) |
| | 913238 | 2198 | | { |
| | 913238 | 2199 | | x[xBase] = (int)val; |
| | 913238 | 2200 | | } |
| | | 2201 | | else |
| | 0 | 2202 | | { |
| | 0 | 2203 | | Debug.Assert(val == 0); |
| | 0 | 2204 | | } |
| | 913238 | 2205 | | } |
| | 913238 | 2206 | | while (i > 0); |
| | | 2207 | | |
| | 104171 | 2208 | | return x; |
| | 104171 | 2209 | | } |
| | | 2210 | | |
| | | 2211 | | /** |
| | | 2212 | | * Calculate mQuote = -m^(-1) mod b with b = 2^32 (32 = word size) |
| | | 2213 | | */ |
| | | 2214 | | private int GetMQuote() |
| | 0 | 2215 | | { |
| | 0 | 2216 | | if (mQuote != 0) |
| | 0 | 2217 | | { |
| | 0 | 2218 | | return mQuote; // already calculated |
| | | 2219 | | } |
| | | 2220 | | |
| | 0 | 2221 | | Debug.Assert(this.sign > 0); |
| | | 2222 | | |
| | 0 | 2223 | | int d = -magnitude[magnitude.Length - 1]; |
| | | 2224 | | |
| | 0 | 2225 | | Debug.Assert((d & 1) != 0); |
| | | 2226 | | |
| | 0 | 2227 | | return mQuote = ModInverse32(d); |
| | 0 | 2228 | | } |
| | | 2229 | | |
| | | 2230 | | private static void MontgomeryReduce(int[] x, int[] m, uint mDash) // mDash = -m^(-1) mod b |
| | 0 | 2231 | | { |
| | | 2232 | | // NOTE: Not a general purpose reduction (which would allow x up to twice the bitlength of m) |
| | 0 | 2233 | | Debug.Assert(x.Length == m.Length); |
| | | 2234 | | |
| | 0 | 2235 | | int n = m.Length; |
| | | 2236 | | |
| | 0 | 2237 | | for (int i = n - 1; i >= 0; --i) |
| | 0 | 2238 | | { |
| | 0 | 2239 | | uint x0 = (uint)x[n - 1]; |
| | 0 | 2240 | | ulong t = x0 * mDash; |
| | | 2241 | | |
| | 0 | 2242 | | ulong carry = t * (uint)m[n - 1] + x0; |
| | 0 | 2243 | | Debug.Assert((uint)carry == 0); |
| | 0 | 2244 | | carry >>= 32; |
| | | 2245 | | |
| | 0 | 2246 | | for (int j = n - 2; j >= 0; --j) |
| | 0 | 2247 | | { |
| | 0 | 2248 | | carry += t * (uint)m[j] + (uint)x[j]; |
| | 0 | 2249 | | x[j + 1] = (int)carry; |
| | 0 | 2250 | | carry >>= 32; |
| | 0 | 2251 | | } |
| | | 2252 | | |
| | 0 | 2253 | | x[0] = (int)carry; |
| | 0 | 2254 | | Debug.Assert(carry >> 32 == 0); |
| | 0 | 2255 | | } |
| | | 2256 | | |
| | 0 | 2257 | | if (CompareTo(0, x, 0, m) >= 0) |
| | 0 | 2258 | | { |
| | 0 | 2259 | | Subtract(0, x, 0, m); |
| | 0 | 2260 | | } |
| | 0 | 2261 | | } |
| | | 2262 | | |
| | | 2263 | | /** |
| | | 2264 | | * Montgomery multiplication: a = x * y * R^(-1) mod m |
| | | 2265 | | * <br/> |
| | | 2266 | | * Based algorithm 14.36 of Handbook of Applied Cryptography. |
| | | 2267 | | * <br/> |
| | | 2268 | | * <li> m, x, y should have length n </li> |
| | | 2269 | | * <li> a should have length (n + 1) </li> |
| | | 2270 | | * <li> b = 2^32, R = b^n </li> |
| | | 2271 | | * <br/> |
| | | 2272 | | * The result is put in x |
| | | 2273 | | * <br/> |
| | | 2274 | | * NOTE: the indices of x, y, m, a different in HAC and in Java |
| | | 2275 | | */ |
| | | 2276 | | private static void MultiplyMonty(int[] a, int[] x, int[] y, int[] m, uint mDash, bool smallMontyModulus) |
| | | 2277 | | // mDash = -m^(-1) mod b |
| | 0 | 2278 | | { |
| | 0 | 2279 | | int n = m.Length; |
| | | 2280 | | |
| | 0 | 2281 | | if (n == 1) |
| | 0 | 2282 | | { |
| | 0 | 2283 | | x[0] = (int)MultiplyMontyNIsOne((uint)x[0], (uint)y[0], (uint)m[0], mDash); |
| | 0 | 2284 | | return; |
| | | 2285 | | } |
| | | 2286 | | |
| | 0 | 2287 | | uint y0 = (uint)y[n - 1]; |
| | | 2288 | | int aMax; |
| | | 2289 | | |
| | 0 | 2290 | | { |
| | 0 | 2291 | | ulong xi = (uint)x[n - 1]; |
| | | 2292 | | |
| | 0 | 2293 | | ulong carry = xi * y0; |
| | 0 | 2294 | | ulong t = (uint)carry * mDash; |
| | | 2295 | | |
| | 0 | 2296 | | ulong prod2 = t * (uint)m[n - 1]; |
| | 0 | 2297 | | carry += (uint)prod2; |
| | 0 | 2298 | | Debug.Assert((uint)carry == 0); |
| | 0 | 2299 | | carry = (carry >> 32) + (prod2 >> 32); |
| | | 2300 | | |
| | 0 | 2301 | | for (int j = n - 2; j >= 0; --j) |
| | 0 | 2302 | | { |
| | 0 | 2303 | | ulong prod1 = xi * (uint)y[j]; |
| | 0 | 2304 | | prod2 = t * (uint)m[j]; |
| | | 2305 | | |
| | 0 | 2306 | | carry += (prod1 & UIMASK) + (uint)prod2; |
| | 0 | 2307 | | a[j + 2] = (int)carry; |
| | 0 | 2308 | | carry = (carry >> 32) + (prod1 >> 32) + (prod2 >> 32); |
| | 0 | 2309 | | } |
| | | 2310 | | |
| | 0 | 2311 | | a[1] = (int)carry; |
| | 0 | 2312 | | aMax = (int)(carry >> 32); |
| | 0 | 2313 | | } |
| | | 2314 | | |
| | 0 | 2315 | | for (int i = n - 2; i >= 0; --i) |
| | 0 | 2316 | | { |
| | 0 | 2317 | | uint a0 = (uint)a[n]; |
| | 0 | 2318 | | ulong xi = (uint)x[i]; |
| | | 2319 | | |
| | 0 | 2320 | | ulong prod1 = xi * y0; |
| | 0 | 2321 | | ulong carry = (prod1 & UIMASK) + a0; |
| | 0 | 2322 | | ulong t = (uint)carry * mDash; |
| | | 2323 | | |
| | 0 | 2324 | | ulong prod2 = t * (uint)m[n - 1]; |
| | 0 | 2325 | | carry += (uint)prod2; |
| | 0 | 2326 | | Debug.Assert((uint)carry == 0); |
| | 0 | 2327 | | carry = (carry >> 32) + (prod1 >> 32) + (prod2 >> 32); |
| | | 2328 | | |
| | 0 | 2329 | | for (int j = n - 2; j >= 0; --j) |
| | 0 | 2330 | | { |
| | 0 | 2331 | | prod1 = xi * (uint)y[j]; |
| | 0 | 2332 | | prod2 = t * (uint)m[j]; |
| | | 2333 | | |
| | 0 | 2334 | | carry += (prod1 & UIMASK) + (uint)prod2 + (uint)a[j + 1]; |
| | 0 | 2335 | | a[j + 2] = (int)carry; |
| | 0 | 2336 | | carry = (carry >> 32) + (prod1 >> 32) + (prod2 >> 32); |
| | 0 | 2337 | | } |
| | | 2338 | | |
| | 0 | 2339 | | carry += (uint)aMax; |
| | 0 | 2340 | | a[1] = (int)carry; |
| | 0 | 2341 | | aMax = (int)(carry >> 32); |
| | 0 | 2342 | | } |
| | | 2343 | | |
| | 0 | 2344 | | a[0] = aMax; |
| | | 2345 | | |
| | 0 | 2346 | | if (!smallMontyModulus && CompareTo(0, a, 0, m) >= 0) |
| | 0 | 2347 | | { |
| | 0 | 2348 | | Subtract(0, a, 0, m); |
| | 0 | 2349 | | } |
| | | 2350 | | |
| | 0 | 2351 | | Array.Copy(a, 1, x, 0, n); |
| | 0 | 2352 | | } |
| | | 2353 | | |
| | | 2354 | | private static void SquareMonty(int[] a, int[] x, int[] m, uint mDash, bool smallMontyModulus) |
| | | 2355 | | // mDash = -m^(-1) mod b |
| | 0 | 2356 | | { |
| | 0 | 2357 | | int n = m.Length; |
| | | 2358 | | |
| | 0 | 2359 | | if (n == 1) |
| | 0 | 2360 | | { |
| | 0 | 2361 | | uint xVal = (uint)x[0]; |
| | 0 | 2362 | | x[0] = (int)MultiplyMontyNIsOne(xVal, xVal, (uint)m[0], mDash); |
| | 0 | 2363 | | return; |
| | | 2364 | | } |
| | | 2365 | | |
| | 0 | 2366 | | ulong x0 = (uint)x[n - 1]; |
| | | 2367 | | int aMax; |
| | | 2368 | | |
| | 0 | 2369 | | { |
| | 0 | 2370 | | ulong carry = x0 * x0; |
| | 0 | 2371 | | ulong t = (uint)carry * mDash; |
| | | 2372 | | |
| | 0 | 2373 | | ulong prod2 = t * (uint)m[n - 1]; |
| | 0 | 2374 | | carry += (uint)prod2; |
| | 0 | 2375 | | Debug.Assert((uint)carry == 0); |
| | 0 | 2376 | | carry = (carry >> 32) + (prod2 >> 32); |
| | | 2377 | | |
| | 0 | 2378 | | for (int j = n - 2; j >= 0; --j) |
| | 0 | 2379 | | { |
| | 0 | 2380 | | ulong prod1 = x0 * (uint)x[j]; |
| | 0 | 2381 | | prod2 = t * (uint)m[j]; |
| | | 2382 | | |
| | 0 | 2383 | | carry += (prod2 & UIMASK) + ((uint)prod1 << 1); |
| | 0 | 2384 | | a[j + 2] = (int)carry; |
| | 0 | 2385 | | carry = (carry >> 32) + (prod1 >> 31) + (prod2 >> 32); |
| | 0 | 2386 | | } |
| | | 2387 | | |
| | 0 | 2388 | | a[1] = (int)carry; |
| | 0 | 2389 | | aMax = (int)(carry >> 32); |
| | 0 | 2390 | | } |
| | | 2391 | | |
| | 0 | 2392 | | for (int i = n - 2; i >= 0; --i) |
| | 0 | 2393 | | { |
| | 0 | 2394 | | uint a0 = (uint)a[n]; |
| | 0 | 2395 | | ulong t = a0 * mDash; |
| | | 2396 | | |
| | 0 | 2397 | | ulong carry = t * (uint)m[n - 1] + a0; |
| | 0 | 2398 | | Debug.Assert((uint)carry == 0); |
| | 0 | 2399 | | carry >>= 32; |
| | | 2400 | | |
| | 0 | 2401 | | for (int j = n - 2; j > i; --j) |
| | 0 | 2402 | | { |
| | 0 | 2403 | | carry += t * (uint)m[j] + (uint)a[j + 1]; |
| | 0 | 2404 | | a[j + 2] = (int)carry; |
| | 0 | 2405 | | carry >>= 32; |
| | 0 | 2406 | | } |
| | | 2407 | | |
| | 0 | 2408 | | ulong xi = (uint)x[i]; |
| | | 2409 | | |
| | 0 | 2410 | | { |
| | 0 | 2411 | | ulong prod1 = xi * xi; |
| | 0 | 2412 | | ulong prod2 = t * (uint)m[i]; |
| | | 2413 | | |
| | 0 | 2414 | | carry += (prod1 & UIMASK) + (uint)prod2 + (uint)a[i + 1]; |
| | 0 | 2415 | | a[i + 2] = (int)carry; |
| | 0 | 2416 | | carry = (carry >> 32) + (prod1 >> 32) + (prod2 >> 32); |
| | 0 | 2417 | | } |
| | | 2418 | | |
| | 0 | 2419 | | for (int j = i - 1; j >= 0; --j) |
| | 0 | 2420 | | { |
| | 0 | 2421 | | ulong prod1 = xi * (uint)x[j]; |
| | 0 | 2422 | | ulong prod2 = t * (uint)m[j]; |
| | | 2423 | | |
| | 0 | 2424 | | carry += (prod2 & UIMASK) + ((uint)prod1 << 1) + (uint)a[j + 1]; |
| | 0 | 2425 | | a[j + 2] = (int)carry; |
| | 0 | 2426 | | carry = (carry >> 32) + (prod1 >> 31) + (prod2 >> 32); |
| | 0 | 2427 | | } |
| | | 2428 | | |
| | 0 | 2429 | | carry += (uint)aMax; |
| | 0 | 2430 | | a[1] = (int)carry; |
| | 0 | 2431 | | aMax = (int)(carry >> 32); |
| | 0 | 2432 | | } |
| | | 2433 | | |
| | 0 | 2434 | | a[0] = aMax; |
| | | 2435 | | |
| | 0 | 2436 | | if (!smallMontyModulus && CompareTo(0, a, 0, m) >= 0) |
| | 0 | 2437 | | { |
| | 0 | 2438 | | Subtract(0, a, 0, m); |
| | 0 | 2439 | | } |
| | | 2440 | | |
| | 0 | 2441 | | Array.Copy(a, 1, x, 0, n); |
| | 0 | 2442 | | } |
| | | 2443 | | |
| | | 2444 | | private static uint MultiplyMontyNIsOne(uint x, uint y, uint m, uint mDash) |
| | 0 | 2445 | | { |
| | 0 | 2446 | | ulong carry = (ulong)x * y; |
| | 0 | 2447 | | uint t = (uint)carry * mDash; |
| | 0 | 2448 | | ulong um = m; |
| | 0 | 2449 | | ulong prod2 = um * t; |
| | 0 | 2450 | | carry += (uint)prod2; |
| | 0 | 2451 | | Debug.Assert((uint)carry == 0); |
| | 0 | 2452 | | carry = (carry >> 32) + (prod2 >> 32); |
| | 0 | 2453 | | if (carry > um) |
| | 0 | 2454 | | { |
| | 0 | 2455 | | carry -= um; |
| | 0 | 2456 | | } |
| | 0 | 2457 | | Debug.Assert(carry < um); |
| | 0 | 2458 | | return (uint)carry; |
| | 0 | 2459 | | } |
| | | 2460 | | |
| | | 2461 | | public BigInteger Multiply( |
| | | 2462 | | BigInteger val) |
| | 126701 | 2463 | | { |
| | 126701 | 2464 | | if (val == this) |
| | 22376 | 2465 | | return Square(); |
| | | 2466 | | |
| | 104325 | 2467 | | if ((sign & val.sign) == 0) |
| | 126 | 2468 | | return Zero; |
| | | 2469 | | |
| | 104199 | 2470 | | if (val.QuickPow2Check()) // val is power of two |
| | 0 | 2471 | | { |
| | 0 | 2472 | | BigInteger result = this.ShiftLeft(val.Abs().BitLength - 1); |
| | 0 | 2473 | | return val.sign > 0 ? result : result.Negate(); |
| | | 2474 | | } |
| | | 2475 | | |
| | 104199 | 2476 | | if (this.QuickPow2Check()) // this is power of two |
| | 28 | 2477 | | { |
| | 28 | 2478 | | BigInteger result = val.ShiftLeft(this.Abs().BitLength - 1); |
| | 28 | 2479 | | return this.sign > 0 ? result : result.Negate(); |
| | | 2480 | | } |
| | | 2481 | | |
| | 104171 | 2482 | | int resLength = magnitude.Length + val.magnitude.Length; |
| | 104171 | 2483 | | int[] res = new int[resLength]; |
| | | 2484 | | |
| | 104171 | 2485 | | Multiply(res, this.magnitude, val.magnitude); |
| | | 2486 | | |
| | 104171 | 2487 | | int resSign = sign ^ val.sign ^ 1; |
| | 104171 | 2488 | | return new BigInteger(resSign, res, true); |
| | 126701 | 2489 | | } |
| | | 2490 | | |
| | | 2491 | | public BigInteger Square() |
| | 22376 | 2492 | | { |
| | 22376 | 2493 | | if (sign == 0) |
| | 0 | 2494 | | return Zero; |
| | 22376 | 2495 | | if (this.QuickPow2Check()) |
| | 0 | 2496 | | return ShiftLeft(Abs().BitLength - 1); |
| | 22376 | 2497 | | int resLength = magnitude.Length << 1; |
| | 22376 | 2498 | | if ((uint)magnitude[0] >> 16 == 0) |
| | 9816 | 2499 | | --resLength; |
| | 22376 | 2500 | | int[] res = new int[resLength]; |
| | 22376 | 2501 | | Square(res, magnitude); |
| | 22376 | 2502 | | return new BigInteger(1, res, false); |
| | 22376 | 2503 | | } |
| | | 2504 | | |
| | | 2505 | | public BigInteger Negate() |
| | 24565 | 2506 | | { |
| | 24565 | 2507 | | if (sign == 0) |
| | 0 | 2508 | | return this; |
| | | 2509 | | |
| | 24565 | 2510 | | return new BigInteger(-sign, magnitude, false); |
| | 24565 | 2511 | | } |
| | | 2512 | | |
| | | 2513 | | public BigInteger NextProbablePrime() |
| | 0 | 2514 | | { |
| | 0 | 2515 | | if (sign < 0) |
| | 0 | 2516 | | throw new ArithmeticException("Cannot be called on value < 0"); |
| | | 2517 | | |
| | 0 | 2518 | | if (CompareTo(Two) < 0) |
| | 0 | 2519 | | return Two; |
| | | 2520 | | |
| | 0 | 2521 | | BigInteger n = Inc().SetBit(0); |
| | | 2522 | | |
| | 0 | 2523 | | while (!n.CheckProbablePrime(100, RandomSource, false)) |
| | 0 | 2524 | | { |
| | 0 | 2525 | | n = n.Add(Two); |
| | 0 | 2526 | | } |
| | | 2527 | | |
| | 0 | 2528 | | return n; |
| | 0 | 2529 | | } |
| | | 2530 | | |
| | | 2531 | | public BigInteger Not() |
| | 0 | 2532 | | { |
| | 0 | 2533 | | return Inc().Negate(); |
| | 0 | 2534 | | } |
| | | 2535 | | |
| | | 2536 | | public BigInteger Pow(int exp) |
| | 4 | 2537 | | { |
| | 4 | 2538 | | if (exp <= 0) |
| | 0 | 2539 | | { |
| | 0 | 2540 | | if (exp < 0) |
| | 0 | 2541 | | throw new ArithmeticException("Negative exponent"); |
| | | 2542 | | |
| | 0 | 2543 | | return One; |
| | | 2544 | | } |
| | | 2545 | | |
| | 4 | 2546 | | if (sign == 0) |
| | 0 | 2547 | | { |
| | 0 | 2548 | | return this; |
| | | 2549 | | } |
| | | 2550 | | |
| | 4 | 2551 | | if (QuickPow2Check()) |
| | 3 | 2552 | | { |
| | 3 | 2553 | | long powOf2 = (long)exp * (BitLength - 1); |
| | 3 | 2554 | | if (powOf2 > Int32.MaxValue) |
| | 0 | 2555 | | { |
| | 0 | 2556 | | throw new ArithmeticException("Result too large"); |
| | | 2557 | | } |
| | 3 | 2558 | | return One.ShiftLeft((int)powOf2); |
| | | 2559 | | } |
| | | 2560 | | |
| | 1 | 2561 | | BigInteger y = One; |
| | 1 | 2562 | | BigInteger z = this; |
| | | 2563 | | |
| | | 2564 | | for (;;) |
| | 5 | 2565 | | { |
| | 5 | 2566 | | if ((exp & 0x1) == 1) |
| | 3 | 2567 | | { |
| | 3 | 2568 | | y = y.Multiply(z); |
| | 3 | 2569 | | } |
| | 5 | 2570 | | exp >>= 1; |
| | 6 | 2571 | | if (exp == 0) break; |
| | 4 | 2572 | | z = z.Multiply(z); |
| | 4 | 2573 | | } |
| | | 2574 | | |
| | 1 | 2575 | | return y; |
| | 4 | 2576 | | } |
| | | 2577 | | |
| | | 2578 | | public static BigInteger ProbablePrime( |
| | | 2579 | | int bitLength, |
| | | 2580 | | Random random) |
| | 0 | 2581 | | { |
| | 0 | 2582 | | return new BigInteger(bitLength, 100, random); |
| | 0 | 2583 | | } |
| | | 2584 | | |
| | | 2585 | | private int Remainder( |
| | | 2586 | | int m) |
| | 0 | 2587 | | { |
| | 0 | 2588 | | Debug.Assert(m > 0); |
| | | 2589 | | |
| | 0 | 2590 | | long acc = 0; |
| | 0 | 2591 | | for (int pos = 0; pos < magnitude.Length; ++pos) |
| | 0 | 2592 | | { |
| | 0 | 2593 | | long posVal = (uint) magnitude[pos]; |
| | 0 | 2594 | | acc = (acc << 32 | posVal) % m; |
| | 0 | 2595 | | } |
| | | 2596 | | |
| | 0 | 2597 | | return (int) acc; |
| | 0 | 2598 | | } |
| | | 2599 | | |
| | | 2600 | | /** |
| | | 2601 | | * return x = x % y - done in place (y value preserved) |
| | | 2602 | | */ |
| | | 2603 | | private static int[] Remainder( |
| | | 2604 | | int[] x, |
| | | 2605 | | int[] y) |
| | 0 | 2606 | | { |
| | 0 | 2607 | | int xStart = 0; |
| | 0 | 2608 | | while (xStart < x.Length && x[xStart] == 0) |
| | 0 | 2609 | | { |
| | 0 | 2610 | | ++xStart; |
| | 0 | 2611 | | } |
| | | 2612 | | |
| | 0 | 2613 | | int yStart = 0; |
| | 0 | 2614 | | while (yStart < y.Length && y[yStart] == 0) |
| | 0 | 2615 | | { |
| | 0 | 2616 | | ++yStart; |
| | 0 | 2617 | | } |
| | | 2618 | | |
| | 0 | 2619 | | Debug.Assert(yStart < y.Length); |
| | | 2620 | | |
| | 0 | 2621 | | int xyCmp = CompareNoLeadingZeroes(xStart, x, yStart, y); |
| | | 2622 | | |
| | 0 | 2623 | | if (xyCmp > 0) |
| | 0 | 2624 | | { |
| | 0 | 2625 | | int yBitLength = CalcBitLength(1, yStart, y); |
| | 0 | 2626 | | int xBitLength = CalcBitLength(1, xStart, x); |
| | 0 | 2627 | | int shift = xBitLength - yBitLength; |
| | | 2628 | | |
| | | 2629 | | int[] c; |
| | 0 | 2630 | | int cStart = 0; |
| | 0 | 2631 | | int cBitLength = yBitLength; |
| | 0 | 2632 | | if (shift > 0) |
| | 0 | 2633 | | { |
| | 0 | 2634 | | c = ShiftLeft(y, shift); |
| | 0 | 2635 | | cBitLength += shift; |
| | 0 | 2636 | | Debug.Assert(c[0] != 0); |
| | 0 | 2637 | | } |
| | | 2638 | | else |
| | 0 | 2639 | | { |
| | 0 | 2640 | | int len = y.Length - yStart; |
| | 0 | 2641 | | c = new int[len]; |
| | 0 | 2642 | | Array.Copy(y, yStart, c, 0, len); |
| | 0 | 2643 | | } |
| | | 2644 | | |
| | | 2645 | | for (;;) |
| | 0 | 2646 | | { |
| | 0 | 2647 | | if (cBitLength < xBitLength |
| | 0 | 2648 | | || CompareNoLeadingZeroes(xStart, x, cStart, c) >= 0) |
| | 0 | 2649 | | { |
| | 0 | 2650 | | Subtract(xStart, x, cStart, c); |
| | | 2651 | | |
| | 0 | 2652 | | while (x[xStart] == 0) |
| | 0 | 2653 | | { |
| | 0 | 2654 | | if (++xStart == x.Length) |
| | 0 | 2655 | | return x; |
| | 0 | 2656 | | } |
| | | 2657 | | |
| | | 2658 | | //xBitLength = CalcBitLength(xStart, x); |
| | 0 | 2659 | | xBitLength = 32 * (x.Length - xStart - 1) + BitLen(x[xStart]); |
| | | 2660 | | |
| | 0 | 2661 | | if (xBitLength <= yBitLength) |
| | 0 | 2662 | | { |
| | 0 | 2663 | | if (xBitLength < yBitLength) |
| | 0 | 2664 | | return x; |
| | | 2665 | | |
| | 0 | 2666 | | xyCmp = CompareNoLeadingZeroes(xStart, x, yStart, y); |
| | | 2667 | | |
| | 0 | 2668 | | if (xyCmp <= 0) |
| | 0 | 2669 | | break; |
| | 0 | 2670 | | } |
| | 0 | 2671 | | } |
| | | 2672 | | |
| | 0 | 2673 | | shift = cBitLength - xBitLength; |
| | | 2674 | | |
| | | 2675 | | // NB: The case where c[cStart] is 1-bit is harmless |
| | 0 | 2676 | | if (shift == 1) |
| | 0 | 2677 | | { |
| | 0 | 2678 | | uint firstC = (uint) c[cStart] >> 1; |
| | 0 | 2679 | | uint firstX = (uint) x[xStart]; |
| | 0 | 2680 | | if (firstC > firstX) |
| | 0 | 2681 | | ++shift; |
| | 0 | 2682 | | } |
| | | 2683 | | |
| | 0 | 2684 | | if (shift < 2) |
| | 0 | 2685 | | { |
| | 0 | 2686 | | ShiftRightOneInPlace(cStart, c); |
| | 0 | 2687 | | --cBitLength; |
| | 0 | 2688 | | } |
| | | 2689 | | else |
| | 0 | 2690 | | { |
| | 0 | 2691 | | ShiftRightInPlace(cStart, c, shift); |
| | 0 | 2692 | | cBitLength -= shift; |
| | 0 | 2693 | | } |
| | | 2694 | | |
| | | 2695 | | //cStart = c.Length - ((cBitLength + 31) / 32); |
| | 0 | 2696 | | while (c[cStart] == 0) |
| | 0 | 2697 | | { |
| | 0 | 2698 | | ++cStart; |
| | 0 | 2699 | | } |
| | 0 | 2700 | | } |
| | 0 | 2701 | | } |
| | | 2702 | | |
| | 0 | 2703 | | if (xyCmp == 0) |
| | 0 | 2704 | | { |
| | 0 | 2705 | | Array.Clear(x, xStart, x.Length - xStart); |
| | 0 | 2706 | | } |
| | | 2707 | | |
| | 0 | 2708 | | return x; |
| | 0 | 2709 | | } |
| | | 2710 | | |
| | | 2711 | | public BigInteger Remainder( |
| | | 2712 | | BigInteger n) |
| | 91539 | 2713 | | { |
| | 91539 | 2714 | | if (n.sign == 0) |
| | 0 | 2715 | | throw new ArithmeticException("Division by zero error"); |
| | | 2716 | | |
| | 91539 | 2717 | | if (this.sign == 0) |
| | 126 | 2718 | | return Zero; |
| | | 2719 | | |
| | | 2720 | | // For small values, use fast remainder method |
| | 91413 | 2721 | | if (n.magnitude.Length == 1) |
| | 0 | 2722 | | { |
| | 0 | 2723 | | int val = n.magnitude[0]; |
| | | 2724 | | |
| | 0 | 2725 | | if (val > 0) |
| | 0 | 2726 | | { |
| | 0 | 2727 | | if (val == 1) |
| | 0 | 2728 | | return Zero; |
| | | 2729 | | |
| | | 2730 | | // TODO Make this func work on uint, and handle val == 1? |
| | 0 | 2731 | | int rem = Remainder(val); |
| | | 2732 | | |
| | 0 | 2733 | | return rem == 0 |
| | 0 | 2734 | | ? Zero |
| | 0 | 2735 | | : new BigInteger(sign, new int[]{ rem }, false); |
| | | 2736 | | } |
| | 0 | 2737 | | } |
| | | 2738 | | |
| | 91413 | 2739 | | if (CompareNoLeadingZeroes(0, magnitude, 0, n.magnitude) < 0) |
| | 577 | 2740 | | return this; |
| | | 2741 | | |
| | | 2742 | | int[] result; |
| | 90836 | 2743 | | if (n.QuickPow2Check()) // n is power of two |
| | 90836 | 2744 | | { |
| | | 2745 | | // TODO Move before small values branch above? |
| | 90836 | 2746 | | result = LastNBits(n.Abs().BitLength - 1); |
| | 90836 | 2747 | | } |
| | | 2748 | | else |
| | 0 | 2749 | | { |
| | 0 | 2750 | | result = (int[]) this.magnitude.Clone(); |
| | 0 | 2751 | | result = Remainder(result, n.magnitude); |
| | 0 | 2752 | | } |
| | | 2753 | | |
| | 90836 | 2754 | | return new BigInteger(sign, result, true); |
| | 91539 | 2755 | | } |
| | | 2756 | | |
| | | 2757 | | private int[] LastNBits( |
| | | 2758 | | int n) |
| | 90836 | 2759 | | { |
| | 90836 | 2760 | | if (n < 1) |
| | 0 | 2761 | | return ZeroMagnitude; |
| | | 2762 | | |
| | 90836 | 2763 | | int numWords = (n + BitsPerInt - 1) / BitsPerInt; |
| | 90836 | 2764 | | numWords = System.Math.Min(numWords, this.magnitude.Length); |
| | 90836 | 2765 | | int[] result = new int[numWords]; |
| | | 2766 | | |
| | 90836 | 2767 | | Array.Copy(this.magnitude, this.magnitude.Length - numWords, result, 0, numWords); |
| | | 2768 | | |
| | 90836 | 2769 | | int excessBits = (numWords << 5) - n; |
| | 90836 | 2770 | | if (excessBits > 0) |
| | 25605 | 2771 | | { |
| | 25605 | 2772 | | result[0] &= (int)(UInt32.MaxValue >> excessBits); |
| | 25605 | 2773 | | } |
| | | 2774 | | |
| | 90836 | 2775 | | return result; |
| | 90836 | 2776 | | } |
| | | 2777 | | |
| | | 2778 | | private BigInteger DivideWords(int w) |
| | 0 | 2779 | | { |
| | 0 | 2780 | | Debug.Assert(w >= 0); |
| | 0 | 2781 | | int n = magnitude.Length; |
| | 0 | 2782 | | if (w >= n) |
| | 0 | 2783 | | return Zero; |
| | 0 | 2784 | | int[] mag = new int[n - w]; |
| | 0 | 2785 | | Array.Copy(magnitude, 0, mag, 0, n - w); |
| | 0 | 2786 | | return new BigInteger(sign, mag, false); |
| | 0 | 2787 | | } |
| | | 2788 | | |
| | | 2789 | | private BigInteger RemainderWords(int w) |
| | 0 | 2790 | | { |
| | 0 | 2791 | | Debug.Assert(w >= 0); |
| | 0 | 2792 | | int n = magnitude.Length; |
| | 0 | 2793 | | if (w >= n) |
| | 0 | 2794 | | return this; |
| | 0 | 2795 | | int[] mag = new int[w]; |
| | 0 | 2796 | | Array.Copy(magnitude, n - w, mag, 0, w); |
| | 0 | 2797 | | return new BigInteger(sign, mag, false); |
| | 0 | 2798 | | } |
| | | 2799 | | |
| | | 2800 | | /** |
| | | 2801 | | * do a left shift - this returns a new array. |
| | | 2802 | | */ |
| | | 2803 | | private static int[] ShiftLeft( |
| | | 2804 | | int[] mag, |
| | | 2805 | | int n) |
| | 59375 | 2806 | | { |
| | 59375 | 2807 | | int nInts = (int)((uint)n >> 5); |
| | 59375 | 2808 | | int nBits = n & 0x1f; |
| | 59375 | 2809 | | int magLen = mag.Length; |
| | | 2810 | | int[] newMag; |
| | | 2811 | | |
| | 59375 | 2812 | | if (nBits == 0) |
| | 33373 | 2813 | | { |
| | 33373 | 2814 | | newMag = new int[magLen + nInts]; |
| | 33373 | 2815 | | mag.CopyTo(newMag, 0); |
| | 33373 | 2816 | | } |
| | | 2817 | | else |
| | 26002 | 2818 | | { |
| | 26002 | 2819 | | int i = 0; |
| | 26002 | 2820 | | int nBits2 = 32 - nBits; |
| | 26002 | 2821 | | int highBits = (int)((uint)mag[0] >> nBits2); |
| | | 2822 | | |
| | 26002 | 2823 | | if (highBits != 0) |
| | 2 | 2824 | | { |
| | 2 | 2825 | | newMag = new int[magLen + nInts + 1]; |
| | 2 | 2826 | | newMag[i++] = highBits; |
| | 2 | 2827 | | } |
| | | 2828 | | else |
| | 26000 | 2829 | | { |
| | 26000 | 2830 | | newMag = new int[magLen + nInts]; |
| | 26000 | 2831 | | } |
| | | 2832 | | |
| | 26002 | 2833 | | int m = mag[0]; |
| | 52222 | 2834 | | for (int j = 0; j < magLen - 1; j++) |
| | 109 | 2835 | | { |
| | 109 | 2836 | | int next = mag[j + 1]; |
| | | 2837 | | |
| | 109 | 2838 | | newMag[i++] = (m << nBits) | (int)((uint)next >> nBits2); |
| | 109 | 2839 | | m = next; |
| | 109 | 2840 | | } |
| | | 2841 | | |
| | 26002 | 2842 | | newMag[i] = mag[magLen - 1] << nBits; |
| | 26002 | 2843 | | } |
| | | 2844 | | |
| | 59375 | 2845 | | return newMag; |
| | 59375 | 2846 | | } |
| | | 2847 | | |
| | | 2848 | | private static int ShiftLeftOneInPlace(int[] x, int carry) |
| | 0 | 2849 | | { |
| | 0 | 2850 | | Debug.Assert(carry == 0 || carry == 1); |
| | 0 | 2851 | | int pos = x.Length; |
| | 0 | 2852 | | while (--pos >= 0) |
| | 0 | 2853 | | { |
| | 0 | 2854 | | uint val = (uint)x[pos]; |
| | 0 | 2855 | | x[pos] = (int)(val << 1) | carry; |
| | 0 | 2856 | | carry = (int)(val >> 31); |
| | 0 | 2857 | | } |
| | 0 | 2858 | | return carry; |
| | 0 | 2859 | | } |
| | | 2860 | | |
| | | 2861 | | public BigInteger ShiftLeft( |
| | | 2862 | | int n) |
| | 59402 | 2863 | | { |
| | 59402 | 2864 | | if (sign == 0 || magnitude.Length == 0) |
| | 0 | 2865 | | return Zero; |
| | | 2866 | | |
| | 59402 | 2867 | | if (n == 0) |
| | 28 | 2868 | | return this; |
| | | 2869 | | |
| | 59374 | 2870 | | if (n < 0) |
| | 0 | 2871 | | return ShiftRight(-n); |
| | | 2872 | | |
| | 59374 | 2873 | | BigInteger result = new BigInteger(sign, ShiftLeft(magnitude, n), true); |
| | | 2874 | | |
| | 59374 | 2875 | | if (this.nBits != -1) |
| | 59365 | 2876 | | { |
| | 59365 | 2877 | | result.nBits = sign > 0 |
| | 59365 | 2878 | | ? this.nBits |
| | 59365 | 2879 | | : this.nBits + n; |
| | 59365 | 2880 | | } |
| | | 2881 | | |
| | 59374 | 2882 | | if (this.nBitLength != -1) |
| | 59363 | 2883 | | { |
| | 59363 | 2884 | | result.nBitLength = this.nBitLength + n; |
| | 59363 | 2885 | | } |
| | | 2886 | | |
| | 59374 | 2887 | | return result; |
| | 59402 | 2888 | | } |
| | | 2889 | | |
| | | 2890 | | /** |
| | | 2891 | | * do a right shift - this does it in place. |
| | | 2892 | | */ |
| | | 2893 | | private static void ShiftRightInPlace( |
| | | 2894 | | int start, |
| | | 2895 | | int[] mag, |
| | | 2896 | | int n) |
| | 4 | 2897 | | { |
| | 4 | 2898 | | int nInts = (int)((uint)n >> 5) + start; |
| | 4 | 2899 | | int nBits = n & 0x1f; |
| | 4 | 2900 | | int magEnd = mag.Length - 1; |
| | | 2901 | | |
| | 4 | 2902 | | if (nInts != start) |
| | 4 | 2903 | | { |
| | 4 | 2904 | | int delta = (nInts - start); |
| | | 2905 | | |
| | 76 | 2906 | | for (int i = magEnd; i >= nInts; i--) |
| | 34 | 2907 | | { |
| | 34 | 2908 | | mag[i] = mag[i - delta]; |
| | 34 | 2909 | | } |
| | 16 | 2910 | | for (int i = nInts - 1; i >= start; i--) |
| | 4 | 2911 | | { |
| | 4 | 2912 | | mag[i] = 0; |
| | 4 | 2913 | | } |
| | 4 | 2914 | | } |
| | | 2915 | | |
| | 4 | 2916 | | if (nBits != 0) |
| | 2 | 2917 | | { |
| | 2 | 2918 | | int nBits2 = 32 - nBits; |
| | 2 | 2919 | | int m = mag[magEnd]; |
| | | 2920 | | |
| | 22 | 2921 | | for (int i = magEnd; i > nInts; --i) |
| | 9 | 2922 | | { |
| | 9 | 2923 | | int next = mag[i - 1]; |
| | | 2924 | | |
| | 9 | 2925 | | mag[i] = (int)((uint)m >> nBits) | (next << nBits2); |
| | 9 | 2926 | | m = next; |
| | 9 | 2927 | | } |
| | | 2928 | | |
| | 2 | 2929 | | mag[nInts] = (int)((uint)mag[nInts] >> nBits); |
| | 2 | 2930 | | } |
| | 4 | 2931 | | } |
| | | 2932 | | |
| | | 2933 | | /** |
| | | 2934 | | * do a right shift by one - this does it in place. |
| | | 2935 | | */ |
| | | 2936 | | private static void ShiftRightOneInPlace( |
| | | 2937 | | int start, |
| | | 2938 | | int[] mag) |
| | 326 | 2939 | | { |
| | 326 | 2940 | | int i = mag.Length; |
| | 326 | 2941 | | int m = mag[i - 1]; |
| | | 2942 | | |
| | 2931 | 2943 | | while (--i > start) |
| | 2605 | 2944 | | { |
| | 2605 | 2945 | | int next = mag[i - 1]; |
| | 2605 | 2946 | | mag[i] = ((int)((uint)m >> 1)) | (next << 31); |
| | 2605 | 2947 | | m = next; |
| | 2605 | 2948 | | } |
| | | 2949 | | |
| | 326 | 2950 | | mag[start] = (int)((uint)mag[start] >> 1); |
| | 326 | 2951 | | } |
| | | 2952 | | |
| | | 2953 | | public BigInteger ShiftRight( |
| | | 2954 | | int n) |
| | 92979 | 2955 | | { |
| | 92979 | 2956 | | if (n == 0) |
| | 3 | 2957 | | return this; |
| | | 2958 | | |
| | 92976 | 2959 | | if (n < 0) |
| | 0 | 2960 | | return ShiftLeft(-n); |
| | | 2961 | | |
| | 92976 | 2962 | | if (n >= BitLength) |
| | 203 | 2963 | | return (this.sign < 0 ? One.Negate() : Zero); |
| | | 2964 | | |
| | | 2965 | | // int[] res = (int[]) this.magnitude.Clone(); |
| | | 2966 | | // |
| | | 2967 | | // ShiftRightInPlace(0, res, n); |
| | | 2968 | | // |
| | | 2969 | | // return new BigInteger(this.sign, res, true); |
| | | 2970 | | |
| | 92773 | 2971 | | int resultLength = (BitLength - n + 31) >> 5; |
| | 92773 | 2972 | | int[] res = new int[resultLength]; |
| | | 2973 | | |
| | 92773 | 2974 | | int numInts = n >> 5; |
| | 92773 | 2975 | | int numBits = n & 31; |
| | | 2976 | | |
| | 92773 | 2977 | | if (numBits == 0) |
| | 66651 | 2978 | | { |
| | 66651 | 2979 | | Array.Copy(this.magnitude, 0, res, 0, res.Length); |
| | 66651 | 2980 | | } |
| | | 2981 | | else |
| | 26122 | 2982 | | { |
| | 26122 | 2983 | | int numBits2 = 32 - numBits; |
| | | 2984 | | |
| | 26122 | 2985 | | int magPos = this.magnitude.Length - 1 - numInts; |
| | 921812 | 2986 | | for (int i = resultLength - 1; i >= 0; --i) |
| | 434784 | 2987 | | { |
| | 434784 | 2988 | | res[i] = (int)((uint) this.magnitude[magPos--] >> numBits); |
| | | 2989 | | |
| | 434784 | 2990 | | if (magPos >= 0) |
| | 409432 | 2991 | | { |
| | 409432 | 2992 | | res[i] |= this.magnitude[magPos] << numBits2; |
| | 409432 | 2993 | | } |
| | 434784 | 2994 | | } |
| | 26122 | 2995 | | } |
| | | 2996 | | |
| | 92773 | 2997 | | Debug.Assert(res[0] != 0); |
| | | 2998 | | |
| | 92773 | 2999 | | return new BigInteger(this.sign, res, false); |
| | 92979 | 3000 | | } |
| | | 3001 | | |
| | | 3002 | | public int SignValue |
| | | 3003 | | { |
| | 889563 | 3004 | | get { return sign; } |
| | | 3005 | | } |
| | | 3006 | | |
| | | 3007 | | /** |
| | | 3008 | | * returns x = x - y - we assume x is >= y |
| | | 3009 | | */ |
| | | 3010 | | private static int[] Subtract( |
| | | 3011 | | int xStart, |
| | | 3012 | | int[] x, |
| | | 3013 | | int yStart, |
| | | 3014 | | int[] y) |
| | 77980 | 3015 | | { |
| | 77980 | 3016 | | Debug.Assert(yStart < y.Length); |
| | 77980 | 3017 | | Debug.Assert(x.Length - xStart >= y.Length - yStart); |
| | | 3018 | | |
| | 77980 | 3019 | | int iT = x.Length; |
| | 77980 | 3020 | | int iV = y.Length; |
| | | 3021 | | long m; |
| | 77980 | 3022 | | int borrow = 0; |
| | | 3023 | | |
| | | 3024 | | do |
| | 1024390 | 3025 | | { |
| | 1024390 | 3026 | | m = (x[--iT] & IMASK) - (y[--iV] & IMASK) + borrow; |
| | 1024390 | 3027 | | x[iT] = (int) m; |
| | | 3028 | | |
| | | 3029 | | // borrow = (m < 0) ? -1 : 0; |
| | 1024390 | 3030 | | borrow = (int)(m >> 63); |
| | 1024390 | 3031 | | } |
| | 1024390 | 3032 | | while (iV > yStart); |
| | | 3033 | | |
| | 77980 | 3034 | | if (borrow != 0) |
| | 11883 | 3035 | | { |
| | 11883 | 3036 | | while (--x[--iT] == -1) |
| | 0 | 3037 | | { |
| | 0 | 3038 | | } |
| | 11883 | 3039 | | } |
| | | 3040 | | |
| | 77980 | 3041 | | return x; |
| | 77980 | 3042 | | } |
| | | 3043 | | |
| | | 3044 | | public BigInteger Subtract( |
| | | 3045 | | BigInteger n) |
| | 77946 | 3046 | | { |
| | 77946 | 3047 | | if (n.sign == 0) |
| | 126 | 3048 | | return this; |
| | | 3049 | | |
| | 77820 | 3050 | | if (this.sign == 0) |
| | 0 | 3051 | | return n.Negate(); |
| | | 3052 | | |
| | 77820 | 3053 | | if (this.sign != n.sign) |
| | 0 | 3054 | | return Add(n.Negate()); |
| | | 3055 | | |
| | 77820 | 3056 | | int compare = CompareNoLeadingZeroes(0, magnitude, 0, n.magnitude); |
| | 77820 | 3057 | | if (compare == 0) |
| | 0 | 3058 | | return Zero; |
| | | 3059 | | |
| | | 3060 | | BigInteger bigun, lilun; |
| | 77820 | 3061 | | if (compare < 0) |
| | 10931 | 3062 | | { |
| | 10931 | 3063 | | bigun = n; |
| | 10931 | 3064 | | lilun = this; |
| | 10931 | 3065 | | } |
| | | 3066 | | else |
| | 66889 | 3067 | | { |
| | 66889 | 3068 | | bigun = this; |
| | 66889 | 3069 | | lilun = n; |
| | 66889 | 3070 | | } |
| | | 3071 | | |
| | 77820 | 3072 | | return new BigInteger(this.sign * compare, doSubBigLil(bigun.magnitude, lilun.magnitude), true); |
| | 77946 | 3073 | | } |
| | | 3074 | | |
| | | 3075 | | private static int[] doSubBigLil( |
| | | 3076 | | int[] bigMag, |
| | | 3077 | | int[] lilMag) |
| | 77820 | 3078 | | { |
| | 77820 | 3079 | | int[] res = (int[]) bigMag.Clone(); |
| | | 3080 | | |
| | 77820 | 3081 | | return Subtract(0, res, 0, lilMag); |
| | 77820 | 3082 | | } |
| | | 3083 | | |
| | | 3084 | | public byte[] ToByteArray() |
| | 393 | 3085 | | { |
| | 393 | 3086 | | return ToByteArray(false); |
| | 393 | 3087 | | } |
| | | 3088 | | |
| | | 3089 | | public byte[] ToByteArrayUnsigned() |
| | 36 | 3090 | | { |
| | 36 | 3091 | | return ToByteArray(true); |
| | 36 | 3092 | | } |
| | | 3093 | | |
| | | 3094 | | private byte[] ToByteArray( |
| | | 3095 | | bool unsigned) |
| | 429 | 3096 | | { |
| | 429 | 3097 | | if (sign == 0) |
| | 0 | 3098 | | return unsigned ? ZeroEncoding : new byte[1]; |
| | | 3099 | | |
| | 429 | 3100 | | int nBits = (unsigned && sign > 0) |
| | 429 | 3101 | | ? BitLength |
| | 429 | 3102 | | : BitLength + 1; |
| | | 3103 | | |
| | 429 | 3104 | | int nBytes = GetByteLength(nBits); |
| | 429 | 3105 | | byte[] bytes = new byte[nBytes]; |
| | | 3106 | | |
| | 429 | 3107 | | int magIndex = magnitude.Length; |
| | 429 | 3108 | | int bytesIndex = bytes.Length; |
| | | 3109 | | |
| | 429 | 3110 | | if (sign > 0) |
| | 429 | 3111 | | { |
| | 5291 | 3112 | | while (magIndex > 1) |
| | 4862 | 3113 | | { |
| | 4862 | 3114 | | uint mag = (uint) magnitude[--magIndex]; |
| | 4862 | 3115 | | bytes[--bytesIndex] = (byte) mag; |
| | 4862 | 3116 | | bytes[--bytesIndex] = (byte)(mag >> 8); |
| | 4862 | 3117 | | bytes[--bytesIndex] = (byte)(mag >> 16); |
| | 4862 | 3118 | | bytes[--bytesIndex] = (byte)(mag >> 24); |
| | 4862 | 3119 | | } |
| | | 3120 | | |
| | 429 | 3121 | | uint lastMag = (uint) magnitude[0]; |
| | 1362 | 3122 | | while (lastMag > byte.MaxValue) |
| | 933 | 3123 | | { |
| | 933 | 3124 | | bytes[--bytesIndex] = (byte) lastMag; |
| | 933 | 3125 | | lastMag >>= 8; |
| | 933 | 3126 | | } |
| | | 3127 | | |
| | 429 | 3128 | | bytes[--bytesIndex] = (byte) lastMag; |
| | 429 | 3129 | | } |
| | | 3130 | | else // sign < 0 |
| | 0 | 3131 | | { |
| | 0 | 3132 | | bool carry = true; |
| | | 3133 | | |
| | 0 | 3134 | | while (magIndex > 1) |
| | 0 | 3135 | | { |
| | 0 | 3136 | | uint mag = ~((uint) magnitude[--magIndex]); |
| | | 3137 | | |
| | 0 | 3138 | | if (carry) |
| | 0 | 3139 | | { |
| | 0 | 3140 | | carry = (++mag == uint.MinValue); |
| | 0 | 3141 | | } |
| | | 3142 | | |
| | 0 | 3143 | | bytes[--bytesIndex] = (byte) mag; |
| | 0 | 3144 | | bytes[--bytesIndex] = (byte)(mag >> 8); |
| | 0 | 3145 | | bytes[--bytesIndex] = (byte)(mag >> 16); |
| | 0 | 3146 | | bytes[--bytesIndex] = (byte)(mag >> 24); |
| | 0 | 3147 | | } |
| | | 3148 | | |
| | 0 | 3149 | | uint lastMag = (uint) magnitude[0]; |
| | | 3150 | | |
| | 0 | 3151 | | if (carry) |
| | 0 | 3152 | | { |
| | | 3153 | | // Never wraps because magnitude[0] != 0 |
| | 0 | 3154 | | --lastMag; |
| | 0 | 3155 | | } |
| | | 3156 | | |
| | 0 | 3157 | | while (lastMag > byte.MaxValue) |
| | 0 | 3158 | | { |
| | 0 | 3159 | | bytes[--bytesIndex] = (byte) ~lastMag; |
| | 0 | 3160 | | lastMag >>= 8; |
| | 0 | 3161 | | } |
| | | 3162 | | |
| | 0 | 3163 | | bytes[--bytesIndex] = (byte) ~lastMag; |
| | | 3164 | | |
| | 0 | 3165 | | if (bytesIndex > 0) |
| | 0 | 3166 | | { |
| | 0 | 3167 | | bytes[--bytesIndex] = byte.MaxValue; |
| | 0 | 3168 | | } |
| | 0 | 3169 | | } |
| | | 3170 | | |
| | 429 | 3171 | | return bytes; |
| | 429 | 3172 | | } |
| | | 3173 | | |
| | | 3174 | | public override string ToString() |
| | 0 | 3175 | | { |
| | 0 | 3176 | | return ToString(10); |
| | 0 | 3177 | | } |
| | | 3178 | | |
| | | 3179 | | public string ToString(int radix) |
| | 0 | 3180 | | { |
| | | 3181 | | // TODO Make this method work for other radices (ideally 2 <= radix <= 36 as in Java) |
| | | 3182 | | |
| | 0 | 3183 | | switch (radix) |
| | | 3184 | | { |
| | | 3185 | | case 2: |
| | | 3186 | | case 8: |
| | | 3187 | | case 10: |
| | | 3188 | | case 16: |
| | 0 | 3189 | | break; |
| | | 3190 | | default: |
| | 0 | 3191 | | throw new FormatException("Only bases 2, 8, 10, 16 are allowed"); |
| | | 3192 | | } |
| | | 3193 | | |
| | | 3194 | | // NB: Can only happen to internally managed instances |
| | 0 | 3195 | | if (magnitude == null) |
| | 0 | 3196 | | return "null"; |
| | | 3197 | | |
| | 0 | 3198 | | if (sign == 0) |
| | 0 | 3199 | | return "0"; |
| | | 3200 | | |
| | | 3201 | | |
| | | 3202 | | // NOTE: This *should* be unnecessary, since the magnitude *should* never have leading zero digits |
| | 0 | 3203 | | int firstNonZero = 0; |
| | 0 | 3204 | | while (firstNonZero < magnitude.Length) |
| | 0 | 3205 | | { |
| | 0 | 3206 | | if (magnitude[firstNonZero] != 0) |
| | 0 | 3207 | | { |
| | 0 | 3208 | | break; |
| | | 3209 | | } |
| | 0 | 3210 | | ++firstNonZero; |
| | 0 | 3211 | | } |
| | | 3212 | | |
| | 0 | 3213 | | if (firstNonZero == magnitude.Length) |
| | 0 | 3214 | | { |
| | 0 | 3215 | | return "0"; |
| | | 3216 | | } |
| | | 3217 | | |
| | | 3218 | | |
| | 0 | 3219 | | StringBuilder sb = new StringBuilder(); |
| | 0 | 3220 | | if (sign == -1) |
| | 0 | 3221 | | { |
| | 0 | 3222 | | sb.Append('-'); |
| | 0 | 3223 | | } |
| | | 3224 | | |
| | 0 | 3225 | | switch (radix) |
| | | 3226 | | { |
| | | 3227 | | case 2: |
| | 0 | 3228 | | { |
| | 0 | 3229 | | int pos = firstNonZero; |
| | 0 | 3230 | | sb.Append(Convert.ToString(magnitude[pos], 2)); |
| | 0 | 3231 | | while (++pos < magnitude.Length) |
| | 0 | 3232 | | { |
| | 0 | 3233 | | AppendZeroExtendedString(sb, Convert.ToString(magnitude[pos], 2), 32); |
| | 0 | 3234 | | } |
| | 0 | 3235 | | break; |
| | | 3236 | | } |
| | | 3237 | | case 8: |
| | 0 | 3238 | | { |
| | 0 | 3239 | | int mask = (1 << 30) - 1; |
| | 0 | 3240 | | BigInteger u = this.Abs(); |
| | 0 | 3241 | | int bits = u.BitLength; |
| | 0 | 3242 | | IList S = new List<object>(); |
| | 0 | 3243 | | while (bits > 30) |
| | 0 | 3244 | | { |
| | 0 | 3245 | | S.Add(Convert.ToString(u.IntValue & mask, 8)); |
| | 0 | 3246 | | u = u.ShiftRight(30); |
| | 0 | 3247 | | bits -= 30; |
| | 0 | 3248 | | } |
| | 0 | 3249 | | sb.Append(Convert.ToString(u.IntValue, 8)); |
| | 0 | 3250 | | for (int i = S.Count - 1; i >= 0; --i) |
| | 0 | 3251 | | { |
| | 0 | 3252 | | AppendZeroExtendedString(sb, (string)S[i], 10); |
| | 0 | 3253 | | } |
| | 0 | 3254 | | break; |
| | | 3255 | | } |
| | | 3256 | | case 16: |
| | 0 | 3257 | | { |
| | 0 | 3258 | | int pos = firstNonZero; |
| | 0 | 3259 | | sb.Append(Convert.ToString(magnitude[pos], 16)); |
| | 0 | 3260 | | while (++pos < magnitude.Length) |
| | 0 | 3261 | | { |
| | 0 | 3262 | | AppendZeroExtendedString(sb, Convert.ToString(magnitude[pos], 16), 8); |
| | 0 | 3263 | | } |
| | 0 | 3264 | | break; |
| | | 3265 | | } |
| | | 3266 | | // TODO This could work for other radices if there is an alternative to Convert.ToString method |
| | | 3267 | | //default: |
| | | 3268 | | case 10: |
| | 0 | 3269 | | { |
| | 0 | 3270 | | BigInteger q = this.Abs(); |
| | 0 | 3271 | | if (q.BitLength < 64) |
| | 0 | 3272 | | { |
| | 0 | 3273 | | sb.Append(Convert.ToString(q.LongValue, radix)); |
| | 0 | 3274 | | break; |
| | | 3275 | | } |
| | | 3276 | | |
| | | 3277 | | // TODO Could cache the moduli for each radix (soft reference?) |
| | 0 | 3278 | | IList moduli = new List<object>(); |
| | 0 | 3279 | | BigInteger R = BigInteger.ValueOf(radix); |
| | 0 | 3280 | | while (R.CompareTo(q) <= 0) |
| | 0 | 3281 | | { |
| | 0 | 3282 | | moduli.Add(R); |
| | 0 | 3283 | | R = R.Square(); |
| | 0 | 3284 | | } |
| | | 3285 | | |
| | 0 | 3286 | | int scale = moduli.Count; |
| | 0 | 3287 | | sb.EnsureCapacity(sb.Length + (1 << scale)); |
| | | 3288 | | |
| | 0 | 3289 | | ToString(sb, radix, moduli, scale, q); |
| | | 3290 | | |
| | 0 | 3291 | | break; |
| | | 3292 | | } |
| | | 3293 | | } |
| | | 3294 | | |
| | 0 | 3295 | | return sb.ToString(); |
| | 0 | 3296 | | } |
| | | 3297 | | |
| | | 3298 | | private static void ToString(StringBuilder sb, int radix, IList moduli, int scale, BigInteger pos) |
| | 0 | 3299 | | { |
| | 0 | 3300 | | if (pos.BitLength < 64) |
| | 0 | 3301 | | { |
| | 0 | 3302 | | string s = Convert.ToString(pos.LongValue, radix); |
| | 0 | 3303 | | if (sb.Length > 1 || (sb.Length == 1 && sb[0] != '-')) |
| | 0 | 3304 | | { |
| | 0 | 3305 | | AppendZeroExtendedString(sb, s, 1 << scale); |
| | 0 | 3306 | | } |
| | 0 | 3307 | | else if (pos.SignValue != 0) |
| | 0 | 3308 | | { |
| | 0 | 3309 | | sb.Append(s); |
| | 0 | 3310 | | } |
| | 0 | 3311 | | return; |
| | | 3312 | | } |
| | | 3313 | | |
| | 0 | 3314 | | BigInteger[] qr = pos.DivideAndRemainder((BigInteger)moduli[--scale]); |
| | | 3315 | | |
| | 0 | 3316 | | ToString(sb, radix, moduli, scale, qr[0]); |
| | 0 | 3317 | | ToString(sb, radix, moduli, scale, qr[1]); |
| | 0 | 3318 | | } |
| | | 3319 | | |
| | | 3320 | | private static void AppendZeroExtendedString(StringBuilder sb, string s, int minLength) |
| | 0 | 3321 | | { |
| | 0 | 3322 | | for (int len = s.Length; len < minLength; ++len) |
| | 0 | 3323 | | { |
| | 0 | 3324 | | sb.Append('0'); |
| | 0 | 3325 | | } |
| | 0 | 3326 | | sb.Append(s); |
| | 0 | 3327 | | } |
| | | 3328 | | |
| | | 3329 | | private static BigInteger CreateUValueOf( |
| | | 3330 | | ulong value) |
| | 16 | 3331 | | { |
| | 16 | 3332 | | int msw = (int)(value >> 32); |
| | 16 | 3333 | | int lsw = (int)value; |
| | | 3334 | | |
| | 16 | 3335 | | if (msw != 0) |
| | 0 | 3336 | | return new BigInteger(1, new int[] { msw, lsw }, false); |
| | | 3337 | | |
| | 16 | 3338 | | if (lsw != 0) |
| | 16 | 3339 | | { |
| | 16 | 3340 | | BigInteger n = new BigInteger(1, new int[] { lsw }, false); |
| | | 3341 | | // Check for a power of two |
| | 16 | 3342 | | if ((lsw & -lsw) == lsw) |
| | 5 | 3343 | | { |
| | 5 | 3344 | | n.nBits = 1; |
| | 5 | 3345 | | } |
| | 16 | 3346 | | return n; |
| | | 3347 | | } |
| | | 3348 | | |
| | 0 | 3349 | | return Zero; |
| | 16 | 3350 | | } |
| | | 3351 | | |
| | | 3352 | | private static BigInteger CreateValueOf( |
| | | 3353 | | long value) |
| | 0 | 3354 | | { |
| | 0 | 3355 | | if (value < 0) |
| | 0 | 3356 | | { |
| | 0 | 3357 | | if (value == long.MinValue) |
| | 0 | 3358 | | return CreateValueOf(~value).Not(); |
| | | 3359 | | |
| | 0 | 3360 | | return CreateValueOf(-value).Negate(); |
| | | 3361 | | } |
| | | 3362 | | |
| | 0 | 3363 | | return CreateUValueOf((ulong)value); |
| | 0 | 3364 | | } |
| | | 3365 | | |
| | | 3366 | | public static BigInteger ValueOf( |
| | | 3367 | | long value) |
| | 4 | 3368 | | { |
| | 4 | 3369 | | if (value >= 0 && value < SMALL_CONSTANTS.Length) |
| | 4 | 3370 | | { |
| | 4 | 3371 | | return SMALL_CONSTANTS[value]; |
| | | 3372 | | } |
| | | 3373 | | |
| | 0 | 3374 | | return CreateValueOf(value); |
| | 4 | 3375 | | } |
| | | 3376 | | |
| | | 3377 | | public int GetLowestSetBit() |
| | 0 | 3378 | | { |
| | 0 | 3379 | | if (this.sign == 0) |
| | 0 | 3380 | | return -1; |
| | | 3381 | | |
| | 0 | 3382 | | return GetLowestSetBitMaskFirst(-1); |
| | 0 | 3383 | | } |
| | | 3384 | | |
| | | 3385 | | private int GetLowestSetBitMaskFirst(int firstWordMask) |
| | 0 | 3386 | | { |
| | 0 | 3387 | | int w = magnitude.Length, offset = 0; |
| | | 3388 | | |
| | 0 | 3389 | | uint word = (uint)(magnitude[--w] & firstWordMask); |
| | 0 | 3390 | | Debug.Assert(magnitude[0] != 0); |
| | | 3391 | | |
| | 0 | 3392 | | while (word == 0) |
| | 0 | 3393 | | { |
| | 0 | 3394 | | word = (uint)magnitude[--w]; |
| | 0 | 3395 | | offset += 32; |
| | 0 | 3396 | | } |
| | | 3397 | | |
| | 0 | 3398 | | while ((word & 0xFF) == 0) |
| | 0 | 3399 | | { |
| | 0 | 3400 | | word >>= 8; |
| | 0 | 3401 | | offset += 8; |
| | 0 | 3402 | | } |
| | | 3403 | | |
| | 0 | 3404 | | while ((word & 1) == 0) |
| | 0 | 3405 | | { |
| | 0 | 3406 | | word >>= 1; |
| | 0 | 3407 | | ++offset; |
| | 0 | 3408 | | } |
| | | 3409 | | |
| | 0 | 3410 | | return offset; |
| | 0 | 3411 | | } |
| | | 3412 | | |
| | | 3413 | | public bool TestBit( |
| | | 3414 | | int n) |
| | 1026 | 3415 | | { |
| | 1026 | 3416 | | if (n < 0) |
| | 0 | 3417 | | throw new ArithmeticException("Bit position must not be negative"); |
| | | 3418 | | |
| | 1026 | 3419 | | if (sign < 0) |
| | 0 | 3420 | | return !Not().TestBit(n); |
| | | 3421 | | |
| | 1026 | 3422 | | int wordNum = n / 32; |
| | 1026 | 3423 | | if (wordNum >= magnitude.Length) |
| | 0 | 3424 | | return false; |
| | | 3425 | | |
| | 1026 | 3426 | | int word = magnitude[magnitude.Length - 1 - wordNum]; |
| | 1026 | 3427 | | return ((word >> (n % 32)) & 1) > 0; |
| | 1026 | 3428 | | } |
| | | 3429 | | |
| | | 3430 | | public BigInteger Or( |
| | | 3431 | | BigInteger value) |
| | 0 | 3432 | | { |
| | 0 | 3433 | | if (this.sign == 0) |
| | 0 | 3434 | | return value; |
| | | 3435 | | |
| | 0 | 3436 | | if (value.sign == 0) |
| | 0 | 3437 | | return this; |
| | | 3438 | | |
| | 0 | 3439 | | int[] aMag = this.sign > 0 |
| | 0 | 3440 | | ? this.magnitude |
| | 0 | 3441 | | : Add(One).magnitude; |
| | | 3442 | | |
| | 0 | 3443 | | int[] bMag = value.sign > 0 |
| | 0 | 3444 | | ? value.magnitude |
| | 0 | 3445 | | : value.Add(One).magnitude; |
| | | 3446 | | |
| | 0 | 3447 | | bool resultNeg = sign < 0 || value.sign < 0; |
| | 0 | 3448 | | int resultLength = System.Math.Max(aMag.Length, bMag.Length); |
| | 0 | 3449 | | int[] resultMag = new int[resultLength]; |
| | | 3450 | | |
| | 0 | 3451 | | int aStart = resultMag.Length - aMag.Length; |
| | 0 | 3452 | | int bStart = resultMag.Length - bMag.Length; |
| | | 3453 | | |
| | 0 | 3454 | | for (int i = 0; i < resultMag.Length; ++i) |
| | 0 | 3455 | | { |
| | 0 | 3456 | | int aWord = i >= aStart ? aMag[i - aStart] : 0; |
| | 0 | 3457 | | int bWord = i >= bStart ? bMag[i - bStart] : 0; |
| | | 3458 | | |
| | 0 | 3459 | | if (this.sign < 0) |
| | 0 | 3460 | | { |
| | 0 | 3461 | | aWord = ~aWord; |
| | 0 | 3462 | | } |
| | | 3463 | | |
| | 0 | 3464 | | if (value.sign < 0) |
| | 0 | 3465 | | { |
| | 0 | 3466 | | bWord = ~bWord; |
| | 0 | 3467 | | } |
| | | 3468 | | |
| | 0 | 3469 | | resultMag[i] = aWord | bWord; |
| | | 3470 | | |
| | 0 | 3471 | | if (resultNeg) |
| | 0 | 3472 | | { |
| | 0 | 3473 | | resultMag[i] = ~resultMag[i]; |
| | 0 | 3474 | | } |
| | 0 | 3475 | | } |
| | | 3476 | | |
| | 0 | 3477 | | BigInteger result = new BigInteger(1, resultMag, true); |
| | | 3478 | | |
| | | 3479 | | // TODO Optimise this case |
| | 0 | 3480 | | if (resultNeg) |
| | 0 | 3481 | | { |
| | 0 | 3482 | | result = result.Not(); |
| | 0 | 3483 | | } |
| | | 3484 | | |
| | 0 | 3485 | | return result; |
| | 0 | 3486 | | } |
| | | 3487 | | |
| | | 3488 | | public BigInteger Xor( |
| | | 3489 | | BigInteger value) |
| | 9 | 3490 | | { |
| | 9 | 3491 | | if (this.sign == 0) |
| | 0 | 3492 | | return value; |
| | | 3493 | | |
| | 9 | 3494 | | if (value.sign == 0) |
| | 0 | 3495 | | return this; |
| | | 3496 | | |
| | 9 | 3497 | | int[] aMag = this.sign > 0 |
| | 9 | 3498 | | ? this.magnitude |
| | 9 | 3499 | | : Add(One).magnitude; |
| | | 3500 | | |
| | 9 | 3501 | | int[] bMag = value.sign > 0 |
| | 9 | 3502 | | ? value.magnitude |
| | 9 | 3503 | | : value.Add(One).magnitude; |
| | | 3504 | | |
| | | 3505 | | // TODO Can just replace with sign != value.sign? |
| | 9 | 3506 | | bool resultNeg = (sign < 0 && value.sign >= 0) || (sign >= 0 && value.sign < 0); |
| | 9 | 3507 | | int resultLength = System.Math.Max(aMag.Length, bMag.Length); |
| | 9 | 3508 | | int[] resultMag = new int[resultLength]; |
| | | 3509 | | |
| | 9 | 3510 | | int aStart = resultMag.Length - aMag.Length; |
| | 9 | 3511 | | int bStart = resultMag.Length - bMag.Length; |
| | | 3512 | | |
| | 246 | 3513 | | for (int i = 0; i < resultMag.Length; ++i) |
| | 114 | 3514 | | { |
| | 114 | 3515 | | int aWord = i >= aStart ? aMag[i - aStart] : 0; |
| | 114 | 3516 | | int bWord = i >= bStart ? bMag[i - bStart] : 0; |
| | | 3517 | | |
| | 114 | 3518 | | if (this.sign < 0) |
| | 0 | 3519 | | { |
| | 0 | 3520 | | aWord = ~aWord; |
| | 0 | 3521 | | } |
| | | 3522 | | |
| | 114 | 3523 | | if (value.sign < 0) |
| | 0 | 3524 | | { |
| | 0 | 3525 | | bWord = ~bWord; |
| | 0 | 3526 | | } |
| | | 3527 | | |
| | 114 | 3528 | | resultMag[i] = aWord ^ bWord; |
| | | 3529 | | |
| | 114 | 3530 | | if (resultNeg) |
| | 0 | 3531 | | { |
| | 0 | 3532 | | resultMag[i] = ~resultMag[i]; |
| | 0 | 3533 | | } |
| | 114 | 3534 | | } |
| | | 3535 | | |
| | 9 | 3536 | | BigInteger result = new BigInteger(1, resultMag, true); |
| | | 3537 | | |
| | | 3538 | | // TODO Optimise this case |
| | 9 | 3539 | | if (resultNeg) |
| | 0 | 3540 | | { |
| | 0 | 3541 | | result = result.Not(); |
| | 0 | 3542 | | } |
| | | 3543 | | |
| | 9 | 3544 | | return result; |
| | 9 | 3545 | | } |
| | | 3546 | | |
| | | 3547 | | public BigInteger SetBit( |
| | | 3548 | | int n) |
| | 0 | 3549 | | { |
| | 0 | 3550 | | if (n < 0) |
| | 0 | 3551 | | throw new ArithmeticException("Bit address less than zero"); |
| | | 3552 | | |
| | 0 | 3553 | | if (TestBit(n)) |
| | 0 | 3554 | | return this; |
| | | 3555 | | |
| | | 3556 | | // TODO Handle negative values and zero |
| | 0 | 3557 | | if (sign > 0 && n < (BitLength - 1)) |
| | 0 | 3558 | | return FlipExistingBit(n); |
| | | 3559 | | |
| | 0 | 3560 | | return Or(One.ShiftLeft(n)); |
| | 0 | 3561 | | } |
| | | 3562 | | |
| | | 3563 | | public BigInteger ClearBit( |
| | | 3564 | | int n) |
| | 0 | 3565 | | { |
| | 0 | 3566 | | if (n < 0) |
| | 0 | 3567 | | throw new ArithmeticException("Bit address less than zero"); |
| | | 3568 | | |
| | 0 | 3569 | | if (!TestBit(n)) |
| | 0 | 3570 | | return this; |
| | | 3571 | | |
| | | 3572 | | // TODO Handle negative values |
| | 0 | 3573 | | if (sign > 0 && n < (BitLength - 1)) |
| | 0 | 3574 | | return FlipExistingBit(n); |
| | | 3575 | | |
| | 0 | 3576 | | return AndNot(One.ShiftLeft(n)); |
| | 0 | 3577 | | } |
| | | 3578 | | |
| | | 3579 | | public BigInteger FlipBit( |
| | | 3580 | | int n) |
| | 0 | 3581 | | { |
| | 0 | 3582 | | if (n < 0) |
| | 0 | 3583 | | throw new ArithmeticException("Bit address less than zero"); |
| | | 3584 | | |
| | | 3585 | | // TODO Handle negative values and zero |
| | 0 | 3586 | | if (sign > 0 && n < (BitLength - 1)) |
| | 0 | 3587 | | return FlipExistingBit(n); |
| | | 3588 | | |
| | 0 | 3589 | | return Xor(One.ShiftLeft(n)); |
| | 0 | 3590 | | } |
| | | 3591 | | |
| | | 3592 | | private BigInteger FlipExistingBit( |
| | | 3593 | | int n) |
| | 0 | 3594 | | { |
| | 0 | 3595 | | Debug.Assert(sign > 0); |
| | 0 | 3596 | | Debug.Assert(n >= 0); |
| | 0 | 3597 | | Debug.Assert(n < BitLength - 1); |
| | | 3598 | | |
| | 0 | 3599 | | int[] mag = (int[]) this.magnitude.Clone(); |
| | 0 | 3600 | | mag[mag.Length - 1 - (n >> 5)] ^= (1 << (n & 31)); // Flip bit |
| | | 3601 | | //mag[mag.Length - 1 - (n / 32)] ^= (1 << (n % 32)); |
| | 0 | 3602 | | return new BigInteger(this.sign, mag, false); |
| | 0 | 3603 | | } |
| | | 3604 | | } |
| | | 3605 | | } |