当前位置: 首页>>代码示例>>C#>>正文


C# BigInteger.Remainder方法代码示例

本文整理汇总了C#中BigInteger.Remainder方法的典型用法代码示例。如果您正苦于以下问题:C# BigInteger.Remainder方法的具体用法?C# BigInteger.Remainder怎么用?C# BigInteger.Remainder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BigInteger的用法示例。


在下文中一共展示了BigInteger.Remainder方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ModReduce

 protected virtual BigInteger ModReduce(BigInteger x)
 {
     if (r == null)
     {
         x = x.Mod(q);
     }
     else
     {
         bool negative = x.SignValue < 0;
         if (negative)
         {
             x = x.Abs();
         }
         int qLen = q.BitLength;
         if (r.SignValue > 0)
         {
             BigInteger qMod = BigInteger.One.ShiftLeft(qLen);
             bool rIsOne = r.Equals(BigInteger.One);
             while (x.BitLength > (qLen + 1))
             {
                 BigInteger u = x.ShiftRight(qLen);
                 BigInteger v = x.Remainder(qMod);
                 if (!rIsOne)
                 {
                     u = u.Multiply(r);
                 }
                 x = u.Add(v);
             }
         }
         else
         {
             int d = ((qLen - 1) & 31) + 1;
             BigInteger mu = r.Negate();
             BigInteger u = mu.Multiply(x.ShiftRight(qLen - d));
             BigInteger quot = u.ShiftRight(qLen + d);
             BigInteger v = quot.Multiply(q);
             BigInteger bk1 = BigInteger.One.ShiftLeft(qLen + d);
             v = v.Remainder(bk1);
             x = x.Remainder(bk1);
             x = x.Subtract(v);
             if (x.SignValue < 0)
             {
                 x = x.Add(bk1);
             }
         }
         while (x.CompareTo(q) >= 0)
         {
             x = x.Subtract(q);
         }
         if (negative && x.SignValue != 0)
         {
             x = q.Subtract(x);
         }
     }
     return x;
 }
开发者ID:ubberkid,项目名称:PeerATT,代码行数:56,代码来源:ECFieldElement.cs

示例2: GenerateSafePrimes

		/*
		 * Finds a pair of prime BigInteger's {p, q: p = 2q + 1}
		 * 
		 * (see: Handbook of Applied Cryptography 4.86)
		 */
		internal static BigInteger[] GenerateSafePrimes(int size, int certainty, SecureRandom random)
		{
			BigInteger p, q;
			int qLength = size - 1;

			if (size <= 32)
			{
				for (;;)
				{
					q = new BigInteger(qLength, 2, random);

					p = q.ShiftLeft(1).Add(BigInteger.One);

					if (p.IsProbablePrime(certainty)
						&& (certainty <= 2 || q.IsProbablePrime(certainty)))
							break;
				}
			}
			else
			{
				// Note: Modified from Java version for speed
				for (;;)
				{
					q = new BigInteger(qLength, 0, random);

				retry:
					for (int i = 0; i < primeLists.Length; ++i)
					{
						int test = q.Remainder(PrimeProducts[i]).IntValue;

						if (i == 0)
						{
							int rem3 = test % 3;
							if (rem3 != 2)
							{
								int diff = 2 * rem3 + 2;
								q = q.Add(BigInteger.ValueOf(diff));
								test = (test + diff) % primeProducts[i];
							}
						}

						int[] primeList = primeLists[i];
						for (int j = 0; j < primeList.Length; ++j)
						{
							int prime = primeList[j];
							int qRem = test % prime;
							if (qRem == 0 || qRem == (prime >> 1))
							{
								q = q.Add(Six);
								goto retry;
							}
						}
					}


					if (q.BitLength != qLength)
						continue;

					if (!q.RabinMillerTest(2, random))
						continue;

					p = q.ShiftLeft(1).Add(BigInteger.One);

					if (p.RabinMillerTest(certainty, random)
						&& (certainty <= 2 || q.RabinMillerTest(certainty - 2, random)))
						break;
				}
			}

			return new BigInteger[] { p, q };
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:76,代码来源:DHParametersHelper.cs

示例3: ProcessBlock

		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);
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:40,代码来源:RSACoreEngine.cs

示例4: TestDiv

        private void TestDiv(BigInteger i1, BigInteger i2)
        {
            BigInteger q = i1.Divide(i2);
            BigInteger r = i1.Remainder(i2);
            BigInteger remainder;
            BigInteger quotient = i1.DivideAndRemainder(i2, out remainder);

            Assert.IsTrue(q.Equals(quotient), "Divide and DivideAndRemainder do not agree");
            Assert.IsTrue(r.Equals(remainder), "Remainder and DivideAndRemainder do not agree");
            Assert.IsTrue(q.Sign != 0 || q.Equals(zero), "signum and equals(zero) do not agree on quotient");
            Assert.IsTrue(r.Sign != 0 || r.Equals(zero), "signum and equals(zero) do not agree on remainder");
            Assert.IsTrue(q.Sign == 0 || q.Sign == i1.Sign * i2.Sign, "wrong sign on quotient");
            Assert.IsTrue(r.Sign == 0 || r.Sign == i1.Sign, "wrong sign on remainder");
            Assert.IsTrue(r.Abs().CompareTo(i2.Abs()) < 0, "remainder out of range");
            Assert.IsTrue(q.Abs().Add(one).Multiply(i2.Abs()).CompareTo(i1.Abs()) > 0, "quotient too small");
            Assert.IsTrue(q.Abs().Multiply(i2.Abs()).CompareTo(i1.Abs()) <= 0, "quotient too large");
            BigInteger p = q.Multiply(i2);
            BigInteger a = p.Add(r);
            Assert.IsTrue(a.Equals(i1), "(a/b)*b+(a%b) != a");
            try {
                BigInteger mod = i1.Mod(i2);
                Assert.IsTrue(mod.Sign >= 0, "mod is negative");
                Assert.IsTrue(mod.Abs().CompareTo(i2.Abs()) < 0, "mod out of range");
                Assert.IsTrue(r.Sign < 0 || r.Equals(mod), "positive remainder == mod");
                Assert.IsTrue(r.Sign >= 0 || r.Equals(mod.Subtract(i2)), "negative remainder == mod - divisor");
            } catch (ArithmeticException e) {
                Assert.IsTrue(i2.Sign <= 0, "mod fails on negative divisor only");
            }
        }
开发者ID:deveel,项目名称:deveel-math,代码行数:29,代码来源:BigIntegerTest.cs

示例5: GcdBinary

        /// <summary>
        /// Return the greatest common divisor of X and Y
        /// </summary>
        /// 
        /// <param name="X">Operand 1, must be greater than zero</param>
        /// <param name="Y">Operand 2, must be greater than zero</param>
        /// 
        /// <returns>Returns <c>GCD(X, Y)</c></returns>
        internal static BigInteger GcdBinary(BigInteger X, BigInteger Y)
        {
            // Divide both number the maximal possible times by 2 without rounding * gcd(2*a, 2*b) = 2 * gcd(a,b)
            int lsb1 = X.LowestSetBit;
            int lsb2 = Y.LowestSetBit;
            int pow2Count = System.Math.Min(lsb1, lsb2);

            BitLevel.InplaceShiftRight(X, lsb1);
            BitLevel.InplaceShiftRight(Y, lsb2);
            BigInteger swap;

            // I want op2 > op1
            if (X.CompareTo(Y) == BigInteger.GREATER)
            {
                swap = X;
                X = Y;
                Y = swap;
            }

            do
            { // INV: op2 >= op1 && both are odd unless op1 = 0

                // Optimization for small operands (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64)
                if ((Y._numberLength == 1) || ((Y._numberLength == 2) && (Y._digits[1] > 0)))
                {
                    Y = BigInteger.ValueOf(Division.GcdBinary(X.ToInt64(), Y.ToInt64()));
                    break;
                }

                // Implements one step of the Euclidean algorithm
                // To reduce one operand if it's much smaller than the other one
                if (Y._numberLength > X._numberLength * 1.2)
                {
                    Y = Y.Remainder(X);

                    if (Y.Signum() != 0)
                        BitLevel.InplaceShiftRight(Y, Y.LowestSetBit);
                }
                else
                {

                    // Use Knuth's algorithm of successive subtract and shifting
                    do
                    {
                        Elementary.InplaceSubtract(Y, X); // both are odd
                        BitLevel.InplaceShiftRight(Y, Y.LowestSetBit); // op2 is even
                    } while (Y.CompareTo(X) >= BigInteger.EQUALS);
                }
                // now op1 >= op2
                swap = Y;
                Y = X;
                X = swap;
            } while (X._sign != 0);

            return Y.ShiftLeft(pow2Count);
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:64,代码来源:Division.cs

示例6: IsSpp

        public bool IsSpp(BigInteger n, BigInteger a)
        {
            BigInteger two = BigInteger.Parse("" + 2);

            /* numbers less than 2 are not prime
                */
            if (n.CompareTo(two) == -1)
                return false;
            /* 2 is prime
                */
            if (n.CompareTo(two) == 0)
                return true;
            /* even numbers >2 are not prime
                */
            if (n.Remainder(two).CompareTo(BigInteger.Zero) == 0)
                return false;

            /* q= n- 1 = d *2^s with d odd
                        */
            BigInteger q = n.Subtract(BigInteger.One);
            int s = q.LowestSetBit;
            BigInteger d = q.ShiftRight(s);

            /* test whether a^d = 1 (mod n)
                        */
            if (a.ModPow(d, n).CompareTo(BigInteger.One) == 0)
                return true;

            /* test whether a^(d*2^r) = -1 (mod n), 0<=r<s
                        */
            for (int r = 0; r < s; r++) {
                if (a.ModPow(d.ShiftLeft(r), n).CompareTo(q) == 0)
                    return true;
            }
            return false;
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:36,代码来源:Prime.cs

示例7: GrowTo

        private void GrowTo(BigInteger n)
        {
            while (NMax.CompareTo(n) == -1) {
                NMax = NMax.Add(BigInteger.One);
                bool isp = true;
                for (int p = 0; p < numbers.Count; p++) {
                    /*
                    * Test the list of known primes only up to sqrt(n)
                    */
                    if (numbers[p].Multiply(numbers[p]).CompareTo(NMax) == 1)
                        break;

                    /*
                    * The next case means that the p'th number in the list of known primes divides
                    * nMax and nMax cannot be a prime.
                    */
                    if (NMax.Remainder(numbers[p]).CompareTo(BigInteger.Zero) == 0) {
                        isp = false;
                        break;
                    }
                }
                if (isp)
                    numbers.Add(NMax);
            }
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:25,代码来源:Prime.cs

示例8: GcdBinary

        /**
         * @param m a positive modulus
         * Return the greatest common divisor of op1 and op2,
         *
         * @param op1
         *            must be greater than zero
         * @param op2
         *            must be greater than zero
         * @see BigInteger#gcd(BigInteger)
         * @return {@code GCD(op1, op2)}
         */
        public static BigInteger GcdBinary(BigInteger op1, BigInteger op2)
        {
            // PRE: (op1 > 0) and (op2 > 0)

            /*
             * Divide both number the maximal possible times by 2 without rounding
                     * gcd(2*a, 2*b) = 2 * gcd(a,b)
             */
            int lsb1 = op1.LowestSetBit;
            int lsb2 = op2.LowestSetBit;
            int pow2Count = System.Math.Min(lsb1, lsb2);

            BitLevel.InplaceShiftRight(op1, lsb1);
            BitLevel.InplaceShiftRight(op2, lsb2);

            BigInteger swap;
            // I want op2 > op1
            if (op1.CompareTo(op2) == BigInteger.GREATER) {
                swap = op1;
                op1 = op2;
                op2 = swap;
            }

            do {
                // INV: op2 >= op1 && both are odd unless op1 = 0

                // Optimization for small operands
                // (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64)
                if ((op2.numberLength == 1)
                    || ((op2.numberLength == 2) && (op2.Digits[1] > 0))) {
                    op2 = BigInteger.ValueOf(Division.GcdBinary(op1.ToInt64(),
                        op2.ToInt64()));
                    break;
                }

                // Implements one step of the Euclidean algorithm
                // To reduce one operand if it's much smaller than the other one
                if (op2.numberLength > op1.numberLength*1.2) {
                    op2 = op2.Remainder(op1);
                    if (op2.Sign != 0)
                        BitLevel.InplaceShiftRight(op2, op2.LowestSetBit);
                } else {
                    // Use Knuth's algorithm of successive subtract and shifting
                    do {
                        Elementary.inplaceSubtract(op2, op1); // both are odd
                        BitLevel.InplaceShiftRight(op2, op2.LowestSetBit); // op2 is even
                    } while (op2.CompareTo(op1) >= BigInteger.EQUALS);
                }
                // now op1 >= op2
                swap = op2;
                op2 = op1;
                op1 = swap;
            } while (op1.Sign != 0);
            return op2.ShiftLeft(pow2Count);
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:66,代码来源:Division.cs


注:本文中的BigInteger.Remainder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。