Multi–Precision Math

Total Page:16

File Type:pdf, Size:1020Kb

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
Recommended publications
  • Representing Information in English Until 1598
    In the twenty-first century, digital computers are everywhere. You al- most certainly have one with you now, although you may call it a “phone.” You are more likely to be reading this book on a digital device than on paper. We do so many things with our digital devices that we may forget the original purpose: computation. When Dr. Howard Figure 1-1 Aiken was in charge of the huge, Howard Aiken, center, and Grace Murray Hopper, mechanical Harvard Mark I calcu- on Aiken’s left, with other members of the Bureau of Ordnance Computation Project, in front of the lator during World War II, he Harvard Mark I computer at Harvard University, wasn’t happy unless the machine 1944. was “making numbers,” that is, U.S. Department of Defense completing the numerical calculations needed for the war effort. We still use computers for purely computational tasks, to make numbers. It may be less ob- vious, but the other things for which we use computers today are, underneath, also computational tasks. To make that phone call, your phone, really a com- puter, converts an electrical signal that’s an analog of your voice to numbers, encodes and compresses them, and passes them onward to a cellular radio which it itself is controlled by numbers. Numbers are fundamental to our digital devices, and so to understand those digital devices, we have to understand the numbers that flow through them. 1.1 Numbers as Abstractions It is easy to suppose that as soon as humans began to accumulate possessions, they wanted to count them and make a record of the count.
    [Show full text]
  • ACP Course07
    Applied Computer Programming Representation of Numbers. Bitwise Operators Course 07 Lect.eng. Adriana ALBU, PhD Politehnica University Timisoara Internal representation • All data, of any type, processed by a computer must have the same fundamental representation • In order to work correctly with these data, a programmer should understand: – the representation of numbers and their memory storage – the fundamental manner a computer operates with numbers (bitwise operations) Representation of numbers Binary Octal Hexadecimal Representation of numbers • Most of our programs don’t need concern regarding operations at the bit level • We’re free to think in bytes, or integers and doubles, or even higher level data types composed of a combination of these • But there are times when we'd like to be able to go to the level of an individual bit Representation of numbers • Any value (variable or constant) that is processed by our programs needs a memory space and it has a memory representation (sequence of bits) • Bit – unit of data storage – may have one of two values: 0 or 1 – usually is not individually addressable • Byte – unit of addressable data – in all usual computer architectures it contains 8 bits (CHAR_BIT from limits.h specifies its dimension) Representation of numbers • The byte is the lowest level at which we can access data; there's no "bit" type, and we can't ask for an individual bit • In fact, we can't even perform operations on a single bit – every bitwise operator will be applied to, at a minimum, an entire byte at a time • This
    [Show full text]
  • Binary Numbers Bob Brown Information Technology Department Southern Polytechnic State University
    Binary Numbers Bob Brown Information Technology Department Southern Polytechnic State University Positional Number Systems The idea of “number” is a mathematical abstraction. To use numbers, we must represent them in some way, whether by piles of pebbles or in some other way. It is common to create a code for representing numbers. Such codes are called number systems. Every number system uses symbols to convey information about the value of a number. A positional (or radix) number system is one in which the value that a symbol contributes to a number is determined by the both symbol itself and the position of the symbol within the number. That's just a fancy way of saying that 300 is far different from 3. Compare the idea of a positional number system with Roman numbers, where X means 10 no matter where in a number it appears. The decimal number system we use every day is a positional number system. Decimal numbers are also called base 10 or radix 10 numbers. The symbols are the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, the plus and minus signs, and the period or decimal point. The position of each digit within the number tells us the multiplier used with it. Consider the number 1037. We learned in elementary school to call the rightmost digit the ones place, the next digit the tens place, then the hundreds place, then the thousands place, 3 2 1 0 and so on. Mathematically, 1037 means 110 + 010 + 310 + 710 . Each digit in the number is multiplied by some power of ten, starting with 100 at the right and increasing by one for each position to the left.
    [Show full text]
  • On Two Conversion Methods of Decimal-To-Binary
    On Two Conversion Methods of Decimal-to-Binary Zhengjun Cao Department of Mathematics, Shanghai University, Shanghai, China. [email protected] Abstract Decimal-to-binary conversion is important to modern binary computers. The classical method to solve this problem is based on division operation. In this paper, we investigate a decimal-to-binary conversion method based on addition operation. The method is very easily implemented by software. The cost analysis shows that the latter is more preferable than the classical method. Thus the current Input/Output translation hardware to convert between the internal digit pairs and the external standard BCD codes can be reasonably removed. Keywords. Decimal-to-binary conversion; division operation; addition operation. 1 Introduction IF OUR ANCESTORS had invented arithmetic by counting with their two fists or their eight fingers, instead of their ten “digits”, we would never have to worry about writing binary-decimal conversion routines. — Donald E. Knuth arXiv:1308.0555v1 [cs.OH] 1 Aug 2013 We know a digit a ∈ {0, 1, · · · , 9} received from a numeric key on a keyboard (see Picture 1) is represented as a string of 4 bits. The mechanism is usually referred to as Binary Coded Decimal (BCD for short). For instance, the digit 9 is represented as 1001 . When we input the integer 79 using numeric keys ‘7’ and ‘9’, it is first represented as two strings 0111 1001 . We here omit the other leftmost bits in each byte which represent numeric type or sign. The two strings then will be converted into the resulting binary representation 1001111 . In 1988.
    [Show full text]
  • Multi–Precision Math
    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.............................
    [Show full text]