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


C# BigInteger.BitCount方法代码示例

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


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

示例1: RabinMillerTest

        /// <summary>
        ///     Probabilistic prime test based on Rabin-Miller's test
        /// </summary>
        /// <param name="n" type="BigInteger.BigInteger">
        ///     <para>
        ///         The number to test.
        ///     </para>
        /// </param>
        /// <param name="confidence" type="int">
        ///     <para>
        ///	The number of chosen bases. The test has at least a
        ///	1/4^confidence chance of falsely returning True.
        ///     </para>
        /// </param>
        /// <returns>
        ///	<para>
        ///		True if "this" is a strong pseudoprime to randomly chosen bases.
        ///	</para>
        ///	<para>
        ///		False if "this" is definitely NOT prime.
        ///	</para>
        /// </returns>
        public static bool RabinMillerTest(BigInteger n, ConfidenceFactor confidence)
        {
            int bits = n.BitCount ();
            int t = GetSPPRounds (bits, confidence);

            // n - 1 == 2^s * r, r is odd
            BigInteger n_minus_1 = n - 1;
            int s = n_minus_1.LowestSetBit ();
            BigInteger r = n_minus_1 >> s;

            BigInteger.ModulusRing mr = new BigInteger.ModulusRing (n);

            // Applying optimization from HAC section 4.50 (base == 2)
            // not a really random base but an interesting (and speedy) one
            BigInteger y = null;
            // FIXME - optimization disable for small primes due to bug #81857
            if (n.BitCount () > 100)
                y = mr.Pow (2, r);

            // still here ? start at round 1 (round 0 was a == 2)
            for (int round = 0; round < t; round++) {

                if ((round > 0) || (y == null)) {
                    BigInteger a = null;

                    // check for 2 <= a <= n - 2
                    // ...but we already did a == 2 previously as an optimization
                    do {
                        a = BigInteger.GenerateRandom (bits);
                    } while ((a <= 2) && (a >= n_minus_1));

                    y = mr.Pow (a, r);
                }

                if (y == 1)
                    continue;

                for (int j = 0; ((j < s) && (y != n_minus_1)); j++) {

                    y = mr.Pow (y, 2);
                    if (y == 1)
                        return false;
                }

                if (y != n_minus_1)
                    return false;
            }
            return true;
        }
开发者ID:tt,项目名称:dotnetopenid,代码行数:71,代码来源:PrimalityTests.cs

示例2: GetSPPRounds

		private static int GetSPPRounds(BigInteger bi, ConfidenceFactor confidence)
		{
			int bc = bi.BitCount();

			int Rounds;

			// Data from HAC, 4.49
			if (bc <= 100) Rounds = 27;
			else if (bc <= 150) Rounds = 18;
			else if (bc <= 200) Rounds = 15;
			else if (bc <= 250) Rounds = 12;
			else if (bc <= 300) Rounds = 9;
			else if (bc <= 350) Rounds = 8;
			else if (bc <= 400) Rounds = 7;
			else if (bc <= 500) Rounds = 6;
			else if (bc <= 600) Rounds = 5;
			else if (bc <= 800) Rounds = 4;
			else if (bc <= 1250) Rounds = 3;
			else Rounds = 2;

			switch (confidence)
			{
				case ConfidenceFactor.ExtraLow:
					Rounds >>= 2;
					return Rounds != 0 ? Rounds : 1;
				case ConfidenceFactor.Low:
					Rounds >>= 1;
					return Rounds != 0 ? Rounds : 1;
				case ConfidenceFactor.Medium:
					return Rounds;
				case ConfidenceFactor.High:
					return Rounds << 1;
				case ConfidenceFactor.ExtraHigh:
					return Rounds << 2;
				case ConfidenceFactor.Provable:
					throw new Exception("The Rabin-Miller test can not be executed in a way such that its results are provable");
				default:
					throw new ArgumentOutOfRangeException("confidence");
			}
		}
开发者ID:vikewoods,项目名称:MTProto,代码行数:40,代码来源:PrimalityTests.cs

示例3: Test

		public static bool Test (BigInteger n, ConfidenceFactor confidence)
		{
			// Rabin-Miller fails with smaller primes (at least with our BigInteger code)
			if (n.BitCount () < 33)
				return SmallPrimeSppTest (n, confidence);
			else
				return RabinMillerTest (n, confidence);
		}
开发者ID:poderosaproject,项目名称:poderosa,代码行数:8,代码来源:PrimalityTests.cs

示例4: RabinMillerTest

		/// <summary>
		///     Probabilistic prime test based on Rabin-Miller's test
		/// </summary>
		/// <param name="bi" type="BigInteger.BigInteger">
		///     <para>
		///         The number to test.
		///     </para>
		/// </param>
		/// <param name="confidence" type="int">
		///     <para>
		///	The number of chosen bases. The test has at least a
		///	1/4^confidence chance of falsely returning True.
		///     </para>
		/// </param>
		/// <returns>
		///	<para>
		///		True if "this" is a strong pseudoprime to randomly chosen bases.
		///	</para>
		///	<para>
		///		False if "this" is definitely NOT prime.
		///	</para>
		/// </returns>
		public static bool RabinMillerTest (BigInteger bi, ConfidenceFactor confidence)
		{
			int Rounds = GetSPPRounds (bi, confidence);

			// calculate values of s and t
			BigInteger p_sub1 = bi - 1;
			int s = p_sub1.LowestSetBit ();

			BigInteger t = p_sub1 >> s;

			int bits = bi.BitCount ();
			BigInteger a = null;
			BigInteger.ModulusRing mr = new BigInteger.ModulusRing (bi);
			
			// Applying optimization from HAC section 4.50 (base == 2)
			// not a really random base but an interesting (and speedy) one
			BigInteger b = mr.Pow (2, t);
			if (b != 1) {
				bool result = false;
				for (int j=0; j < s; j++) {
					if (b == p_sub1) {         // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
						result = true;
						break;
					}

					b = (b * b) % bi;
				}
				if (!result)
					return false;
			}

			// still here ? start at round 1 (round 0 was a == 2)
			for (int round = 1; round < Rounds; round++) {
				while (true) {		           // generate a < n
					a = BigInteger.GenerateRandom (bits);

					// make sure "a" is not 0 (and not 2 as we have already tested that)
					if (a > 2 && a < bi)
						break;
				}

				if (a.GCD (bi) != 1)
					return false;

				b = mr.Pow (a, t);

				if (b == 1)
					continue;              // a^t mod p = 1

				bool result = false;
				for (int j = 0; j < s; j++) {

					if (b == p_sub1) {         // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
						result = true;
						break;
					}

					b = (b * b) % bi;
				}

				if (!result)
					return false;
			}
			return true;
		}
开发者ID:Dawn-of-Light,项目名称:TomCryptLibNET,代码行数:87,代码来源:PrimalityTests.cs


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