private static BigInteger GenKey(GeneratorWrap gen, BigInteger modulus) { var key = gen.GetBig() % modulus; while (!KeyCheck(key, modulus)) { key = gen.GetBig() % modulus; } return(key); }
/// <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); }
//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); }
/// <summary> /// Start the protocol and return the init for Verifier.Step1. /// </summary> /// <returns>number to send to Verifier</returns> public BigInteger Step1() { _synch = true; _sessionNumber = _generator.GetBig() % _key.Modulus; var square = _sessionNumber * _sessionNumber; var squareMod = square % _key.Modulus; //avoid comunication of the key while (_sessionNumber < 3 && 1 != BigInteger.GreatestCommonDivisor(_sessionNumber, _key.Modulus) && 1 != BigInteger.GreatestCommonDivisor(squareMod, _key.Modulus) && square == squareMod) { _sessionNumber = _generator.GetBig() % _key.Modulus; square = _sessionNumber * _sessionNumber; squareMod = square % _key.Modulus; } return(squareMod); }
//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); }