Beispiel #1
0
        private static BigInteger GenKey(GeneratorWrap gen, BigInteger modulus)
        {
            var key = gen.GetBig() % modulus;

            while (!KeyCheck(key, modulus))
            {
                key = gen.GetBig() % modulus;
            }
            return(key);
        }
Beispiel #2
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, int threads = 1)
        {
            if (wordSize < 128 || testPrecision < 1)
            {
                throw new ArgumentNullException("FiatShamirIdentification test invalid input\n");
            }

            uint       iteration = 0;
            bool       choice;
            BigInteger number;
            var        result   = true;
            var        priv     = PrivateKey.NewKey(generator, wordSize, threads, primePrecision);
            var        pub      = priv.GetPublicKey();
            var        verifier = pub.GetVerifier();
            var        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
            {
                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++;
            }

            return(!result);
        }
Beispiel #3
0
        //Generates the modulus using one thread
        private static BigInteger SeqGenMod(GeneratorWrap gen, uint wordSize, uint precision)
        {
            IPrime generator   = new SequentialPrime(gen.GetInt(), precision, wordSize);
            var    firstPrime  = gen.GetBig();
            var    secondPrime = gen.GetBig();

            while (!SecurityCheck(firstPrime, secondPrime))
            {
                secondPrime = gen.GetBig();
            }

            firstPrime  = generator.NextPrime(firstPrime);
            secondPrime = generator.NextPrime(secondPrime);

            return(firstPrime * secondPrime);
        }
Beispiel #4
0
        //Generates the modulus using more thread
        private static BigInteger ParGenMod(GeneratorWrap gen, uint wordSize, int threads, uint precision)
        {
            IPrime mainGenerator, workerGenerator;

            //threads distribution for primes creation
            if (threads < 4)
            {
                mainGenerator   = new SequentialPrime(gen.GetInt(), precision, wordSize);
                workerGenerator = new SequentialPrime(gen.GetInt(), precision, wordSize);
            }
            else
            {
                mainGenerator   = new ParallelPrime(gen.GetInt(), precision, wordSize, threads - threads / 2);
                workerGenerator = new ParallelPrime(gen.GetInt(), precision, wordSize, threads / 2);
            }

            //primes creation
            var firstPrime  = gen.GetBig();
            var secondPrime = gen.GetBig();

            while (!SecurityCheck(firstPrime, secondPrime))
            {
                secondPrime = gen.GetBig();
            }

            var worker = new Task <BigInteger>(() => workerGenerator.NextPrime(firstPrime));

            worker.Start();
            secondPrime = mainGenerator.NextPrime(secondPrime);

            worker.Wait();
            firstPrime = worker.Result;
            worker.Dispose();

            return(firstPrime * secondPrime);
        }
Beispiel #5
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;
 }