Org.BouncyCastle.Math.BigInteger.DivideAndRemainder C# (CSharp) Method

DivideAndRemainder() public method

public DivideAndRemainder ( BigInteger val ) : Org.BouncyCastle.Math.BigInteger[]
val BigInteger
return Org.BouncyCastle.Math.BigInteger[]
		public BigInteger[] DivideAndRemainder(
			BigInteger val)
		{
			if (val.sign == 0)
				throw new ArithmeticException("Division by zero error");

			BigInteger[] biggies = new BigInteger[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;
				BigInteger quotient = this.Abs().ShiftRight(e);
				int[] remainder = this.LastNBits(e);

				biggies[0] = val.sign == this.sign ? quotient : quotient.Negate();
				biggies[1] = new BigInteger(this.sign, remainder, true);
			}
			else
			{
				int[] remainder = (int[]) this.magnitude.Clone();
				int[] quotient = Divide(remainder, val.magnitude);

				biggies[0] = new BigInteger(this.sign * val.sign, quotient, true);
				biggies[1] = new BigInteger(this.sign, remainder, true);
			}

			return biggies;
		}

Usage Example

Example #1
0
        public static BigInteger H1hash(string ID, BigInteger p)
        {
            // H1 je sha256(ID) mod p
            SHA256Managed crypt = new SHA256Managed();
            StringBuilder hash = new StringBuilder();
            byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(ID), 0, Encoding.UTF8.GetByteCount(ID));
            foreach (byte theByte in crypto)
            {
                hash.Append(theByte.ToString("x2"));
            }

            BigInteger hsh = new BigInteger(hash.ToString(), 16);
            // hash mod p
            BigInteger x = hsh.DivideAndRemainder(p)[1];

            return x;
        }
All Usage Examples Of Org.BouncyCastle.Math.BigInteger::DivideAndRemainder