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


C# BigInteger.ModPow方法代码示例

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


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

示例1: TestModPow

        public void TestModPow()
        {
            var value = new BigInteger(50);
            var result = value.ModPow(new BigInteger(2), new BigInteger(3));

            Assert.AreEqual(new BigInteger(1), result);
        }
开发者ID:hanson-huang,项目名称:Encore,代码行数:7,代码来源:BigIntegerTest.cs

示例2: DH

        public DHOutParams DH(byte[] b, byte[] g, byte[] ga, byte[] p)
        {
            var bi = new BigInteger(1, b);
            var gi = new BigInteger(1, g);
            var gai = new BigInteger(1, ga);
            var pi = new BigInteger(1, p);

            byte[] gb = gi.ModPow(bi, pi).ToByteArray();
            byte[] s = gai.ModPow(bi, pi).ToByteArray();

            return new DHOutParams(gb, s);
        }
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:12,代码来源:EncryptionServices.cs

示例3: Calculate

        public static byte[] Calculate(BigInteger.BigInteger a, BigInteger.BigInteger b)
        {
            byte[] bytes;
            lock (Locker)
                bytes = a.ModPow(b, Prime).GetBytes();

            if (bytes.Length < 96)
            {
                var oldBytes = bytes;
                bytes = new byte[96];
                Array.Copy(oldBytes, 0, bytes, 96 - oldBytes.Length, oldBytes.Length);
                for (var i = 0; i < (96 - oldBytes.Length); i++)
                    bytes[i] = 0;
            }

            return bytes;
        }
开发者ID:haroldma,项目名称:Universal.Torrent,代码行数:17,代码来源:ModuloCalculator.cs

示例4: CalculateGenerator_FIPS186_3_Verifiable

		private static BigInteger CalculateGenerator_FIPS186_3_Verifiable(IDigest d, BigInteger p, BigInteger q,
			byte[] seed, int index)
		{
			// A.2.3 Verifiable Canonical Generation of the Generator g
			BigInteger e = p.Subtract(BigInteger.One).Divide(q);
			byte[] ggen = Hex.Decode("6767656E");

			// 7. U = domain_parameter_seed || "ggen" || index || count.
			byte[] U = new byte[seed.Length + ggen.Length + 1 + 2];
			Array.Copy(seed, 0, U, 0, seed.Length);
			Array.Copy(ggen, 0, U, seed.Length, ggen.Length);
			U[U.Length - 3] = (byte)index; 

			byte[] w = new byte[d.GetDigestSize()];
			for (int count = 1; count < (1 << 16); ++count)
			{
				Inc(U);
				Hash(d, U, w);
				BigInteger W = new BigInteger(1, w);
				BigInteger g = W.ModPow(e, p);

				if (g.CompareTo(BigInteger.Two) >= 0)
					return g;
			}

			return null;
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:27,代码来源:DsaParametersGenerator.cs

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

示例6: RSATest2

    //***********************************************************************
    // Tests the correct implementation of the modulo exponential and
    // inverse modulo functions using RSA encryption and decryption.  The two
    // pseudoprimes p and q are fixed, but the two RSA keys are generated
    // for each round of testing.
    //***********************************************************************

    public static void RSATest2(int rounds)
    {
        Random rand = new Random();
        byte[] val = new byte[64];

        byte[] pseudoPrime1 = {
                        (byte)0x85, (byte)0x84, (byte)0x64, (byte)0xFD, (byte)0x70, (byte)0x6A,
                        (byte)0x9F, (byte)0xF0, (byte)0x94, (byte)0x0C, (byte)0x3E, (byte)0x2C,
                        (byte)0x74, (byte)0x34, (byte)0x05, (byte)0xC9, (byte)0x55, (byte)0xB3,
                        (byte)0x85, (byte)0x32, (byte)0x98, (byte)0x71, (byte)0xF9, (byte)0x41,
                        (byte)0x21, (byte)0x5F, (byte)0x02, (byte)0x9E, (byte)0xEA, (byte)0x56,
                        (byte)0x8D, (byte)0x8C, (byte)0x44, (byte)0xCC, (byte)0xEE, (byte)0xEE,
                        (byte)0x3D, (byte)0x2C, (byte)0x9D, (byte)0x2C, (byte)0x12, (byte)0x41,
                        (byte)0x1E, (byte)0xF1, (byte)0xC5, (byte)0x32, (byte)0xC3, (byte)0xAA,
                        (byte)0x31, (byte)0x4A, (byte)0x52, (byte)0xD8, (byte)0xE8, (byte)0xAF,
                        (byte)0x42, (byte)0xF4, (byte)0x72, (byte)0xA1, (byte)0x2A, (byte)0x0D,
                        (byte)0x97, (byte)0xB1, (byte)0x31, (byte)0xB3,
                };

        byte[] pseudoPrime2 = {
                        (byte)0x99, (byte)0x98, (byte)0xCA, (byte)0xB8, (byte)0x5E, (byte)0xD7,
                        (byte)0xE5, (byte)0xDC, (byte)0x28, (byte)0x5C, (byte)0x6F, (byte)0x0E,
                        (byte)0x15, (byte)0x09, (byte)0x59, (byte)0x6E, (byte)0x84, (byte)0xF3,
                        (byte)0x81, (byte)0xCD, (byte)0xDE, (byte)0x42, (byte)0xDC, (byte)0x93,
                        (byte)0xC2, (byte)0x7A, (byte)0x62, (byte)0xAC, (byte)0x6C, (byte)0xAF,
                        (byte)0xDE, (byte)0x74, (byte)0xE3, (byte)0xCB, (byte)0x60, (byte)0x20,
                        (byte)0x38, (byte)0x9C, (byte)0x21, (byte)0xC3, (byte)0xDC, (byte)0xC8,
                        (byte)0xA2, (byte)0x4D, (byte)0xC6, (byte)0x2A, (byte)0x35, (byte)0x7F,
                        (byte)0xF3, (byte)0xA9, (byte)0xE8, (byte)0x1D, (byte)0x7B, (byte)0x2C,
                        (byte)0x78, (byte)0xFA, (byte)0xB8, (byte)0x02, (byte)0x55, (byte)0x80,
                        (byte)0x9B, (byte)0xC2, (byte)0xA5, (byte)0xCB,
                };


        BigInteger bi_p = new BigInteger(pseudoPrime1);
        BigInteger bi_q = new BigInteger(pseudoPrime2);
        BigInteger bi_pq = (bi_p - 1) * (bi_q - 1);
        BigInteger bi_n = bi_p * bi_q;

        for (int count = 0; count < rounds; count++)
        {
            // generate private and public key
            BigInteger bi_e = bi_pq.genCoPrime(512, rand);
            BigInteger bi_d = bi_e.ModInverse(bi_pq);

            Console.WriteLine("\ne =\n" + bi_e.ToString(10));
            Console.WriteLine("\nd =\n" + bi_d.ToString(10));
            Console.WriteLine("\nn =\n" + bi_n.ToString(10) + "\n");

            // generate data of random length
            int t1 = 0;
            while (t1 == 0)
                t1 = (int)(rand.NextDouble() * 65);

            bool done = false;
            while (!done)
            {
                for (int i = 0; i < 64; i++)
                {
                    if (i < t1)
                        val[i] = (byte)(rand.NextDouble() * 256);
                    else
                        val[i] = 0;

                    if (val[i] != 0)
                        done = true;
                }
            }

            while (val[0] == 0)
                val[0] = (byte)(rand.NextDouble() * 256);

            Console.Write("Round = " + count);

            // encrypt and decrypt data
            BigInteger bi_data = new BigInteger(val, t1);
            BigInteger bi_encrypted = bi_data.ModPow(bi_e, bi_n);
            BigInteger bi_decrypted = bi_encrypted.ModPow(bi_d, bi_n);

            // compare
            if (bi_decrypted != bi_data)
            {
                Console.WriteLine("\nError at round " + count);
                Console.WriteLine(bi_data + "\n");
                return;
            }
            Console.WriteLine(" <PASSED>.");
        }

    }
开发者ID:hanwangkun,项目名称:npoi,代码行数:97,代码来源:BigInteger.cs

示例7: SolovayStrassenTest

    //***********************************************************************
    // Probabilistic prime test based on Solovay-Strassen (Euler Criterion)
    //
    // p is probably prime if for any a < p (a is not multiple of p),
    // a^((p-1)/2) mod p = J(a, p)
    //
    // where J is the Jacobi symbol.
    //
    // Otherwise, p is composite.
    //
    // Returns
    // -------
    // True if "this" is a Euler pseudoprime to randomly chosen
    // bases.  The number of chosen bases is given by the "confidence"
    // parameter.
    //
    // False if "this" is definitely NOT prime.
    //
    //***********************************************************************

    public bool SolovayStrassenTest(int confidence)
    {
        BigInteger thisVal;
        if ((this.data[maxLength - 1] & 0x80000000) != 0)        // negative
            thisVal = -this;
        else
            thisVal = this;

        if (thisVal.dataLength == 1)
        {
            // test small numbers
            if (thisVal.data[0] == 0 || thisVal.data[0] == 1)
                return false;
            else if (thisVal.data[0] == 2 || thisVal.data[0] == 3)
                return true;
        }

        if ((thisVal.data[0] & 0x1) == 0)     // even numbers
            return false;


        int bits = thisVal.bitCount();
        BigInteger a = new BigInteger();
        BigInteger p_sub1 = thisVal - 1;
        BigInteger p_sub1_shift = p_sub1 >> 1;

        Random rand = new Random();

        for (int round = 0; round < confidence; round++)
        {
            bool done = false;

            while (!done)		// generate a < n
            {
                int testBits = 0;

                // make sure "a" has at least 2 bits
                while (testBits < 2)
                    testBits = (int)(rand.NextDouble() * bits);

                a.genRandomBits(testBits, rand);

                int byteLen = a.dataLength;

                // make sure "a" is not 0
                if (byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
                    done = true;
            }

            // check whether a factor exists (fix for version 1.03)
            BigInteger gcdTest = a.gcd(thisVal);
            if (gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                return false;

            // calculate a^((p-1)/2) mod p

            BigInteger expResult = a.ModPow(p_sub1_shift, thisVal);
            if (expResult == p_sub1)
                expResult = -1;

            // calculate Jacobi symbol
            BigInteger jacob = Jacobi(a, thisVal);

            //Console.WriteLine("a = " + a.ToString(10) + " b = " + thisVal.ToString(10));
            //Console.WriteLine("expResult = " + expResult.ToString(10) + " Jacob = " + jacob.ToString(10));

            // if they are different then it is not prime
            if (expResult != jacob)
                return false;
        }

        return true;
    }
开发者ID:hanwangkun,项目名称:npoi,代码行数:93,代码来源:BigInteger.cs

示例8: FermatLittleTest

    //***********************************************************************
    // Probabilistic prime test based on Fermat's little theorem
    //
    // for any a < p (p does not divide a) if
    //      a^(p-1) mod p != 1 then p is not prime.
    //
    // Otherwise, p is probably prime (pseudoprime to the chosen base).
    //
    // Returns
    // -------
    // True if "this" is a pseudoprime to randomly chosen
    // bases.  The number of chosen bases is given by the "confidence"
    // parameter.
    //
    // False if "this" is definitely NOT prime.
    //
    // Note - this method is fast but fails for Carmichael numbers except
    // when the randomly chosen base is a factor of the number.
    //
    //***********************************************************************

    public bool FermatLittleTest(int confidence)
    {
        BigInteger thisVal;
        if ((this.data[maxLength - 1] & 0x80000000) != 0)        // negative
            thisVal = -this;
        else
            thisVal = this;

        if (thisVal.dataLength == 1)
        {
            // test small numbers
            if (thisVal.data[0] == 0 || thisVal.data[0] == 1)
                return false;
            else if (thisVal.data[0] == 2 || thisVal.data[0] == 3)
                return true;
        }

        if ((thisVal.data[0] & 0x1) == 0)     // even numbers
            return false;

        int bits = thisVal.bitCount();
        BigInteger a = new BigInteger();
        BigInteger p_sub1 = thisVal - (new BigInteger(1));
        Random rand = new Random();

        for (int round = 0; round < confidence; round++)
        {
            bool done = false;

            while (!done)		// generate a < n
            {
                int testBits = 0;

                // make sure "a" has at least 2 bits
                while (testBits < 2)
                    testBits = (int)(rand.NextDouble() * bits);

                a.genRandomBits(testBits, rand);

                int byteLen = a.dataLength;

                // make sure "a" is not 0
                if (byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
                    done = true;
            }

            // check whether a factor exists (fix for version 1.03)
            BigInteger gcdTest = a.gcd(thisVal);
            if (gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                return false;

            // calculate a^(p-1) mod p
            BigInteger expResult = a.ModPow(p_sub1, thisVal);

            int resultLen = expResult.dataLength;

            // is NOT prime is a^(p-1) mod p != 1

            if (resultLen > 1 || (resultLen == 1 && expResult.data[0] != 1))
            {
                //Console.WriteLine("a = " + a.ToString());
                return false;
            }
        }

        return true;
    }
开发者ID:hanwangkun,项目名称:npoi,代码行数:88,代码来源:BigInteger.cs

示例9: ModPow_on_power_of_two_exponent_works

 public void ModPow_on_power_of_two_exponent_works()
 {
     BigInteger i = new BigInteger(1, 0x2);
     BigInteger m = new BigInteger(1, 0x7);
     BigInteger e = BigInteger.Create(256);
     BigInteger p = i.ModPow(e, m);
     BigInteger a = new BigInteger(1, 0x2);
     Expect(p == a);
 }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:9,代码来源:BigIntegerTests.cs

示例10: ModPow_on_completely_odd_exponent_works

 public void ModPow_on_completely_odd_exponent_works()
 {
     BigInteger i = new BigInteger(1, 0x2);
     BigInteger m = new BigInteger(1, 0x7);
     BigInteger e = BigInteger.Create(255);
     BigInteger p = i.ModPow(e, m);
     BigInteger a = new BigInteger(1, 0x1);
     Expect(p == a);
 }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:9,代码来源:BigIntegerTests.cs

示例11: CalculatePublicKey

		private static BigInteger CalculatePublicKey(BigInteger p, BigInteger g, BigInteger x)
		{
			return g.ModPow(x, p);
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:4,代码来源:DsaKeyPairGenerator.cs

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

示例13: VerifySignature

		/**
		 * return true if the value r and s represent a Gost3410 signature for
		 * the passed in message for standard Gost3410 the message should be a
		 * Gost3411 hash of the real message to be verified.
		 */
		public bool VerifySignature(
			byte[]		message,
			BigInteger	r,
			BigInteger	s)
		{
			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;

			if (r.Sign < 0 || parameters.Q.CompareTo(r) <= 0)
			{
				return false;
			}

			if (s.Sign < 0 || parameters.Q.CompareTo(s) <= 0)
			{
				return false;
			}

			BigInteger v = m.ModPow(parameters.Q.Subtract(BigInteger.Two), parameters.Q);

			BigInteger z1 = s.Multiply(v).Mod(parameters.Q);
			BigInteger z2 = (parameters.Q.Subtract(r)).Multiply(v).Mod(parameters.Q);

			z1 = parameters.A.ModPow(z1, parameters.P);
			z2 = ((Gost3410PublicKeyParameters)key).Y.ModPow(z2, parameters.P);

			BigInteger u = z1.Multiply(z2).Mod(parameters.P).Mod(parameters.Q);

			return u.Equals(r);
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:41,代码来源:GOST3410Signer.cs

示例14: Should_mod_pow

        public void Should_mod_pow(int x, int e, int m, int expectedResult)
        {
            var bx = new BigInteger(x);
            var bm = new BigInteger(m);
            var be = new BigInteger(e);

            BigInteger result = bx.ModPow(be, bm);
            var actualResult = (int) result;
            actualResult.Should().Be(expectedResult);
        }
开发者ID:ChemicalRocketeer,项目名称:BigMath,代码行数:10,代码来源:BigIntegerFacts.cs

示例15: CalculateAgreement

		/**
		 * given a message from a given party and the corresponding public key
		 * calculate the next message in the agreement sequence. In this case
		 * this will represent the shared secret.
		 */
		public BigInteger CalculateAgreement(
			DHPublicKeyParameters	pub,
			BigInteger				message)
		{
			if (pub == null)
				throw new ArgumentNullException("pub");
			if (message == null)
				throw new ArgumentNullException("message");

			if (!pub.Parameters.Equals(dhParams))
			{
				throw new ArgumentException("Diffie-Hellman public key has wrong parameters.");
			}

			BigInteger p = dhParams.P;

			return message.ModPow(key.X, p).Multiply(pub.Y.ModPow(privateValue, p)).Mod(p);
		}
开发者ID:Xanagandr,项目名称:DisaOpenSource,代码行数:23,代码来源:DHAgreement.cs


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