Multi–Precision Math

Multi–Precision Math

Multi–Precision Math Tom St Denis Algonquin College Mads Rasmussen Open Communications Security Greg Rose QUALCOMM Australia March 10, 2007 This text has been placed in the public domain. This text corresponds to the v0.39 release of the LibTomMath project. Tom St Denis 111 Banning Rd Ottawa, Ontario K2L 1C3 Canada Phone: 1-613-836-3160 Email: [email protected] 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 Multiple Precision Arithmetic . 1 1.1.1 What is Multiple Precision Arithmetic? . 1 1.1.2 The Need for Multiple Precision Arithmetic . 1 1.1.3 Benefits of Multiple Precision Arithmetic . 3 1.2 PurposeofThisText......................... 4 1.3 DiscussionandNotation . 5 1.3.1 Notation............................ 5 1.3.2 PrecisionNotation . 5 1.3.3 Algorithm Inputs and Outputs . 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 Portability and Stability . 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...................... 21 2.6 MaintenanceAlgorithms . 22 2.6.1 Augmenting an mp int’sPrecision . 23 2.6.2 Initializing Variable Precision mp ints ........... 25 2.6.3 Multiple Integer Initializations and Clearings . ... 26 2.6.4 ClampingExcessDigits . 27 3 Basic Operations 31 3.1 Introduction.............................. 31 3.2 Assigning Values to mp intStructures . 31 3.2.1 Copying an mp int...................... 31 3.2.2 CreatingaClone ....................... 34 3.3 ZeroinganInteger .......................... 35 3.4 SignManipulation .......................... 36 3.4.1 AbsoluteValue ........................ 36 3.4.2 IntegerNegation .. .. .. .. .. .. .. .. .. .. 36 3.5 SmallConstants ........................... 37 3.5.1 SettingSmallConstants . 37 3.5.2 SettingLargeConstants . 38 3.6 Comparisons ............................. 39 3.6.1 UnsignedComparisions . 39 3.6.2 SignedComparisons . 41 4 Basic Arithmetic 43 4.1 Introduction.............................. 43 4.2 AdditionandSubtraction . 44 4.2.1 LowLevelAddition . 44 4.2.2 LowLevelSubtraction . 47 4.2.3 HighLevelAddition . 50 4.2.4 HighLevelSubtraction . 52 4.3 BitandDigitShifting . .. .. .. .. .. .. .. .. .. .. 54 4.3.1 MultiplicationbyTwo . 54 4.3.2 DivisionbyTwo ....................... 56 4.4 PolynomialBasisOperations . 57 4.4.1 Multiplication by x ...................... 58 4.4.2 Division by x ......................... 60 4.5 PowersofTwo ............................ 62 4.5.1 Multiplication by Power of Two . 62 4.5.2 DivisionbyPowerofTwo . 64 4.5.3 Remainder of Division by Power of Two . 66 5 Multiplication and Squaring 69 5.1 TheMultipliers............................ 69 5.2 Multiplication............................. 70 5.2.1 TheBaselineMultiplication . 70 5.2.2 Faster Multiplication by the “Comba” Method . 73 5.2.3 Polynomial Basis Multiplication . 79 5.2.4 KaratsubaMultiplication . 81 5.2.5 Toom-Cook3-WayMultiplication. 85 5.2.6 SignedMultiplication . 88 5.3 Squaring................................ 88 5.3.1 TheBaselineSquaringAlgorithm. 90 5.3.2 Faster Squaring by the “Comba” Method . 92 5.3.3 PolynomialBasisSquaring . 94 5.3.4 KaratsubaSquaring . 94 5.3.5 Toom-CookSquaring. 98 5.3.6 HighLevelSquaring . 98 6 Modular Reduction 101 6.1 BasicsofModularReduction . 101 6.2 TheBarrettReduction. 102 6.2.1 FixedPointArithmetic . 102 6.2.2 ChoosingaRadixPoint . 104 6.2.3 TrimmingtheQuotient . 105 6.2.4 TrimmingtheResidue . 106 6.2.5 TheBarrettAlgorithm. 106 6.2.6 TheBarrettSetupAlgorithm . 108 6.3 TheMontgomeryReduction. 109 6.3.1 DigitBasedMontgomeryReduction . 113 6.3.2 BaselineMontgomeryReduction . 114 6.3.3 Faster “Comba” Montgomery Reduction . 116 6.3.4 MontgomerySetup. 120 6.4 The Diminished Radix Algorithm . 120 6.4.1 ChoiceofModuli . 123 6.4.2 Choice of k ..........................123 6.4.3 Restricted Diminished Radix Reduction . 123 6.4.4 Unrestricted Diminished Radix Reduction . 127 6.5 AlgorithmComparison. 130 7 Exponentiation 133 7.1 ExponentiationBasics . 133 7.1.1 SingleDigitExponentiation . 135 7.2 k-aryExponentiation. 137 7.2.1 Optimal Values of k .....................138 7.2.2 Sliding-Window Exponentiation . 138 7.3 ModularExponentiation . 140 7.3.1 BarrettModularExponentiation . 141 7.4 QuickPowerofTwo ......................... 146 8 Higher Level Algorithms 147 8.1 IntegerDivisionwithRemainder . 147 8.1.1 QuotientEstimation . 149 8.1.2 NormalizedIntegers . 150 8.1.3 Radix-β DivisionwithRemainder . 150 8.2 SingleDigitHelpers . 155 8.2.1 Single Digit Addition and Subtraction . 155 8.2.2 Single Digit Multiplication . 156 8.2.3 SingleDigitDivision . 157 8.2.4 SingleDigitRootExtraction . 159 8.3 RandomNumberGeneration . 161 8.4 FormattedRepresentations . 162 8.4.1 ReadingRadix-nInput . 162 8.4.2 Generating Radix-n Output................. 165 9 Number Theoretic Algorithms 169 9.1 GreatestCommonDivisor . 169 9.1.1 Complete GreatestCommonDivisor . 172 9.2 LeastCommonMultiple . 175 9.3 JacobiSymbolComputation. 176 9.3.1 JacobiSymbol ........................ 177 9.4 ModularInverse ........................... 180 9.4.1 GeneralCase ......................... 181 9.5 PrimalityTests ............................ 183 9.5.1 TrialDivision. 184 9.5.2 TheFermatTest . 185 9.5.3 TheMiller-RabinTest . 186 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.......................... 21 2.6 Algorithm mp grow.......................... 24 2.7 Algorithm mp init size........................ 25 2.8 Algorithm mp init multi....................... 27 2.9 Algorithm mp clamp......................... 28 3.1 Algorithm mp copy.......................... 32 3.2 Algorithm mp init copy ....................... 35 3.3 Algorithm mp zero.......................... 35 3.4 Algorithm mp abs .......................... 36 3.5 Algorithm mp neg .......................... 37 3.6 Algorithm mp set........................... 38 3.7 Algorithm mp set int......................... 39 3.8 ComparisonReturnCodes. 40 3.9 Algorithm mp cmp mag....................... 40 3.10 Algorithm mp cmp.......................... 41 4.1 Algorithm s mp add ......................... 45 4.2 Algorithm s mp sub ......................... 48 4.3 Algorithm mp add .......................... 50 4.4 AdditionGuideChart . .. .. .. .. .. .. .. .. .. .. 51 ix 4.5 Algorithm mp sub .......................... 53 4.6 SubtractionGuideChart. 53 4.7 Algorithm mp mul 2......................... 55 4.8 Algorithm mp div 2 ......................... 57 4.9 Algorithm mp lshd.......................... 59 4.10 Sliding Window Movement . 60 4.11 Algorithm mp rshd.......................... 61 4.12 Algorithm mp mul 2d ........................ 63 4.13 Algorithm mp div 2d......................... 65 4.14 Algorithm mp mod 2d........................ 66 5.1 Algorithm s mp mul digs ...................... 71 5.2 Long-HandMultiplicationDiagram. 72 5.3 CombaMultiplicationDiagram . 75 5.4 AlgorithmCombaFixup. 75 5.5 Algorithm fast s mp mul digs.................... 77 5.6 Asymptotic Running Time of Polynomial Basis Multiplication. 80 5.7 Algorithm mp karatsuba mul .................... 83 5.8 Algorithm mp toom mul....................... 86 5.9 Algorithm mp toom mul(continued) . 87 5.10 Algorithm mp mul .......................... 89 5.11 SquaringOptimizationDiagram. 90 5.12 Algorithm s mp sqr.......................... 91 5.13 Algorithm fast s mp sqr ....................... 93 5.14 Algorithm mp karatsuba sqr..................... 96 5.15 Algorithm mp sqr .......................... 99 6.1 Algorithm mp reduce......................... 107 6.2 Algorithm mp reduce setup ..................... 109 6.3 AlgorithmMontgomeryReduction . 111 6.4 ExampleofMontgomeryReduction(I). 111 6.5 Algorithm Montgomery Reduction (modified I) . 112 6.6 ExampleofMontgomeryReduction(II) . 112 6.7 Algorithm Montgomery Reduction (modified II) . 113 6.8 ExampleofMontgomeryReduction. 114 6.9 Algorithm mp montgomery reduce ................. 115 6.10 Algorithm fast mp montgomery reduce............... 118 6.11 Algorithm mp montgomery setup.................. 120 6.12 Algorithm Diminished Radix Reduction . 121 6.13 Example Diminished Radix Reduction . 122 6.14 Algorithm mp dr reduce....................... 125 6.15 Algorithm mp dr setup .......................126 6.16 Algorithm mp dr is modulus .................... 127 6.17 Algorithm mp reduce 2k.......................128 6.18 Algorithm mp reduce 2k setup ................... 129 6.19 Algorithm mp reduce is 2k .....................129 7.1 LefttoRightExponentiation . 134 7.2 ExampleofLefttoRightExponentiation . 135 7.3 Algorithm mp expt

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    207 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us