155714Skris/* crypto/bn/bn_lcl.h */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296465Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296465Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296465Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296465Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296465Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5868651Skris/* ==================================================================== 5968651Skris * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 6068651Skris * 6168651Skris * Redistribution and use in source and binary forms, with or without 6268651Skris * modification, are permitted provided that the following conditions 6368651Skris * are met: 6468651Skris * 6568651Skris * 1. Redistributions of source code must retain the above copyright 66296465Sdelphij * notice, this list of conditions and the following disclaimer. 6768651Skris * 6868651Skris * 2. Redistributions in binary form must reproduce the above copyright 6968651Skris * notice, this list of conditions and the following disclaimer in 7068651Skris * the documentation and/or other materials provided with the 7168651Skris * distribution. 7268651Skris * 7368651Skris * 3. All advertising materials mentioning features or use of this 7468651Skris * software must display the following acknowledgment: 7568651Skris * "This product includes software developed by the OpenSSL Project 7668651Skris * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 7768651Skris * 7868651Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 7968651Skris * endorse or promote products derived from this software without 8068651Skris * prior written permission. For written permission, please contact 8168651Skris * openssl-core@openssl.org. 8268651Skris * 8368651Skris * 5. Products derived from this software may not be called "OpenSSL" 8468651Skris * nor may "OpenSSL" appear in their names without prior written 8568651Skris * permission of the OpenSSL Project. 8668651Skris * 8768651Skris * 6. Redistributions of any form whatsoever must retain the following 8868651Skris * acknowledgment: 8968651Skris * "This product includes software developed by the OpenSSL Project 9068651Skris * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 9168651Skris * 9268651Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 9368651Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9468651Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 9568651Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 9668651Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 9768651Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 9868651Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 9968651Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 10068651Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 10168651Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10268651Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 10368651Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 10468651Skris * ==================================================================== 10568651Skris * 10668651Skris * This product includes cryptographic software written by Eric Young 10768651Skris * (eay@cryptsoft.com). This product includes software written by Tim 10868651Skris * Hudson (tjh@cryptsoft.com). 10968651Skris * 11068651Skris */ 11155714Skris 11255714Skris#ifndef HEADER_BN_LCL_H 113296465Sdelphij# define HEADER_BN_LCL_H 11455714Skris 115296465Sdelphij# include <openssl/bn.h> 11655714Skris 11755714Skris#ifdef __cplusplus 11855714Skrisextern "C" { 11955714Skris#endif 12055714Skris 121296465Sdelphij/*- 12268651Skris * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions 12368651Skris * 12468651Skris * 12568651Skris * For window size 'w' (w >= 2) and a random 'b' bits exponent, 12668651Skris * the number of multiplications is a constant plus on average 12768651Skris * 12868651Skris * 2^(w-1) + (b-w)/(w+1); 12968651Skris * 13068651Skris * here 2^(w-1) is for precomputing the table (we actually need 13168651Skris * entries only for windows that have the lowest bit set), and 13268651Skris * (b-w)/(w+1) is an approximation for the expected number of 13368651Skris * w-bit windows, not counting the first one. 13468651Skris * 13568651Skris * Thus we should use 13668651Skris * 13768651Skris * w >= 6 if b > 671 13868651Skris * w = 5 if 671 > b > 239 13968651Skris * w = 4 if 239 > b > 79 14068651Skris * w = 3 if 79 > b > 23 14168651Skris * w <= 2 if 23 > b 14268651Skris * 14368651Skris * (with draws in between). Very small exponents are often selected 14468651Skris * with low Hamming weight, so we use w = 1 for b <= 23. 14568651Skris */ 146296465Sdelphij# if 1 147296465Sdelphij# define BN_window_bits_for_exponent_size(b) \ 148296465Sdelphij ((b) > 671 ? 6 : \ 149296465Sdelphij (b) > 239 ? 5 : \ 150296465Sdelphij (b) > 79 ? 4 : \ 151296465Sdelphij (b) > 23 ? 3 : 1) 152296465Sdelphij# else 153296465Sdelphij/* 154296465Sdelphij * Old SSLeay/OpenSSL table. Maximum window size was 5, so this table differs 155296465Sdelphij * for b==1024; but it coincides for other interesting values (b==160, 156296465Sdelphij * b==512). 15768651Skris */ 158296465Sdelphij# define BN_window_bits_for_exponent_size(b) \ 159296465Sdelphij ((b) > 255 ? 5 : \ 160296465Sdelphij (b) > 127 ? 4 : \ 161296465Sdelphij (b) > 17 ? 3 : 1) 162296465Sdelphij# endif 16368651Skris 164296465Sdelphij/* 165296465Sdelphij * BN_mod_exp_mont_conttime is based on the assumption that the L1 data cache 166296465Sdelphij * line width of the target processor is at least the following value. 167160814Ssimon */ 168296465Sdelphij# define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH ( 64 ) 169296465Sdelphij# define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1) 170160814Ssimon 171296465Sdelphij/* 172296465Sdelphij * Window sizes optimized for fixed window size modular exponentiation 173296465Sdelphij * algorithm (BN_mod_exp_mont_consttime). To achieve the security goals of 174296465Sdelphij * BN_mode_exp_mont_consttime, the maximum size of the window must not exceed 175296465Sdelphij * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). Window size thresholds are 176296465Sdelphij * defined for cache line sizes of 32 and 64, cache line sizes where 177296465Sdelphij * log_2(32)=5 and log_2(64)=6 respectively. A window size of 7 should only be 178296465Sdelphij * used on processors that have a 128 byte or greater cache line size. 179160814Ssimon */ 180296465Sdelphij# if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64 181160814Ssimon 182160814Ssimon# define BN_window_bits_for_ctime_exponent_size(b) \ 183296465Sdelphij ((b) > 937 ? 6 : \ 184296465Sdelphij (b) > 306 ? 5 : \ 185296465Sdelphij (b) > 89 ? 4 : \ 186296465Sdelphij (b) > 22 ? 3 : 1) 187296465Sdelphij# define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (6) 188160814Ssimon 189296465Sdelphij# elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32 190160814Ssimon 191160814Ssimon# define BN_window_bits_for_ctime_exponent_size(b) \ 192296465Sdelphij ((b) > 306 ? 5 : \ 193296465Sdelphij (b) > 89 ? 4 : \ 194296465Sdelphij (b) > 22 ? 3 : 1) 195296465Sdelphij# define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE (5) 196160814Ssimon 197296465Sdelphij# endif 198160814Ssimon 19955714Skris/* Pentium pro 16,16,16,32,64 */ 20055714Skris/* Alpha 16,16,16,16.64 */ 201296465Sdelphij# define BN_MULL_SIZE_NORMAL (16)/* 32 */ 202296465Sdelphij# define BN_MUL_RECURSIVE_SIZE_NORMAL (16)/* 32 less than */ 203296465Sdelphij# define BN_SQR_RECURSIVE_SIZE_NORMAL (16)/* 32 */ 204296465Sdelphij# define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL (32)/* 32 */ 205296465Sdelphij# define BN_MONT_CTX_SET_SIZE_WORD (64)/* 32 */ 20655714Skris 207296465Sdelphij# if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC) 20859191Skris/* 20959191Skris * BN_UMULT_HIGH section. 21059191Skris * 21159191Skris * No, I'm not trying to overwhelm you when stating that the 21259191Skris * product of N-bit numbers is 2*N bits wide:-) No, I don't expect 21359191Skris * you to be impressed when I say that if the compiler doesn't 21459191Skris * support 2*N integer type, then you have to replace every N*N 21559191Skris * multiplication with 4 (N/2)*(N/2) accompanied by some shifts 21659191Skris * and additions which unavoidably results in severe performance 21759191Skris * penalties. Of course provided that the hardware is capable of 21859191Skris * producing 2*N result... That's when you normally start 21959191Skris * considering assembler implementation. However! It should be 22059191Skris * pointed out that some CPUs (most notably Alpha, PowerPC and 22159191Skris * upcoming IA-64 family:-) provide *separate* instruction 22259191Skris * calculating the upper half of the product placing the result 22359191Skris * into a general purpose register. Now *if* the compiler supports 22459191Skris * inline assembler, then it's not impossible to implement the 22559191Skris * "bignum" routines (and have the compiler optimize 'em) 22659191Skris * exhibiting "native" performance in C. That's what BN_UMULT_HIGH 22759191Skris * macro is about:-) 22859191Skris * 229296465Sdelphij * <appro@fy.chalmers.se> 23059191Skris */ 231296465Sdelphij# if defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT)) 232296465Sdelphij# if defined(__DECC) 233296465Sdelphij# include <c_asm.h> 234296465Sdelphij# define BN_UMULT_HIGH(a,b) (BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b)) 235296465Sdelphij# elif defined(__GNUC__) 236296465Sdelphij# define BN_UMULT_HIGH(a,b) ({ \ 237296465Sdelphij register BN_ULONG ret; \ 238296465Sdelphij asm ("umulh %1,%2,%0" \ 239296465Sdelphij : "=r"(ret) \ 240296465Sdelphij : "r"(a), "r"(b)); \ 241296465Sdelphij ret; }) 242296465Sdelphij# endif /* compiler */ 243296465Sdelphij# elif defined(_ARCH_PPC) && defined(__64BIT__) && defined(SIXTY_FOUR_BIT_LONG) 244296465Sdelphij# if defined(__GNUC__) 245296465Sdelphij# define BN_UMULT_HIGH(a,b) ({ \ 246296465Sdelphij register BN_ULONG ret; \ 247296465Sdelphij asm ("mulhdu %0,%1,%2" \ 248296465Sdelphij : "=r"(ret) \ 249296465Sdelphij : "r"(a), "r"(b)); \ 250296465Sdelphij ret; }) 251296465Sdelphij# endif /* compiler */ 252296465Sdelphij# elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG) 253296465Sdelphij# if defined(__GNUC__) 254296465Sdelphij# define BN_UMULT_HIGH(a,b) ({ \ 255296465Sdelphij register BN_ULONG ret,discard; \ 256296465Sdelphij asm ("mulq %3" \ 257296465Sdelphij : "=a"(discard),"=d"(ret) \ 258296465Sdelphij : "a"(a), "g"(b) \ 259296465Sdelphij : "cc"); \ 260296465Sdelphij ret; }) 261296465Sdelphij# define BN_UMULT_LOHI(low,high,a,b) \ 262296465Sdelphij asm ("mulq %3" \ 263296465Sdelphij : "=a"(low),"=d"(high) \ 264296465Sdelphij : "a"(a),"g"(b) \ 265296465Sdelphij : "cc"); 266296465Sdelphij# endif 267296465Sdelphij# elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT) 268296465Sdelphij# if defined(_MSC_VER) && _MSC_VER>=1400 269296465Sdelphijunsigned __int64 __umulh(unsigned __int64 a, unsigned __int64 b); 270296465Sdelphijunsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b, 271296465Sdelphij unsigned __int64 *h); 272296465Sdelphij# pragma intrinsic(__umulh,_umul128) 273296465Sdelphij# define BN_UMULT_HIGH(a,b) __umulh((a),(b)) 274296465Sdelphij# define BN_UMULT_LOHI(low,high,a,b) ((low)=_umul128((a),(b),&(high))) 275296465Sdelphij# endif 276296465Sdelphij# endif /* cpu */ 277296465Sdelphij# endif /* OPENSSL_NO_ASM */ 27855714Skris 27955714Skris/************************************************************* 28055714Skris * Using the long long type 28155714Skris */ 282296465Sdelphij# define Lw(t) (((BN_ULONG)(t))&BN_MASK2) 283296465Sdelphij# define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2) 28455714Skris 285296465Sdelphij# ifdef BN_DEBUG_RAND 286296465Sdelphij# define bn_clear_top2max(a) \ 287296465Sdelphij { \ 288296465Sdelphij int ind = (a)->dmax - (a)->top; \ 289296465Sdelphij BN_ULONG *ftl = &(a)->d[(a)->top-1]; \ 290296465Sdelphij for (; ind != 0; ind--) \ 291296465Sdelphij *(++ftl) = 0x0; \ 292296465Sdelphij } 293296465Sdelphij# else 294296465Sdelphij# define bn_clear_top2max(a) 295296465Sdelphij# endif 29655714Skris 297296465Sdelphij# ifdef BN_LLONG 298296465Sdelphij# define mul_add(r,a,w,c) { \ 299296465Sdelphij BN_ULLONG t; \ 300296465Sdelphij t=(BN_ULLONG)w * (a) + (r) + (c); \ 301296465Sdelphij (r)= Lw(t); \ 302296465Sdelphij (c)= Hw(t); \ 303296465Sdelphij } 30455714Skris 305296465Sdelphij# define mul(r,a,w,c) { \ 306296465Sdelphij BN_ULLONG t; \ 307296465Sdelphij t=(BN_ULLONG)w * (a) + (c); \ 308296465Sdelphij (r)= Lw(t); \ 309296465Sdelphij (c)= Hw(t); \ 310296465Sdelphij } 31155714Skris 312296465Sdelphij# define sqr(r0,r1,a) { \ 313296465Sdelphij BN_ULLONG t; \ 314296465Sdelphij t=(BN_ULLONG)(a)*(a); \ 315296465Sdelphij (r0)=Lw(t); \ 316296465Sdelphij (r1)=Hw(t); \ 317296465Sdelphij } 31859191Skris 319296465Sdelphij# elif defined(BN_UMULT_LOHI) 320296465Sdelphij# define mul_add(r,a,w,c) { \ 321296465Sdelphij BN_ULONG high,low,ret,tmp=(a); \ 322296465Sdelphij ret = (r); \ 323296465Sdelphij BN_UMULT_LOHI(low,high,w,tmp); \ 324296465Sdelphij ret += (c); \ 325296465Sdelphij (c) = (ret<(c))?1:0; \ 326296465Sdelphij (c) += high; \ 327296465Sdelphij ret += low; \ 328296465Sdelphij (c) += (ret<low)?1:0; \ 329296465Sdelphij (r) = ret; \ 330296465Sdelphij } 331160814Ssimon 332296465Sdelphij# define mul(r,a,w,c) { \ 333296465Sdelphij BN_ULONG high,low,ret,ta=(a); \ 334296465Sdelphij BN_UMULT_LOHI(low,high,w,ta); \ 335296465Sdelphij ret = low + (c); \ 336296465Sdelphij (c) = high; \ 337296465Sdelphij (c) += (ret<low)?1:0; \ 338296465Sdelphij (r) = ret; \ 339296465Sdelphij } 340160814Ssimon 341296465Sdelphij# define sqr(r0,r1,a) { \ 342296465Sdelphij BN_ULONG tmp=(a); \ 343296465Sdelphij BN_UMULT_LOHI(r0,r1,tmp,tmp); \ 344296465Sdelphij } 345160814Ssimon 346296465Sdelphij# elif defined(BN_UMULT_HIGH) 347296465Sdelphij# define mul_add(r,a,w,c) { \ 348296465Sdelphij BN_ULONG high,low,ret,tmp=(a); \ 349296465Sdelphij ret = (r); \ 350296465Sdelphij high= BN_UMULT_HIGH(w,tmp); \ 351296465Sdelphij ret += (c); \ 352296465Sdelphij low = (w) * tmp; \ 353296465Sdelphij (c) = (ret<(c))?1:0; \ 354296465Sdelphij (c) += high; \ 355296465Sdelphij ret += low; \ 356296465Sdelphij (c) += (ret<low)?1:0; \ 357296465Sdelphij (r) = ret; \ 358296465Sdelphij } 35959191Skris 360296465Sdelphij# define mul(r,a,w,c) { \ 361296465Sdelphij BN_ULONG high,low,ret,ta=(a); \ 362296465Sdelphij low = (w) * ta; \ 363296465Sdelphij high= BN_UMULT_HIGH(w,ta); \ 364296465Sdelphij ret = low + (c); \ 365296465Sdelphij (c) = high; \ 366296465Sdelphij (c) += (ret<low)?1:0; \ 367296465Sdelphij (r) = ret; \ 368296465Sdelphij } 36959191Skris 370296465Sdelphij# define sqr(r0,r1,a) { \ 371296465Sdelphij BN_ULONG tmp=(a); \ 372296465Sdelphij (r0) = tmp * tmp; \ 373296465Sdelphij (r1) = BN_UMULT_HIGH(tmp,tmp); \ 374296465Sdelphij } 37559191Skris 376296465Sdelphij# else 37755714Skris/************************************************************* 37855714Skris * No long long type 37955714Skris */ 38055714Skris 381296465Sdelphij# define LBITS(a) ((a)&BN_MASK2l) 382296465Sdelphij# define HBITS(a) (((a)>>BN_BITS4)&BN_MASK2l) 383296465Sdelphij# define L2HBITS(a) (((a)<<BN_BITS4)&BN_MASK2) 38455714Skris 385296465Sdelphij# define LLBITS(a) ((a)&BN_MASKl) 386296465Sdelphij# define LHBITS(a) (((a)>>BN_BITS2)&BN_MASKl) 387296465Sdelphij# define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2) 38855714Skris 389296465Sdelphij# define mul64(l,h,bl,bh) \ 390296465Sdelphij { \ 391296465Sdelphij BN_ULONG m,m1,lt,ht; \ 39255714Skris \ 393296465Sdelphij lt=l; \ 394296465Sdelphij ht=h; \ 395296465Sdelphij m =(bh)*(lt); \ 396296465Sdelphij lt=(bl)*(lt); \ 397296465Sdelphij m1=(bl)*(ht); \ 398296465Sdelphij ht =(bh)*(ht); \ 399296465Sdelphij m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \ 400296465Sdelphij ht+=HBITS(m); \ 401296465Sdelphij m1=L2HBITS(m); \ 402296465Sdelphij lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \ 403296465Sdelphij (l)=lt; \ 404296465Sdelphij (h)=ht; \ 405296465Sdelphij } 40655714Skris 407296465Sdelphij# define sqr64(lo,ho,in) \ 408296465Sdelphij { \ 409296465Sdelphij BN_ULONG l,h,m; \ 41055714Skris \ 411296465Sdelphij h=(in); \ 412296465Sdelphij l=LBITS(h); \ 413296465Sdelphij h=HBITS(h); \ 414296465Sdelphij m =(l)*(h); \ 415296465Sdelphij l*=l; \ 416296465Sdelphij h*=h; \ 417296465Sdelphij h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \ 418296465Sdelphij m =(m&BN_MASK2l)<<(BN_BITS4+1); \ 419296465Sdelphij l=(l+m)&BN_MASK2; if (l < m) h++; \ 420296465Sdelphij (lo)=l; \ 421296465Sdelphij (ho)=h; \ 422296465Sdelphij } 42355714Skris 424296465Sdelphij# define mul_add(r,a,bl,bh,c) { \ 425296465Sdelphij BN_ULONG l,h; \ 42655714Skris \ 427296465Sdelphij h= (a); \ 428296465Sdelphij l=LBITS(h); \ 429296465Sdelphij h=HBITS(h); \ 430296465Sdelphij mul64(l,h,(bl),(bh)); \ 43155714Skris \ 432296465Sdelphij /* non-multiply part */ \ 433296465Sdelphij l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 434296465Sdelphij (c)=(r); \ 435296465Sdelphij l=(l+(c))&BN_MASK2; if (l < (c)) h++; \ 436296465Sdelphij (c)=h&BN_MASK2; \ 437296465Sdelphij (r)=l; \ 438296465Sdelphij } 43955714Skris 440296465Sdelphij# define mul(r,a,bl,bh,c) { \ 441296465Sdelphij BN_ULONG l,h; \ 44255714Skris \ 443296465Sdelphij h= (a); \ 444296465Sdelphij l=LBITS(h); \ 445296465Sdelphij h=HBITS(h); \ 446296465Sdelphij mul64(l,h,(bl),(bh)); \ 44755714Skris \ 448296465Sdelphij /* non-multiply part */ \ 449296465Sdelphij l+=(c); if ((l&BN_MASK2) < (c)) h++; \ 450296465Sdelphij (c)=h&BN_MASK2; \ 451296465Sdelphij (r)=l&BN_MASK2; \ 452296465Sdelphij } 453296465Sdelphij# endif /* !BN_LLONG */ 45455714Skris 455296465Sdelphijvoid bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb); 456296465Sdelphijvoid bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 457296465Sdelphijvoid bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b); 458109998Smarkmvoid bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp); 459296465Sdelphijvoid bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a); 460296465Sdelphijvoid bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a); 461296465Sdelphijint bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n); 462296465Sdelphijint bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl); 463296465Sdelphijvoid bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, 464296465Sdelphij int dna, int dnb, BN_ULONG *t); 465296465Sdelphijvoid bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, 466296465Sdelphij int n, int tna, int tnb, BN_ULONG *t); 467296465Sdelphijvoid bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t); 468296465Sdelphijvoid bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n); 469296465Sdelphijvoid bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, 470296465Sdelphij BN_ULONG *t); 471296465Sdelphijvoid bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, 472296465Sdelphij BN_ULONG *t); 473160814SsimonBN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 474296465Sdelphij int cl, int dl); 475160814SsimonBN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, 476296465Sdelphij int cl, int dl); 477296465Sdelphijint bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, 478296465Sdelphij const BN_ULONG *np, const BN_ULONG *n0, int num); 47955714Skris 48055714Skris#ifdef __cplusplus 48155714Skris} 48255714Skris#endif 48355714Skris 48455714Skris#endif 485