예제 #1
0
        internal void GreatestCommonDivisor(Integer X, Integer Y, Integer Gcd)
        {
            // This is the basic Euclidean Algorithm.
            if (X.IsZero())
            {
                throw(new Exception("Doing GCD with a parameter that is zero."));
            }

            if (Y.IsZero())
            {
                throw(new Exception("Doing GCD with a parameter that is zero."));
            }

            if (X.IsEqual(Y))
            {
                Gcd.Copy(X);
                return;
            }

            // Don't change the original numbers that came in as parameters.
            if (X.ParamIsGreater(Y))
            {
                GcdX.Copy(Y);
                GcdY.Copy(X);
            }
            else
            {
                GcdX.Copy(X);
                GcdY.Copy(Y);
            }

            while (true)
            {
                IntMath.Divider.Divide(GcdX, GcdY, Quotient, Remainder);
                if (Remainder.IsZero())
                {
                    Gcd.Copy(GcdY); // It's the smaller one.
                    // It can't return from this loop until the remainder is zero.
                    return;
                }

                GcdX.Copy(GcdY);
                GcdY.Copy(Remainder);
            }
        }
예제 #2
0
        internal string ToString10(Integer From)
        {
            if (From.IsULong())
            {
                ulong N = From.GetAsULong();
                if (From.IsNegative)
                {
                    return("-" + N.ToString("N0"));
                }
                else
                {
                    return(N.ToString("N0"));
                }
            }

            string Result = "";

            ToDivide.Copy(From);
            int CommaCount = 0;

            while (!ToDivide.IsZero())
            {
                uint Digit = (uint)Divider.ShortDivideRem(ToDivide, 10, Quotient);
                ToDivide.Copy(Quotient);
                if (((CommaCount % 3) == 0) && (CommaCount != 0))
                {
                    Result = Digit.ToString() + "," + Result; // Or use a StringBuilder.
                }
                else
                {
                    Result = Digit.ToString() + Result;
                }

                CommaCount++;
            }

            if (From.IsNegative)
            {
                return("-" + Result);
            }
            else
            {
                return(Result);
            }
        }
예제 #3
0
        internal bool FindMultiplicativeInverseSmall(Integer ToFind, Integer KnownNumber, Integer Modulus)
        {
            // This method is for: KnownNumber * ToFind = 1 mod Modulus
            // An example:
            // PublicKeyExponent * X = 1 mod PhiN.
            // PublicKeyExponent * X = 1 mod (P - 1)(Q - 1).
            // This means that
            // (PublicKeyExponent * X) = (Y * PhiN) + 1
            // X is less than PhiN.
            // So Y is less than PublicKExponent.
            // Y can't be zero.
            // If this equation can be solved then it can be solved modulo
            // any number.  So it has to be solvable mod PublicKExponent.
            // See: Hasse Principle.
            // This also depends on the idea that the KnownNumber is prime and
            // that there is one unique modular inverse.
            // if( !KnownNumber-is-a-prime )
            //    then it won't work.
            if (!KnownNumber.IsULong())
            {
                throw(new Exception("FindMultiplicativeInverseSmall() was called with too big of a KnownNumber."));
            }

            ulong KnownNumberULong = KnownNumber.GetAsULong();

            //                       65537
            if (KnownNumberULong > 1000000)
            {
                throw(new Exception("KnownNumberULong > 1000000. FindMultiplicativeInverseSmall() was called with too big of an exponent."));
            }

            // (Y * PhiN) + 1 mod PubKExponent has to be zero if Y is a solution.
            ulong ModulusModKnown = Divider.GetMod32(Modulus, KnownNumberULong);

            // Worker.ReportProgress( 0, "ModulusModExponent: " + ModulusModKnown.ToString( "N0" ));
            // if( Worker.CancellationPending )
            // return false;

            // Y can't be zero.
            // The exponent is a small number like 65537.
            for (uint Y = 1; Y < (uint)KnownNumberULong; Y++)
            {
                ulong X = (ulong)Y * ModulusModKnown;
                X++; // Add 1 to it for (Y * PhiN) + 1.
                X = X % KnownNumberULong;
                if (X == 0)
                {
                    // if( Worker.CancellationPending )
                    // return false;

                    // What is PhiN mod 65537?
                    // That gives me Y.
                    // The private key exponent is X*65537 + ModPart
                    // The CipherText raised to that is the PlainText.
                    // P + zN = C^(X*65537 + ModPart)
                    // P + zN = C^(X*65537)(C^ModPart)
                    // P + zN = ((C^65537)^X)(C^ModPart)
                    // Worker.ReportProgress( 0, "Found Y at: " + Y.ToString( "N0" ));
                    ToFind.Copy(Modulus);
                    Multiplier.MultiplyULong(ToFind, Y);
                    ToFind.AddULong(1);
                    Divider.Divide(ToFind, KnownNumber, Quotient, Remainder);
                    if (!Remainder.IsZero())
                    {
                        throw(new Exception("This can't happen. !Remainder.IsZero()"));
                    }

                    ToFind.Copy(Quotient);
                    // Worker.ReportProgress( 0, "ToFind: " + ToString10( ToFind ));
                    break;
                }
            }

            // if( Worker.CancellationPending )
            // return false;

            TestForModInverse1.Copy(ToFind);
            Multiplier.MultiplyULong(TestForModInverse1, KnownNumberULong);
            Divider.Divide(TestForModInverse1, Modulus, Quotient, Remainder);
            if (!Remainder.IsOne())
            {
                // The definition is that it's congruent to 1 mod the modulus,
                // so this has to be 1.
                // I've only seen this happen once.  Were the primes P and Q not
                // really primes?
                throw(new Exception("Remainder has to be 1: " + ToString10(Remainder)));
            }

            return(true);
        }
예제 #4
0
        internal void Add(Integer Result, Integer ToAdd)
        {
            if (ToAdd.IsZero())
            {
                return;
            }

            // The most common form.  They are both positive.
            if (!Result.IsNegative && !ToAdd.IsNegative)
            {
                Result.Add(ToAdd);
                return;
            }

            if (!Result.IsNegative && ToAdd.IsNegative)
            {
                TempAdd1.Copy(ToAdd);
                TempAdd1.IsNegative = false;
                if (TempAdd1.ParamIsGreater(Result))
                {
                    Subtract(Result, TempAdd1);
                    return;
                }
                else
                {
                    Subtract(TempAdd1, Result);
                    Result.Copy(TempAdd1);
                    Result.IsNegative = true;
                    return;
                }
            }

            if (Result.IsNegative && !ToAdd.IsNegative)
            {
                TempAdd1.Copy(Result);
                TempAdd1.IsNegative = false;
                TempAdd2.Copy(ToAdd);
                if (TempAdd1.ParamIsGreater(TempAdd2))
                {
                    Subtract(TempAdd2, TempAdd1);
                    Result.Copy(TempAdd2);
                    return;
                }
                else
                {
                    Subtract(TempAdd1, TempAdd2);
                    Result.Copy(TempAdd2);
                    Result.IsNegative = true;
                    return;
                }
            }

            if (Result.IsNegative && ToAdd.IsNegative)
            {
                TempAdd1.Copy(Result);
                TempAdd1.IsNegative = false;
                TempAdd2.Copy(ToAdd);
                TempAdd2.IsNegative = false;
                TempAdd1.Add(TempAdd2);
                Result.Copy(TempAdd1);
                Result.IsNegative = true;
                return;
            }
        }
예제 #5
0
        internal void MakeRSAKeys()
        {
            int ShowBits = (PrimeIndex + 1) * 32;

            // int TestLoops = 0;
            Worker.ReportProgress(0, "Making RSA keys.");
            Worker.ReportProgress(0, "Bits size is: " + ShowBits.ToString());
            // ulong Loops = 0;
            while (true)
            {
                if (Worker.CancellationPending)
                {
                    return;
                }

                Thread.Sleep(1); // Let other things run.
                // Make two prime factors.
                // Normally you'd only make new primes when you pay the Certificate
                // Authority for a new certificate.
                if (!MakeAPrime(PrimeP, PrimeIndex, 20))
                {
                    return;
                }

                if (Worker.CancellationPending)
                {
                    return;
                }

                if (!MakeAPrime(PrimeQ, PrimeIndex, 20))
                {
                    return;
                }

                if (Worker.CancellationPending)
                {
                    return;
                }

                // This is extremely unlikely.
                Integer Gcd = new Integer();
                IntMath.GreatestCommonDivisor(PrimeP, PrimeQ, Gcd);
                if (!Gcd.IsOne())
                {
                    Worker.ReportProgress(0, "They had a GCD: " + IntMath.ToString10(Gcd));
                    continue;
                }

                if (Worker.CancellationPending)
                {
                    return;
                }

                IntMath.GreatestCommonDivisor(PrimeP, PubKeyExponent, Gcd);
                if (!Gcd.IsOne())
                {
                    Worker.ReportProgress(0, "They had a GCD with PubKeyExponent: " + IntMath.ToString10(Gcd));
                    continue;
                }

                if (Worker.CancellationPending)
                {
                    return;
                }

                IntMath.GreatestCommonDivisor(PrimeQ, PubKeyExponent, Gcd);
                if (!Gcd.IsOne())
                {
                    Worker.ReportProgress(0, "2) They had a GCD with PubKeyExponent: " + IntMath.ToString10(Gcd));
                    continue;
                }

                // For Modular Reduction.  This only has to be done
                // once, when P and Q are made.
                IntMathForP.ModReduction.SetupGeneralBaseArray(PrimeP);
                IntMathForQ.ModReduction.SetupGeneralBaseArray(PrimeQ);
                PrimePMinus1.Copy(PrimeP);
                IntMath.SubtractULong(PrimePMinus1, 1);
                PrimeQMinus1.Copy(PrimeQ);
                IntMath.SubtractULong(PrimeQMinus1, 1);

                if (Worker.CancellationPending)
                {
                    return;
                }

                // These checks should be more thorough to
                // make sure the primes P and Q are numbers
                // that can be used in a secure way.

                Worker.ReportProgress(0, "The Index of Prime P is: " + PrimeP.GetIndex().ToString());
                Worker.ReportProgress(0, "Prime P:");
                Worker.ReportProgress(0, IntMath.ToString10(PrimeP));
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "Prime Q:");
                Worker.ReportProgress(0, IntMath.ToString10(PrimeQ));
                Worker.ReportProgress(0, " ");
                PubKeyN.Copy(PrimeP);
                IntMath.Multiply(PubKeyN, PrimeQ);
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "PubKeyN:");
                Worker.ReportProgress(0, IntMath.ToString10(PubKeyN));
                Worker.ReportProgress(0, " ");

                // Test Division:
                Integer QuotientTest  = new Integer();
                Integer RemainderTest = new Integer();

                IntMath.Divider.Divide(PubKeyN, PrimeP, QuotientTest, RemainderTest);
                if (!RemainderTest.IsZero())
                {
                    throw(new Exception("RemainderTest should be zero after divide by PrimeP."));
                }

                IntMath.Multiply(QuotientTest, PrimeP);
                if (!QuotientTest.IsEqual(PubKeyN))
                {
                    throw(new Exception("QuotientTest didn't come out right."));
                }

                // Euler's Theorem:
                // https://en.wikipedia.org/wiki/Euler's_theorem

// ==========
// Work on the Least Common Multiple thing for
// P - 1 and Q - 1.
// =====

                IntMath.GreatestCommonDivisor(PrimePMinus1, PrimeQMinus1, Gcd);
                Worker.ReportProgress(0, "GCD of PrimePMinus1, PrimeQMinus1 is: " + IntMath.ToString10(Gcd));
                if (!Gcd.IsULong())
                {
                    Worker.ReportProgress(0, "This GCD number is too big: " + IntMath.ToString10(Gcd));
                    continue;
                }
                else
                {
                    ulong TooBig = Gcd.GetAsULong();
                    // How big of a GCD is too big?
// ==============

                    if (TooBig > 1234567)
                    {
                        // (P - 1)(Q - 1) + (P - 1) + (Q - 1) = PQ - 1
                        Worker.ReportProgress(0, "This GCD number is bigger than 1234567: " + IntMath.ToString10(Gcd));
                        continue;
                    }
                }

                Integer Temp1 = new Integer();
                PhiN.Copy(PrimePMinus1);
                Temp1.Copy(PrimeQMinus1);
                IntMath.Multiply(PhiN, Temp1);
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "PhiN:");
                Worker.ReportProgress(0, IntMath.ToString10(PhiN));
                Worker.ReportProgress(0, " ");
                if (Worker.CancellationPending)
                {
                    return;
                }

                // In RFC 2437 there are commonly used letters/symbols to represent
                // the numbers used.  So the number e is the public exponent.
                // The number e that is used here is called PubKeyExponentUint = 65537.
                // In the RFC the private key d is the multiplicative inverse of
                // e mod PhiN.  Which is mod (P - 1)(Q - 1).  It's called
                // PrivKInverseExponent here.
                if (!IntMath.FindMultiplicativeInverseSmall(PrivKInverseExponent, PubKeyExponent, PhiN, Worker))
                {
                    return;
                }

                if (PrivKInverseExponent.IsZero())
                {
                    continue;
                }

                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "PrivKInverseExponent: " + IntMath.ToString10(PrivKInverseExponent));
                if (Worker.CancellationPending)
                {
                    return;
                }

                // In RFC 2437 it defines a number dP which is the multiplicative
                // inverse, mod (P - 1) of e.  That dP is named PrivKInverseExponentDP here.
                Worker.ReportProgress(0, " ");
                if (!IntMath.FindMultiplicativeInverseSmall(PrivKInverseExponentDP, PubKeyExponent, PrimePMinus1, Worker))
                {
                    return;
                }

                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "PrivKInverseExponentDP: " + IntMath.ToString10(PrivKInverseExponentDP));
                if (PrivKInverseExponentDP.IsZero())
                {
                    continue;
                }

                // PrivKInverseExponentDP is PrivKInverseExponent mod PrimePMinus1.
                Integer Test1 = new Integer();
                Test1.Copy(PrivKInverseExponent);
                IntMath.Divider.Divide(Test1, PrimePMinus1, Quotient, Remainder);
                Test1.Copy(Remainder);
                if (!Test1.IsEqual(PrivKInverseExponentDP))
                {
                    throw(new Exception("This does not match the definition of PrivKInverseExponentDP."));
                }

                if (Worker.CancellationPending)
                {
                    return;
                }

                // In RFC 2437 it defines a number dQ which is the multiplicative
                // inverse, mod (Q - 1) of e.  That dQ is named PrivKInverseExponentDQ here.
                Worker.ReportProgress(0, " ");
                if (!IntMath.FindMultiplicativeInverseSmall(PrivKInverseExponentDQ, PubKeyExponent, PrimeQMinus1, Worker))
                {
                    return;
                }

                if (PrivKInverseExponentDQ.IsZero())
                {
                    continue;
                }

                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "PrivKInverseExponentDQ: " + IntMath.ToString10(PrivKInverseExponentDQ));
                if (Worker.CancellationPending)
                {
                    return;
                }

                Test1.Copy(PrivKInverseExponent);
                IntMath.Divider.Divide(Test1, PrimeQMinus1, Quotient, Remainder);
                Test1.Copy(Remainder);
                if (!Test1.IsEqual(PrivKInverseExponentDQ))
                {
                    throw(new Exception("This does not match the definition of PrivKInverseExponentDQ."));
                }

                // Make a random number to test encryption/decryption.
                Integer ToEncrypt    = new Integer();
                int     HowManyBytes = PrimeIndex * 4;
                byte[]  RandBytes    = MakeRandomBytes(HowManyBytes);
                if (RandBytes == null)
                {
                    Worker.ReportProgress(0, "Error making random bytes in MakeRSAKeys().");
                    return;
                }

                if (!ToEncrypt.MakeRandomOdd(PrimeIndex - 1, RandBytes))
                {
                    Worker.ReportProgress(0, "Error making random number ToEncrypt.");
                    return;
                }

                Integer PlainTextNumber = new Integer();
                PlainTextNumber.Copy(ToEncrypt);
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "Before encrypting number: " + IntMath.ToString10(ToEncrypt));
                Worker.ReportProgress(0, " ");
                IntMath.ModReduction.ModularPower(ToEncrypt, PubKeyExponent, PubKeyN, false);
                if (Worker.CancellationPending)
                {
                    return;
                }

                // Worker.ReportProgress( 0, IntMath.GetStatusString() );

                Integer CipherTextNumber = new Integer();
                CipherTextNumber.Copy(ToEncrypt);
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "Encrypted number: " + IntMath.ToString10(CipherTextNumber));
                Worker.ReportProgress(0, " ");
                ECTime DecryptTime = new ECTime();
                DecryptTime.SetToNow();
                IntMath.ModReduction.ModularPower(ToEncrypt, PrivKInverseExponent, PubKeyN, false);
                Worker.ReportProgress(0, "Decrypted number: " + IntMath.ToString10(ToEncrypt));
                if (!PlainTextNumber.IsEqual(ToEncrypt))
                {
                    throw(new Exception("PlainTextNumber not equal to unencrypted value."));
                    // Because P or Q wasn't really a prime?
                    // Worker.ReportProgress( 0, "PlainTextNumber not equal to unencrypted value." );
                    // continue;
                }

                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "Decrypt time seconds: " + DecryptTime.GetSecondsToNow().ToString("N2"));
                Worker.ReportProgress(0, " ");
                if (Worker.CancellationPending)
                {
                    return;
                }

                // Test the standard optimized way of decrypting:
                if (!ToEncrypt.MakeRandomOdd(PrimeIndex - 1, RandBytes))
                {
                    Worker.ReportProgress(0, "Error making random number in MakeRSAKeys().");
                    return;
                }

                PlainTextNumber.Copy(ToEncrypt);
                IntMath.ModReduction.ModularPower(ToEncrypt, PubKeyExponent, PubKeyN, false);
                if (Worker.CancellationPending)
                {
                    return;
                }

                CipherTextNumber.Copy(ToEncrypt);
                // QInv is the multiplicative inverse of PrimeQ mod PrimeP.
                if (!IntMath.MultiplicativeInverse(PrimeQ, PrimeP, QInv, Worker))
                {
                    throw(new Exception("MultiplicativeInverse() returned false."));
                }

                if (QInv.IsNegative)
                {
                    throw(new Exception("QInv is negative."));
                }

                Worker.ReportProgress(0, "QInv is: " + IntMath.ToString10(QInv));
                DecryptWithQInverse(CipherTextNumber,
                                    ToEncrypt,       // Decrypt it to this.
                                    PlainTextNumber, // Test it against this.
                                    PubKeyN,
                                    PrivKInverseExponentDP,
                                    PrivKInverseExponentDQ,
                                    PrimeP,
                                    PrimeQ,
                                    Worker);

                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(0, "Found the values:");
                Worker.ReportProgress(0, "Seconds: " + StartTime.GetSecondsToNow().ToString("N0"));
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(1, "Prime1: " + IntMath.ToString10(PrimeP));
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(1, "Prime2: " + IntMath.ToString10(PrimeQ));
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(1, "PubKeyN: " + IntMath.ToString10(PubKeyN));
                Worker.ReportProgress(0, " ");
                Worker.ReportProgress(1, "PrivKInverseExponent: " + IntMath.ToString10(PrivKInverseExponent));

                // return; // Comment this out to just leave it while( true ) for testing.
            }
        }
예제 #6
0
        // This works like LongDivide1 except that it
        // estimates the maximum value for the digit and
        // the for-loop for bit testing is called
        // as a separate function.
        private bool LongDivide2(Integer ToDivide,
                                 Integer DivideBy,
                                 Integer Quotient,
                                 Integer Remainder)
        {
            Integer Test1     = new Integer();
            int     TestIndex = ToDivide.GetIndex() - DivideBy.GetIndex();

            // See if TestIndex is too high.
            if (TestIndex != 0)
            {
                // Is 1 too high?
                Test1.SetDigitAndClear(TestIndex, 1);
                IntMath.MultiplyTopOne(Test1, DivideBy);
                if (ToDivide.ParamIsGreater(Test1))
                {
                    TestIndex--;
                }
            }

            // If you were multiplying 99 times 97 you'd get
            // 9,603 and the upper two digits [96] are used
            // to find the MaxValue.  But if you multiply
            // 12 * 13 you'd have 156 and only the upper one
            // digit is used to find the MaxValue.
            // Here it checks if it should use one digit or
            // two:
            ulong MaxValue;

            if ((ToDivide.GetIndex() - 1) > (DivideBy.GetIndex() + TestIndex))
            {
                MaxValue = ToDivide.GetD(ToDivide.GetIndex());
            }
            else
            {
                MaxValue  = ToDivide.GetD(ToDivide.GetIndex()) << 32;
                MaxValue |= ToDivide.GetD(ToDivide.GetIndex() - 1);
            }

            MaxValue = MaxValue / DivideBy.GetD(DivideBy.GetIndex());
            Quotient.SetDigitAndClear(TestIndex, 1);
            Quotient.SetD(TestIndex, 0);
            TestDivideBits(MaxValue,
                           true,
                           TestIndex,
                           ToDivide,
                           DivideBy,
                           Quotient,
                           Remainder);

            if (TestIndex == 0)
            {
                Test1.Copy(Quotient);
                IntMath.Multiply(Test1, DivideBy);
                Remainder.Copy(ToDivide);
                IntMath.Subtract(Remainder, Test1);
                ///////////////
                if (DivideBy.ParamIsGreater(Remainder))
                {
                    throw(new Exception("Remainder > DivideBy in LongDivide2()."));
                }

                //////////////
                if (Remainder.IsZero())
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            TestIndex--;
            while (true)
            {
                // This remainder is used the same way you do
                // long division with paper and pen and you
                // keep working with a remainder until the
                // remainder is reduced to something smaller
                // than DivideBy.  You look at the remainder
                // to estimate your next quotient digit.
                Test1.Copy(Quotient);
                IntMath.Multiply(Test1, DivideBy);
                Remainder.Copy(ToDivide);
                IntMath.Subtract(Remainder, Test1);
                MaxValue  = Remainder.GetD(Remainder.GetIndex()) << 32;
                MaxValue |= Remainder.GetD(Remainder.GetIndex() - 1);
                MaxValue  = MaxValue / DivideBy.GetD(DivideBy.GetIndex());
                TestDivideBits(MaxValue,
                               false,
                               TestIndex,
                               ToDivide,
                               DivideBy,
                               Quotient,
                               Remainder);

                if (TestIndex == 0)
                {
                    break;
                }

                TestIndex--;
            }

            Test1.Copy(Quotient);
            IntMath.Multiply(Test1, DivideBy);
            Remainder.Copy(ToDivide);
            IntMath.Subtract(Remainder, Test1);
            //////////////////////////////
            if (DivideBy.ParamIsGreater(Remainder))
            {
                throw(new Exception("Remainder > DivideBy in LongDivide2()."));
            }

            ////////////////////////////////
            if (Remainder.IsZero())
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #7
0
        internal void Divide(Integer ToDivideOriginal,
                             Integer DivideByOriginal,
                             Integer Quotient,
                             Integer Remainder)
        {
            if (ToDivideOriginal.IsNegative)
            {
                throw(new Exception("Divide() can't be called with negative numbers."));
            }

            if (DivideByOriginal.IsNegative)
            {
                throw(new Exception("Divide() can't be called with negative numbers."));
            }

            // Returns true if it divides exactly with zero remainder.
            // This first checks for some basics before trying to divide it:
            if (DivideByOriginal.IsZero())
            {
                throw(new Exception("Divide() dividing by zero."));
            }

            ToDivide.Copy(ToDivideOriginal);
            DivideBy.Copy(DivideByOriginal);
            if (ToDivide.ParamIsGreater(DivideBy))
            {
                Quotient.SetToZero();
                Remainder.Copy(ToDivide);
                return; //  false;
            }

            if (ToDivide.IsEqual(DivideBy))
            {
                Quotient.SetFromULong(1);
                Remainder.SetToZero();
                return; //  true;
            }

            // At this point DivideBy is smaller than ToDivide.
            if (ToDivide.IsULong())
            {
                ulong ToDivideU  = ToDivide.GetAsULong();
                ulong DivideByU  = DivideBy.GetAsULong();
                ulong QuotientU  = ToDivideU / DivideByU;
                ulong RemainderU = ToDivideU % DivideByU;
                Quotient.SetFromULong(QuotientU);
                Remainder.SetFromULong(RemainderU);
                // if( RemainderU == 0 )
                return; //  true;
                // else
                // return false;
            }

            if (DivideBy.GetIndex() == 0)
            {
                ShortDivide(ToDivide, DivideBy, Quotient, Remainder);
                return;
            }

            Integer ToDivideTest2  = new Integer();
            Integer DivideByTest2  = new Integer();
            Integer QuotientTest2  = new Integer();
            Integer RemainderTest2 = new Integer();

            Integer ToDivideTest3  = new Integer();
            Integer DivideByTest3  = new Integer();
            Integer QuotientTest3  = new Integer();
            Integer RemainderTest3 = new Integer();

            ToDivideTest2.Copy(ToDivide);
            ToDivideTest3.Copy(ToDivide);

            DivideByTest2.Copy(DivideBy);
            DivideByTest3.Copy(DivideBy);

            LongDivide1(ToDivideTest2, DivideByTest2, QuotientTest2, RemainderTest2);
            LongDivide2(ToDivideTest3, DivideByTest3, QuotientTest3, RemainderTest3);
            LongDivide3(ToDivide, DivideBy, Quotient, Remainder);

            if (!Quotient.IsEqual(QuotientTest2))
            {
                throw(new Exception("!Quotient.IsEqual( QuotientTest2 )"));
            }

            if (!Quotient.IsEqual(QuotientTest3))
            {
                throw(new Exception("!Quotient.IsEqual( QuotientTest3 )"));
            }

            if (!Remainder.IsEqual(RemainderTest2))
            {
                throw(new Exception("!Remainder.IsEqual( RemainderTest2 )"));
            }

            if (!Remainder.IsEqual(RemainderTest3))
            {
                throw(new Exception("!Remainder.IsEqual( RemainderTest3 )"));
            }
        }
예제 #8
0
        // This is the standard modular power algorithm that
        // you could find in any reference, but its use of
        // my modular reduction algorithm in it is new (in 2015).
        // (I mean as opposed to using some other modular reduction
        // algorithm.)
        // The square and multiply method is in Wikipedia:
        // https://en.wikipedia.org/wiki/Exponentiation_by_squaring
        internal void ModularPower(Integer Result, Integer Exponent, Integer Modulus, bool UsePresetBaseArray)
        {
            if (Result.IsZero())
            {
                return; // With Result still zero.
            }
            if (Result.IsEqual(Modulus))
            {
                // It is congruent to zero % ModN.
                Result.SetToZero();
                return;
            }

            // Result is not zero at this point.
            if (Exponent.IsZero())
            {
                Result.SetFromULong(1);
                return;
            }

            if (Modulus.ParamIsGreater(Result))
            {
                // throw( new Exception( "This is not supposed to be input for RSA plain text." ));
                IntMath.Divider.Divide(Result, Modulus, Quotient, Remainder);
                Result.Copy(Remainder);
            }

            if (Exponent.IsOne())
            {
                // Result stays the same.
                return;
            }

            if (!UsePresetBaseArray)
            {
                IntMath.ModReduction.SetupGeneralBaseArray(Modulus);
            }

            XForModPower.Copy(Result);
            ExponentCopy.Copy(Exponent);
            // int TestIndex = 0;
            Result.SetFromULong(1);
            while (true)
            {
                if ((ExponentCopy.GetD(0) & 1) == 1) // If the bottom bit is 1.
                {
                    IntMath.Multiplier.Multiply(Result, XForModPower);

                    // if( Result.ParamIsGreater( CurrentModReductionBase ))
                    // TestForModReduction2.Copy( Result );

                    IntMath.ModReduction.Reduce(TempForModPower, Result);
                    // ModularReduction2( TestForModReduction2ForModPower, TestForModReduction2 );
                    // if( !TestForModReduction2ForModPower.IsEqual( TempForModPower ))
                    // {
                    // throw( new Exception( "Mod Reduction 2 is not right." ));
                    // }

                    Result.Copy(TempForModPower);
                }

                ExponentCopy.ShiftRight(1); // Divide by 2.
                if (ExponentCopy.IsZero())
                {
                    break;
                }

                // Square it.
                IntMath.Multiplier.Multiply(XForModPower, XForModPower);

                // if( XForModPower.ParamIsGreater( CurrentModReductionBase ))
                IntMath.ModReduction.Reduce(TempForModPower, XForModPower);
                XForModPower.Copy(TempForModPower);
            }

            // When ModularReduction() gets called it multiplies a base number
            // by a uint sized digit.  So that can make the result one digit bigger
            // than GeneralBase.  Then when they are added up you can get carry
            // bits that can make it a little bigger.
            int HowBig = Result.GetIndex() - Modulus.GetIndex();

            // if( HowBig > 1 )
            // throw( new Exception( "This does happen. Diff: " + HowBig.ToString() ));

            // Do a proof for how big this can be.
            if (HowBig > 2)
            {
                throw(new Exception("This never happens. Diff: " + HowBig.ToString()));
            }

            IntMath.ModReduction.Reduce(TempForModPower, Result);
            Result.Copy(TempForModPower);

            // Notice that this Divide() is done once.  Not
            // a thousand or two thousand times.

/*
 *  Integer ResultTest = new Integer();
 *  Integer ModulusTest = new Integer();
 *  Integer QuotientTest = new Integer();
 *  Integer RemainderTest = new Integer();
 *
 *  ResultTest.Copy( Result );
 *  ModulusTest.Copy( Modulus );
 *  IntMath.Divider.DivideForSmallQuotient( ResultTest,
 *                          ModulusTest,
 *                          QuotientTest,
 *                          RemainderTest );
 *
 */

            IntMath.Divider.Divide(Result, Modulus, Quotient, Remainder);

            // if( !RemainderTest.IsEqual( Remainder ))
            // throw( new Exception( "DivideForSmallQuotient() !RemainderTest.IsEqual( Remainder )." ));

            // if( !QuotientTest.IsEqual( Quotient ))
            // throw( new Exception( "DivideForSmallQuotient() !QuotientTest.IsEqual( Quotient )." ));


            Result.Copy(Remainder);
            if (Quotient.GetIndex() > 1)
            {
                throw(new Exception("This never happens. The quotient index is never more than 1."));
            }
        }
예제 #9
0
        internal bool MultiplicativeInverse(Integer X, Integer Modulus, Integer MultInverse)
        {
            // This is the extended Euclidean Algorithm.
            // A*X + B*Y = Gcd
            // A*X + B*Y = 1 If there's a multiplicative inverse.
            // A*X = 1 - B*Y so A is the multiplicative inverse of X mod Y.
            if (X.IsZero())
            {
                throw(new Exception("Doing Multiplicative Inverse with a parameter that is zero."));
            }

            if (Modulus.IsZero())
            {
                throw(new Exception("Doing Multiplicative Inverse with a parameter that is zero."));
            }

            // This happens sometimes:
            // if( Modulus.ParamIsGreaterOrEq( X ))
            // throw( new Exception( "Modulus.ParamIsGreaterOrEq( X ) for Euclid." ));

            // Worker.ReportProgress( 0, " " );
            // Worker.ReportProgress( 0, " " );
            // Worker.ReportProgress( 0, "Top of mod inverse." );
            // U is the old part to keep.
            U0.SetToZero();
            U1.SetToOne();
            U2.Copy(Modulus); // Don't change the original numbers that came in as parameters.
            // V is the new part.
            V0.SetToOne();
            V1.SetToZero();
            V2.Copy(X);
            T0.SetToZero();
            T1.SetToZero();
            T2.SetToZero();
            Quotient.SetToZero();
            // while( not forever if there's a problem )
            for (int Count = 0; Count < 10000; Count++)
            {
                if (U2.IsNegative)
                {
                    throw(new Exception("U2 was negative."));
                }

                if (V2.IsNegative)
                {
                    throw(new Exception("V2 was negative."));
                }

                IntMath.Divider.Divide(U2, V2, Quotient, Remainder);
                if (Remainder.IsZero())
                {
                    // Worker.ReportProgress( 0, "Remainder is zero. No multiplicative-inverse." );
                    return(false);
                }

                TempEuclid1.Copy(U0);
                TempEuclid2.Copy(V0);
                IntMath.Multiplier.Multiply(TempEuclid2, Quotient);
                IntMath.Subtract(TempEuclid1, TempEuclid2);
                T0.Copy(TempEuclid1);
                TempEuclid1.Copy(U1);
                TempEuclid2.Copy(V1);
                IntMath.Multiplier.Multiply(TempEuclid2, Quotient);
                IntMath.Subtract(TempEuclid1, TempEuclid2);
                T1.Copy(TempEuclid1);
                TempEuclid1.Copy(U2);
                TempEuclid2.Copy(V2);
                IntMath.Multiplier.Multiply(TempEuclid2, Quotient);
                IntMath.Subtract(TempEuclid1, TempEuclid2);
                T2.Copy(TempEuclid1);
                U0.Copy(V0);
                U1.Copy(V1);
                U2.Copy(V2);
                V0.Copy(T0);
                V1.Copy(T1);
                V2.Copy(T2);
                if (Remainder.IsOne())
                {
                    // Worker.ReportProgress( 0, " " );
                    // Worker.ReportProgress( 0, "Remainder is 1. There is a multiplicative-inverse." );
                    break;
                }
            }

            MultInverse.Copy(T0);
            if (MultInverse.IsNegative)
            {
                IntMath.Add(MultInverse, Modulus);
            }

            // Worker.ReportProgress( 0, "MultInverse: " + ToString10( MultInverse ));
            TestForModInverse1.Copy(MultInverse);
            TestForModInverse2.Copy(X);
            IntMath.Multiplier.Multiply(TestForModInverse1, TestForModInverse2);
            IntMath.Divider.Divide(TestForModInverse1, Modulus, Quotient, Remainder);
            if (!Remainder.IsOne()) // By the definition of Multiplicative inverse:
            {
                throw(new Exception("MultInverse is wrong: " + IntMath.ToString10(Remainder)));
            }

            // Worker.ReportProgress( 0, "MultInverse is the right number: " + ToString10( MultInverse ));
            return(true);
        }
예제 #10
0
        internal void Multiply(Integer Result, Integer ToMul)
        {
            // try
            // {
            if (Result.IsZero())
            {
                return;
            }

            if (ToMul.IsULong())
            {
                MultiplyULong(Result, ToMul.GetAsULong());
                SetMultiplySign(Result, ToMul);
                return;
            }

            // It could never get here if ToMul is zero because GetIsULong()
            // would be true for zero.
            // if( ToMul.IsZero())
            int TotalIndex = Result.GetIndex() + ToMul.GetIndex();

            if (TotalIndex >= Integer.DigitArraySize)
            {
                throw(new Exception("Multiply() overflow."));
            }

            int CountTo = ToMul.GetIndex();

            for (int Row = 0; Row <= CountTo; Row++)
            {
                if (ToMul.GetD(Row) == 0)
                {
                    int CountZeros = Result.GetIndex();
                    for (int Column = 0; Column <= CountZeros; Column++)
                    {
                        M[Column + Row, Row] = 0;
                    }
                }
                else
                {
                    int CountMult = Result.GetIndex();
                    for (int Column = 0; Column <= CountMult; Column++)
                    {
                        M[Column + Row, Row] = ToMul.GetD(Row) * Result.GetD(Column);
                    }
                }
            }

            // Add the columns up with a carry.
            Result.SetD(0, M[0, 0] & 0xFFFFFFFF);
            ulong Carry       = M[0, 0] >> 32;
            int   ResultIndex = Result.GetIndex();
            int   MulIndex    = ToMul.GetIndex();

            for (int Column = 1; Column <= TotalIndex; Column++)
            {
                ulong TotalLeft  = 0;
                ulong TotalRight = 0;
                for (int Row = 0; Row <= MulIndex; Row++)
                {
                    if (Row > Column)
                    {
                        break;
                    }

                    if (Column > (ResultIndex + Row))
                    {
                        continue;
                    }

                    // Split the ulongs into right and left sides
                    // so that they don't overflow.
                    TotalRight += M[Column, Row] & 0xFFFFFFFF;
                    TotalLeft  += M[Column, Row] >> 32;
                }

                TotalRight += Carry;
                Result.SetD(Column, TotalRight & 0xFFFFFFFF);
                Carry  = TotalRight >> 32;
                Carry += TotalLeft;
            }

            Result.SetIndex(TotalIndex);
            if (Carry != 0)
            {
                Result.IncrementIndex(); // This can throw an exception if it overflowed the index.
                Result.SetD(Result.GetIndex(), Carry);
            }

            SetMultiplySign(Result, ToMul);
        }
예제 #11
0
        internal void MultiplyULong(Integer Result, ulong ToMul)
        {
            // Using compile-time checks, this one overflows:
            // const ulong Test = ((ulong)0xFFFFFFFF + 1) * ((ulong)0xFFFFFFFF + 1);
            // This one doesn't:
            // const ulong Test = (ulong)0xFFFFFFFF * ((ulong)0xFFFFFFFF + 1);
            if (Result.IsZero())
            {
                return; // Then the answer is zero, which it already is.
            }
            if (ToMul == 0)
            {
                Result.SetToZero();
                return;
            }

            ulong B0 = ToMul & 0xFFFFFFFF;
            ulong B1 = ToMul >> 32;

            if (B1 == 0)
            {
                MultiplyUInt(Result, (uint)B0);
                return;
            }

            // Since B1 is not zero:
            if ((Result.GetIndex() + 1) >= Integer.DigitArraySize)
            {
                throw(new Exception("Overflow in MultiplyULong."));
            }

            int CountTo = Result.GetIndex();

            for (int Column = 0; Column <= CountTo; Column++)
            {
                ulong Digit = Result.GetD(Column);
                M[Column, 0] = B0 * Digit;
                // Column + 1 and Row is 1, so it's just like pen and paper.
                M[Column + 1, 1] = B1 * Digit;
            }

            // Since B1 is not zero, the index is set one higher.
            Result.IncrementIndex();     // Might throw an exception if it goes out of range.
            M[Result.GetIndex(), 0] = 0; // Otherwise it would be undefined
                                         // when it's added up below.
            // Add these up with a carry.
            Result.SetD(0, M[0, 0] & 0xFFFFFFFF);
            ulong Carry = M[0, 0] >> 32;

            CountTo = Result.GetIndex();
            for (int Column = 1; Column <= CountTo; Column++)
            {
                // This does overflow:
                // const ulong Test = ((ulong)0xFFFFFFFF * (ulong)(0xFFFFFFFF))
                //                  + ((ulong)0xFFFFFFFF * (ulong)(0xFFFFFFFF));
                // Split the ulongs into right and left sides
                // so that they don't overflow.
                ulong TotalLeft  = 0;
                ulong TotalRight = 0;
                // There's only the two rows for this.
                for (int Row = 0; Row <= 1; Row++)
                {
                    ulong MValue = M[Column, Row];
                    TotalRight += MValue & 0xFFFFFFFF;
                    TotalLeft  += MValue >> 32;
                }

                TotalRight += Carry;
                Result.SetD(Column, TotalRight & 0xFFFFFFFF);
                Carry  = TotalRight >> 32;
                Carry += TotalLeft;
            }

            if (Carry != 0)
            {
                Result.IncrementIndex(); // This can throw an exception.
                Result.SetD(Result.GetIndex(), Carry);
            }
        }