bn_sqr.c revision 65c4736d9c0ebc6d9b1d991593b55566909da9cd
ddad35552931651426ad70912d29d9ab7d2a8d80Automatic Updater/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * All rights reserved.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * This package is an SSL implementation written
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * by Eric Young (eay@cryptsoft.com).
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * The implementation was written so as to conform with Netscapes SSL.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * This library is free for commercial and non-commercial use as long as
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * the following conditions are aheared to. The following conditions
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * apply to all code found in this distribution, be it the RC4, RSA,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * lhash, DES, etc., code; not just the SSL code. The SSL documentation
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * included with this distribution is covered by the same copyright terms
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * except that the holder is Tim Hudson (tjh@cryptsoft.com).
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * Copyright remains Eric Young's, and as such any Copyright notices in
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * the code are not to be removed.
321b1c0501d6d97c852ae02e13bdfef6f0bbe776Automatic Updater * If this package is used in a product, Eric Young should be given attribution
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * as the author of the parts of the library used.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * This can be in the form of a textual message at program startup or
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * in documentation (online or textual) provided with the package.
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * Redistribution and use in source and binary forms, with or without
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * modification, are permitted provided that the following conditions
07072c9456f1112705db701bb35f12dbdcb217b0Mark Andrews * 1. Redistributions of source code must retain the copyright
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * notice, this list of conditions and the following disclaimer.
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * 2. Redistributions in binary form must reproduce the above copyright
80617c8faa03b2c425e320597eb768073fd0b987Michael Graff * notice, this list of conditions and the following disclaimer in the
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff * documentation and/or other materials provided with the distribution.
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff * 3. All advertising materials mentioning features or use of this software
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * must display the following acknowledgement:
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * "This product includes cryptographic software written by
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * Eric Young (eay@cryptsoft.com)"
3d181bc9f12154a56bfbb536198a6c481cbcd525David Lawrence * The word 'cryptographic' can be left out if the rouines from the library
32eddfc189108fa93e31761e13150594c7a79d2bDavid Lawrence * being used are not cryptographic related :-).
a095cd0ef599eb04d74fb9f9bc46ae13081c5bcbMark Andrews * 4. If you include any Windows specific code (or a derivative thereof) from
440be4c866f6935ac069db79a414304507a664c2Michael Graff * the apps directory (application code) you must include an acknowledgement:
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20c266cbc999c724e03e6edd437fb4181b92f095Michael Graff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8d1943e8ffa991d54c5406342e44d7134762e7eaMichael Graff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
b312748a11d27fe387984973ba79975a9d6863c4Mark Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8907d8fa04fdaa65baf0bc6b01230b2ebde93106Mark Andrews * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
c6066a8e67f40c7c12925c5634485f55713c06d6Michael Graff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews * SUCH DAMAGE.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * The licence and distribution terms for any publically available version or
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * derivative of this code cannot be changed. i.e. this code cannot simply be
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * copied and put under another distribution licence
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * [including the GNU Public Licence.]
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews/* r must not be a */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (bn_wexpand(rr,max+1) == NULL) return(0);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff if ((max > 0) && (rr->d[max-1] == 0)) rr->top--;
ab0e5066083abcbec62513a3cc041d1f1eb9098aMichael Graff/* tmp must have 2*n words */
1ec39fc7a8b6ad92de3363d4c50b75e24fcd6accMichael Graffvoid bn_sqr_normal(BN_ULONG *r, BN_ULONG *a, int n, BN_ULONG *tmp)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 for (i=n-2; i>0; i--)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /* There will not be a carry */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉/* r is 2*n words in size,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * a and b are both n words in size.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * n must be a power of 2.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * We multiply and return the result.
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * t must be 2*n words in size
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉void bn_sqr_recursive(BN_ULONG *r, BN_ULONG *a, int n2, BN_ULONG *t)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein /* r=(a[0]-a[1])*(a[1]-a[0]) */
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff bn_sub_words(t,a,&(a[n]),n);
83f8c56f43852bf9a9c6964eae285284b23f9d8dMichael Graff else if (c1 < 0)
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews bn_sub_words(t,&(a[n]),a,n);
ca9739800f045cd4d39014f98b920d4354b5bd14Michael Graff /* The result will always be negative unless it is zero */
a8dcebd0419f27234664e89b9cd48bc54cad08a7Michael Graff /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews * r[10] holds (a[0]*b[0])
a7c76f1924d5fc914c579fd3b0276ffbddd2f65aMark Andrews * r[32] holds (b[1]*b[1])
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /* t[32] is negative */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * r[10] holds (a[0]*a[0])
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * r[32] holds (a[1]*a[1])
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * c1 holds the carry bits
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));
35f06ab0e6d5ad26176b7584de7b4d405272ba68Brian Wellington /* The overflow will stop before we over write
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 * words we should not overwrite */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 } while (ln == 0);