internal static Digits NewPrime(int pBitN, Random generator) { if (pBitN < Digit.BitN) { throw new ArgumentException(); } int pDigitN = (pBitN + (Digit.BitN - 1)) / Digit.BitN; Digits p = new Digits(pDigitN); while (true) { for ( int iteration = 0; iteration < _IterationsAllowed; iteration++ ) { Digits.Random(p, pDigitN, generator); p[pDigitN - 1] >>= Digit.BitN * pDigitN - pBitN; p[0] |= 1; Digits.SetBit(p, pBitN - 1, 1); Digits.SetBit(p, pBitN - 2, 1); if (DivisibleBySomeLowPrime(p, pDigitN)) { continue; } Modulus modulus = new Modulus(p, pDigitN, true); Digits minus1 = new Digits(pDigitN); Modular .Neg(modulus._one, minus1, modulus._mod, modulus._digitN); Digits exp = new Digits(pDigitN); Digits.Shift(p, -1, exp, pDigitN); Digits @base = new Digits(pDigitN); for (int i = 1; i <= _RabinTestN; i++) { if (i == 1) { Modular.Add(modulus._one , modulus._one , @base , modulus._mod , modulus._digitN); } else { Modular.NonZeroRandom( p, @base, pDigitN, generator); } Digits result = new Digits(pDigitN); modulus._Exp(@base, exp, pDigitN, result); if (Digits.Compare(result, minus1, pDigitN) == 0) { return(p); } if ( Digits.Compare(result, modulus._one, pDigitN) != 0 ) { break; } } } Debug.Assert(false, "too many iterations"); } }
public Key(int bitN, Random generator) { _modBitN = bitN; // The public exponent is 2^16 + 1 _eBitN = 17; _eBytes = new byte[] { 1, 0, 1 }; int p1BitN = (bitN + 1) / 2 , p1ByteN = (p1BitN + 7) / 8 , p1DigitN = (p1BitN + (Digit.BitN - 1)) / Digit.BitN , p2BitN = bitN / 2 , p2ByteN = (p2BitN + 7) / 8 , p2DigitN = (p2BitN + (Digit.BitN - 1)) / Digit.BitN , longerDigitN = p1DigitN > p2DigitN ? p1DigitN : p2DigitN; Digits d1 = new Digits(p1DigitN), d2 = new Digits(p2DigitN); _modDigits = new Digits(_modDigitN); _eDigits = new Digits(1); Digits gcd = new Digits(longerDigitN) , temp = new Digits(longerDigitN); Digits.BytesToDigits(_eBytes, 0, _eDigits, _eBitN); int[] pBitN = new int[] { p1BitN, p2BitN }; int nPrimeFound = 0; Digits p1 = null, p2 = null; while (nPrimeFound != 2) { int pNowBitN = pBitN[nPrimeFound] , pNowDigitN = (pNowBitN + (Digit.BitN - 1)) / Digit.BitN; Digits pNow = Prime.NewPrime(pNowBitN, generator); if (nPrimeFound == 0) { p1 = pNow; } else { p2 = pNow; } Digits.Sub(pNow, 1, temp, pNowDigitN); int lgcd; Digits.ExtendedGcd(_eDigits , _eDigitN , temp , pNowDigitN , nPrimeFound == 0 ? d1 : d2 , null , gcd , out lgcd); if (Digits.Compare(gcd, 1, lgcd) != 0) { Debug.Assert(false, "untested code"); continue; } if ( nPrimeFound == 1 && Digits.Compare(p1, p1DigitN, p2, p2DigitN) == 0 ) { Debug.Assert(false, "untested code"); continue; } nPrimeFound++; } Digits.Mul(p1, p1DigitN, p2, p2DigitN, _modDigits); int modBitN = Digits.SigBitN(_modDigits, _modDigitN); Debug.Assert(modBitN == p1BitN + p2BitN && modBitN == _modBitN , "internal error"); _primeBitN = new int[2] { p1BitN, p2BitN }; _modBytes = new byte[_modByteN]; _primeBytes = new byte[2][] { new byte[p1ByteN], new byte[p2ByteN] }; _dBytes = new byte[2][] { new byte[p1ByteN], new byte[p2ByteN] }; Digits.DigitsToBytes(_modDigits, _modBytes, 0, _modBitN); for (int ip = 0; ip != 2; ip++) { Digits.DigitsToBytes( ip == 0 ? p1 : p2, _primeBytes[ip], 0, pBitN[ip]); Digits.DigitsToBytes( ip == 0 ? d1 : d2, _dBytes[ip], 0, pBitN[ip]); } int moduliCreated = 0; Digits.BytesToDigits(_eBytes, 0, _eDigits, _eBitN); _modulus = new Modulus(_modDigits, _modDigitN, true); _privateModulus = new Modulus[2]; _dDigits = new Digits[2]; _chineseDigits = new Digits[2]; for (int ip = 0; ip != 2; ip++) { Digits temp2 = new Digits(_modDigitN); _dDigits[ip] = new Digits(p1DigitN); _chineseDigits[ip] = new Digits(p1DigitN); Digits.BytesToDigits( _primeBytes[ip], 0, temp2, _primeBitN[ip]); _privateModulus[ip] = new Modulus(temp2 , (_primeBitN[ip] + (Digit.BitN - 1)) / Digit.BitN , true); moduliCreated++; Digits.BytesToDigits( _dBytes[ip], 0, _dDigits[ip], _primeBitN[ip]); } int lgcd2 = 0; Digits gcd2 = new Digits(_modDigitN); Digits.ExtendedGcd(_privateModulus[0]._mod , p1DigitN , _privateModulus[1]._mod , p2DigitN , _chineseDigits[1] , _chineseDigits[0] , gcd2 , out lgcd2); if (Digits.Compare(gcd2, 1, lgcd2) != 0) { throw new ArgumentException(); } }