Esempio n. 1
0
        // Copyright Eric Chauvin 2015 - 2018.
        internal int Reduce(Integer Result, Integer ToReduce)
        {
            try
            {
                if (ToReduce.ParamIsGreater(CurrentModReductionBase))
                {
                    Result.Copy(ToReduce);
                    return(Result.GetIndex());
                }

                if (GeneralBaseArray == null)
                {
                    throw(new Exception("SetupGeneralBaseArray() should have already been called."));
                }

                Result.SetToZero();
                int TopOfToReduce = ToReduce.GetIndex() + 1;
                if (TopOfToReduce > GeneralBaseArray.Length)
                {
                    throw(new Exception("The Input number should have been reduced first. HowManyToAdd > GeneralBaseArray.Length"));
                }

                // If it gets this far then ToReduce is at
                // least this big.

                int HighestCopyIndex = CurrentModReductionBase.GetIndex();
                Result.CopyUpTo(ToReduce, HighestCopyIndex - 1);

                int BiggestIndex = 0;
                for (int Count = HighestCopyIndex; Count < TopOfToReduce; Count++)
                {
                    // The size of the numbers in GeneralBaseArray
                    // are all less than the size of GeneralBase.
                    // This multiplication by a uint is with a
                    // number that is not bigger than GeneralBase.
                    // Compare this with the two full Muliply()
                    // calls done on each digit of the quotient
                    // in LongDivide3().

                    // AccumulateBase is set to a new value here.
                    int CheckIndex = IntMath.MultiplyUIntFromCopy(AccumulateBase, GeneralBaseArray[Count], ToReduce.GetD(Count));
                    if (CheckIndex > BiggestIndex)
                    {
                        BiggestIndex = CheckIndex;
                    }

                    Result.Add(AccumulateBase);
                }

                return(Result.GetIndex());
            }
            catch (Exception Except)
            {
                throw(new Exception("Exception in ModularReduction(): " + Except.Message));
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        internal void Subtract(Integer Result, Integer ToSub)
        {
            // This checks that the sign is equal too.
            if (Result.IsEqual(ToSub))
            {
                Result.SetToZero();
                return;
            }

            // ParamIsGreater() handles positive and negative values, so if the
            // parameter is more toward the positive side then it's true.  It's greater.
            // The most common form.  They are both positive.
            if (!Result.IsNegative && !ToSub.IsNegative)
            {
                if (ToSub.ParamIsGreater(Result))
                {
                    SubtractPositive(Result, ToSub);
                    return;
                }

                // ToSub is bigger.
                TempSub1.Copy(Result);
                TempSub2.Copy(ToSub);
                SubtractPositive(TempSub2, TempSub1);
                Result.Copy(TempSub2);
                Result.IsNegative = true;
                return;
            }

            if (Result.IsNegative && !ToSub.IsNegative)
            {
                TempSub1.Copy(Result);
                TempSub1.IsNegative = false;
                TempSub1.Add(ToSub);
                Result.Copy(TempSub1);
                Result.IsNegative = true;
                return;
            }

            if (!Result.IsNegative && ToSub.IsNegative)
            {
                TempSub1.Copy(ToSub);
                TempSub1.IsNegative = false;
                Result.Add(TempSub1);
                return;
            }

            if (Result.IsNegative && ToSub.IsNegative)
            {
                TempSub1.Copy(Result);
                TempSub1.IsNegative = false;
                TempSub2.Copy(ToSub);
                TempSub2.IsNegative = false;
                // -12 - -7 = -12 + 7 = -5
                // Comparing the positive numbers here.
                if (TempSub2.ParamIsGreater(TempSub1))
                {
                    SubtractPositive(TempSub1, TempSub2);
                    Result.Copy(TempSub1);
                    Result.IsNegative = true;
                    return;
                }

                // -7 - -12 = -7 + 12 = 5
                SubtractPositive(TempSub2, TempSub1);
                Result.Copy(TempSub2);
                Result.IsNegative = false;
                return;
            }
        }
Esempio n. 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;
            }
        }
Esempio n. 5
0
        internal bool DecryptWithQInverse(Integer EncryptedNumber,
                                          Integer DecryptedNumber,
                                          Integer TestDecryptedNumber,
                                          Integer PubKeyN,
                                          Integer PrivKInverseExponentDP,
                                          Integer PrivKInverseExponentDQ,
                                          Integer PrimeP,
                                          Integer PrimeQ,
                                          BackgroundWorker Worker)
        {
            Worker.ReportProgress(0, " ");
            Worker.ReportProgress(0, "Top of DecryptWithQInverse().");
            // QInv and the dP and dQ numbers are normally already set up before
            // you start your listening socket.
            ECTime DecryptTime = new ECTime();

            DecryptTime.SetToNow();
            // See section 5.1.2 of RFC 2437 for these steps:
            // http://tools.ietf.org/html/rfc2437
            //      2.2 Let m_1 = c^dP mod p.
            //      2.3 Let m_2 = c^dQ mod q.
            //      2.4 Let h = qInv ( m_1 - m_2 ) mod p.
            //      2.5 Let m = m_2 + hq.
            Worker.ReportProgress(0, "EncryptedNumber: " + IntMath.ToString10(EncryptedNumber));
            //      2.2 Let m_1 = c^dP mod p.
            TestForDecrypt.Copy(EncryptedNumber);
            IntMathForP.ModReduction.ModularPower(TestForDecrypt, PrivKInverseExponentDP, PrimeP, true);
            if (Worker.CancellationPending)
            {
                return(false);
            }

            M1ForInverse.Copy(TestForDecrypt);
            //      2.3 Let m_2 = c^dQ mod q.
            TestForDecrypt.Copy(EncryptedNumber);
            IntMathForQ.ModReduction.ModularPower(TestForDecrypt, PrivKInverseExponentDQ, PrimeQ, true);
            if (Worker.CancellationPending)
            {
                return(false);
            }

            M2ForInverse.Copy(TestForDecrypt);
            //      2.4 Let h = qInv ( m_1 - m_2 ) mod p.
            // How many is optimal to avoid the division?
            int HowManyIsOptimal = (PrimeP.GetIndex() * 3);

            for (int Count = 0; Count < HowManyIsOptimal; Count++)
            {
                if (M1ForInverse.ParamIsGreater(M2ForInverse))
                {
                    M1ForInverse.Add(PrimeP);
                }
                else
                {
                    break;
                }
            }

            if (M1ForInverse.ParamIsGreater(M2ForInverse))
            {
                M1M2SizeDiff.Copy(M2ForInverse);
                IntMath.Subtract(M1M2SizeDiff, M1ForInverse);
                // Unfortunately this long Divide() has to be done.
                IntMath.Divider.Divide(M1M2SizeDiff, PrimeP, Quotient, Remainder);
                Quotient.AddULong(1);
                Worker.ReportProgress(0, "The Quotient for M1M2SizeDiff is: " + IntMath.ToString10(Quotient));
                IntMath.Multiply(Quotient, PrimeP);
                M1ForInverse.Add(Quotient);
            }

            M1MinusM2.Copy(M1ForInverse);
            IntMath.Subtract(M1MinusM2, M2ForInverse);
            if (M1MinusM2.IsNegative)
            {
                throw(new Exception("M1MinusM2.IsNegative is true."));
            }

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

            HForQInv.Copy(M1MinusM2);
            IntMath.Multiply(HForQInv, QInv);
            if (HForQInv.IsNegative)
            {
                throw(new Exception("HForQInv.IsNegative is true."));
            }

            if (PrimeP.ParamIsGreater(HForQInv))
            {
                IntMath.Divider.Divide(HForQInv, PrimeP, Quotient, Remainder);
                HForQInv.Copy(Remainder);
            }

            //      2.5 Let m = m_2 + hq.
            DecryptedNumber.Copy(HForQInv);
            IntMath.Multiply(DecryptedNumber, PrimeQ);
            DecryptedNumber.Add(M2ForInverse);
            if (!TestDecryptedNumber.IsEqual(DecryptedNumber))
            {
                throw(new Exception("!TestDecryptedNumber.IsEqual( DecryptedNumber )."));
            }

            Worker.ReportProgress(0, " ");
            Worker.ReportProgress(0, "DecryptedNumber: " + IntMath.ToString10(DecryptedNumber));
            Worker.ReportProgress(0, " ");
            Worker.ReportProgress(0, "TestDecryptedNumber: " + IntMath.ToString10(TestDecryptedNumber));
            Worker.ReportProgress(0, " ");
            Worker.ReportProgress(0, "Decrypt with QInv time seconds: " + DecryptTime.GetSecondsToNow().ToString("N2"));
            Worker.ReportProgress(0, " ");
            return(true);
        }
Esempio n. 6
0
        private void LongDivide3(Integer ToDivide,
                                 Integer DivideBy,
                                 Integer Quotient,
                                 Integer Remainder)
        {
            //////////////////
            Integer Test2 = new Integer();
            /////////////////

            int TestIndex = ToDivide.GetIndex() - DivideBy.GetIndex();

            if (TestIndex < 0)
            {
                throw(new Exception("TestIndex < 0 in Divide3."));
            }

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

            // Keep a copy of the originals.
            ToDivideKeep.Copy(ToDivide);
            DivideByKeep.Copy(DivideBy);
            ulong TestBits = DivideBy.GetD(DivideBy.GetIndex());
            int   ShiftBy  = FindShiftBy(TestBits);

            ToDivide.ShiftLeft(ShiftBy); // Multiply the numerator and the denominator
            DivideBy.ShiftLeft(ShiftBy); // by the same amount.
            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);
            }

            ulong Denom = DivideBy.GetD(DivideBy.GetIndex());

            if (Denom != 0)
            {
                MaxValue = MaxValue / Denom;
            }
            else
            {
                MaxValue = 0xFFFFFFFF;
            }

            if (MaxValue > 0xFFFFFFFF)
            {
                MaxValue = 0xFFFFFFFF;
            }

            if (MaxValue == 0)
            {
                throw(new Exception("MaxValue is zero at the top in LongDivide3()."));
            }

            Quotient.SetDigitAndClear(TestIndex, 1);
            Quotient.SetD(TestIndex, 0);
            TestForDivide1.Copy(Quotient);
            TestForDivide1.SetD(TestIndex, MaxValue);
            IntMath.MultiplyTop(TestForDivide1, DivideBy);


/////////////
            Test2.Copy(Quotient);
            Test2.SetD(TestIndex, MaxValue);
            IntMath.Multiply(Test2, DivideBy);
            if (!Test2.IsEqual(TestForDivide1))
            {
                throw(new Exception("In Divide3() !IsEqual( Test2, TestForDivide1 )"));
            }
///////////



            if (TestForDivide1.ParamIsGreaterOrEq(ToDivide))
            {
                // ToMatchExactCount++;
                // Most of the time (roughly 5 out of every 6
                // times) this MaxValue estimate is exactly
                // right:
                Quotient.SetD(TestIndex, MaxValue);
            }
            else
            {
                // MaxValue can't be zero here. If it was it
                // would already be low enough before it got
                // here.
                MaxValue--;
                if (MaxValue == 0)
                {
                    throw(new Exception("After decrement: MaxValue is zero in LongDivide3()."));
                }

                TestForDivide1.Copy(Quotient);
                TestForDivide1.SetD(TestIndex, MaxValue);
                IntMath.MultiplyTop(TestForDivide1, DivideBy);
                if (TestForDivide1.ParamIsGreaterOrEq(ToDivide))
                {
                    // ToMatchDecCount++;
                    Quotient.SetD(TestIndex, MaxValue);
                }
                else
                {
                    // TestDivideBits is done as a last resort,
                    // but it's rare.  But it does at least limit
                    // it to a worst case scenario of trying 32
                    // bits, rather than 4 billion or so
                    // decrements.
                    TestDivideBits(MaxValue,
                                   true,
                                   TestIndex,
                                   ToDivide,
                                   DivideBy,
                                   Quotient,
                                   Remainder);
                }

                // TestGap = MaxValue - LgQuotient.D[TestIndex];
                // if( TestGap > HighestToMatchGap )
                // HighestToMatchGap = TestGap;

                // HighestToMatchGap: 4,294,967,293
                // uint size:         4,294,967,295 uint
            }

            // If it's done.
            if (TestIndex == 0)
            {
                TestForDivide1.Copy(Quotient);
                IntMath.Multiply(TestForDivide1, DivideByKeep);
                Remainder.Copy(ToDivideKeep);
                IntMath.Subtract(Remainder, TestForDivide1);
                if (DivideByKeep.ParamIsGreater(Remainder))
                {
                    throw(new Exception("Remainder > DivideBy in LongDivide3()."));
                }

                return;
            }

            // Now do the rest of the digits.
            TestIndex--;
            while (true)
            {
                TestForDivide1.Copy(Quotient);
                // First Multiply() for each digit.
                IntMath.Multiply(TestForDivide1, DivideBy);

                if (ToDivide.ParamIsGreater(TestForDivide1))
                {
                    throw(new Exception("Problem here in LongDivide3()."));
                }

                Remainder.Copy(ToDivide);
                IntMath.Subtract(Remainder, TestForDivide1);
                MaxValue = Remainder.GetD(Remainder.GetIndex()) << 32;
                int CheckIndex = Remainder.GetIndex() - 1;
                if (CheckIndex > 0)
                {
                    MaxValue |= Remainder.GetD(CheckIndex);
                }

                Denom = DivideBy.GetD(DivideBy.GetIndex());
                if (Denom != 0)
                {
                    MaxValue = MaxValue / Denom;
                }
                else
                {
                    MaxValue = 0xFFFFFFFF;
                }

                if (MaxValue > 0xFFFFFFFF)
                {
                    MaxValue = 0xFFFFFFFF;
                }

                TestForDivide1.Copy(Quotient);
                TestForDivide1.SetD(TestIndex, MaxValue);
                // There's a minimum of two full Multiply() operations per digit.
                IntMath.Multiply(TestForDivide1, DivideBy);
                if (TestForDivide1.ParamIsGreaterOrEq(ToDivide))
                {
                    // Most of the time this MaxValue estimate is exactly right:
                    // ToMatchExactCount++;
                    Quotient.SetD(TestIndex, MaxValue);
                }
                else
                {
                    MaxValue--;
                    TestForDivide1.Copy(Quotient);
                    TestForDivide1.SetD(TestIndex, MaxValue);
                    IntMath.Multiply(TestForDivide1, DivideBy);
                    if (TestForDivide1.ParamIsGreaterOrEq(ToDivide))
                    {
                        // ToMatchDecCount++;
                        Quotient.SetD(TestIndex, MaxValue);
                    }
                    else
                    {
                        TestDivideBits(MaxValue,
                                       false,
                                       TestIndex,
                                       ToDivide,
                                       DivideBy,
                                       Quotient,
                                       Remainder);

                        // TestGap = MaxValue - LgQuotient.D[TestIndex];
                        // if( TestGap > HighestToMatchGap )
                        // HighestToMatchGap = TestGap;
                    }
                }

                if (TestIndex == 0)
                {
                    break;
                }

                TestIndex--;
            }

            TestForDivide1.Copy(Quotient);
            IntMath.Multiply(TestForDivide1, DivideByKeep);
            Remainder.Copy(ToDivideKeep);
            IntMath.Subtract(Remainder, TestForDivide1);
            if (DivideByKeep.ParamIsGreater(Remainder))
            {
                throw(new Exception("Remainder > DivideBy in LongDivide3()."));
            }
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
0
        private bool LongDivide1(Integer ToDivide,
                                 Integer DivideBy,
                                 Integer Quotient,
                                 Integer Remainder)
        {
            uint    Digit     = 0;
            Integer Test1     = new Integer();
            int     TestIndex = ToDivide.GetIndex() - DivideBy.GetIndex();

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

            Quotient.SetDigitAndClear(TestIndex, 1);
            Quotient.SetD(TestIndex, 0);
            uint BitTest = 0x80000000;

            while (true)
            {
                // For-loop to test each bit:
                for (int BitCount = 31; BitCount >= 0; BitCount--)
                {
                    Test1.Copy(Quotient);
                    Digit = (uint)Test1.GetD(TestIndex) | BitTest;
                    Test1.SetD(TestIndex, Digit);
                    IntMath.Multiply(Test1, DivideBy);
                    if (Test1.ParamIsGreaterOrEq(ToDivide))
                    {
                        Digit = (uint)Quotient.GetD(TestIndex) | BitTest;
                        // I want to keep this bit because it
                        // passed the test.
                        Quotient.SetD(TestIndex, Digit);
                    }

                    BitTest >>= 1;
                }

                if (TestIndex == 0)
                {
                    break;
                }

                TestIndex--;
                BitTest = 0x80000000;
            }

            Test1.Copy(Quotient);
            IntMath.Multiply(Test1, DivideBy);
            if (Test1.IsEqual(ToDivide))
            {
                Remainder.SetToZero();
                return(true); // Divides exactly.
            }

            Remainder.Copy(ToDivide);
            IntMath.Subtract(Remainder, Test1);

            // Does not divide it exactly.
            return(false);
        }
Esempio n. 9
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 )"));
            }
        }
Esempio n. 10
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."));
            }
        }