private static BigIntegerBC createUValueOf( ulong value) { int msw = (int)(value >> 32); int lsw = (int)value; if (msw != 0) return new BigIntegerBC(1, new int[] { msw, lsw }, false); if (lsw != 0) { BigIntegerBC n = new BigIntegerBC(1, new int[] { lsw }, false); // Check for a power of two if ((lsw & -lsw) == lsw) { n.nBits = 1; } return n; } return Zero; }
public BigIntegerBC ShiftLeft( int n) { if (sign == 0 || magnitude.Length == 0) return Zero; if (n == 0) return this; if (n < 0) return ShiftRight(-n); BigIntegerBC result = new BigIntegerBC(sign, ShiftLeft(magnitude, n), true); if (this.nBits != -1) { result.nBits = sign > 0 ? this.nBits : this.nBits + n; } if (this.nBitLength != -1) { result.nBitLength = this.nBitLength + n; } return result; }
public BigIntegerBC Subtract( BigIntegerBC n) { if (n.sign == 0) return this; if (this.sign == 0) return n.Negate(); if (this.sign != n.sign) return Add(n.Negate()); int compare = CompareNoLeadingZeroes(0, magnitude, 0, n.magnitude); if (compare == 0) return Zero; BigIntegerBC bigun, lilun; if (compare < 0) { bigun = n; lilun = this; } else { bigun = this; lilun = n; } return new BigIntegerBC(this.sign * compare, doSubBigLil(bigun.magnitude, lilun.magnitude), true); }
public BigIntegerBC Multiply( BigIntegerBC val) { if (sign == 0 || val.sign == 0) return Zero; if (val.QuickPow2Check()) // val is power of two { BigIntegerBC result = this.ShiftLeft(val.Abs().BitLength - 1); return val.sign > 0 ? result : result.Negate(); } if (this.QuickPow2Check()) // this is power of two { BigIntegerBC result = val.ShiftLeft(this.Abs().BitLength - 1); return this.sign > 0 ? result : result.Negate(); } int maxBitLength = this.BitLength + val.BitLength; int resLength = (maxBitLength + BitsPerInt - 1) / BitsPerInt; int[] res = new int[resLength]; if (val == this) { Square(res, this.magnitude); } else { Multiply(res, this.magnitude, val.magnitude); } return new BigIntegerBC(sign * val.sign, res, true); }
public BigIntegerBC Remainder( BigIntegerBC n) { if (n.sign == 0) throw new ArithmeticException("Division by zero error"); if (this.sign == 0) return Zero; // For small values, use fast remainder method if (n.magnitude.Length == 1) { int val = n.magnitude[0]; if (val > 0) { if (val == 1) return Zero; // TODO Make this func work on uint, and handle val == 1? int rem = Remainder(val); return rem == 0 ? Zero : new BigIntegerBC(sign, new int[] { rem }, false); } } if (CompareNoLeadingZeroes(0, magnitude, 0, n.magnitude) < 0) return this; int[] result; if (n.QuickPow2Check()) // n is power of two { // TODO Move before small values branch above? result = LastNBits(n.Abs().BitLength - 1); } else { result = (int[])this.magnitude.Clone(); result = Remainder(result, n.magnitude); } return new BigIntegerBC(sign, result, true); }
public BigIntegerBC AndNot( BigIntegerBC val) { return And(val.Not()); }
public BigIntegerBC[] DivideAndRemainder( BigIntegerBC val) { if (val.sign == 0) throw new ArithmeticException("Division by zero error"); BigIntegerBC[] biggies = new BigIntegerBC[2]; if (sign == 0) { biggies[0] = Zero; biggies[1] = Zero; } else if (val.QuickPow2Check()) // val is power of two { int e = val.Abs().BitLength - 1; BigIntegerBC quotient = this.Abs().ShiftRight(e); int[] remainder = this.LastNBits(e); biggies[0] = val.sign == this.sign ? quotient : quotient.Negate(); biggies[1] = new BigIntegerBC(this.sign, remainder, true); } else { int[] remainder = (int[])this.magnitude.Clone(); int[] quotient = Divide(remainder, val.magnitude); biggies[0] = new BigIntegerBC(this.sign * val.sign, quotient, true); biggies[1] = new BigIntegerBC(this.sign, remainder, true); } return biggies; }
public BigIntegerBC ModInverse( BigIntegerBC m) { if (m.sign < 1) throw new ArithmeticException("Modulus must be positive"); // TODO Too slow at the moment // // "Fast Key Exchange with Elliptic Curve Systems" R.Schoeppel // if (m.TestBit(0)) // { // //The Almost Inverse Algorithm // int k = 0; // BigIntegerBC B = One, C = Zero, F = this, G = m, tmp; // // for (;;) // { // // While F is even, do F=F/u, C=C*u, k=k+1. // int zeroes = F.GetLowestSetBit(); // if (zeroes > 0) // { // F = F.ShiftRight(zeroes); // C = C.ShiftLeft(zeroes); // k += zeroes; // } // // // If F = 1, then return B,k. // if (F.Equals(One)) // { // BigIntegerBC half = m.Add(One).ShiftRight(1); // BigIntegerBC halfK = half.ModPow(BigIntegerBC.ValueOf(k), m); // return B.Multiply(halfK).Mod(m); // } // // if (F.CompareTo(G) < 0) // { // tmp = G; G = F; F = tmp; // tmp = B; B = C; C = tmp; // } // // F = F.Add(G); // B = B.Add(C); // } // } BigIntegerBC x = new BigIntegerBC(); BigIntegerBC gcd = ExtEuclid(this, m, x, null); if (!gcd.Equals(One)) throw new ArithmeticException("Numbers not relatively prime."); if (x.sign < 0) { x.sign = 1; //x = m.Subtract(x); x.magnitude = doSubBigLil(m.magnitude, x.magnitude); } return x; }
/** * Calculate the numbers u1, u2, and u3 such that: * * u1 * a + u2 * b = u3 * * where u3 is the greatest common divider of a and b. * a and b using the extended Euclid algorithm (refer p. 323 * of The Art of Computer Programming vol 2, 2nd ed). * This also seems to have the side effect of calculating * some form of multiplicative inverse. * * @param a First number to calculate gcd for * @param b Second number to calculate gcd for * @param u1Out the return object for the u1 value * @param u2Out the return object for the u2 value * @return The greatest common divisor of a and b */ private static BigIntegerBC ExtEuclid( BigIntegerBC a, BigIntegerBC b, BigIntegerBC u1Out, BigIntegerBC u2Out) { BigIntegerBC u1 = One; BigIntegerBC u3 = a; BigIntegerBC v1 = Zero; BigIntegerBC v3 = b; while (v3.sign > 0) { BigIntegerBC[] q = u3.DivideAndRemainder(v3); BigIntegerBC tmp = v1.Multiply(q[0]); BigIntegerBC tn = u1.Subtract(tmp); u1 = v1; v1 = tn; u3 = v3; v3 = q[1]; } if (u1Out != null) { u1Out.sign = u1.sign; u1Out.magnitude = u1.magnitude; } if (u2Out != null) { BigIntegerBC tmp = u1.Multiply(a); tmp = u3.Subtract(tmp); BigIntegerBC res = tmp.Divide(b); u2Out.sign = res.sign; u2Out.magnitude = res.magnitude; } return u3; }
public BigIntegerBC Min( BigIntegerBC value) { return CompareTo(value) < 0 ? this : value; }
public BigIntegerBC Mod( BigIntegerBC m) { if (m.sign < 1) throw new ArithmeticException("Modulus must be positive"); BigIntegerBC biggie = Remainder(m); return (biggie.sign >= 0 ? biggie : biggie.Add(m)); }
public BigIntegerBC Max( BigIntegerBC value) { return CompareTo(value) > 0 ? this : value; }
internal bool RabinMillerTest( int certainty, Random random) { Debug.Assert(certainty > 0); Debug.Assert(BitLength > 2); Debug.Assert(TestBit(0)); // let n = 1 + d . 2^s BigIntegerBC n = this; BigIntegerBC nMinusOne = n.Subtract(One); int s = nMinusOne.GetLowestSetBit(); BigIntegerBC r = nMinusOne.ShiftRight(s); Debug.Assert(s >= 1); do { // TODO Make a method for random BigIntegerBCs in range 0 < x < n) // - Method can be optimized by only replacing examined bits at each trial BigIntegerBC a; do { a = new BigIntegerBC(n.BitLength, random); } while (a.CompareTo(One) <= 0 || a.CompareTo(nMinusOne) >= 0); BigIntegerBC y = a.ModPow(r, n); if (!y.Equals(One)) { int j = 0; while (!y.Equals(nMinusOne)) { if (++j == s) return false; y = y.ModPow(Two, n); if (y.Equals(One)) return false; } } certainty -= 2; // composites pass for only 1/4 possible 'a' } while (certainty > 0); return true; }
public BigIntegerBC Gcd( BigIntegerBC value) { if (value.sign == 0) return Abs(); if (sign == 0) return value.Abs(); BigIntegerBC r; BigIntegerBC u = this; BigIntegerBC v = value; while (v.sign != 0) { r = u.Mod(v); u = v; v = r; } return u; }
public BigIntegerBC Add( BigIntegerBC value) { if (this.sign == 0) return value; if (this.sign != value.sign) { if (value.sign == 0) return this; if (value.sign < 0) return Subtract(value.Negate()); return value.Subtract(Negate()); } return AddToMagnitude(value.magnitude); }
public BigIntegerBC ModPow( BigIntegerBC exponent, BigIntegerBC m) { if (m.sign < 1) throw new ArithmeticException("Modulus must be positive"); if (m.Equals(One)) return Zero; if (exponent.sign == 0) return One; if (sign == 0) return Zero; int[] zVal = null; int[] yAccum = null; int[] yVal; // Montgomery exponentiation is only possible if the modulus is odd, // but AFAIK, this is always the case for crypto algo's bool useMonty = ((m.magnitude[m.magnitude.Length - 1] & 1) == 1); long mQ = 0; if (useMonty) { mQ = m.GetMQuote(); // tmp = this * R mod m BigIntegerBC tmp = ShiftLeft(32 * m.magnitude.Length).Mod(m); zVal = tmp.magnitude; useMonty = (zVal.Length <= m.magnitude.Length); if (useMonty) { yAccum = new int[m.magnitude.Length + 1]; if (zVal.Length < m.magnitude.Length) { int[] longZ = new int[m.magnitude.Length]; zVal.CopyTo(longZ, longZ.Length - zVal.Length); zVal = longZ; } } } if (!useMonty) { if (magnitude.Length <= m.magnitude.Length) { //zAccum = new int[m.magnitude.Length * 2]; zVal = new int[m.magnitude.Length]; magnitude.CopyTo(zVal, zVal.Length - magnitude.Length); } else { // // in normal practice we'll never see this... // BigIntegerBC tmp = Remainder(m); //zAccum = new int[m.magnitude.Length * 2]; zVal = new int[m.magnitude.Length]; tmp.magnitude.CopyTo(zVal, zVal.Length - tmp.magnitude.Length); } yAccum = new int[m.magnitude.Length * 2]; } yVal = new int[m.magnitude.Length]; // // from LSW to MSW // for (int i = 0; i < exponent.magnitude.Length; i++) { int v = exponent.magnitude[i]; int bits = 0; if (i == 0) { while (v > 0) { v <<= 1; bits++; } // // first time in initialise y // zVal.CopyTo(yVal, 0); v <<= 1; bits++; } while (v != 0) { if (useMonty) { // Montgomery square algo doesn't exist, and a normal // square followed by a Montgomery reduction proved to // be almost as heavy as a Montgomery mulitply. MultiplyMonty(yAccum, yVal, yVal, m.magnitude, mQ); } else { Square(yAccum, yVal); Remainder(yAccum, m.magnitude); Array.Copy(yAccum, yAccum.Length - yVal.Length, yVal, 0, yVal.Length); ZeroOut(yAccum); } bits++; if (v < 0) { if (useMonty) { MultiplyMonty(yAccum, yVal, zVal, m.magnitude, mQ); } else { Multiply(yAccum, yVal, zVal); Remainder(yAccum, m.magnitude); Array.Copy(yAccum, yAccum.Length - yVal.Length, yVal, 0, yVal.Length); ZeroOut(yAccum); } } v <<= 1; } while (bits < 32) { if (useMonty) { MultiplyMonty(yAccum, yVal, yVal, m.magnitude, mQ); } else { Square(yAccum, yVal); Remainder(yAccum, m.magnitude); Array.Copy(yAccum, yAccum.Length - yVal.Length, yVal, 0, yVal.Length); ZeroOut(yAccum); } bits++; } } if (useMonty) { // Return y * R^(-1) mod m by doing y * 1 * R^(-1) mod m ZeroOut(zVal); zVal[zVal.Length - 1] = 1; MultiplyMonty(yAccum, yVal, zVal, m.magnitude, mQ); } BigIntegerBC result = new BigIntegerBC(1, yVal, true); return exponent.sign > 0 ? result : result.ModInverse(m); }
public BigIntegerBC And( BigIntegerBC value) { if (this.sign == 0 || value.sign == 0) { return Zero; } int[] aMag = this.sign > 0 ? this.magnitude : Add(One).magnitude; int[] bMag = value.sign > 0 ? value.magnitude : value.Add(One).magnitude; bool resultNeg = sign < 0 && value.sign < 0; int resultLength = System.Math.Max(aMag.Length, bMag.Length); int[] resultMag = new int[resultLength]; int aStart = resultMag.Length - aMag.Length; int bStart = resultMag.Length - bMag.Length; for (int i = 0; i < resultMag.Length; ++i) { int aWord = i >= aStart ? aMag[i - aStart] : 0; int bWord = i >= bStart ? bMag[i - bStart] : 0; if (this.sign < 0) { aWord = ~aWord; } if (value.sign < 0) { bWord = ~bWord; } resultMag[i] = aWord & bWord; if (resultNeg) { resultMag[i] = ~resultMag[i]; } } BigIntegerBC result = new BigIntegerBC(1, resultMag, true); // TODO Optimise this case if (resultNeg) { result = result.Not(); } return result; }
public BigIntegerBC Modulus( BigIntegerBC val) { return this.Mod(val); }
public int CompareTo( BigIntegerBC value) { return sign < value.sign ? -1 : sign > value.sign ? 1 : sign == 0 ? 0 : sign * CompareNoLeadingZeroes(0, magnitude, 0, value.magnitude); }
public BigIntegerBC Divide( BigIntegerBC val) { if (val.sign == 0) throw new ArithmeticException("Division by zero error"); if (sign == 0) return Zero; if (val.QuickPow2Check()) // val is power of two { BigIntegerBC result = this.Abs().ShiftRight(val.Abs().BitLength - 1); return val.sign == this.sign ? result : result.Negate(); } int[] mag = (int[])this.magnitude.Clone(); return new BigIntegerBC(this.sign * val.sign, Divide(mag, val.magnitude), true); }