[pgsqlclient-checkins] pgsqlclient_10/Mono.Security/Mono.Security/Mono.Math BigInteger.cs,1.1,1.2
Status: Inactive
Brought to you by:
carlosga_fb
From: Carlos Guzm?n ?l. <car...@us...> - 2004-05-09 11:59:06
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Math In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28829 Modified Files: BigInteger.cs Log Message: Sync Mono.Security stuff wit Mono Beta 1 sources Index: BigInteger.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Math/BigInteger.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BigInteger.cs 10 Feb 2004 09:40:51 -0000 1.1 --- BigInteger.cs 9 May 2004 11:58:48 -0000 1.2 *************** *** 1,3 **** ! // // BigInteger.cs - Big Integer implementation // --- 1,3 ---- ! // // BigInteger.cs - Big Integer implementation // *************** *** 5,9 **** // Ben Maurer // Chew Keong TAN ! // Sebastien Pouliot (spo...@mo...) // // Copyright (c) 2003 Ben Maurer --- 5,10 ---- // Ben Maurer // Chew Keong TAN ! // Sebastien Pouliot <seb...@xi...> ! // Pieter Philippaerts <Pi...@me...> // // Copyright (c) 2003 Ben Maurer *************** *** 20,25 **** namespace Mono.Math { ! [CLSCompliant(false)] ! internal class BigInteger { #region Data Storage --- 21,30 ---- namespace Mono.Math { ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! class BigInteger { #region Data Storage *************** *** 58,62 **** /// </para> /// </remarks> ! public static readonly uint [] smallPrimes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, --- 63,67 ---- /// </para> /// </remarks> ! internal static uint [] smallPrimes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, *************** *** 146,149 **** --- 151,155 ---- } + [CLSCompliant (false)] public BigInteger (Sign sign, uint len) { *************** *** 158,161 **** --- 164,168 ---- } + [CLSCompliant (false)] public BigInteger (BigInteger bi, uint len) { *************** *** 188,192 **** (inData [i-2] << (2*8)) | (inData [i-1] << (1*8)) | ! (inData [i-0] << (0*8)) ); } --- 195,199 ---- (inData [i-2] << (2*8)) | (inData [i-1] << (1*8)) | ! (inData [i]) ); } *************** *** 201,204 **** --- 208,212 ---- } + [CLSCompliant (false)] public BigInteger (uint [] inData) { *************** *** 213,216 **** --- 221,225 ---- } + [CLSCompliant (false)] public BigInteger (uint ui) { *************** *** 218,221 **** --- 227,231 ---- } + [CLSCompliant (false)] public BigInteger (ulong ul) { *************** *** 226,229 **** --- 236,240 ---- } + [CLSCompliant (false)] public static implicit operator BigInteger (uint value) { *************** *** 237,240 **** --- 248,252 ---- } + [CLSCompliant (false)] public static implicit operator BigInteger (ulong value) { *************** *** 242,245 **** --- 254,302 ---- } + /* This is the BigInteger.Parse method I use. This method works + because BigInteger.ToString returns the input I gave to Parse. */ + public static BigInteger Parse (string number) + { + if (number == null) + throw new ArgumentNullException ("number"); + + int i = 0, len = number.Length; + char c; + bool digits_seen = false; + BigInteger val = new BigInteger (0); + if (number [i] == '+') { + i++; + } + else if (number [i] == '-') { + throw new FormatException (WouldReturnNegVal); + } + + for (; i < len; i++) { + c = number [i]; + if (c == '\0') { + i = len; + continue; + } + if (c >= '0' && c <= '9') { + val = val * 10 + (c - '0'); + digits_seen = true; + } + else { + if (Char.IsWhiteSpace (c)) { + for (i++; i < len; i++) { + if (!Char.IsWhiteSpace (number [i])) + throw new FormatException (); + } + break; + } + else + throw new FormatException (); + } + } + if (!digits_seen) + throw new FormatException (); + return val; + } + #endregion *************** *** 287,290 **** --- 344,348 ---- } + [CLSCompliant (false)] public static uint operator % (BigInteger bi, uint ui) { *************** *** 349,352 **** --- 407,462 ---- #endregion + #region Friendly names for operators + + // with names suggested by FxCop 1.30 + + public static BigInteger Add (BigInteger bi1, BigInteger bi2) + { + return (bi1 + bi2); + } + + public static BigInteger Subtract (BigInteger bi1, BigInteger bi2) + { + return (bi1 - bi2); + } + + public static int Modulus (BigInteger bi, int i) + { + return (bi % i); + } + + [CLSCompliant (false)] + public static uint Modulus (BigInteger bi, uint ui) + { + return (bi % ui); + } + + public static BigInteger Modulus (BigInteger bi1, BigInteger bi2) + { + return (bi1 % bi2); + } + + public static BigInteger Divid (BigInteger bi, int i) + { + return (bi / i); + } + + public static BigInteger Divid (BigInteger bi1, BigInteger bi2) + { + return (bi1 / bi2); + } + + public static BigInteger Multiply (BigInteger bi1, BigInteger bi2) + { + return (bi1 * bi2); + } + + public static BigInteger Multiply (BigInteger bi, int i) + { + return (bi * i); + } + + #endregion + #region Random private static RandomNumberGenerator rng; *************** *** 365,369 **** /// <param name="rng">A random number generator to use to obtain the bits.</param> /// <returns>A random number of the specified length.</returns> ! public static BigInteger genRandom (int bits, RandomNumberGenerator rng) { int dwords = bits >> 5; --- 475,479 ---- /// <param name="rng">A random number generator to use to obtain the bits.</param> /// <returns>A random number of the specified length.</returns> ! public static BigInteger GenerateRandom (int bits, RandomNumberGenerator rng) { int dwords = bits >> 5; *************** *** 398,404 **** /// <param name="bits">The number of bits for the new number.</param> /// <returns>A random number of the specified length.</returns> ! public static BigInteger genRandom (int bits) { ! return genRandom (bits, Rng); } --- 508,514 ---- /// <param name="bits">The number of bits for the new number.</param> /// <returns>A random number of the specified length.</returns> ! public static BigInteger GenerateRandom (int bits) { ! return GenerateRandom (bits, Rng); } *************** *** 407,413 **** /// </summary> /// <param name="rng">A RNG.</param> ! public void randomize (RandomNumberGenerator rng) { ! int bits = this.bitCount (); int dwords = bits >> 5; int remBits = bits & 0x1F; --- 517,523 ---- /// </summary> /// <param name="rng">A RNG.</param> ! public void Randomize (RandomNumberGenerator rng) { ! int bits = this.BitCount (); int dwords = bits >> 5; int remBits = bits & 0x1F; *************** *** 438,444 **** /// Randomizes the bits in "this" from the default RNG. /// </summary> ! public void randomize () { ! randomize (Rng); } --- 548,554 ---- /// Randomizes the bits in "this" from the default RNG. /// </summary> ! public void Randomize () { ! Randomize (Rng); } *************** *** 447,451 **** #region Bitwise ! public int bitCount () { this.Normalize (); --- 557,561 ---- #region Bitwise ! public int BitCount () { this.Normalize (); *************** *** 469,473 **** /// <param name="bitNum">The bit to test. The least significant bit is 0.</param> /// <returns>True if bitNum is set to 1, else false.</returns> ! public bool testBit (uint bitNum) { uint bytePos = bitNum >> 5; // divide by 32 --- 579,584 ---- /// <param name="bitNum">The bit to test. The least significant bit is 0.</param> /// <returns>True if bitNum is set to 1, else false.</returns> ! [CLSCompliant (false)] ! public bool TestBit (uint bitNum) { uint bytePos = bitNum >> 5; // divide by 32 *************** *** 478,482 **** } ! public bool testBit (int bitNum) { if (bitNum < 0) throw new IndexOutOfRangeException ("bitNum out of range"); --- 589,593 ---- } ! public bool TestBit (int bitNum) { if (bitNum < 0) throw new IndexOutOfRangeException ("bitNum out of range"); *************** *** 489,502 **** } ! public void setBit (uint bitNum) { ! setBit (bitNum, true); } ! public void clearBit (uint bitNum) { ! setBit (bitNum, false); } ! public void setBit (uint bitNum, bool val) { uint bytePos = bitNum >> 5; // divide by 32 --- 600,617 ---- } ! [CLSCompliant (false)] ! public void SetBit (uint bitNum) { ! SetBit (bitNum, true); } ! ! [CLSCompliant (false)] ! public void ClearBit (uint bitNum) { ! SetBit (bitNum, false); } ! [CLSCompliant (false)] ! public void SetBit (uint bitNum, bool value) { uint bytePos = bitNum >> 5; // divide by 32 *************** *** 504,508 **** if (bytePos < this.length) { uint mask = (uint)1 << (int)(bitNum & 0x1F); ! if (val) this.data [bytePos] |= mask; else --- 619,623 ---- if (bytePos < this.length) { uint mask = (uint)1 << (int)(bitNum & 0x1F); ! if (value) this.data [bytePos] |= mask; else *************** *** 515,527 **** if (this == 0) return -1; int i = 0; ! while (!testBit (i)) i++; return i; } ! public byte [] getBytes () { if (this == 0) return new byte [1]; ! int numBits = bitCount (); int numBytes = numBits >> 3; if ((numBits & 0x7) != 0) --- 630,642 ---- if (this == 0) return -1; int i = 0; ! while (!TestBit (i)) i++; return i; } ! public byte[] GetBytes () { if (this == 0) return new byte [1]; ! int numBits = BitCount (); int numBytes = numBits >> 3; if ((numBits & 0x7) != 0) *************** *** 550,553 **** --- 665,669 ---- #region Compare + [CLSCompliant (false)] public static bool operator == (BigInteger bi1, uint ui) { *************** *** 556,559 **** --- 672,676 ---- } + [CLSCompliant (false)] public static bool operator != (BigInteger bi1, uint ui) { *************** *** 611,614 **** --- 728,732 ---- #region Formatting + [CLSCompliant (false)] public string ToString (uint radix) { *************** *** 616,623 **** } ! public string ToString (uint radix, string charSet) { ! if (charSet.Length < radix) ! throw new ArgumentException ("charSet length less than radix", "charSet"); if (radix == 1) throw new ArgumentException ("There is no such thing as radix one notation", "radix"); --- 734,742 ---- } ! [CLSCompliant (false)] ! public string ToString (uint radix, string characterSet) { ! if (characterSet.Length < radix) ! throw new ArgumentException ("charSet length less than radix", "characterSet"); if (radix == 1) throw new ArgumentException ("There is no such thing as radix one notation", "radix"); *************** *** 632,636 **** while (a != 0) { uint rem = Kernel.SingleByteDivideInPlace (a, radix); ! result = charSet [ (int)rem] + result; } --- 751,755 ---- while (a != 0) { uint rem = Kernel.SingleByteDivideInPlace (a, radix); ! result = characterSet [(int) rem] + result; } *************** *** 694,708 **** #region Number Theory ! public BigInteger gcd (BigInteger bi) { return Kernel.gcd (this, bi); } ! public BigInteger modInverse (BigInteger mod) { ! return Kernel.modInverse (this, mod); } ! public BigInteger modPow (BigInteger exp, BigInteger n) { ModulusRing mr = new ModulusRing (n); --- 813,827 ---- #region Number Theory ! public BigInteger GCD (BigInteger bi) { return Kernel.gcd (this, bi); } ! public BigInteger ModInverse (BigInteger modulus) { ! return Kernel.modInverse (this, modulus); } ! public BigInteger ModPow (BigInteger exp, BigInteger n) { ModulusRing mr = new ModulusRing (n); *************** *** 714,740 **** #region Prime Testing ! public bool isProbablePrime () ! { ! ! for (int p = 0; p < smallPrimes.Length; p++) { ! if (this % smallPrimes [p] == 0) ! return this == smallPrimes [p]; ! } ! ! return ! PrimalityTests.SmallPrimeSppTest (this, Prime.ConfidenceFactor.Medium); ! } ! ! [Obsolete] ! public bool isProbablePrime (int notUsed) { - for (int p = 0; p < smallPrimes.Length; p++) { if (this % smallPrimes [p] == 0) ! return this == smallPrimes [p]; } ! ! return ! PrimalityTests.SmallPrimeSppTest (this, Prime.ConfidenceFactor.Medium); } --- 833,845 ---- #region Prime Testing ! public bool IsProbablePrime () { for (int p = 0; p < smallPrimes.Length; p++) { + if (this == smallPrimes [p]) + return true; if (this % smallPrimes [p] == 0) ! return false; } ! return PrimalityTests.RabinMillerTest (this, Prime.ConfidenceFactor.Medium); } *************** *** 748,752 **** /// <param name="bi">A BigInteger</param> /// <returns>The smallest prime >= bi. More mathematically, if bi is prime: bi, else Prime [PrimePi [bi] + 1].</returns> ! public static BigInteger NextHightestPrime (BigInteger bi) { NextPrimeFinder npf = new NextPrimeFinder (); --- 853,857 ---- /// <param name="bi">A BigInteger</param> /// <returns>The smallest prime >= bi. More mathematically, if bi is prime: bi, else Prime [PrimePi [bi] + 1].</returns> ! public static BigInteger NextHighestPrime (BigInteger bi) { NextPrimeFinder npf = new NextPrimeFinder (); *************** *** 754,758 **** } ! public static BigInteger genPseudoPrime (int bits) { SequentialSearchPrimeGeneratorBase sspg = new SequentialSearchPrimeGeneratorBase (); --- 859,863 ---- } ! public static BigInteger GeneratePseudoPrime (int bits) { SequentialSearchPrimeGeneratorBase sspg = new SequentialSearchPrimeGeneratorBase (); *************** *** 787,797 **** #endregion ! public sealed class ModulusRing { BigInteger mod, constant; ! public ModulusRing (BigInteger mod) { ! this.mod = mod; // calculate constant = b^ (2k) / m --- 892,907 ---- #endregion ! #if INSIDE_CORLIB ! internal ! #else ! public ! #endif ! sealed class ModulusRing { BigInteger mod, constant; ! public ModulusRing (BigInteger modulus) { ! this.mod = modulus; // calculate constant = b^ (2k) / m *************** *** 919,923 **** BigInteger tempNum = new BigInteger (b % mod, mod.length << 1); // ensures (tempNum * tempNum) < b^ (2k) ! uint totalBits = (uint)exp.bitCount (); uint [] wkspace = new uint [mod.length << 1]; --- 1029,1033 ---- BigInteger tempNum = new BigInteger (b % mod, mod.length << 1); // ensures (tempNum * tempNum) < b^ (2k) ! uint totalBits = (uint)exp.BitCount (); uint [] wkspace = new uint [mod.length << 1]; *************** *** 925,929 **** // perform squaring and multiply exponentiation for (uint pos = 0; pos < totalBits; pos++) { ! if (exp.testBit (pos)) { Array.Clear (wkspace, 0, wkspace.Length); --- 1035,1039 ---- // perform squaring and multiply exponentiation for (uint pos = 0; pos < totalBits; pos++) { ! if (exp.TestBit (pos)) { Array.Clear (wkspace, 0, wkspace.Length); *************** *** 953,957 **** BigInteger tempNum = new BigInteger (Montgomery.ToMont (b, mod), mod.length << 1); // ensures (tempNum * tempNum) < b^ (2k) uint mPrime = Montgomery.Inverse (mod.data [0]); ! uint totalBits = (uint)exp.bitCount (); uint [] wkspace = new uint [mod.length << 1]; --- 1063,1067 ---- BigInteger tempNum = new BigInteger (Montgomery.ToMont (b, mod), mod.length << 1); // ensures (tempNum * tempNum) < b^ (2k) uint mPrime = Montgomery.Inverse (mod.data [0]); ! uint totalBits = (uint)exp.BitCount (); uint [] wkspace = new uint [mod.length << 1]; *************** *** 959,963 **** // perform squaring and multiply exponentiation for (uint pos = 0; pos < totalBits; pos++) { ! if (exp.testBit (pos)) { Array.Clear (wkspace, 0, wkspace.Length); --- 1069,1073 ---- // perform squaring and multiply exponentiation for (uint pos = 0; pos < totalBits; pos++) { ! if (exp.TestBit (pos)) { Array.Clear (wkspace, 0, wkspace.Length); *************** *** 983,986 **** --- 1093,1097 ---- // TODO: Make tests for this, not really needed b/c prime stuff // checks it, but still would be nice + [CLSCompliant (false)] public BigInteger Pow (uint b, BigInteger exp) { *************** *** 994,997 **** --- 1105,1109 ---- } + [CLSCompliant (false)] private unsafe BigInteger OddPow (uint b, BigInteger exp) { *************** *** 1004,1008 **** uint mPrime = Montgomery.Inverse (mod.data [0]); ! uint pos = (uint)exp.bitCount () - 2; // --- 1116,1120 ---- uint mPrime = Montgomery.Inverse (mod.data [0]); ! uint pos = (uint)exp.BitCount () - 2; // *************** *** 1017,1021 **** resultNum = Montgomery.Reduce (resultNum, mod, mPrime); ! if (exp.testBit (pos)) { // --- 1129,1133 ---- resultNum = Montgomery.Reduce (resultNum, mod, mPrime); ! if (exp.TestBit (pos)) { // *************** *** 1104,1108 **** BigInteger resultNum = new BigInteger ((BigInteger)b, mod.length << 1 + 1); ! uint pos = (uint)exp.bitCount () - 2; // --- 1216,1220 ---- BigInteger resultNum = new BigInteger ((BigInteger)b, mod.length << 1 + 1); ! uint pos = (uint)exp.BitCount () - 2; // *************** *** 1118,1122 **** BarrettReduction (resultNum); ! if (exp.testBit (pos)) { // --- 1230,1234 ---- BarrettReduction (resultNum); ! if (exp.TestBit (pos)) { // *************** *** 1282,1286 **** // TODO: eat small bits, the ones we can do with no modular reduction // ! uint pos = (uint)exp.bitCount () - 2; do { --- 1394,1398 ---- // TODO: eat small bits, the ones we can do with no modular reduction // ! uint pos = (uint)exp.BitCount () - 2; do { *************** *** 1288,1292 **** resultNum = Montgomery.Reduce (resultNum, mod, mPrime); ! if (exp.testBit (pos)) { // // resultNum = (resultNum * 2) % mod --- 1400,1404 ---- resultNum = Montgomery.Reduce (resultNum, mod, mPrime); ! if (exp.TestBit (pos)) { // // resultNum = (resultNum * 2) % mod *************** *** 1333,1337 **** } ! public sealed class Montgomery { public static uint Inverse (uint n) { --- 1445,1455 ---- } ! internal sealed class Montgomery { ! ! private Montgomery () ! { ! } ! ! [CLSCompliant (false)] public static uint Inverse (uint n) { *************** *** 1353,1356 **** --- 1471,1475 ---- } + [CLSCompliant (false)] public static unsafe BigInteger Reduce (BigInteger n, BigInteger m, uint mPrime) { *************** *** 1372,1376 **** uint* mP = mm, aSP = a, aDP = a; ! ulong c = (ulong)u_i * (ulong)*(mP++) + *(aSP++); c >>= 32; uint j = 1; --- 1491,1495 ---- uint* mP = mm, aSP = a, aDP = a; ! ulong c = (ulong)u_i * ((ulong)*(mP++)) + *(aSP++); c >>= 32; uint j = 1; *************** *** 1406,1414 **** return A; } ! public static BigInteger Reduce (BigInteger n, BigInteger m) { return Reduce (n, m, Inverse (m.data [0])); } } --- 1525,1534 ---- return A; } ! #if _NOT_USED_ public static BigInteger Reduce (BigInteger n, BigInteger m) { return Reduce (n, m, Inverse (m.data [0])); } + #endif } *************** *** 2079,2083 **** // Keep adding until no carry ! while ((*tP3++) == 0x0) (*tP3)++; } --- 2199,2203 ---- // Keep adding until no carry ! while ((*tP3++) == 0) (*tP3)++; } *************** *** 2093,2097 **** } ! public static bool Double (uint [] u, int l) { uint x, carry = 0; --- 2213,2219 ---- } ! /* ! * Never called in BigInteger (and part of a private class) ! * public static bool Double (uint [] u, int l) { uint x, carry = 0; *************** *** 2105,2109 **** if (carry != 0) u [l] = carry; return carry != 0; ! } #endregion --- 2227,2231 ---- if (carry != 0) u [l] = carry; return carry != 0; ! }*/ #endregion |