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


C# BigInteger.ShiftLeft方法代码示例

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


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

示例1: GenerateCompactNaf

        public static int[] GenerateCompactNaf(BigInteger k)
        {
            if ((k.BitLength >> 16) != 0)
                throw new ArgumentException("must have bitlength < 2^16", "k");
            if (k.SignValue == 0)
                return EMPTY_INTS;

            BigInteger _3k = k.ShiftLeft(1).Add(k);

            int bits = _3k.BitLength;
            int[] naf = new int[bits >> 1];

            BigInteger diff = _3k.Xor(k);

            int highBit = bits - 1, length = 0, zeroes = 0;
            for (int i = 1; i < highBit; ++i)
            {
                if (!diff.TestBit(i))
                {
                    ++zeroes;
                    continue;
                }

                int digit = k.TestBit(i) ? -1 : 1;
                naf[length++] = (digit << 16) | zeroes;
                zeroes = 1;
                ++i;
            }

            naf[length++] = (1 << 16) | zeroes;

            if (naf.Length > length)
            {
                naf = Trim(naf, length);
            }

            return naf;
        }
开发者ID:bitcoinkit,项目名称:BitcoinKit-CSharp,代码行数:38,代码来源:WNafUtilities.cs

示例2: Add

		public SimpleBigDecimal Add(BigInteger b)
		{
			return new SimpleBigDecimal(bigInt.Add(b.ShiftLeft(scale)), scale);
		}
开发者ID:Nethereum,项目名称:Nethereum,代码行数:4,代码来源:SimpleBigDecimal.cs

示例3: GetInstance

		/**
		* Returns a <code>SimpleBigDecimal</code> representing the same numerical
		* value as <code>value</code>.
		* @param value The value of the <code>SimpleBigDecimal</code> to be
		* created. 
		* @param scale The scale of the <code>SimpleBigDecimal</code> to be
		* created. 
		* @return The such created <code>SimpleBigDecimal</code>.
		*/
		public static SimpleBigDecimal GetInstance(BigInteger val, int scale)
		{
			return new SimpleBigDecimal(val.ShiftLeft(scale), scale);
		}
开发者ID:Nethereum,项目名称:Nethereum,代码行数:13,代码来源:SimpleBigDecimal.cs

示例4: CompareTo

		public int CompareTo(BigInteger val)
		{
			return bigInt.CompareTo(val.ShiftLeft(scale));
		}
开发者ID:Nethereum,项目名称:Nethereum,代码行数:4,代码来源:SimpleBigDecimal.cs

示例5: DblToRgbPrecise


//.........这里部分代码省略.........
                } else {
                    c2Num = 0;
                    c2Den = -wExp2;
                }

                if (wExp10 >= 0) {
                    c5Num = 0;
                    c5Den = wExp10;
                    c2Den += wExp10;
                } else {
                    c2Num -= wExp10;
                    c5Num = -wExp10;
                    c5Den = 0;
                }

                if (c2Num > 0 && c2Den > 0) {
                    w1 = c2Num < c2Den ? c2Num : c2Den;
                    c2Num -= w1;
                    c2Den -= w1;
                }
                // We need a bit for the Hi and Lo values.
                c2Num++;
                c2Den++;

                // Initialize biNum and multiply by powers of 5.
                if (c5Num > 0) {
                    Debug.Assert(0 == c5Den);
                    biHi.MulPow5(c5Num);
                    biNum.InitFromBigint(biHi);
                    if (1 == cu) {
                        biNum.MulAdd(rgu0, 0);
                    } else {
                        biNum.MulAdd(rgu1, 0);
                        biNum.ShiftLeft(32);
                        if (0 != rgu0) {
                            biT.InitFromBigint(biHi);
                            biT.MulAdd(rgu0, 0);
                            biNum.Add(biT);
                        }
                    }
                } else {
                    Debug.Assert(cu <= 2);
                    biNum.InitFromDigits(rgu0, rgu1, cu);
                    if (c5Den > 0) {
                        biDen.MulPow5(c5Den);
                    }
                }

                // BigInteger.DivRem only works if the 4 high bits of the divisor are 0.
                // It works most efficiently if there are exactly 4 zero high bits.
                // Adjust c2Den and c2Num to guarantee this.
                w1 = CbitZeroLeft(biDen[biDen.Length - 1]);
                w1 = (w1 + 28 - c2Den) & 0x1F;
                c2Num += w1;
                c2Den += w1;

                // Multiply by powers of 2.
                Debug.Assert(c2Num > 0 && c2Den > 0);
                biNum.ShiftLeft(c2Num);
                if (c2Num > 1) {
                    biHi.ShiftLeft(c2Num - 1);
                }
                biDen.ShiftLeft(c2Den);
                Debug.Assert(0 == (biDen[biDen.Length - 1] & 0xF0000000));
                Debug.Assert(0 != (biDen[biDen.Length - 1] & 0x08000000));
开发者ID:uQr,项目名称:referencesource,代码行数:66,代码来源:XPathConvert.cs

示例6: DivideAndRound

        /// <summary>
        /// Divide two BigIntegers and return the rounded result
        /// </summary>
        /// 
        /// <param name="A">The first BigInteger</param>
        /// <param name="B">The second BigInteger</param>
        /// 
        /// <returns>The rounded result</returns>
        public static BigInteger DivideAndRound(BigInteger A, BigInteger B)
        {
            if (A.Signum() < 0)
                return DivideAndRound(A.Negate(), B).Negate();
            if (B.Signum() < 0)
                return DivideAndRound(A, B.Negate()).Negate();

            return A.ShiftLeft(1).Add(B).Divide(B.ShiftLeft(1));
        }
开发者ID:Steppenwolfe65,项目名称:Rainbow-NET,代码行数:17,代码来源:BigMath.cs

示例7: 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

示例8: TestMultiply

        public void TestMultiply()
        {
            BigInteger one = BigInteger.One;

            Assert.AreEqual(one, one.Negate().Multiply(one.Negate()));

            for (int i = 0; i < 100; ++i)
            {
                int aLen = 64 + Rnd.Next(64);
                int bLen = 64 + Rnd.Next(64);

                BigInteger a = new BigInteger(aLen, Rnd).SetBit(aLen);
                BigInteger b = new BigInteger(bLen, Rnd).SetBit(bLen);
                var c = new BigInteger(32, Rnd);

                BigInteger ab = a.Multiply(b);
                BigInteger bc = b.Multiply(c);

                Assert.AreEqual(ab.Add(bc), a.Add(c).Multiply(b));
                Assert.AreEqual(ab.Subtract(bc), a.Subtract(c).Multiply(b));
            }

            // Special tests for power of two since uses different code path internally
            for (int i = 0; i < 100; ++i)
            {
                int shift = Rnd.Next(64);
                BigInteger a = one.ShiftLeft(shift);
                var b = new BigInteger(64 + Rnd.Next(64), Rnd);
                BigInteger bShift = b.ShiftLeft(shift);

                Assert.AreEqual(bShift, a.Multiply(b));
                Assert.AreEqual(bShift.Negate(), a.Multiply(b.Negate()));
                Assert.AreEqual(bShift.Negate(), a.Negate().Multiply(b));
                Assert.AreEqual(bShift, a.Negate().Multiply(b.Negate()));

                Assert.AreEqual(bShift, b.Multiply(a));
                Assert.AreEqual(bShift.Negate(), b.Multiply(a.Negate()));
                Assert.AreEqual(bShift.Negate(), b.Negate().Multiply(a));
                Assert.AreEqual(bShift, b.Negate().Multiply(a.Negate()));
            }
        }
开发者ID:ChemicalRocketeer,项目名称:BigMath,代码行数:41,代码来源:BigIntegerTest.cs

示例9: GenerateNaf

        public static byte[] GenerateNaf(BigInteger k)
        {
            if (k.SignValue == 0)
                return EMPTY_BYTES;

            BigInteger _3k = k.ShiftLeft(1).Add(k);

            int digits = _3k.BitLength - 1;
            byte[] naf = new byte[digits];

            BigInteger diff = _3k.Xor(k);

            for (int i = 1; i < digits; ++i)
            {
                if (diff.TestBit(i))
                {
                    naf[i - 1] = (byte)(k.TestBit(i) ? -1 : 1);
                    ++i;
                }
            }

            naf[digits - 1] = 1;

            return naf;
        }
开发者ID:bitcoinkit,项目名称:BitcoinKit-CSharp,代码行数:25,代码来源:WNafUtilities.cs

示例10: multZModF

        /**
            * Computes <code>z * a(z) mod f(z)</code>, where <code>f(z)</code> is
            * the reduction polynomial of <code>this</code>.
            * @param a The polynomial <code>a(z)</code> to be multiplied by
            * <code>z mod f(z)</code>.
            * @return <code>z * a(z) mod f(z)</code>
            */
        private BigInteger multZModF(
			BigInteger a)
        {
            // Left-shift of a(z)
            BigInteger az = a.ShiftLeft(1);
            if (az.TestBit(this.m))
            {
                // If the coefficient of z^m in a(z) Equals 1, reduction
                // modulo f(z) is performed: Add f(z) to to a(z):
                // Step 1: Unset mth coeffient of a(z)
                az = az.ClearBit(this.m);

                // Step 2: Add r(z) to a(z), where r(z) is defined as
                // f(z) = z^m + r(z), and k1, k2, k3 are the positions of
                // the non-zero coefficients in r(z)
                az = az.FlipBit(0);
                az = az.FlipBit(this.k1);
                if (this.representation == Ppb)
                {
                    az = az.FlipBit(this.k2);
                    az = az.FlipBit(this.k3);
                }
            }
            return az;
        }
开发者ID:hjgode,项目名称:iTextSharpCF,代码行数:32,代码来源:ECFieldElement.cs

示例11: 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

示例12: GenerateParameters_FIPS186_2

		private DsaParameters GenerateParameters_FIPS186_2()
		{
            byte[] seed = new byte[20];
            byte[] part1 = new byte[20];
            byte[] part2 = new byte[20];
            byte[] u = new byte[20];
            Sha1Digest sha1 = new Sha1Digest();
			int n = (L - 1) / 160;
			byte[] w = new byte[L / 8];

			for (;;)
			{
				random.NextBytes(seed);

				Hash(sha1, seed, part1);
				Array.Copy(seed, 0, part2, 0, seed.Length);
				Inc(part2);
				Hash(sha1, part2, part2);

				for (int i = 0; i != u.Length; i++)
				{
					u[i] = (byte)(part1[i] ^ part2[i]);
				}

				u[0] |= (byte)0x80;
				u[19] |= (byte)0x01;

				BigInteger q = new BigInteger(1, u);

				if (!q.IsProbablePrime(certainty))
					continue;

				byte[] offset = Arrays.Clone(seed);
				Inc(offset);

				for (int counter = 0; counter < 4096; ++counter)
				{
					for (int k = 0; k < n; k++)
					{
						Inc(offset);
						Hash(sha1, offset, part1);
						Array.Copy(part1, 0, w, w.Length - (k + 1) * part1.Length, part1.Length);
					}

					Inc(offset);
					Hash(sha1, offset, part1);
					Array.Copy(part1, part1.Length - ((w.Length - (n) * part1.Length)), w, 0, w.Length - n * part1.Length);

					w[0] |= (byte)0x80;

					BigInteger x = new BigInteger(1, w);

					BigInteger c = x.Mod(q.ShiftLeft(1));

					BigInteger p = x.Subtract(c.Subtract(BigInteger.One));

					if (p.BitLength != L)
						continue;

					if (p.IsProbablePrime(certainty))
					{
						BigInteger g = CalculateGenerator_FIPS186_2(p, q, random);

						return new DsaParameters(p, q, g, new DsaValidationParameters(seed, counter));
					}
				}
			}
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:68,代码来源:DsaParametersGenerator.cs

示例13: GenerateParameters_FIPS186_3

		/**
		 * generate suitable parameters for DSA, in line with
		 * <i>FIPS 186-3 A.1 Generation of the FFC Primes p and q</i>.
		 */
		private DsaParameters GenerateParameters_FIPS186_3()
		{
// A.1.1.2 Generation of the Probable Primes p and q Using an Approved Hash Function
			// FIXME This should be configurable (digest size in bits must be >= N)
			IDigest d = new Sha256Digest();
			int outlen = d.GetDigestSize() * 8;

// 1. Check that the (L, N) pair is in the list of acceptable (L, N pairs) (see Section 4.2). If
//    the pair is not in the list, then return INVALID.
			// Note: checked at initialisation
			
// 2. If (seedlen < N), then return INVALID.
			// FIXME This should be configurable (must be >= N)
			int seedlen = N;
			byte[] seed = new byte[seedlen / 8];

// 3. n = ceiling(L ⁄ outlen) – 1.
			int n = (L - 1) / outlen;

// 4. b = L – 1 – (n ∗ outlen).
			int b = (L - 1) % outlen;

			byte[] output = new byte[d.GetDigestSize()];
			for (;;)
			{
// 5. Get an arbitrary sequence of seedlen bits as the domain_parameter_seed.
				random.NextBytes(seed);

// 6. U = Hash (domain_parameter_seed) mod 2^(N–1).
				Hash(d, seed, output);
				BigInteger U = new BigInteger(1, output).Mod(BigInteger.One.ShiftLeft(N - 1));

// 7. q = 2^(N–1) + U + 1 – ( U mod 2).
				BigInteger q = BigInteger.One.ShiftLeft(N - 1).Add(U).Add(BigInteger.One).Subtract(
					U.Mod(BigInteger.Two));

// 8. Test whether or not q is prime as specified in Appendix C.3.
				// TODO Review C.3 for primality checking
				if (!q.IsProbablePrime(certainty))
				{
// 9. If q is not a prime, then go to step 5.
					continue;
				}

// 10. offset = 1.
				// Note: 'offset' value managed incrementally
				byte[] offset = Arrays.Clone(seed);

// 11. For counter = 0 to (4L – 1) do
				int counterLimit = 4 * L;
				for (int counter = 0; counter < counterLimit; ++counter)
				{
// 11.1 For j = 0 to n do
//      Vj = Hash ((domain_parameter_seed + offset + j) mod 2^seedlen).
// 11.2 W = V0 + (V1 ∗ 2^outlen) + ... + (V^(n–1) ∗ 2^((n–1) ∗ outlen)) + ((Vn mod 2^b) ∗ 2^(n ∗ outlen)).
					// TODO Assemble w as a byte array
					BigInteger W = BigInteger.Zero;
					for (int j = 0, exp = 0; j <= n; ++j, exp += outlen)
					{
						Inc(offset);
						Hash(d, offset, output);

						BigInteger Vj = new BigInteger(1, output);
						if (j == n)
						{
							Vj = Vj.Mod(BigInteger.One.ShiftLeft(b));
						}

						W = W.Add(Vj.ShiftLeft(exp));
					}

// 11.3 X = W + 2^(L–1). Comment: 0 ≤ W < 2L–1; hence, 2L–1 ≤ X < 2L.
					BigInteger X = W.Add(BigInteger.One.ShiftLeft(L - 1));

// 11.4 c = X mod 2q.
					BigInteger c = X.Mod(q.ShiftLeft(1));

// 11.5 p = X - (c - 1). Comment: p ≡ 1 (mod 2q).
					BigInteger p = X.Subtract(c.Subtract(BigInteger.One));

					// 11.6 If (p < 2^(L - 1)), then go to step 11.9
					if (p.BitLength != L)
						continue;

// 11.7 Test whether or not p is prime as specified in Appendix C.3.
					// TODO Review C.3 for primality checking
					if (p.IsProbablePrime(certainty))
					{
// 11.8 If p is determined to be prime, then return VALID and the values of p, q and
//      (optionally) the values of domain_parameter_seed and counter.
						// TODO Make configurable (8-bit unsigned)?
//	                    int index = 1;
//	                    BigInteger g = CalculateGenerator_FIPS186_3_Verifiable(d, p, q, seed, index);
//	                    if (g != null)
//	                    {
//	                        // TODO Should 'index' be a part of the validation parameters?
//.........这里部分代码省略.........
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:101,代码来源:DsaParametersGenerator.cs

示例14: OddModPow

        /// <summary>
        /// Performs modular exponentiation using the Montgomery Reduction.
        /// <para>It requires that all parameters be positive and the modulus be odd. </para>
        /// </summary>
        /// 
        /// <param name="X">The BigInteger</param>
        /// <param name="Y">The exponent</param>
        /// <param name="Modulus">The modulus</param>
        /// 
        /// <returns><c>(modulus[0]^(-1)) (mod 2^32)</c></returns>
        internal static BigInteger OddModPow(BigInteger X, BigInteger Y, BigInteger Modulus)
        {
            // PRE: (base > 0), (exponent > 0), (modulus > 0) and (odd modulus)
            int k = (Modulus._numberLength << 5); // r = 2^k
            // n-residue of base [base * r (mod modulus)]
            BigInteger a2 = X.ShiftLeft(k).Mod(Modulus);
            // n-residue of base [1 * r (mod modulus)]
            BigInteger x2 = BigInteger.GetPowerOfTwo(k).Mod(Modulus);
            BigInteger res;
            // Compute (modulus[0]^(-1)) (mod 2^32) for odd modulus

            int n2 = CalcN(Modulus);

            if (Modulus._numberLength == 1)
                res = SquareAndMultiply(x2, a2, Y, Modulus, n2);
            else
                res = SlidingWindow(x2, a2, Y, Modulus, n2);

            return MonPro(res, BigInteger.One, Modulus, n2);
        }
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:30,代码来源:Division.cs

示例15: Subtract

		public SimpleBigDecimal Subtract(BigInteger b)
		{
			return new SimpleBigDecimal(bigInt.Subtract(b.ShiftLeft(scale)), scale);
		}
开发者ID:Nethereum,项目名称:Nethereum,代码行数:4,代码来源:SimpleBigDecimal.cs


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