I would be interested in seeing implicit operators in BouncyCastle¹s BigInt, and don¹t see them in the most recent release of BC.
Does anyone have an objection to adding these methods, or have a better approach? public bool IsEven = true; public void SetEven() { if (magnitude.Length == 0 || (magnitude[magnitude.Length - 1] & 1) == 0) { IsEven = true; } else { IsEven = false; } } /// <summary> /// Implicit conversion operator from long to BigInteger. /// </summary> /// <param name="n">The long to be converted to a BigInteger</param> /// <returns>The BigInteger converted from the given long</returns> public static implicit operator BigInteger(long n) { BigInteger result = BigInteger.CreateValueOf(n); result.SetEven(); return result; } /// <summary> /// Incremetation by one operation of a BigInteger. /// </summary> /// <param name="n">The BigInteger to be incremented by one</param> /// <returns>The BigInteger result of incrementing by one</returns> public static BigInteger operator ++(BigInteger n) { BigInteger result = n + One; result.SetEven(); return result; } /// <summary> /// Equality test between two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>True if a == b, false otherwise</returns> public static bool operator ==(BigInteger a, BigInteger b) { return Equals(a, b); } public override bool Equals( object obj) { if (obj == this) return true; BigInteger biggie = obj as BigInteger; if (biggie == null) return false; return sign == biggie.sign && IsEqualMagnitude(biggie); } private bool IsEqualMagnitude(BigInteger x) { int[] xMag = x.magnitude; if (magnitude.Length != x.magnitude.Length) return false; for (int i = 0; i < magnitude.Length; i++) { if (magnitude[i] != x.magnitude[i]) return false; } return true; } /// <summary> /// Inequality test between two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>True if a != b, false otherwise</returns> public static bool operator !=(BigInteger a, BigInteger b) { return !Equals(a, b); } /// <summary> /// Greater test between two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>True if a > b, false otherwise</returns> public static bool operator >(BigInteger a, BigInteger b) { if (a.CompareTo(b) > 0) { return true; } else { return false; } } /// <summary> /// Smaller test between two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>True if a < b, false otherwise</returns> public static bool operator <(BigInteger a, BigInteger b) { if (a.CompareTo(b) < 0) { return true; } else { return false; } } /// <summary> /// Greater or equal test between two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>True if a >= b, false otherwise</returns> public static bool operator >=(BigInteger a, BigInteger b) { if (a.CompareTo(b) >= 0) { return true; } else { return false; } } /// <summary> /// Smaller or equal test between two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>True if a <= b, false otherwise</returns> public static bool operator <=(BigInteger a, BigInteger b) { if (a.CompareTo(b) <= 0) { return true; } else { return false; } } /// <summary> /// BigInteger inverse with respect to addition. /// </summary> /// <param name="n">The BigInteger whose opposite is to be computed</param> /// <returns>The BigInteger inverse with respect to addition</returns> public static BigInteger operator -(BigInteger n) { BigInteger result = Opposite(n); result.SetEven(); return result; } /// <summary> /// BigInteger inverse with respect to addition. /// </summary> /// <param name="n">The BigInteger whose opposite is to be computed</param> /// <returns>The BigInteger inverse with respect to addition</returns> public static BigInteger Opposite(BigInteger n) { BigInteger result = n; if (result != Zero) { if (result.sign == -1) result.sign = 1; else result.sign = 1; } result.SetEven(); return result; } /// <summary> /// Addition operation of two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>The BigInteger result of the addition</returns> public static BigInteger operator +(BigInteger a, BigInteger b) { BigInteger result = a.Add(b); result.SetEven(); return result; } /// <summary> /// Subtraction operation of two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>The BigInteger result of the subtraction</returns> public static BigInteger operator -(BigInteger a, BigInteger b) { BigInteger result = a.Subtract(b); result.SetEven(); return result; } /// <summary> /// Multiplication operation of two BigIntegers. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>The BigInteger result of the multiplication</returns> public static BigInteger operator *(BigInteger a, BigInteger b) { BigInteger result = a.Multiply(b); result.SetEven(); return result; } /// <summary> /// Division operation of two BigIntegers a and b, b != 0. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>The BigInteger result of the division</returns> /// <exception cref="BigIntegerException">Cannot divide by zero exception</exception> public static BigInteger operator /(BigInteger a, BigInteger b) { BigInteger result = a.Divide(b); result.SetEven(); return result; } /// <summary> /// Modulo operation of two BigIntegers a and b, b != 0. /// </summary> /// <param name="a">The 1st BigInteger</param> /// <param name="b">The 2nd BigInteger</param> /// <returns>The BigInteger result of the modulo</returns> /// <exception cref="BigIntegerException">Cannot divide by zero exception</exception> public static BigInteger operator %(BigInteger a, BigInteger b) { BigInteger result = a.Mod(b); result.SetEven(); return result; } > ********************************************************************** This e-mail may contain information that is privileged, confidential or protected under state or federal law. If you are not an intended recipient of this email, please delete it, notify the sender immediately, and do not copy, use or disseminate any information in the e-mail. Pursuant to IRS Circular 230, any tax advice in this email may not be used to avoid any penalties imposed under U.S. tax laws. E-mail sent to or from this e-mail address may be monitored, reviewed and archived.