Ejemplo n.º 1
0
 /// <summary>
 /// </summary>
 /// <param name="key">private key</param>
 /// <param name="gen">random number generator, it is not disposed.</param>
 internal Proover(PrivateKey key, RandomNumberGenerator gen)
 {
     _key = key;
     _generator = new GeneratorWrap(gen, key.Size);
     _synch = false;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Simple test.
        /// </summary>
        /// <param name="wordSize">number of byte of the key</param>
        /// <param name="testPrecision">percision of the test, error = 1/2^precision</param>
        /// <returns>result of the test</returns>
        public static bool DefaultTest(uint wordSize=128, uint testPrecision=20)
        {
            if (wordSize < 8 || testPrecision < 1)
            {
                System.Console.WriteLine("FiatShamirIdentification test invalid input\n");
                return false;
            }

            uint iteration = 0;
            bool choice;
            BigInteger number;
            bool result = true;
            var generator = new RNGCryptoServiceProvider();
            var priv = PrivateKey.NewKey(generator, wordSize);
            var pub = priv.GetPublicKey();
            Verifier verifier = pub.GetVerifier();
            Proover proover = priv.GetProover(generator);

            //test with key
            while (iteration < testPrecision && result)
            {
                number = proover.Step1();
                choice = verifier.Step1(ref number);
                number = proover.Step2(choice);
                verifier.Step2(number);
                result = verifier.CheckState();
                iteration++;
            }

            if (!result) //if not verified, fail
            {
                System.Console.WriteLine("FiatShamirIdentification test ERROR\n");
                generator.Dispose();
                return false;
            }


            //test without key
            var genwrap = new GeneratorWrap(generator, wordSize);
            var falseKey = new PrivateKey(genwrap.GetBig(), genwrap.GetBig(), wordSize);
            proover = new Proover(falseKey, generator);
            iteration = 0;
            while (iteration < testPrecision && result)
            {
                number = proover.Step1();
                choice = verifier.Step1(ref number);
                number = proover.Step2(choice);
                verifier.Step2(number);
                result = verifier.CheckState();
                iteration++;
            }

            if (result) //if verified, fail
            {
                System.Console.WriteLine("FiatShamirIdentification test ERROR\n");
                generator.Dispose();
                return false;
            }

            System.Console.WriteLine("FiatShamirIdentification test OK\n");
            generator.Dispose();
            return true;
        }
 //only for test
 internal static bool EqTest(PrivateKey first, PrivateKey second)
 {
     return first._key == second._key && first._modulus == second._modulus;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Customizable version with full NewKey parameters.
        /// </summary>
        /// <param name="wordSize">number of byte of the key</param>
        /// <param name="testPrecision">percision of the test, error = 1/2^precision</param>
        /// <param name="primePrecision">percision of primality test, error = 1/2^(2*precision)</param>
        /// <param name="generator">random number generator, it is not disposed</param>
        /// <param name="threads">number of threads to use</param>
        /// <returns>result of the test</returns>
        public static bool CustomTest(uint wordSize, uint testPrecision, uint primePrecision, RandomNumberGenerator generator, ulong primeDistance=uint.MaxValue, int threads = 1)
        {
            if (wordSize < 8 || testPrecision < 1)
            {
                System.Console.WriteLine("FiatShamirIdentification test invalid input\n");
                return false;
            }

            uint iteration = 0;
            bool choice;
            BigInteger number;
            bool result = true;
            var priv = PrivateKey.NewKey(generator, wordSize, threads, primePrecision);
            var pub = priv.GetPublicKey();
            Verifier verifier = pub.GetVerifier();
            Proover proover = priv.GetProover(generator);

            //test with key
            while (iteration < testPrecision && result)
            {
                number = proover.Step1();
                choice = verifier.Step1(ref number);
                number = proover.Step2(choice);
                verifier.Step2(number);
                result = verifier.CheckState();
                iteration++;
            }

            if (!result) //if not verified, fail
            {
                System.Console.WriteLine("FiatShamirIdentification test ERROR\n");
                return false;
            }


            //test without key
            var genwrap = new GeneratorWrap(generator, wordSize);
            var falseKey = new PrivateKey(genwrap.GetBig(), genwrap.GetBig(), wordSize);
            proover = new Proover(falseKey, generator);
            iteration = 0;
            while (iteration < testPrecision && result)
            {
                number = proover.Step1();
                choice = verifier.Step1(ref number);
                number = proover.Step2(choice);
                verifier.Step2(number);
                result = verifier.CheckState();
                iteration++;
            }

            if (result) //if verified, fail
            {
                System.Console.WriteLine("FiatShamirIdentification test ERROR\n");
                return false;
            }

            System.Console.WriteLine("FiatShamirIdentification test OK\n");
            return true;
        }