00001 /* 00002 * Copyright(C) 2006 Cameron Rich 00003 * 00004 * This library is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as published by 00006 * the Free Software Foundation; either version 2.1 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this library; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #ifndef BIGINT_IMPL_HEADER 00020 #define BIGINT_IMPL_HEADER 00021 00022 /* Maintain a number of precomputed variables when doing reduction */ 00023 #define BIGINT_M_OFFSET 0 /**< Normal modulo offset. */ 00024 #ifdef CONFIG_BIGINT_CRT 00025 #define BIGINT_P_OFFSET 1 /**< p modulo offset. */ 00026 #define BIGINT_Q_OFFSET 2 /**< q module offset. */ 00027 #define BIGINT_NUM_MODS 3 /**< The number of modulus constants used. */ 00028 #else 00029 #define BIGINT_NUM_MODS 1 00030 #endif 00031 00032 /* Architecture specific functions for big ints */ 00033 #ifdef WIN32 00034 #define COMP_RADIX 4294967296i64 00035 #define COMP_BIG_MSB 0x8000000000000000i64 00036 #else 00037 #define COMP_RADIX 4294967296ULL /**< Max component + 1 */ 00038 #define COMP_BIG_MSB 0x8000000000000000ULL /**< (Max dbl comp + 1)/ 2 */ 00039 #endif 00040 #define COMP_BIT_SIZE 32 /**< Number of bits in a component. */ 00041 #define COMP_BYTE_SIZE 4 /**< Number of bytes in a component. */ 00042 #define COMP_NUM_NIBBLES 8 /**< Used For diagnostics only. */ 00043 00044 typedef uint32_t comp; /**< A single precision component. */ 00045 typedef uint64_t long_comp; /**< A double precision component. */ 00046 typedef int64_t slong_comp; /**< A signed double precision component. */ 00047 00048 /** 00049 * @struct _bigint 00050 * @brief A big integer basic object 00051 */ 00052 struct _bigint 00053 { 00054 struct _bigint* next; /**< The next bigint in the cache. */ 00055 short size; /**< The number of components in this bigint. */ 00056 short max_comps; /**< The heapsize allocated for this bigint */ 00057 int refs; /**< An internal reference count. */ 00058 comp* comps; /**< A ptr to the actual component data */ 00059 }; 00060 00061 typedef struct _bigint bigint; /**< An alias for _bigint */ 00062 00063 /** 00064 * Maintains the state of the cache, and a number of variables used in 00065 * reduction. 00066 */ 00067 typedef struct /**< A big integer "session" context. */ 00068 { 00069 bigint *active_list; /**< Bigints currently used. */ 00070 bigint *free_list; /**< Bigints not used. */ 00071 bigint *bi_radix; /**< The radix used. */ 00072 bigint *bi_mod[BIGINT_NUM_MODS]; /**< modulus */ 00073 00074 #if defined(CONFIG_BIGINT_MONTGOMERY) 00075 bigint *bi_RR_mod_m[BIGINT_NUM_MODS]; /**< R^2 mod m */ 00076 bigint *bi_R_mod_m[BIGINT_NUM_MODS]; /**< R mod m */ 00077 comp N0_dash[BIGINT_NUM_MODS]; 00078 #elif defined(CONFIG_BIGINT_BARRETT) 00079 bigint *bi_mu[BIGINT_NUM_MODS]; /**< Storage for mu */ 00080 #endif 00081 bigint *bi_normalised_mod[BIGINT_NUM_MODS]; /**< Normalised mod storage. */ 00082 bigint **g; /**< Used by sliding-window. */ 00083 int window; /**< The size of the sliding window */ 00084 int active_count; /**< Number of active bigints. */ 00085 int free_count; /**< Number of free bigints. */ 00086 00087 #ifdef CONFIG_BIGINT_MONTGOMERY 00088 uint8_t use_classical; /**< Use classical reduction. */ 00089 #endif 00090 uint8_t mod_offset; /**< The mod offset we are using */ 00091 } BI_CTX; 00092 00093 #ifndef WIN32 00094 #define max(a,b) ((a)>(b)?(a):(b)) /**< Find the maximum of 2 numbers. */ 00095 #define min(a,b) ((a)<(b)?(a):(b)) /**< Find the minimum of 2 numbers. */ 00096 #endif 00097 00098 #define PERMANENT 0x7FFF55AA /**< A magic number for permanents. */ 00099 00100 #define V1 v->comps[v->size-1] /**< v1 for division */ 00101 #define V2 v->comps[v->size-2] /**< v2 for division */ 00102 #define U(j) tmp_u->comps[tmp_u->size-j-1] /**< uj for division */ 00103 #define Q(j) quotient->comps[quotient->size-j-1] /**< qj for division */ 00104 00105 #endif
1.5.7.1