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


C# BigInteger.Add方法代码示例

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


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

示例1: Add_neg_pos_same_mag

 public void Add_neg_pos_same_mag()
 {
     BigInteger x = new BigInteger(-1, new uint[] { 0x3 });
     BigInteger y = new BigInteger(1, new uint[] { 0x3 });
     BigInteger z = x.Add(y);
     Expect(z.IsZero);
 }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:7,代码来源:BigIntegerTests.cs

示例2: Add_neg_pos_second_larger_mag

 public void Add_neg_pos_second_larger_mag()
 {
     BigInteger x = new BigInteger(-1, new uint[] { 0x3 });
     BigInteger y = new BigInteger(1, new uint[] { 0x5 });
     BigInteger z = x.Add(y);
     Expect(SameValue(z, 1, new uint[] { 0x2 }));
 }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:7,代码来源:BigIntegerTests.cs

示例3: Add_neg_same_length_some_carry

        public void Add_neg_same_length_some_carry()
        {
            BigInteger x = new BigInteger(-1, new uint[] { 0x12345678, 0x12345678, 0xFFFFFFFF });
            BigInteger y = new BigInteger(-1, new uint[] { 0x23456789, 0x13243546, 0x11111111 });
            BigInteger z = x.Add(y);

            Expect(SameValue(z, -1, new uint[] { 0x3579BE01, 0x25588BBF, 0x11111110 }));
        }
开发者ID:jlomax,项目名称:clojure-clr,代码行数:8,代码来源:BigIntegerTests.cs

示例4: Ressol

        /// <summary>
        /// Computes the square root of a BigInteger modulo a prime employing the Shanks-Tonelli algorithm
        /// </summary>
        /// 
        /// <param name="X">The value out of which we extract the square root</param>
        /// <param name="P">The prime modulus that determines the underlying field</param>
        /// 
        /// <returns>Returns a number <c>B</c> such that B^2 = A (mod P) if <c>A</c> is a quadratic residue modulo <c>P</c></returns>
        public static BigInteger Ressol(BigInteger X, BigInteger P)
        {
            BigInteger v = null;

            if (X.CompareTo(ZERO) < 0)
                X = X.Add(P);
            if (X.Equals(ZERO))
                return ZERO;
            if (P.Equals(TWO))
                return X;

            // p = 3 mod 4
            if (P.TestBit(0) && P.TestBit(1))
            {
                if (Jacobi(X, P) == 1)
                {
                    // a quadr. residue mod p
                    v = P.Add(ONE); // v = p+1
                    v = v.ShiftRight(2); // v = v/4
                    return X.ModPow(v, P); // return a^v mod p
                }
                throw new ArgumentException("No quadratic residue: " + X + ", " + P);
            }

            long t = 0;

            // initialization
            // compute k and s, where p = 2^s (2k+1) +1

            BigInteger k = P.Subtract(ONE); // k = p-1
            long s = 0;
            while (!k.TestBit(0))
            { // while k is even
                s++; // s = s+1
                k = k.ShiftRight(1); // k = k/2
            }

            k = k.Subtract(ONE); // k = k - 1
            k = k.ShiftRight(1); // k = k/2

            // initial values
            BigInteger r = X.ModPow(k, P); // r = a^k mod p

            BigInteger n = r.Multiply(r).Remainder(P); // n = r^2 % p
            n = n.Multiply(X).Remainder(P); // n = n * a % p
            r = r.Multiply(X).Remainder(P); // r = r * a %p

            if (n.Equals(ONE))
            {
                return r;
            }

            // non-quadratic residue
            BigInteger z = TWO; // z = 2
            while (Jacobi(z, P) == 1)
            {
                // while z quadratic residue
                z = z.Add(ONE); // z = z + 1
            }

            v = k;
            v = v.Multiply(TWO); // v = 2k
            v = v.Add(ONE); // v = 2k + 1
            BigInteger c = z.ModPow(v, P); // c = z^v mod p

            // iteration
            while (n.CompareTo(ONE) == 1)
            { // n > 1
                k = n; // k = n
                t = s; // t = s
                s = 0;

                while (!k.Equals(ONE))
                { // k != 1
                    k = k.Multiply(k).Mod(P); // k = k^2 % p
                    s++; // s = s + 1
                }

                t -= s; // t = t - s
                if (t == 0)
                {
                    throw new ArgumentException("No quadratic residue: " + X + ", " + P);
                }

                v = ONE;
                for (long i = 0; i < t - 1; i++)
                {
                    v = v.ShiftLeft(1); // v = 1 * 2^(t - 1)
                }
                c = c.ModPow(v, P); // c = c^v mod p
                r = r.Multiply(c).Remainder(P); // r = r * c % p
                c = c.Multiply(c).Remainder(P); // c = c^2 % p
//.........这里部分代码省略.........
开发者ID:Steppenwolfe65,项目名称:Rainbow-NET,代码行数:101,代码来源:BigMath.cs

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

示例6: ModAdd

 protected virtual BigInteger ModAdd(BigInteger x1, BigInteger x2)
 {
     BigInteger x3 = x1.Add(x2);
     if (x3.CompareTo(q) >= 0)
     {
         x3 = x3.Subtract(q);
     }
     return x3;
 }
开发者ID:ubberkid,项目名称:PeerATT,代码行数:9,代码来源:ECFieldElement.cs

示例7: ModInverseLorencz

        /**
         *
         * Based on "New Algorithm for Classical Modular Inverse" Róbert Lórencz.
         * LNCS 2523 (2002)
         *
         * @return a^(-1) mod m
         */
        private static BigInteger ModInverseLorencz(BigInteger a, BigInteger modulo)
        {
            // PRE: a is coprime with modulo, a < modulo

            int max = System.Math.Max(a.numberLength, modulo.numberLength);
            int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation
            int[] vDigits = new int[max + 1];
            Array.Copy(modulo.Digits, 0, uDigits, 0, modulo.numberLength);
            Array.Copy(a.Digits, 0, vDigits, 0, a.numberLength);
            BigInteger u = new BigInteger(modulo.Sign,
                modulo.numberLength,
                uDigits);
            BigInteger v = new BigInteger(a.Sign, a.numberLength, vDigits);

            BigInteger r = new BigInteger(0, 1, new int[max + 1]); // BigInteger.ZERO;
            BigInteger s = new BigInteger(1, 1, new int[max + 1]);
            s.Digits[0] = 1;
            // r == 0 && s == 1, but with enough place

            int coefU = 0, coefV = 0;
            int n = modulo.BitLength;
            int k;
            while (!IsPowerOfTwo(u, coefU) && !IsPowerOfTwo(v, coefV)) {
                // modification of original algorithm: I calculate how many times the algorithm will enter in the same branch of if
                k = HowManyIterations(u, n);

                if (k != 0) {
                    BitLevel.InplaceShiftLeft(u, k);
                    if (coefU >= coefV)
                        BitLevel.InplaceShiftLeft(r, k);
                    else {
                        BitLevel.InplaceShiftRight(s, System.Math.Min(coefV - coefU, k));
                        if (k - (coefV - coefU) > 0)
                            BitLevel.InplaceShiftLeft(r, k - coefV + coefU);
                    }
                    coefU += k;
                }

                k = HowManyIterations(v, n);
                if (k != 0) {
                    BitLevel.InplaceShiftLeft(v, k);
                    if (coefV >= coefU)
                        BitLevel.InplaceShiftLeft(s, k);
                    else {
                        BitLevel.InplaceShiftRight(r, System.Math.Min(coefU - coefV, k));
                        if (k - (coefU - coefV) > 0)
                            BitLevel.InplaceShiftLeft(s, k - coefU + coefV);
                    }
                    coefV += k;
                }

                if (u.Sign == v.Sign) {
                    if (coefU <= coefV) {
                        Elementary.completeInPlaceSubtract(u, v);
                        Elementary.completeInPlaceSubtract(r, s);
                    } else {
                        Elementary.completeInPlaceSubtract(v, u);
                        Elementary.completeInPlaceSubtract(s, r);
                    }
                } else {
                    if (coefU <= coefV) {
                        Elementary.completeInPlaceAdd(u, v);
                        Elementary.completeInPlaceAdd(r, s);
                    } else {
                        Elementary.completeInPlaceAdd(v, u);
                        Elementary.completeInPlaceAdd(s, r);
                    }
                }
                if (v.Sign == 0 || u.Sign == 0) {
                    // math.19: BigInteger not invertible
                    throw new ArithmeticException(Messages.math19);
                }
            }

            if (IsPowerOfTwo(v, coefV)) {
                r = s;
                if (v.Sign != u.Sign)
                    u = u.Negate();
            }
            if (u.TestBit(n)) {
                if (r.Sign < 0)
                    r = r.Negate();
                else
                    r = modulo.Subtract(r);
            }
            if (r.Sign < 0)
                r = r.Add(modulo);

            return r;
        }
开发者ID:tupunco,项目名称:deveel-math,代码行数:97,代码来源:Division.cs

示例8: TestModPow

        public void TestModPow()
        {
            try
            {
                Two.ModPow(One, Zero);
                Assert.Fail("expected ArithmeticException");
            }
            catch (ArithmeticException)
            {
            }

            Assert.AreEqual(Zero, Zero.ModPow(Zero, One));
            Assert.AreEqual(One, Zero.ModPow(Zero, Two));
            Assert.AreEqual(Zero, Two.ModPow(One, One));
            Assert.AreEqual(One, Two.ModPow(Zero, Two));

            for (int i = 0; i < 10; ++i)
            {
                BigInteger m = BigInteger.ProbablePrime(10 + i*3, Rnd);
                var x = new BigInteger(m.BitLength - 1, Rnd);

                Assert.AreEqual(x, x.ModPow(m, m));
                if (x.Sign != 0)
                {
                    Assert.AreEqual(Zero, Zero.ModPow(x, m));
                    Assert.AreEqual(One, x.ModPow(m.Subtract(One), m));
                }

                var y = new BigInteger(m.BitLength - 1, Rnd);
                var n = new BigInteger(m.BitLength - 1, Rnd);
                BigInteger n3 = n.ModPow(Three, m);

                BigInteger resX = n.ModPow(x, m);
                BigInteger resY = n.ModPow(y, m);
                BigInteger res = resX.Multiply(resY).Mod(m);
                BigInteger res3 = res.ModPow(Three, m);

                Assert.AreEqual(res3, n3.ModPow(x.Add(y), m));

                BigInteger a = x.Add(One); // Make sure it's not zero
                BigInteger b = y.Add(One); // Make sure it's not zero

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

示例9: Add_pos_first_longer_one_carry

        public void Add_pos_first_longer_one_carry()
        {
            BigInteger x = new BigInteger(1, new uint[] { 0x12345678, 0x12345678, 0xFFFFFFFF, 0x22222222 });
            BigInteger y = new BigInteger(1, new uint[] {   0x11111111, 0x11111111 });
            BigInteger z = x.Add(y);

            Expect(SameValue(z, 1, new uint[] { 0x12345678, 0x12345679, 0x11111110, 0x33333333 }));
        }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:8,代码来源:BigIntegerTests.cs

示例10: Add_pos_first_longer_carry_extend

        public void Add_pos_first_longer_carry_extend()
        {
            BigInteger x = new BigInteger(1, new uint[] { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x22222222 });
            BigInteger y = new BigInteger(1, new uint[] { 0x11111111, 0x11111111 });
            BigInteger z = x.Add(y);

            Expect(SameValue(z, 1, new uint[] { 0x00000001, 0x00000000, 0x00000000, 0x11111110, 0x33333333 }));
        }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:8,代码来源:BigIntegerTests.cs

示例11: Divide

        /// <summary>
        /// Divides each coefficient by a <c>BigInteger</c> and rounds the result to the nearest whole number.
        /// <para>Does not return a new polynomial but modifies this polynomial.</para>
        /// </summary>
        /// 
        /// <param name="Divisor">The divisor</param>
        public void Divide(BigInteger Divisor)
        {
            BigInteger d = Divisor.Add(BigInteger.One.ShiftRight(1));   // d = ceil(divisor/2)

            for (int i = 0; i < Coeffs.Length; i++)
            {
                Coeffs[i] = Coeffs[i].Signum() > 0 ? Coeffs[i].Add(d) : Coeffs[i].Add(d.Negate());
                Coeffs[i] = Coeffs[i].Divide(Divisor);
            }
        }
开发者ID:modulexcite,项目名称:CEX,代码行数:16,代码来源:BigIntPolynomial.cs

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

示例13: TestModPow

        public void TestModPow()
        {
            try
            {
                two.ModPow(one, zero);
                Assert.Fail("expected ArithmeticException");
            }
            catch (ArithmeticException) {}

            Assert.AreEqual(zero, zero.ModPow(zero, one));
            Assert.AreEqual(one, zero.ModPow(zero, two));
            Assert.AreEqual(zero, two.ModPow(one, one));
            Assert.AreEqual(one, two.ModPow(zero, two));

            for (int i = 0; i < 10; ++i)
            {
                IBigInteger m = BigInteger.ProbablePrime(10 + i * 3, _random);
                IBigInteger x = new BigInteger(m.BitLength - 1, _random);

                Assert.AreEqual(x, x.ModPow(m, m));
                if (x.SignValue != 0)
                {
                    Assert.AreEqual(zero, zero.ModPow(x, m));
                    Assert.AreEqual(one, x.ModPow(m.Subtract(one), m));
                }

                IBigInteger y = new BigInteger(m.BitLength - 1, _random);
                IBigInteger n = new BigInteger(m.BitLength - 1, _random);
                IBigInteger n3 = n.ModPow(three, m);

                IBigInteger resX = n.ModPow(x, m);
                IBigInteger resY = n.ModPow(y, m);
                IBigInteger res = resX.Multiply(resY).Mod(m);
                IBigInteger res3 = res.ModPow(three, m);

                Assert.AreEqual(res3, n3.ModPow(x.Add(y), m));

                IBigInteger a = x.Add(one); // Make sure it's not zero
                IBigInteger b = y.Add(one); // Make sure it's not zero

                Assert.AreEqual(a.ModPow(b, m).ModInverse(m), a.ModPow(b.Negate(), m));
            }
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:43,代码来源:BigIntegerTest.cs

示例14: Calculate

 public override Number Calculate(BigInteger bigint1, BigInteger bigint2)
 {
     if (bigint1 == null || bigint2 == null)
     {
         return 0;
     }
     return bigint1.Add(bigint2);
 }
开发者ID:tupunco,项目名称:Tup.Cobar4Net,代码行数:8,代码来源:ArithmeticAddExpression.cs

示例15: ModInverseLorencz

        private static BigInteger ModInverseLorencz(BigInteger X, BigInteger Modulo)
        {
            // Based on "New Algorithm for Classical Modular Inverse" Róbert Lórencz. LNCS 2523 (2002)
            // PRE: a is coprime with modulo, a < modulo
            int max = System.Math.Max(X._numberLength, Modulo._numberLength);
            int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation
            int[] vDigits = new int[max + 1];
            Array.Copy(Modulo._digits, 0, uDigits, 0, Modulo._numberLength);
            Array.Copy(X._digits, 0, vDigits, 0, X._numberLength);

            BigInteger u = new BigInteger(Modulo._sign, Modulo._numberLength, uDigits);
            BigInteger v = new BigInteger(X._sign, X._numberLength, vDigits);
            BigInteger r = new BigInteger(0, 1, new int[max + 1]); // BigInteger.ZERO;
            BigInteger s = new BigInteger(1, 1, new int[max + 1]);
            s._digits[0] = 1;
            // r == 0 && s == 1, but with enough place

            int coefU = 0, coefV = 0;
            int n = Modulo.BitLength;
            int k;

            while (!IsPowerOfTwo(u, coefU) && !IsPowerOfTwo(v, coefV))
            {
                // modification of original algorithm: I calculate how many times the algorithm will enter in the same branch of if
                k = HowManyIterations(u, n);
                if (k != 0)
                {
                    BitLevel.InplaceShiftLeft(u, k);
                    if (coefU >= coefV)
                    {
                        BitLevel.InplaceShiftLeft(r, k);
                    }
                    else
                    {
                        BitLevel.InplaceShiftRight(s, System.Math.Min(coefV - coefU, k));

                        if (k - (coefV - coefU) > 0)
                            BitLevel.InplaceShiftLeft(r, k - coefV + coefU);
                    }
                    coefU += k;
                }

                k = HowManyIterations(v, n);
                if (k != 0)
                {
                    BitLevel.InplaceShiftLeft(v, k);
                    if (coefV >= coefU)
                    {
                        BitLevel.InplaceShiftLeft(s, k);
                    }
                    else
                    {
                        BitLevel.InplaceShiftRight(r, System.Math.Min(coefU - coefV, k));

                        if (k - (coefU - coefV) > 0)
                            BitLevel.InplaceShiftLeft(s, k - coefU + coefV);
                    }
                    coefV += k;

                }

                if (u.Signum() == v.Signum())
                {
                    if (coefU <= coefV)
                    {
                        Elementary.CompleteInPlaceSubtract(u, v);
                        Elementary.CompleteInPlaceSubtract(r, s);
                    }
                    else
                    {
                        Elementary.CompleteInPlaceSubtract(v, u);
                        Elementary.CompleteInPlaceSubtract(s, r);
                    }
                }
                else
                {
                    if (coefU <= coefV)
                    {
                        Elementary.CompleteInPlaceAdd(u, v);
                        Elementary.CompleteInPlaceAdd(r, s);
                    }
                    else
                    {
                        Elementary.CompleteInPlaceAdd(v, u);
                        Elementary.CompleteInPlaceAdd(s, r);
                    }
                }

                if (v.Signum() == 0 || u.Signum() == 0)
                    throw new ArithmeticException("BigInteger not invertible");
            }

            if (IsPowerOfTwo(v, coefV))
            {
                r = s;
                if (v.Signum() != u.Signum())
                    u = u.Negate();
            }
            if (u.TestBit(n))
            {
//.........这里部分代码省略.........
开发者ID:DeadlyEmbrace,项目名称:NTRU-NET,代码行数:101,代码来源:Division.cs


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