Multi–Precision Math Tom St Denis Algonquin College Mads Rasmussen Open Communications Security Greg Rose QUALCOMM Australia August 29, 2017 This text has been placed in the public domain. This text corresponds to the v0.39 release of the LibTomMath project. This text is formatted to the international B5 paper size of 176mm wide by 250mm tall using the LATEX book macro package and the Perl booker package. Contents 1 Introduction 1 1.1 MultiplePrecisionArithmetic . 1 1.1.1 WhatisMultiplePrecisionArithmetic? . 1 1.1.2 The Need for Multiple Precision Arithmetic . 1 1.1.3 BenefitsofMultiplePrecisionArithmetic . 3 1.2 PurposeofThisText......................... 4 1.3 DiscussionandNotation . 5 1.3.1 Notation............................ 5 1.3.2 PrecisionNotation . 5 1.3.3 AlgorithmInputsandOutputs . 6 1.3.4 MathematicalExpressions . 6 1.3.5 WorkEffort.......................... 7 1.4 Exercises ............................... 7 1.5 IntroductiontoLibTomMath . 9 1.5.1 WhatisLibTomMath?. 9 1.5.2 GoalsofLibTomMath . 9 1.6 ChoiceofLibTomMath . 10 1.6.1 CodeBase........................... 10 1.6.2 APISimplicity ........................ 11 1.6.3 Optimizations......................... 11 1.6.4 PortabilityandStability . 11 1.6.5 Choice............................. 12 2 Getting Started 13 2.1 LibraryBasics ............................ 13 2.2 WhatisaMultiplePrecisionInteger? . 14 2.2.1 The mp intStructure .................... 15 iii 2.3 ArgumentPassing .......................... 17 2.4 ReturnValues............................. 18 2.5 InitializationandClearing . 19 2.5.1 Initializing an mp int .................... 19 2.5.2 Clearing an mp int...................... 22 2.6 MaintenanceAlgorithms . 24 2.6.1 Augmenting an mp int’sPrecision . 24 2.6.2 Initializing Variable Precision mp ints ........... 27 2.6.3 Multiple Integer Initializations and Clearings . 29 2.6.4 ClampingExcessDigits . 31 3 Basic Operations 35 3.1 Introduction.............................. 35 3.2 Assigning Values to mp intStructures . 35 3.2.1 Copying an mp int...................... 35 3.2.2 CreatingaClone ....................... 40 3.3 ZeroinganInteger .......................... 41 3.4 SignManipulation .......................... 42 3.4.1 AbsoluteValue ........................ 42 3.4.2 IntegerNegation . 43 3.5 SmallConstants ........................... 45 3.5.1 SettingSmallConstants . 45 3.5.2 SettingLargeConstants . 47 3.6 Comparisons ............................. 48 3.6.1 UnsignedComparisions . 48 3.6.2 SignedComparisons . 51 4 Basic Arithmetic 55 4.1 Introduction.............................. 55 4.2 AdditionandSubtraction . 56 4.2.1 LowLevelAddition . 56 4.2.2 LowLevelSubtraction . 61 4.2.3 HighLevelAddition . 65 4.2.4 HighLevelSubtraction . 68 4.3 BitandDigitShifting . .. 71 4.3.1 MultiplicationbyTwo . 71 4.3.2 DivisionbyTwo ....................... 74 4.4 PolynomialBasisOperations . 77 4.4.1 Multiplication by x ...................... 77 4.4.2 Division by x ......................... 80 4.5 PowersofTwo ............................ 83 4.5.1 MultiplicationbyPowerofTwo. 83 4.5.2 DivisionbyPowerofTwo . 87 4.5.3 RemainderofDivisionbyPowerofTwo . 90 5 Multiplication and Squaring 95 5.1 TheMultipliers............................ 95 5.2 Multiplication............................. 96 5.2.1 TheBaselineMultiplication . 96 5.2.2 Faster Multiplication by the “Comba” Method . 101 5.2.3 PolynomialBasisMultiplication . 109 5.2.4 KaratsubaMultiplication . 112 5.2.5 Toom-Cook3-WayMultiplication. 118 5.2.6 SignedMultiplication . 128 5.3 Squaring................................ 130 5.3.1 TheBaselineSquaringAlgorithm . 131 5.3.2 Faster Squaring by the “Comba” Method . 134 5.3.3 PolynomialBasisSquaring . 138 5.3.4 KaratsubaSquaring . 139 5.3.5 Toom-CookSquaring. 144 5.3.6 HighLevelSquaring . 144 6 Modular Reduction 149 6.1 BasicsofModularReduction . 149 6.2 TheBarrettReduction. 150 6.2.1 FixedPointArithmetic . 150 6.2.2 ChoosingaRadixPoint . 152 6.2.3 TrimmingtheQuotient . 153 6.2.4 TrimmingtheResidue . 154 6.2.5 TheBarrettAlgorithm. 154 6.2.6 TheBarrettSetupAlgorithm . 158 6.3 TheMontgomeryReduction. 160 6.3.1 DigitBasedMontgomeryReduction . 163 6.3.2 BaselineMontgomeryReduction . 164 6.3.3 Faster “Comba” Montgomery Reduction . 169 6.3.4 MontgomerySetup. 175 6.4 TheDiminishedRadixAlgorithm. 177 6.4.1 ChoiceofModuli . 179 6.4.2 Choice of k ..........................180 6.4.3 Restricted Diminished Radix Reduction . 180 6.4.4 Unrestricted Diminished Radix Reduction . 186 6.5 AlgorithmComparison. 191 7 Exponentiation 193 7.1 ExponentiationBasics . 193 7.1.1 SingleDigitExponentiation . 195 7.2 k-aryExponentiation. 198 7.2.1 Optimal Values of k .....................199 7.2.2 Sliding-WindowExponentiation. 199 7.3 ModularExponentiation . 201 7.3.1 BarrettModularExponentiation . 205 7.4 QuickPowerofTwo ......................... 216 8 Higher Level Algorithms 219 8.1 IntegerDivisionwithRemainder . 219 8.1.1 QuotientEstimation . 221 8.1.2 NormalizedIntegers . 222 8.1.3 Radix-β DivisionwithRemainder . 222 8.2 SingleDigitHelpers . 233 8.2.1 Single Digit Addition and Subtraction . 233 8.2.2 SingleDigitMultiplication . 236 8.2.3 SingleDigitDivision . 239 8.2.4 SingleDigitRootExtraction . 243 8.3 RandomNumberGeneration . 245 8.4 FormattedRepresentations . 248 8.4.1 ReadingRadix-nInput . 248 8.4.2 Generating Radix-n Output................. 252 9 Number Theoretic Algorithms 257 9.1 GreatestCommonDivisor . 257 9.1.1 CompleteGreatestCommonDivisor . 260 9.2 LeastCommonMultiple . 265 9.3 JacobiSymbolComputation. 267 9.3.1 JacobiSymbol ........................ 268 9.4 ModularInverse ........................... 274 9.4.1 GeneralCase ......................... 275 9.5 PrimalityTests ............................ 278 9.5.1 TrialDivision. 278 9.5.2 TheFermatTest . 282 9.5.3 TheMiller-RabinTest . 284 List of Figures 1.1 Typical Data Types for the C Programming Language . 2 1.2 ExerciseScoringSystem . 8 2.1 Design Flow of the First Few Original LibTomMath Functions. 14 2.2 The mp intStructure ........................ 16 2.3 LibTomMathErrorCodes . 18 2.4 Algorithm mp init .......................... 19 2.5 Algorithm mp clear.......................... 22 2.6 Algorithm mp grow.......................... 25 2.7 Algorithm mp init size........................ 27 2.8 Algorithm mp init multi....................... 30 2.9 Algorithm mp clamp......................... 32 3.1 Algorithm mp copy.......................... 36 3.2 Algorithm mp init copy ....................... 40 3.3 Algorithm mp zero.......................... 41 3.4 Algorithm mp abs .......................... 42 3.5 Algorithm mp neg .......................... 44 3.6 Algorithm mp set........................... 46 3.7 Algorithm mp set int......................... 47 3.8 ComparisonReturnCodes. 49 3.9 Algorithm mp cmp mag....................... 49 3.10 Algorithm mp cmp.......................... 51 4.1 Algorithm s mp add ......................... 57 4.2 Algorithm s mp sub ......................... 62 4.3 Algorithm mp add .......................... 66 4.4 AdditionGuideChart . 67 ix 4.5 Algorithm mp sub .......................... 69 4.6 SubtractionGuideChart. 69 4.7 Algorithm mp mul 2......................... 72 4.8 Algorithm mp div 2 ......................... 75 4.9 Algorithm mp lshd.......................... 78 4.10 SlidingWindowMovement . 79 4.11 Algorithm mp rshd.......................... 81 4.12 Algorithm mp mul 2d ........................ 84 4.13 Algorithm mp div 2d......................... 88 4.14 Algorithm mp mod 2d........................ 91 5.1 Algorithm s mp mul digs ...................... 97 5.2 Long-HandMultiplicationDiagram . 98 5.3 CombaMultiplicationDiagram . 103 5.4 AlgorithmCombaFixup. 103 5.5 Algorithm fast s mp mul digs.................... 105 5.6 Asymptotic Running Time of Polynomial Basis Multiplication . 110 5.7 Algorithm mp karatsuba mul ....................113 5.8 Algorithm mp toom mul.......................119 5.9 Algorithm mp toom mul(continued) . 120 5.10 Algorithm mp mul ..........................128 5.11 SquaringOptimizationDiagram. 130 5.12 Algorithm s mp sqr..........................132 5.13 Algorithm fast s mp sqr .......................135 5.14 Algorithm mp karatsuba sqr.....................140 5.15 Algorithm mp sqr ..........................145 6.1 Algorithm mp reduce......................... 155 6.2 Algorithm mp reduce setup ..................... 159 6.3 AlgorithmMontgomeryReduction . 161 6.4 ExampleofMontgomeryReduction(I). 161 6.5 Algorithm Montgomery Reduction (modified I) . 162 6.6 ExampleofMontgomeryReduction(II) . 162 6.7 Algorithm Montgomery Reduction (modified II). 163 6.8 ExampleofMontgomeryReduction. 164 6.9 Algorithm mp montgomery reduce ................. 165 6.10 Algorithm fast mp montgomery reduce............... 170 6.11 Algorithm mp montgomery setup.................. 176 6.12 Algorithm Diminished Radix Reduction . 178 6.13 ExampleDiminishedRadixReduction . 179 6.14 Algorithm mp dr reduce....................... 181 6.15 Algorithm mp dr setup .......................184 6.16 Algorithm mp dr is modulus .................... 185 6.17 Algorithm mp reduce 2k.......................187 6.18 Algorithm mp reduce 2k setup ................... 188 6.19 Algorithm mp reduce is 2k .....................190 7.1 LefttoRightExponentiation . 194 7.2 ExampleofLefttoRightExponentiation . 195 7.3 Algorithm mp expt d.........................196 7.4 k-aryExponentiation. 199 7.5 Optimal Values of k for k-aryExponentiation . 200 7.6 Optimal Values of k for Sliding Window Exponentiation . 200 7.7 Sliding Window k-aryExponentiation . 201 7.8 Algorithm mp exptmod ......................
Details
- 
                                File Typepdf
 - 
                                Upload Time-
 - 
                                Content LanguagesEnglish
 - 
                                Upload UserAnonymous/Not logged-in
 - 
                                File Pages307 Page
 - 
                                File Size-