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


C# BigInteger.bitCount方法代码示例

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


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

示例1: 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;
            RandomNumberGenerator rng = RandomNumberGenerator.Create();
            BigInteger.ModulusRing mr = new BigInteger.ModulusRing(bi);

            for (int round = 0; round < Rounds; round++)
            {
                while (true)
                {		           // generate a < n
                    a = BigInteger.genRandom(bits, rng);

                    // make sure "a" is not 0
                    if (a > 1 && a < bi)
                        break;
                }

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

                BigInteger 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 == false)
                    return false;
            }
            return true;
        }
开发者ID:JamesHagerman,项目名称:sftprelay,代码行数:72,代码来源: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:hnlshzx,项目名称:DotNetOpenAuth,代码行数:39,代码来源:PrimalityTests.cs

示例3: LucasSequenceHelper

        //***********************************************************************
        // Performs the calculation of the kth term in the Lucas Sequence.
        // For details of the algorithm, see reference [9].
        //
        // k must be odd.  i.e LSB == 1
        //***********************************************************************

        private static BigInteger[] LucasSequenceHelper(BigInteger P, BigInteger Q,
                                                        BigInteger k, BigInteger n,
                                                        BigInteger constant, int s)
        {
                BigInteger[] result = new BigInteger[3];

                if((k.data[0] & 0x00000001) == 0)
                        throw (new ArgumentException("Argument k must be odd."));

                int numbits = k.bitCount();
                uint mask = (uint)0x1 << ((numbits & 0x1F) - 1);

                // v = v0, v1 = v1, u1 = u1, Q_k = Q^0

                BigInteger v = 2 % n, Q_k = 1 % n,
                           v1 = P % n, u1 = Q_k;
                bool flag = true;

                for(int i = k.dataLength - 1; i >= 0 ; i--)     // iterate on the binary expansion of k
                {
                        //Console.WriteLine("round");
                        while(mask != 0)
                        {
                                if(i == 0 && mask == 0x00000001)        // last bit
                                        break;

                                if((k.data[i] & mask) != 0)             // bit is set
                                {
                                        // index doubling with addition

                                        u1 = (u1 * v1) % n;

                                        v = ((v * v1) - (P * Q_k)) % n;
                                        v1 = n.BarrettReduction(v1 * v1, n, constant);
                                        v1 = (v1 - ((Q_k * Q) << 1)) % n;

                                        if(flag)
                                                flag = false;
                                        else
                                                Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);

                                        Q_k = (Q_k * Q) % n;
                                }
                                else
                                {
                                        // index doubling
                                        u1 = ((u1 * v) - Q_k) % n;

                                        v1 = ((v * v1) - (P * Q_k)) % n;
                                        v = n.BarrettReduction(v * v, n, constant);
                                        v = (v - (Q_k << 1)) % n;

                                        if(flag)
                                        {
                                                Q_k = Q % n;
                                                flag = false;
                                        }
                                        else
                                                Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);
                               }

                               mask >>= 1;
                        }
                        mask = 0x80000000;
                }

                // at this point u1 = u(n+1) and v = v(n)
                // since the last bit always 1, we need to transform u1 to u(2n+1) and v to v(2n+1)

                u1 = ((u1 * v) - Q_k) % n;
                v = ((v * v1) - (P * Q_k)) % n;
                if(flag)
                        flag = false;
                else
                        Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);

                Q_k = (Q_k * Q) % n;


                for(int i = 0; i < s; i++)
                {
                        // index doubling
                        u1 = (u1 * v) % n;
                        v = ((v * v) - (Q_k << 1)) % n;

                        if(flag)
                        {
                                Q_k = Q % n;
                                flag = false;
                        }
                        else
                                Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);
                }
//.........这里部分代码省略.........
开发者ID:AudriusKniuras,项目名称:ChatClientServer,代码行数:101,代码来源:BigInteger.cs

示例4: modPow

        //***********************************************************************
        // Modulo Exponentiation
        //***********************************************************************

        public BigInteger modPow(BigInteger exp, BigInteger n)
        {
                if((exp.data[maxLength-1] & 0x80000000) != 0)
                        throw (new ArithmeticException("Positive exponents only."));

                BigInteger resultNum = 1;
	        BigInteger tempNum;
	        bool thisNegative = false;

	        if((this.data[maxLength-1] & 0x80000000) != 0)   // negative this
	        {
	                tempNum = -this % n;
	                thisNegative = true;
	        }
	        else
	                tempNum = this % n;  // ensures (tempNum * tempNum) < b^(2k)

	        if((n.data[maxLength-1] & 0x80000000) != 0)   // negative n
	                n = -n;

                // calculate constant = b^(2k) / m
                BigInteger constant = new BigInteger();

                int i = n.dataLength << 1;
                constant.data[i] = 0x00000001;
                constant.dataLength = i + 1;

                constant = constant / n;
                int totalBits = exp.bitCount();
                int count = 0;

                // perform squaring and multiply exponentiation
                for(int pos = 0; pos < exp.dataLength; pos++)
                {
                        uint mask = 0x01;
                        //Console.WriteLine("pos = " + pos);

                        for(int index = 0; index < 32; index++)
                        {
                                if((exp.data[pos] & mask) != 0)
                                        resultNum = BarrettReduction(resultNum * tempNum, n, constant);

                                mask <<= 1;

                                tempNum = BarrettReduction(tempNum * tempNum, n, constant);


                                if(tempNum.dataLength == 1 && tempNum.data[0] == 1)
                                {
                                        if(thisNegative && (exp.data[0] & 0x1) != 0)    //odd exp
                                                return -resultNum;
                                        return resultNum;
                                }
                                count++;
                                if(count == totalBits)
                                        break;
                        }
                }

                if(thisNegative && (exp.data[0] & 0x1) != 0)    //odd exp
                        return -resultNum;

	        return resultNum;
        }
开发者ID:AudriusKniuras,项目名称:ChatClientServer,代码行数:68,代码来源:BigInteger.cs

示例5: WriteBigIntWithBits

 public void WriteBigIntWithBits(BigInteger bi)
 {
     WriteInt32(bi.bitCount());
     Write(bi.getBytes());
 }
开发者ID:FNKGino,项目名称:poderosa,代码行数:5,代码来源:ReaderWriter.cs

示例6: PKCS1PadType1

        public static BigInteger PKCS1PadType1(BigInteger input, int pad_len) {
            int input_byte_length = (input.bitCount() + 7) / 8;
            //System.out.println(String.valueOf(pad_len) + ":" + input_byte_length);
            byte[] pad = new byte[pad_len - input_byte_length - 3];

            for (int i = 0; i < pad.Length; i++) {
                pad[i] = (byte)0xff;
            }

            BigInteger pad_int = new BigInteger(pad);
            pad_int = pad_int << ((input_byte_length + 1) * 8);
            BigInteger result = new BigInteger(1);
            result = result << ((pad_len - 2) * 8);
            result = result | pad_int;
            result = result | input;

            return result;
        }
开发者ID:PavelTorgashov,项目名称:nfx,代码行数:18,代码来源:RSA.cs

示例7: PKCS1PadType2

        public static BigInteger PKCS1PadType2(BigInteger input, int pad_len, Rng rng) {
            int input_byte_length = (input.bitCount() + 7) / 8;
            //System.out.println(String.valueOf(pad_len) + ":" + input_byte_length);
            byte[] pad = new byte[pad_len - input_byte_length - 3];

            for (int i = 0; i < pad.Length; i++) {
                byte[] b = new byte[1];
                rng.GetBytes(b);
                while (b[0] == 0)
                    rng.GetBytes(b); //0ではだめだ
                pad[i] = b[0];
            }

            BigInteger pad_int = new BigInteger(pad);
            pad_int = pad_int << ((input_byte_length + 1) * 8);
            BigInteger result = new BigInteger(2);
            result = result << ((pad_len - 2) * 8);
            result = result | pad_int;
            result = result | input;

            return result;
        }
开发者ID:PavelTorgashov,项目名称:nfx,代码行数:22,代码来源:RSA.cs

示例8: findRandomStrongPrime

        private static BigInteger[] findRandomStrongPrime(uint primeBits, int orderBits,	Random random)
        {
            BigInteger one = new BigInteger(1);
            BigInteger u, aux, aux2;
            long[] table_q, table_u, prime_table;
            PrimeSieve sieve = new PrimeSieve(16000);
            uint table_count  = sieve.AvailablePrimes() - 1;
            int i, j;
            bool flag;
            BigInteger prime = null, order = null;

            order = BigInteger.genPseudoPrime(orderBits, 20, random);

            prime_table = new long[table_count];
            table_q     = new long[table_count];
            table_u     = new long[table_count];

            i = 0;
            for(int pN = 2; pN != 0; pN = sieve.getNextPrime(pN), i++) {
                prime_table[i] = (long)pN;
            }

            for(i = 0; i < table_count; i++) {
                table_q[i] =
                    (((order % new BigInteger(prime_table[i])).LongValue()) *
                    (long)2) % prime_table[i];
            }

            while(true) {
                u = new BigInteger();
                u.genRandomBits((int)primeBits, random);
                u.setBit(primeBits - 1);
                aux = order << 1;
                aux2 = u % aux;
                u = u - aux2;
                u = u + one;

                if(u.bitCount() <= (primeBits - 1))
                    continue;

                for(j = 0; j < table_count; j++) {
                    table_u[j] =
                        (u % new BigInteger(prime_table[j])).LongValue();
                }

                aux2 = order << 1;

                for(i = 0; i < (1 << 24); i++) {
                    long cur_p;
                    long value;

                    flag = true;
                    for(j = 1; j < table_count; j++) {
                        cur_p = prime_table[j];
                        value = table_u[j];
                        if(value >= cur_p)
                            value -= cur_p;
                        if(value == 0)
                            flag = false;
                        table_u[j] = value + table_q[j];
                    }
                    if(!flag)
                        continue;

                    aux   = aux2 * new BigInteger(i);
                    prime = u + aux;

                    if(prime.bitCount() > primeBits)
                        continue;

                    if(prime.isProbablePrime(20))
                        break;
                }

                if(i < (1 << 24))
                    break;
            }

            return new BigInteger[] { prime, order };
        }
开发者ID:rfyiamcool,项目名称:solrex,代码行数:80,代码来源:DSA.cs

示例9: findRandomGenerator

        private static BigInteger findRandomGenerator(BigInteger order, BigInteger modulo, Random random)
        {
            BigInteger one = new BigInteger(1);
            BigInteger aux = modulo - new BigInteger(1);
            BigInteger t   = aux % order;
            BigInteger generator;

            if(t.LongValue() != 0) {
                return null;
            }

            t = aux / order;

            while(true) {
                generator = new BigInteger();
                generator.genRandomBits(modulo.bitCount(), random);
                generator = generator % modulo;
                generator = generator.modPow(t, modulo);
                if(generator!=one)
                    break;
            }

            aux = generator.modPow(order, modulo);

            if(aux!=one) {
                return null;
            }

            return generator;
        }
开发者ID:rfyiamcool,项目名称:solrex,代码行数:30,代码来源:DSA.cs

示例10: roll

        // TODO redesign keep strategy to allow keeping both highest and lowest
        private static BigInteger roll(BigInteger dieType, long dieCount, long keepCount, KeepStrategy keepStrategy)
        {
            if (dieType <= 1) {
                throw new InvalidExpressionException ("invalid die");
            }

            // if the diecount is negative we will roll with the positive diecount, but negate the end result.
            // basically, "-5d6" is treated as "-(5d6)"
            bool negative = false;
            if (dieCount < 0) {
                negative = true;
                dieCount = -dieCount;
            }

            keepCount = Math.Min(keepCount, dieCount);

            // roll the dice and keep them in an array
            BigInteger[] results = new BigInteger[dieCount];
            for (int i = 0; i < dieCount; i++) {
                BigInteger num = new BigInteger ();
                num.genRandomBits (dieType.bitCount (), RAND);
                results [i] = num;
            }

            // add up the results based on the strategy used
            BigInteger result = 0;
            if (keepStrategy == KeepStrategy.ALL) {
                for (int i = 0; i < dieCount; i++) {
                    result += results [i];
                }
            } else { // we are only keeping some, so sort the list
                Array.Sort (results);
                if (keepStrategy == KeepStrategy.HIGHEST) {
                    for (long i = dieCount - 1; i >= dieCount - keepCount; i--) {
                        result += results [i];
                    }
                } else if (keepStrategy == KeepStrategy.LOWEST) {
                    for (int i = 0; i < keepCount; i++) {
                        result += results [i];
                    }
                }
            }
            if (negative) {
                result = -result;
            }
            return result;
        }
开发者ID:ChemicalRocketeer,项目名称:RollifyXamarin,代码行数:48,代码来源:Roller.cs


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