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


C# BigInteger.CompareTo方法代码示例

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


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

示例1: CreateRandomInRange

		/**
		* Return a random BigInteger not less than 'min' and not greater than 'max'
		* 
		* @param min the least value that may be generated
		* @param max the greatest value that may be generated
		* @param random the source of randomness
		* @return a random BigInteger value in the range [min,max]
		*/
		public static BigInteger CreateRandomInRange(
			BigInteger		min,
			BigInteger		max,
			// TODO Should have been just Random class
			SecureRandom	random)
		{
			int cmp = min.CompareTo(max);
			if (cmp >= 0)
			{
				if (cmp > 0)
					throw new ArgumentException("'min' may not be greater than 'max'");

				return min;
			}

			if (min.BitLength > max.BitLength / 2)
			{
				return CreateRandomInRange(BigInteger.Zero, max.Subtract(min), random).Add(min);
			}

			for (int i = 0; i < MaxIterations; ++i)
			{
				BigInteger x = new BigInteger(max.BitLength, random);
				if (x.CompareTo(min) >= 0 && x.CompareTo(max) <= 0)
				{
					return x;
				}
			}

			// fall back to a faster (restricted) method
			return new BigInteger(max.Subtract(min).BitLength - 1, random).Add(min);
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:40,代码来源:BigIntegers.cs

示例2: RsaSecretBcpgKey

		public RsaSecretBcpgKey(
			BigInteger d,
			BigInteger p,
			BigInteger q)
		{
			// PGP requires (p < q)
			int cmp = p.CompareTo(q);
			if (cmp >= 0)
			{
				if (cmp == 0)
					throw new ArgumentException("p and q cannot be equal");

				BigInteger tmp = p;
				p = q;
				q = tmp;
			}

			this.d = new MPInteger(d);
			this.p = new MPInteger(p);
			this.q = new MPInteger(q);
			this.u = new MPInteger(p.ModInverse(q));

			this.expP = d.Remainder(p.Subtract(BigInteger.One));
			this.expQ = d.Remainder(q.Subtract(BigInteger.One));
			this.crt = q.ModInverse(p);
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:26,代码来源:RsaSecretBcpgKey.cs

示例3: GenerateSignature

		/**
		 * generate a signature for the given message using the key we were
		 * initialised with. For conventional Gost3410 the message should be a Gost3411
		 * hash of the message of interest.
		 *
		 * @param message the message that will be verified later.
		 */
		public BigInteger[] GenerateSignature(
			byte[] message)
		{
			byte[] mRev = new byte[message.Length]; // conversion is little-endian
			for (int i = 0; i != mRev.Length; i++)
			{
				mRev[i] = message[mRev.Length - 1 - i];
			}

			BigInteger m = new BigInteger(1, mRev);
			Gost3410Parameters parameters = key.Parameters;
			BigInteger k;

			do
			{
				k = new BigInteger(parameters.Q.BitLength, random);
			}
			while (k.CompareTo(parameters.Q) >= 0);

			BigInteger r = parameters.A.ModPow(k, parameters.P).Mod(parameters.Q);

			BigInteger s = k.Multiply(m).
				Add(((Gost3410PrivateKeyParameters)key).X.Multiply(r)).
				Mod(parameters.Q);

			return new BigInteger[]{ r, s };
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:34,代码来源:GOST3410Signer.cs

示例4: GenerateKeyPair

		public AsymmetricCipherKeyPair GenerateKeyPair()
		{
			SecureRandom random = param.Random;
			Gost3410Parameters gost3410Params = param.Parameters;

			BigInteger q = gost3410Params.Q;
			BigInteger x;
			do
			{
				x = new BigInteger(256, random);
			}
			while (x.Sign < 1 || x.CompareTo(q) >= 0);

			BigInteger p = gost3410Params.P;
			BigInteger a = gost3410Params.A;

			// calculate the public key.
			BigInteger y = a.ModPow(x, p);

			if (param.PublicKeyParamSet != null)
			{
				return new AsymmetricCipherKeyPair(
					new Gost3410PublicKeyParameters(y, param.PublicKeyParamSet),
					new Gost3410PrivateKeyParameters(x, param.PublicKeyParamSet));
			}

			return new AsymmetricCipherKeyPair(
				new Gost3410PublicKeyParameters(y, gost3410Params),
				new Gost3410PrivateKeyParameters(x, gost3410Params));
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:30,代码来源:GOST3410KeyPairGenerator.cs

示例5: SecP384R1FieldElement

        public SecP384R1FieldElement(BigInteger x)
        {
            if (x == null || x.SignValue < 0 || x.CompareTo(Q) >= 0)
                throw new ArgumentException("value invalid for SecP384R1FieldElement", "x");

            this.x = SecP384R1Field.FromBigInteger(x);
        }
开发者ID:MBrekhof,项目名称:pleiobox-clients,代码行数:7,代码来源:SecP384R1FieldElement.cs

示例6: Gost3410PublicKeyParameters

		public Gost3410PublicKeyParameters(
			BigInteger			y,
			DerObjectIdentifier publicKeyParamSet)
			: base(false, publicKeyParamSet)
		{
			if (y.Sign < 1 || y.CompareTo(Parameters.P) >= 0)
				throw new ArgumentException("Invalid y for GOST3410 public key", "y");

			this.y = y;
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:10,代码来源:GOST3410PublicKeyParameters.cs

示例7: FpFieldElement

		public FpFieldElement(
			BigInteger	q,
			BigInteger	x)
		{
			if (x.CompareTo(q) >= 0)
				throw new ArgumentException("x value too large in field element");

			this.q = q;
			this.x = x;
		}
开发者ID:haf,项目名称:bc-csharp,代码行数:10,代码来源:ECFieldElement.cs

示例8: Gost3410PrivateKeyParameters

		public Gost3410PrivateKeyParameters(
			BigInteger			x,
			DerObjectIdentifier	publicKeyParamSet)
			: base(true, publicKeyParamSet)
		{
			if (x.Sign < 1 || x.BitLength > 256 || x.CompareTo(Parameters.Q) >= 0)
				throw new ArgumentException("Invalid x for GOST3410 private key", "x");

			this.x = x;
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:10,代码来源:GOST3410PrivateKeyParameters.cs

示例9: DHParameters

		public DHParameters(
			BigInteger				p,
			BigInteger				g,
			BigInteger				q,
			int						m,
			int						l,
			BigInteger				j,
			DHValidationParameters	validation)
		{
			if (p == null)
				throw new ArgumentNullException("p");
			if (g == null)
				throw new ArgumentNullException("g");
			if (!p.TestBit(0))
				throw new ArgumentException("field must be an odd prime", "p");
			if (g.CompareTo(BigInteger.Two) < 0
				|| g.CompareTo(p.Subtract(BigInteger.Two)) > 0)
				throw new ArgumentException("generator must in the range [2, p - 2]", "g");
			if (q != null && q.BitLength >= p.BitLength)
				throw new ArgumentException("q too big to be a factor of (p-1)", "q");
			if (m >= p.BitLength)
				throw new ArgumentException("m value must be < bitlength of p", "m");
			if (l != 0)
			{ 
	            if (l >= p.BitLength)
                	throw new ArgumentException("when l value specified, it must be less than bitlength(p)", "l");
				if (l < m)
					throw new ArgumentException("when l value specified, it may not be less than m value", "l");
			}
			if (j != null && j.CompareTo(BigInteger.Two) < 0)
				throw new ArgumentException("subgroup factor must be >= 2", "j");

			// TODO If q, j both provided, validate p = jq + 1 ?

			this.p = p;
			this.g = g;
			this.q = q;
			this.m = m;
			this.l = l;
			this.j = j;
			this.validation = validation;
        }
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:42,代码来源:DHParameters.cs

示例10: Calculate

 public override Number Calculate(BigInteger bigint1, BigInteger bigint2)
 {
     if (bigint1 == null || bigint2 == null)
     {
         return 0;
     }
     var comp = bigint2.CompareTo(BigInteger.Zero);
     if (comp == 0)
     {
         return 0;
     }
     if (comp < 0)
     {
         return bigint1.Negate().Mod(bigint2).Negate();
     }
     return bigint1.Mod(bigint2);
 }
开发者ID:tupunco,项目名称:Tup.Cobar4Net,代码行数:17,代码来源:ArithmeticModExpression.cs

示例11: GenerateSignature

		// 5.3 pg 28
		/**
		 * Generate a signature for the given message using the key we were
		 * initialised with. For conventional DSA the message should be a SHA-1
		 * hash of the message of interest.
		 *
		 * @param message the message that will be verified later.
		 */
		public BigInteger[] GenerateSignature(
			byte[] message)
		{
			BigInteger n = key.Parameters.N;
			BigInteger e = calculateE(n, message);

			BigInteger r = null;
			BigInteger s = null;

			// 5.3.2
			do // Generate s
			{
				BigInteger k = null;

				do // Generate r
				{
					do
					{
						k = new BigInteger(n.BitLength, random);
					}
					while (k.Sign == 0 || k.CompareTo(n) >= 0);

					ECPoint p = key.Parameters.G.Multiply(k);

					// 5.3.3
					BigInteger x = p.X.ToBigInteger();

					r = x.Mod(n);
				}
				while (r.Sign == 0);

				BigInteger d = ((ECPrivateKeyParameters)key).D;

				s = k.ModInverse(n).Multiply(e.Add(d.Multiply(r))).Mod(n);
			}
			while (s.Sign == 0);

			return new BigInteger[]{ r, s };
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:47,代码来源:ECDsaSigner.cs

示例12: GenerateSignature

		/**
		 * Generate a signature for the given message using the key we were
		 * initialised with. For conventional DSA the message should be a SHA-1
		 * hash of the message of interest.
		 *
		 * @param message the message that will be verified later.
		 */
		public BigInteger[] GenerateSignature(
			byte[] message)
		{
			DsaParameters parameters = key.Parameters;
			BigInteger q = parameters.Q;
			BigInteger m = calculateE(q, message);
			BigInteger k;

			do
			{
				k = new BigInteger(q.BitLength, random);
			}
			while (k.CompareTo(q) >= 0);

			BigInteger r = parameters.G.ModPow(k, parameters.P).Mod(q);

			k = k.ModInverse(q).Multiply(
				m.Add(((DsaPrivateKeyParameters)key).X.Multiply(r)));

			BigInteger s = k.Mod(q);

			return new BigInteger[]{ r, s };
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:30,代码来源:DsaSigner.cs

示例13: VerifyComparison

        private static void VerifyComparison(BigInteger x, UInt64 y, int expectedResult)
        {
            bool expectedEquals = 0 == expectedResult;
            bool expectedLessThan = expectedResult < 0;
            bool expectedGreaterThan = expectedResult > 0;

            Assert.Equal(expectedEquals, x == y);
            Assert.Equal(expectedEquals, y == x);

            Assert.Equal(!expectedEquals, x != y);
            Assert.Equal(!expectedEquals, y != x);

            Assert.Equal(expectedEquals, x.Equals(y));

            VerifyCompareResult(expectedResult, x.CompareTo(y), "x.CompareTo(y)");

            if (expectedEquals)
            {
                Assert.Equal(x.GetHashCode(), ((BigInteger)y).GetHashCode());
                Assert.Equal(x.ToString(), ((BigInteger)y).ToString());
            }

            Assert.Equal(x.GetHashCode(), x.GetHashCode());
            Assert.Equal(((BigInteger)y).GetHashCode(), ((BigInteger)y).GetHashCode());

            Assert.Equal(expectedLessThan, x < y);
            Assert.Equal(expectedGreaterThan, y < x);

            Assert.Equal(expectedGreaterThan, x > y);
            Assert.Equal(expectedLessThan, y > x);

            Assert.Equal(expectedLessThan || expectedEquals, x <= y);
            Assert.Equal(expectedGreaterThan || expectedEquals, y <= x);

            Assert.Equal(expectedGreaterThan || expectedEquals, x >= y);
            Assert.Equal(expectedLessThan || expectedEquals, y >= x);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:37,代码来源:Comparison.cs

示例14: DblToRgbPrecise


//.........这里部分代码省略.........
                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));

                // Get biHiLo and handle the power of 2 case where biHi needs to be doubled.
                if (fPow2) {
                    biHiLo = biLo;
                    biHiLo.InitFromBigint(biHi);
                    biHi.ShiftLeft(1);
                } else {
                    biHiLo = biHi;
                }

                for (ib = 0; ; ) {
                    bT = (byte)biNum.DivRem(biDen);
                    if (0 == ib && 0 == bT) {
                        // Our estimate of wExp10 was too big. Oh well.
                        wExp10--;
                        goto LSkip;
                    }

                    // w1 = sign(biNum - biHiLo).
                    w1 = biNum.CompareTo(biHiLo);

                    // w2 = sign(biNum + biHi - biDen).
                    if (biDen.CompareTo(biHi) < 0) {
                        w2 = 1;
                    } else {
                        // 
                        biT.InitFromBigint(biDen);
                        biT.Subtract(biHi);
                        w2 = biNum.CompareTo(biT);
                    }

                    // if (biNum + biHi == biDen && even)
                    if (0 == w2 && 0 == (dblLo & 1)) {
                        // Rounding up this digit produces exactly (biNum + biHi) which
                        // StrToDbl will round down to dbl.
                        if (bT == 9) {
                            goto LRoundUp9;
                        }
                        if (w1 > 0) {
                            bT++;
                        }
                        mantissa[ib++] = bT;
                        break;
                    }

                    // if (biNum < biHiLo || biNum == biHiLo && even)
                    if (w1 < 0 || 0 == w1 && 0 == (dblLo & 1)) {
                        // if (biNum + biHi > biDen)
                        if (w2 > 0) {
                            // Decide whether to round up.
                            biNum.ShiftLeft(1);
                            w2 = biNum.CompareTo(biDen);
开发者ID:uQr,项目名称:referencesource,代码行数:67,代码来源:XPathConvert.cs

示例15: Jacobi

        /// <summary>
        /// Computes the value of the Jacobi symbol (A|B). 
        /// </summary>
        /// 
        /// <param name="A">The integer value</param>
        /// <param name="B">The integer value</param>
        /// 
        /// <returns>Returns value of the jacobi symbol (A|B)</returns>
        public static int Jacobi(BigInteger A, BigInteger B)
        {
            BigInteger a, b, v;
            long k = 1;

            // test trivial cases
            if (B.Equals(ZERO))
            {
                a = A.Abs();
                return a.Equals(ONE) ? 1 : 0;
            }

            if (!A.TestBit(0) && !B.TestBit(0))
                return 0;

            a = A;
            b = B;

            if (b.Signum() == -1)
            { // b < 0
                b = b.Negate();
                if (a.Signum() == -1)
                    k = -1;
            }

            v = ZERO;
            while (!b.TestBit(0))
            {
                v = v.Add(ONE);
                b = b.Divide(TWO);
            }

            if (v.TestBit(0))
                k = k * _jacobiTable[a.ToInt32() & 7];

            if (a.Signum() < 0)
            {
                if (b.TestBit(1))
                    k = -k;
                a = a.Negate();
            }

            // main loop
            while (a.Signum() != 0)
            {
                v = ZERO;
                while (!a.TestBit(0))
                { // a is even
                    v = v.Add(ONE);
                    a = a.Divide(TWO);
                }
                if (v.TestBit(0))
                    k = k * _jacobiTable[b.ToInt32() & 7];

                if (a.CompareTo(b) < 0)
                {
                    // swap and correct intermediate result
                    BigInteger x = a;
                    a = b;
                    b = x;
                    if (a.TestBit(1) && b.TestBit(1))
                        k = -k;
                }
                a = a.Subtract(b);
            }

            return b.Equals(ONE) ? (int)k : 0;
        }
开发者ID:Steppenwolfe65,项目名称:Rainbow-NET,代码行数:76,代码来源:BigMath.cs


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