BraintreeEncryption.Library.BouncyCastle.Math.BigInteger.Remainder C# (CSharp) 메소드

Remainder() 공개 메소드

public Remainder ( BigInteger n ) : BigInteger
n BigInteger
리턴 BigInteger
		public BigInteger Remainder(
			BigInteger 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 BigInteger(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 BigInteger(sign, result, true);
		}

Same methods

BigInteger::Remainder ( int m ) : int
BigInteger::Remainder ( int x, int y ) : int[]

Usage Example

        public BigInteger ProcessBlock(
			BigInteger input)
        {
            if (key is RsaPrivateCrtKeyParameters)
            {
                //
                // we have the extra factors, use the Chinese Remainder Theorem - the author
                // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
                // advice regarding the expression of this.
                //
                RsaPrivateCrtKeyParameters crtKey = (RsaPrivateCrtKeyParameters)key;

                BigInteger p = crtKey.P;;
                BigInteger q = crtKey.Q;
                BigInteger dP = crtKey.DP;
                BigInteger dQ = crtKey.DQ;
                BigInteger qInv = crtKey.QInv;

                BigInteger mP, mQ, h, m;

                // mP = ((input Mod p) ^ dP)) Mod p
                mP = (input.Remainder(p)).ModPow(dP, p);

                // mQ = ((input Mod q) ^ dQ)) Mod q
                mQ = (input.Remainder(q)).ModPow(dQ, q);

                // h = qInv * (mP - mQ) Mod p
                h = mP.Subtract(mQ);
                h = h.Multiply(qInv);
                h = h.Mod(p);               // Mod (in Java) returns the positive residual

                // m = h * q + mQ
                m = h.Multiply(q);
                m = m.Add(mQ);

                return m;
            }

            return input.ModPow(key.Exponent, key.Modulus);
        }