os/ossrv/ssl/libcrypto/src/crypto/bn/bn_lcl.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/bn/bn_lcl.h */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/* ====================================================================
sl@0
    59
 * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
sl@0
    60
 *
sl@0
    61
 * Redistribution and use in source and binary forms, with or without
sl@0
    62
 * modification, are permitted provided that the following conditions
sl@0
    63
 * are met:
sl@0
    64
 *
sl@0
    65
 * 1. Redistributions of source code must retain the above copyright
sl@0
    66
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    67
 *
sl@0
    68
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    69
 *    notice, this list of conditions and the following disclaimer in
sl@0
    70
 *    the documentation and/or other materials provided with the
sl@0
    71
 *    distribution.
sl@0
    72
 *
sl@0
    73
 * 3. All advertising materials mentioning features or use of this
sl@0
    74
 *    software must display the following acknowledgment:
sl@0
    75
 *    "This product includes software developed by the OpenSSL Project
sl@0
    76
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    77
 *
sl@0
    78
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    79
 *    endorse or promote products derived from this software without
sl@0
    80
 *    prior written permission. For written permission, please contact
sl@0
    81
 *    openssl-core@openssl.org.
sl@0
    82
 *
sl@0
    83
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    84
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    85
 *    permission of the OpenSSL Project.
sl@0
    86
 *
sl@0
    87
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    88
 *    acknowledgment:
sl@0
    89
 *    "This product includes software developed by the OpenSSL Project
sl@0
    90
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    91
 *
sl@0
    92
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    93
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    95
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    96
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    97
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    98
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    99
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
   100
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
   101
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
   102
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
   103
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
   104
 * ====================================================================
sl@0
   105
 *
sl@0
   106
 * This product includes cryptographic software written by Eric Young
sl@0
   107
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
   108
 * Hudson (tjh@cryptsoft.com).
sl@0
   109
 *
sl@0
   110
 */
sl@0
   111
sl@0
   112
#ifndef HEADER_BN_LCL_H
sl@0
   113
#define HEADER_BN_LCL_H
sl@0
   114
sl@0
   115
#include <openssl/bn.h>
sl@0
   116
sl@0
   117
#ifdef  __cplusplus
sl@0
   118
extern "C" {
sl@0
   119
#endif
sl@0
   120
sl@0
   121
sl@0
   122
/*
sl@0
   123
 * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
sl@0
   124
 *
sl@0
   125
 *
sl@0
   126
 * For window size 'w' (w >= 2) and a random 'b' bits exponent,
sl@0
   127
 * the number of multiplications is a constant plus on average
sl@0
   128
 *
sl@0
   129
 *    2^(w-1) + (b-w)/(w+1);
sl@0
   130
 *
sl@0
   131
 * here  2^(w-1)  is for precomputing the table (we actually need
sl@0
   132
 * entries only for windows that have the lowest bit set), and
sl@0
   133
 * (b-w)/(w+1)  is an approximation for the expected number of
sl@0
   134
 * w-bit windows, not counting the first one.
sl@0
   135
 *
sl@0
   136
 * Thus we should use
sl@0
   137
 *
sl@0
   138
 *    w >= 6  if        b > 671
sl@0
   139
 *     w = 5  if  671 > b > 239
sl@0
   140
 *     w = 4  if  239 > b >  79
sl@0
   141
 *     w = 3  if   79 > b >  23
sl@0
   142
 *    w <= 2  if   23 > b
sl@0
   143
 *
sl@0
   144
 * (with draws in between).  Very small exponents are often selected
sl@0
   145
 * with low Hamming weight, so we use  w = 1  for b <= 23.
sl@0
   146
 */
sl@0
   147
#if 1
sl@0
   148
#define BN_window_bits_for_exponent_size(b) \
sl@0
   149
		((b) > 671 ? 6 : \
sl@0
   150
		 (b) > 239 ? 5 : \
sl@0
   151
		 (b) >  79 ? 4 : \
sl@0
   152
		 (b) >  23 ? 3 : 1)
sl@0
   153
#else
sl@0
   154
/* Old SSLeay/OpenSSL table.
sl@0
   155
 * Maximum window size was 5, so this table differs for b==1024;
sl@0
   156
 * but it coincides for other interesting values (b==160, b==512).
sl@0
   157
 */
sl@0
   158
#define BN_window_bits_for_exponent_size(b) \
sl@0
   159
		((b) > 255 ? 5 : \
sl@0
   160
		 (b) > 127 ? 4 : \
sl@0
   161
		 (b) >  17 ? 3 : 1)
sl@0
   162
#endif	 
sl@0
   163
sl@0
   164
sl@0
   165
sl@0
   166
/* BN_mod_exp_mont_conttime is based on the assumption that the
sl@0
   167
 * L1 data cache line width of the target processor is at least
sl@0
   168
 * the following value.
sl@0
   169
 */
sl@0
   170
#define MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH	( 64 )
sl@0
   171
#define MOD_EXP_CTIME_MIN_CACHE_LINE_MASK	(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - 1)
sl@0
   172
sl@0
   173
/* Window sizes optimized for fixed window size modular exponentiation
sl@0
   174
 * algorithm (BN_mod_exp_mont_consttime).
sl@0
   175
 *
sl@0
   176
 * To achieve the security goals of BN_mode_exp_mont_consttime, the
sl@0
   177
 * maximum size of the window must not exceed
sl@0
   178
 * log_2(MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH). 
sl@0
   179
 *
sl@0
   180
 * Window size thresholds are defined for cache line sizes of 32 and 64,
sl@0
   181
 * cache line sizes where log_2(32)=5 and log_2(64)=6 respectively. A
sl@0
   182
 * window size of 7 should only be used on processors that have a 128
sl@0
   183
 * byte or greater cache line size.
sl@0
   184
 */
sl@0
   185
#if MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 64
sl@0
   186
sl@0
   187
#  define BN_window_bits_for_ctime_exponent_size(b) \
sl@0
   188
		((b) > 937 ? 6 : \
sl@0
   189
		 (b) > 306 ? 5 : \
sl@0
   190
		 (b) >  89 ? 4 : \
sl@0
   191
		 (b) >  22 ? 3 : 1)
sl@0
   192
#  define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE	(6)
sl@0
   193
sl@0
   194
#elif MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH == 32
sl@0
   195
sl@0
   196
#  define BN_window_bits_for_ctime_exponent_size(b) \
sl@0
   197
		((b) > 306 ? 5 : \
sl@0
   198
		 (b) >  89 ? 4 : \
sl@0
   199
		 (b) >  22 ? 3 : 1)
sl@0
   200
#  define BN_MAX_WINDOW_BITS_FOR_CTIME_EXPONENT_SIZE	(5)
sl@0
   201
sl@0
   202
#endif
sl@0
   203
sl@0
   204
sl@0
   205
/* Pentium pro 16,16,16,32,64 */
sl@0
   206
/* Alpha       16,16,16,16.64 */
sl@0
   207
#define BN_MULL_SIZE_NORMAL			(16) /* 32 */
sl@0
   208
#define BN_MUL_RECURSIVE_SIZE_NORMAL		(16) /* 32 less than */
sl@0
   209
#define BN_SQR_RECURSIVE_SIZE_NORMAL		(16) /* 32 */
sl@0
   210
#define BN_MUL_LOW_RECURSIVE_SIZE_NORMAL	(32) /* 32 */
sl@0
   211
#define BN_MONT_CTX_SET_SIZE_WORD		(64) /* 32 */
sl@0
   212
sl@0
   213
#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
sl@0
   214
/*
sl@0
   215
 * BN_UMULT_HIGH section.
sl@0
   216
 *
sl@0
   217
 * No, I'm not trying to overwhelm you when stating that the
sl@0
   218
 * product of N-bit numbers is 2*N bits wide:-) No, I don't expect
sl@0
   219
 * you to be impressed when I say that if the compiler doesn't
sl@0
   220
 * support 2*N integer type, then you have to replace every N*N
sl@0
   221
 * multiplication with 4 (N/2)*(N/2) accompanied by some shifts
sl@0
   222
 * and additions which unavoidably results in severe performance
sl@0
   223
 * penalties. Of course provided that the hardware is capable of
sl@0
   224
 * producing 2*N result... That's when you normally start
sl@0
   225
 * considering assembler implementation. However! It should be
sl@0
   226
 * pointed out that some CPUs (most notably Alpha, PowerPC and
sl@0
   227
 * upcoming IA-64 family:-) provide *separate* instruction
sl@0
   228
 * calculating the upper half of the product placing the result
sl@0
   229
 * into a general purpose register. Now *if* the compiler supports
sl@0
   230
 * inline assembler, then it's not impossible to implement the
sl@0
   231
 * "bignum" routines (and have the compiler optimize 'em)
sl@0
   232
 * exhibiting "native" performance in C. That's what BN_UMULT_HIGH
sl@0
   233
 * macro is about:-)
sl@0
   234
 *
sl@0
   235
 *					<appro@fy.chalmers.se>
sl@0
   236
 */
sl@0
   237
# if defined(__alpha) && (defined(SIXTY_FOUR_BIT_LONG) || defined(SIXTY_FOUR_BIT))
sl@0
   238
#  if defined(__DECC)
sl@0
   239
#   include <c_asm.h>
sl@0
   240
#   define BN_UMULT_HIGH(a,b)	(BN_ULONG)asm("umulh %a0,%a1,%v0",(a),(b))
sl@0
   241
#  elif defined(__GNUC__)
sl@0
   242
#   define BN_UMULT_HIGH(a,b)	({	\
sl@0
   243
	register BN_ULONG ret;		\
sl@0
   244
	asm ("umulh	%1,%2,%0"	\
sl@0
   245
	     : "=r"(ret)		\
sl@0
   246
	     : "r"(a), "r"(b));		\
sl@0
   247
	ret;			})
sl@0
   248
#  endif	/* compiler */
sl@0
   249
# elif defined(_ARCH_PPC) && defined(__64BIT__) && defined(SIXTY_FOUR_BIT_LONG)
sl@0
   250
#  if defined(__GNUC__)
sl@0
   251
#   define BN_UMULT_HIGH(a,b)	({	\
sl@0
   252
	register BN_ULONG ret;		\
sl@0
   253
	asm ("mulhdu	%0,%1,%2"	\
sl@0
   254
	     : "=r"(ret)		\
sl@0
   255
	     : "r"(a), "r"(b));		\
sl@0
   256
	ret;			})
sl@0
   257
#  endif	/* compiler */
sl@0
   258
# elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
sl@0
   259
#  if defined(__GNUC__)
sl@0
   260
#   define BN_UMULT_HIGH(a,b)	({	\
sl@0
   261
	register BN_ULONG ret,discard;	\
sl@0
   262
	asm ("mulq	%3"		\
sl@0
   263
	     : "=a"(discard),"=d"(ret)	\
sl@0
   264
	     : "a"(a), "g"(b)		\
sl@0
   265
	     : "cc");			\
sl@0
   266
	ret;			})
sl@0
   267
#   define BN_UMULT_LOHI(low,high,a,b)	\
sl@0
   268
	asm ("mulq	%3"		\
sl@0
   269
		: "=a"(low),"=d"(high)	\
sl@0
   270
		: "a"(a),"g"(b)		\
sl@0
   271
		: "cc");
sl@0
   272
#  endif
sl@0
   273
# elif (defined(_M_AMD64) || defined(_M_X64)) && defined(SIXTY_FOUR_BIT)
sl@0
   274
#  if defined(_MSC_VER) && _MSC_VER>=1400
sl@0
   275
    unsigned __int64 __umulh	(unsigned __int64 a,unsigned __int64 b);
sl@0
   276
    unsigned __int64 _umul128	(unsigned __int64 a,unsigned __int64 b,
sl@0
   277
				 unsigned __int64 *h);
sl@0
   278
#   pragma intrinsic(__umulh,_umul128)
sl@0
   279
#   define BN_UMULT_HIGH(a,b)		__umulh((a),(b))
sl@0
   280
#   define BN_UMULT_LOHI(low,high,a,b)	((low)=_umul128((a),(b),&(high)))
sl@0
   281
#  endif
sl@0
   282
# endif		/* cpu */
sl@0
   283
#endif		/* OPENSSL_NO_ASM */
sl@0
   284
sl@0
   285
/*************************************************************
sl@0
   286
 * Using the long long type
sl@0
   287
 */
sl@0
   288
#define Lw(t)    (((BN_ULONG)(t))&BN_MASK2)
sl@0
   289
#define Hw(t)    (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
sl@0
   290
sl@0
   291
#ifdef BN_DEBUG_RAND
sl@0
   292
#define bn_clear_top2max(a) \
sl@0
   293
	{ \
sl@0
   294
	int      ind = (a)->dmax - (a)->top; \
sl@0
   295
	BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
sl@0
   296
	for (; ind != 0; ind--) \
sl@0
   297
		*(++ftl) = 0x0; \
sl@0
   298
	}
sl@0
   299
#else
sl@0
   300
#define bn_clear_top2max(a)
sl@0
   301
#endif
sl@0
   302
sl@0
   303
#ifdef BN_LLONG
sl@0
   304
#define mul_add(r,a,w,c) { \
sl@0
   305
	BN_ULLONG t; \
sl@0
   306
	t=(BN_ULLONG)w * (a) + (r) + (c); \
sl@0
   307
	(r)= Lw(t); \
sl@0
   308
	(c)= Hw(t); \
sl@0
   309
	}
sl@0
   310
sl@0
   311
#define mul(r,a,w,c) { \
sl@0
   312
	BN_ULLONG t; \
sl@0
   313
	t=(BN_ULLONG)w * (a) + (c); \
sl@0
   314
	(r)= Lw(t); \
sl@0
   315
	(c)= Hw(t); \
sl@0
   316
	}
sl@0
   317
sl@0
   318
#define sqr(r0,r1,a) { \
sl@0
   319
	BN_ULLONG t; \
sl@0
   320
	t=(BN_ULLONG)(a)*(a); \
sl@0
   321
	(r0)=Lw(t); \
sl@0
   322
	(r1)=Hw(t); \
sl@0
   323
	}
sl@0
   324
sl@0
   325
#elif defined(BN_UMULT_LOHI)
sl@0
   326
#define mul_add(r,a,w,c) {		\
sl@0
   327
	BN_ULONG high,low,ret,tmp=(a);	\
sl@0
   328
	ret =  (r);			\
sl@0
   329
	BN_UMULT_LOHI(low,high,w,tmp);	\
sl@0
   330
	ret += (c);			\
sl@0
   331
	(c) =  (ret<(c))?1:0;		\
sl@0
   332
	(c) += high;			\
sl@0
   333
	ret += low;			\
sl@0
   334
	(c) += (ret<low)?1:0;		\
sl@0
   335
	(r) =  ret;			\
sl@0
   336
	}
sl@0
   337
sl@0
   338
#define mul(r,a,w,c)	{		\
sl@0
   339
	BN_ULONG high,low,ret,ta=(a);	\
sl@0
   340
	BN_UMULT_LOHI(low,high,w,ta);	\
sl@0
   341
	ret =  low + (c);		\
sl@0
   342
	(c) =  high;			\
sl@0
   343
	(c) += (ret<low)?1:0;		\
sl@0
   344
	(r) =  ret;			\
sl@0
   345
	}
sl@0
   346
sl@0
   347
#define sqr(r0,r1,a)	{		\
sl@0
   348
	BN_ULONG tmp=(a);		\
sl@0
   349
	BN_UMULT_LOHI(r0,r1,tmp,tmp);	\
sl@0
   350
	}
sl@0
   351
sl@0
   352
#elif defined(BN_UMULT_HIGH)
sl@0
   353
#define mul_add(r,a,w,c) {		\
sl@0
   354
	BN_ULONG high,low,ret,tmp=(a);	\
sl@0
   355
	ret =  (r);			\
sl@0
   356
	high=  BN_UMULT_HIGH(w,tmp);	\
sl@0
   357
	ret += (c);			\
sl@0
   358
	low =  (w) * tmp;		\
sl@0
   359
	(c) =  (ret<(c))?1:0;		\
sl@0
   360
	(c) += high;			\
sl@0
   361
	ret += low;			\
sl@0
   362
	(c) += (ret<low)?1:0;		\
sl@0
   363
	(r) =  ret;			\
sl@0
   364
	}
sl@0
   365
sl@0
   366
#define mul(r,a,w,c)	{		\
sl@0
   367
	BN_ULONG high,low,ret,ta=(a);	\
sl@0
   368
	low =  (w) * ta;		\
sl@0
   369
	high=  BN_UMULT_HIGH(w,ta);	\
sl@0
   370
	ret =  low + (c);		\
sl@0
   371
	(c) =  high;			\
sl@0
   372
	(c) += (ret<low)?1:0;		\
sl@0
   373
	(r) =  ret;			\
sl@0
   374
	}
sl@0
   375
sl@0
   376
#define sqr(r0,r1,a)	{		\
sl@0
   377
	BN_ULONG tmp=(a);		\
sl@0
   378
	(r0) = tmp * tmp;		\
sl@0
   379
	(r1) = BN_UMULT_HIGH(tmp,tmp);	\
sl@0
   380
	}
sl@0
   381
sl@0
   382
#else
sl@0
   383
/*************************************************************
sl@0
   384
 * No long long type
sl@0
   385
 */
sl@0
   386
sl@0
   387
#define LBITS(a)	((a)&BN_MASK2l)
sl@0
   388
#define HBITS(a)	(((a)>>BN_BITS4)&BN_MASK2l)
sl@0
   389
#define	L2HBITS(a)	(((a)<<BN_BITS4)&BN_MASK2)
sl@0
   390
sl@0
   391
#define LLBITS(a)	((a)&BN_MASKl)
sl@0
   392
#define LHBITS(a)	(((a)>>BN_BITS2)&BN_MASKl)
sl@0
   393
#define	LL2HBITS(a)	((BN_ULLONG)((a)&BN_MASKl)<<BN_BITS2)
sl@0
   394
sl@0
   395
#define mul64(l,h,bl,bh) \
sl@0
   396
	{ \
sl@0
   397
	BN_ULONG m,m1,lt,ht; \
sl@0
   398
 \
sl@0
   399
	lt=l; \
sl@0
   400
	ht=h; \
sl@0
   401
	m =(bh)*(lt); \
sl@0
   402
	lt=(bl)*(lt); \
sl@0
   403
	m1=(bl)*(ht); \
sl@0
   404
	ht =(bh)*(ht); \
sl@0
   405
	m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \
sl@0
   406
	ht+=HBITS(m); \
sl@0
   407
	m1=L2HBITS(m); \
sl@0
   408
	lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \
sl@0
   409
	(l)=lt; \
sl@0
   410
	(h)=ht; \
sl@0
   411
	}
sl@0
   412
sl@0
   413
#define sqr64(lo,ho,in) \
sl@0
   414
	{ \
sl@0
   415
	BN_ULONG l,h,m; \
sl@0
   416
 \
sl@0
   417
	h=(in); \
sl@0
   418
	l=LBITS(h); \
sl@0
   419
	h=HBITS(h); \
sl@0
   420
	m =(l)*(h); \
sl@0
   421
	l*=l; \
sl@0
   422
	h*=h; \
sl@0
   423
	h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
sl@0
   424
	m =(m&BN_MASK2l)<<(BN_BITS4+1); \
sl@0
   425
	l=(l+m)&BN_MASK2; if (l < m) h++; \
sl@0
   426
	(lo)=l; \
sl@0
   427
	(ho)=h; \
sl@0
   428
	}
sl@0
   429
sl@0
   430
#define mul_add(r,a,bl,bh,c) { \
sl@0
   431
	BN_ULONG l,h; \
sl@0
   432
 \
sl@0
   433
	h= (a); \
sl@0
   434
	l=LBITS(h); \
sl@0
   435
	h=HBITS(h); \
sl@0
   436
	mul64(l,h,(bl),(bh)); \
sl@0
   437
 \
sl@0
   438
	/* non-multiply part */ \
sl@0
   439
	l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
sl@0
   440
	(c)=(r); \
sl@0
   441
	l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
sl@0
   442
	(c)=h&BN_MASK2; \
sl@0
   443
	(r)=l; \
sl@0
   444
	}
sl@0
   445
sl@0
   446
#define mul(r,a,bl,bh,c) { \
sl@0
   447
	BN_ULONG l,h; \
sl@0
   448
 \
sl@0
   449
	h= (a); \
sl@0
   450
	l=LBITS(h); \
sl@0
   451
	h=HBITS(h); \
sl@0
   452
	mul64(l,h,(bl),(bh)); \
sl@0
   453
 \
sl@0
   454
	/* non-multiply part */ \
sl@0
   455
	l+=(c); if ((l&BN_MASK2) < (c)) h++; \
sl@0
   456
	(c)=h&BN_MASK2; \
sl@0
   457
	(r)=l&BN_MASK2; \
sl@0
   458
	}
sl@0
   459
#endif /* !BN_LLONG */
sl@0
   460
sl@0
   461
void bn_mul_normal(BN_ULONG *r,BN_ULONG *a,int na,BN_ULONG *b,int nb);
sl@0
   462
IMPORT_C void bn_mul_comba8(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b);
sl@0
   463
IMPORT_C void bn_mul_comba4(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b);
sl@0
   464
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp);
sl@0
   465
IMPORT_C void bn_sqr_comba8(BN_ULONG *r,const BN_ULONG *a);
sl@0
   466
IMPORT_C void bn_sqr_comba4(BN_ULONG *r,const BN_ULONG *a);
sl@0
   467
IMPORT_C int bn_cmp_words(const BN_ULONG *a,const BN_ULONG *b,int n);
sl@0
   468
IMPORT_C int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,
sl@0
   469
	int cl, int dl);
sl@0
   470
void bn_mul_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2,
sl@0
   471
	int dna,int dnb,BN_ULONG *t);
sl@0
   472
void bn_mul_part_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,
sl@0
   473
	int n,int tna,int tnb,BN_ULONG *t);
sl@0
   474
void bn_sqr_recursive(BN_ULONG *r,const BN_ULONG *a, int n2, BN_ULONG *t);
sl@0
   475
void bn_mul_low_normal(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b, int n);
sl@0
   476
void bn_mul_low_recursive(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,int n2,
sl@0
   477
	BN_ULONG *t);
sl@0
   478
void bn_mul_high(BN_ULONG *r,BN_ULONG *a,BN_ULONG *b,BN_ULONG *l,int n2,
sl@0
   479
	BN_ULONG *t);
sl@0
   480
BN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
sl@0
   481
	int cl, int dl);
sl@0
   482
BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
sl@0
   483
	int cl, int dl);
sl@0
   484
sl@0
   485
#ifdef  __cplusplus
sl@0
   486
}
sl@0
   487
#endif
sl@0
   488
sl@0
   489
#endif