mul.txt
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· 文本 代码 · 共 110 行
TXT
110 行
MultiplicationThis describes the multiplication algorithm used by the MPI library.This is basically a standard "schoolbook" algorithm. It is slow --O(mn) for m = #a, n = #b -- but easy to implement and verify.Basically, we run two nested loops, as illustrated here (R is theradix):k = 0for j <- 0 to (#b - 1) for i <- 0 to (#a - 1) w = (a[j] * b[i]) + k + c[i+j] c[i+j] = w mod R k = w div R endfor c[i+j] = k; k = 0;endforIt is necessary that 'w' have room for at least two radix R digits.The product of any two digits in radix R is at most: (R - 1)(R - 1) = R^2 - 2R + 1Since a two-digit radix-R number can hold R^2 - 1 distinct values,this insures that the product will fit into the two-digit register.To insure that two digits is enough for w, we must also show thatthere is room for the carry-in from the previous multiplication, andthe current value of the product digit that is being recomputed.Assuming each of these may be as big as R - 1 (and no larger,certainly), two digits will be enough if and only if: (R^2 - 2R + 1) + 2(R - 1) <= R^2 - 1Solving this equation shows that, indeed, this is the case: R^2 - 2R + 1 + 2R - 2 <= R^2 - 1 R^2 - 1 <= R^2 - 1This suggests that a good radix would be one more than the largestvalue that can be held in half a machine word -- so, for example, asin this implementation, where we used a radix of 65536 on a machinewith 4-byte words. Another advantage of a radix of this sort is thatbinary-level operations are easy on numbers in this representation.Here's an example multiplication worked out longhand in radix-10,using the above algorithm: a = 999 b = x 999 ------------- p = 98001w = (a[jx] * b[ix]) + kin + c[ix + jx]c[ix+jx] = w % RADIXk = w / RADIX productix jx a[jx] b[ix] kin w c[i+j] kout 0000000 0 9 9 0 81+0+0 1 8 0000010 1 9 9 8 81+8+0 9 8 0000910 2 9 9 8 81+8+0 9 8 000991 8 0 0089911 0 9 9 0 81+0+9 0 9 0089011 1 9 9 9 81+9+9 9 9 0089011 2 9 9 9 81+9+8 8 9 008901 9 0 0989012 0 9 9 0 81+0+9 0 9 0980012 1 9 9 9 81+9+8 8 9 0980012 2 9 9 9 81+9+9 9 9 098001------------------------------------------------------------------The contents of this file are subject to the Mozilla PublicLicense Version 1.1 (the "License"); you may not use this fileexcept in compliance with the License. You may obtain a copy ofthe License at http://www.mozilla.org/MPL/Software distributed under the License is distributed on an "ASIS" basis, WITHOUT WARRANTY OF ANY KIND, either express orimplied. See the License for the specific language governingrights and limitations under the License.The Original Code is the MPI Arbitrary Precision Integer Arithmeticlibrary.The Initial Developer of the Original Code is Michael J. Fromberger <sting@linguist.dartmouth.edu>Portions created by Michael J. Fromberger are Copyright (C) 1998, 2000 Michael J. Fromberger. All Rights Reserved.Contributor(s):Alternatively, the contents of this file may be used under theterms of the GNU General Public License Version 2 or later (the"GPL"), in which case the provisions of the GPL are applicableinstead of those above. If you wish to allow use of yourversion of this file only under the terms of the GPL and not toallow others to use your version of this file under the MPL,indicate your decision by deleting the provisions above andreplace them with the notice and other provisions required bythe GPL. If you do not delete the provisions above, a recipientmay use your version of this file under either the MPL or the GPL.$Id: mul.txt,v 1.1 2000/07/14 00:44:35 nelsonb%netscape.com Exp $
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?