square.txt
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· 文本 代码 · 共 105 行
TXT
105 行
Squaring AlgorithmWhen you are squaring a value, you can take advantage of the fact thathalf the multiplications performed by the more general multiplicationalgorithm (see 'mul.txt' for a description) are redundant when themultiplicand equals the multiplier.In particular, the modified algorithm is:k = 0for j <- 0 to (#a - 1) w = c[2*j] + (a[j] ^ 2); k = w div R for i <- j+1 to (#a - 1) w = (2 * a[j] * a[i]) + k + c[i+j] c[i+j] = w mod R k = w div R endfor c[i+j] = k; k = 0;endforOn the surface, this looks identical to the multiplication algorithm;however, note the following differences: - precomputation of the leading term in the outer loop - i runs from j+1 instead of from zero - doubling of a[i] * a[j] in the inner productUnfortunately, the construction of the inner product is such that weneed more than two digits to represent the inner product, in somecases. In a C implementation, this means that some gymnastics must beperformed in order to handle overflow, for which C has no directabstraction. We do this by observing the following:If we have multiplied a[i] and a[j], and the product is more than halfthe maximum value expressible in two digits, then doubling this resultwill overflow into a third digit. If this occurs, we take note of theoverflow, and double it anyway -- C integer arithmetic ignoresoverflow, so the two digits we get back should still be valid, modulothe overflow.Having doubled this value, we now have to add in the remainders andthe digits already computed by earlier steps. If we did not overflowin the previous step, we might still cause an overflow here. Thatwill happen whenever the maximum value expressible in two digits, lessthe amount we have to add, is greater than the result of the previousstep. Thus, the overflow computation is: u = 0 w = a[i] * a[j] if(w > (R - 1)/ 2) u = 1; w = w * 2 v = c[i + j] + k if(u == 0 && (R - 1 - v) < w) u = 1If there is an overflow, u will be 1, otherwise u will be 0. The restof the parameters are the same as they are in the above description.------------------------------------------------------------------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: square.txt,v 1.1 2000/07/14 00:44:37 nelsonb%netscape.com Exp $
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?