⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ltc_tommath.h

📁 这是由Rinick编写的加解密函数库。最近找了不少关于加解密的C源码
💻 H
📖 第 1 页 / 共 2 页
字号:
/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org */#ifndef BN_H_#define BN_H_#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>#include <limits.h>#include <tommath_class.h>#undef MIN#define MIN(x,y) ((x)<(y)?(x):(y))#undef MAX#define MAX(x,y) ((x)>(y)?(x):(y))#ifdef __cplusplusextern "C" {/* C++ compilers don't like assigning void * to mp_digit * */#define  OPT_CAST(x)  (x *)#else/* C on the other hand doesn't care */#define  OPT_CAST(x)#endif/* detect 64-bit mode if possible */#if defined(__x86_64__)    #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))      #define MP_64BIT   #endif#endif/* some default configurations. * * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits * * At the very least a mp_digit must be able to hold 7 bits * [any size beyond that is ok provided it doesn't overflow the data type] */#ifdef MP_8BIT   typedef unsigned char      mp_digit;   typedef unsigned short     mp_word;#elif defined(MP_16BIT)   typedef unsigned short     mp_digit;   typedef unsigned long      mp_word;#elif defined(MP_64BIT)   /* for GCC only on supported platforms */#ifndef CRYPT   typedef unsigned long long ulong64;   typedef signed long long   long64;#endif   typedef unsigned long      mp_digit;   typedef unsigned long      mp_word __attribute__ ((mode(TI)));   #define DIGIT_BIT          60#else   /* this is the default case, 28-bit digits */      /* this is to make porting into LibTomCrypt easier :-) */#ifndef CRYPT   #if defined(_MSC_VER) || defined(__BORLANDC__)       typedef unsigned __int64   ulong64;      typedef signed __int64     long64;   #else      typedef unsigned long long ulong64;      typedef signed long long   long64;   #endif#endif   typedef unsigned long      mp_digit;   typedef ulong64            mp_word;#ifdef MP_31BIT      /* this is an extension that uses 31-bit digits */   #define DIGIT_BIT          31#else   /* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */   #define DIGIT_BIT          28   #define MP_28BIT#endif   #endif/* define heap macros */#ifndef CRYPT   /* default to libc stuff */   #ifndef XMALLOC        #define XMALLOC  malloc       #define XFREE    free       #define XREALLOC realloc       #define XCALLOC  calloc   #else      /* prototypes for our heap functions */      extern void *XMALLOC(size_t n);      extern void *XREALLOC(void *p, size_t n);      extern void *XCALLOC(size_t n, size_t s);      extern void XFREE(void *p);   #endif#endif/* otherwise the bits per digit is calculated automatically from the size of a mp_digit */#ifndef DIGIT_BIT   #define DIGIT_BIT     ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))  /* bits per digit */#endif#define MP_DIGIT_BIT     DIGIT_BIT#define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))#define MP_DIGIT_MAX     MP_MASK/* equalities */#define MP_LT        -1   /* less than */#define MP_EQ         0   /* equal to */#define MP_GT         1   /* greater than */#define MP_ZPOS       0   /* positive integer */#define MP_NEG        1   /* negative */#define MP_OKAY       0   /* ok result */#define MP_MEM        -2  /* out of mem */#define MP_VAL        -3  /* invalid input */#define MP_RANGE      MP_VAL#define MP_YES        1   /* yes response */#define MP_NO         0   /* no response *//* Primality generation flags */#define LTM_PRIME_BBS      0x0001 /* BBS style prime */#define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */#define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */typedef int           mp_err;/* you'll have to tune these... */extern int KARATSUBA_MUL_CUTOFF,           KARATSUBA_SQR_CUTOFF,           TOOM_MUL_CUTOFF,           TOOM_SQR_CUTOFF;/* define this to use lower memory usage routines (exptmods mostly) *//* #define MP_LOW_MEM *//* default precision */#ifndef MP_PREC   #ifndef MP_LOW_MEM      #define MP_PREC                 64     /* default digits of precision */   #else      #define MP_PREC                 8      /* default digits of precision */   #endif   #endif/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */#define MP_WARRAY               (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))/* the infamous mp_int structure */typedef struct  {    int used, alloc, sign;    mp_digit *dp;} mp_int;/* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);#define USED(m)    ((m)->used)#define DIGIT(m,k) ((m)->dp[(k)])#define SIGN(m)    ((m)->sign)/* error code to char* string */char *mp_error_to_string(int code);/* ---> init and deinit bignum functions <--- *//* init a bignum */int mp_init(mp_int *a);/* free a bignum */void mp_clear(mp_int *a);/* init a null terminated series of arguments */int mp_init_multi(mp_int *mp, ...);/* clear a null terminated series of arguments */void mp_clear_multi(mp_int *mp, ...);/* exchange two ints */void mp_exch(mp_int *a, mp_int *b);/* shrink ram required for a bignum */int mp_shrink(mp_int *a);/* grow an int to a given size */int mp_grow(mp_int *a, int size);/* init to a given number of digits */int mp_init_size(mp_int *a, int size);/* ---> Basic Manipulations <--- */#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)#define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)/* set to zero */void mp_zero(mp_int *a);/* set to a digit */void mp_set(mp_int *a, mp_digit b);/* set a 32-bit const */int mp_set_int(mp_int *a, unsigned long b);/* get a 32-bit value */unsigned long mp_get_int(mp_int * a);/* initialize and set a digit */int mp_init_set (mp_int * a, mp_digit b);/* initialize and set 32-bit value */int mp_init_set_int (mp_int * a, unsigned long b);/* copy, b = a */int mp_copy(mp_int *a, mp_int *b);/* inits and copies, a = b */int mp_init_copy(mp_int *a, mp_int *b);/* trim unused digits */void mp_clamp(mp_int *a);/* ---> digit manipulation <--- *//* right shift by "b" digits */void mp_rshd(mp_int *a, int b);/* left shift by "b" digits */int mp_lshd(mp_int *a, int b);/* c = a / 2**b */int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);/* b = a/2 */int mp_div_2(mp_int *a, mp_int *b);/* c = a * 2**b */int mp_mul_2d(mp_int *a, int b, mp_int *c);/* b = a*2 */int mp_mul_2(mp_int *a, mp_int *b);/* c = a mod 2**d */int mp_mod_2d(mp_int *a, int b, mp_int *c);/* computes a = 2**b */int mp_2expt(mp_int *a, int b);/* Counts the number of lsbs which are zero before the first zero bit */int mp_cnt_lsb(mp_int *a);/* I Love Earth! *//* makes a pseudo-random int of a given size */int mp_rand(mp_int *a, int digits);/* ---> binary operations <--- *//* c = a XOR b  */int mp_xor(mp_int *a, mp_int *b, mp_int *c);/* c = a OR b */int mp_or(mp_int *a, mp_int *b, mp_int *c);/* c = a AND b */int mp_and(mp_int *a, mp_int *b, mp_int *c);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -