示例#1
0
        public void TestSTRandomPrime()
        {
            IDigest[] digests = new IDigest[] { new Sha1Digest(), new Sha256Digest() };
            for (int digestIndex = 0; digestIndex < digests.Length; ++digestIndex)
            {
                int coincidenceCount = 0;

                IDigest digest = digests[digestIndex];
                for (int iterations = 0; iterations < ITERATIONS; ++iterations)
                {
                    try
                    {
                        byte[] inputSeed = new byte[16];
                        R.NextBytes(inputSeed);

                        Primes.STOutput st = Primes.GenerateSTRandomPrime(digest, PRIME_BITS, inputSeed);
                        Assert.True(IsPrime(st.Prime));

                        Primes.STOutput st2 = Primes.GenerateSTRandomPrime(digest, PRIME_BITS, inputSeed);
                        Assert.AreEqual(st.Prime, st2.Prime);
                        Assert.AreEqual(st.PrimeGenCounter, st2.PrimeGenCounter);
                        Assert.True(Arrays.AreEqual(st.PrimeSeed, st2.PrimeSeed));

                        for (int i = 0; i < inputSeed.Length; ++i)
                        {
                            inputSeed[i] ^= 0xFF;
                        }

                        Primes.STOutput st3 = Primes.GenerateSTRandomPrime(digest, PRIME_BITS, inputSeed);
                        Assert.True(!st.Prime.Equals(st3.Prime));
                        Assert.False(Arrays.AreEqual(st.PrimeSeed, st3.PrimeSeed));

                        if (st.PrimeGenCounter == st3.PrimeGenCounter)
                        {
                            ++coincidenceCount;
                        }
                    }
                    catch (InvalidOperationException e)
                    {
                        if (e.Message.StartsWith("Too many iterations"))
                        {
                            --iterations;
                            continue;
                        }

                        throw e;
                    }
                }

                Assert.True(coincidenceCount * coincidenceCount < ITERATIONS);
            }
        }