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

📄 miracl.h

📁 RSA算法实现, 里面有详细的代码... ... ... ... ... !
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef MIRACL_H
#define MIRACL_H

/*
 *   main MIRACL header - miracl.h.
 *
 *   Copyright (c) 1988-2001 Shamus Software Ltd.
 */

#include "mirdef.h"

#ifdef __ia64__
#if MIRACL==64
#define MR_ITANIUM
#include <ia64intrin.h>
#endif
#endif

#ifdef MR_FP
#include <math.h>
#endif

#ifndef MR_NO_FILE_IO
#include <stdio.h>
#endif
               /* error returns */

#define MR_ERR_BASE_TOO_BIG    1
#define MR_ERR_DIV_BY_ZERO     2
#define MR_ERR_OVERFLOW        3
#define MR_ERR_NEG_RESULT      4
#define MR_ERR_BAD_FORMAT      5
#define MR_ERR_BAD_BASE        6
#define MR_ERR_BAD_PARAMETERS  7
#define MR_ERR_OUT_OF_MEMORY   8
#define MR_ERR_NEG_ROOT        9
#define MR_ERR_NEG_POWER       10
#define MR_ERR_BAD_ROOT        11
#define MR_ERR_INT_OP          12
#define MR_ERR_FLASH_OVERFLOW  13
#define MR_ERR_TOO_BIG         14
#define MR_ERR_NEG_LOG         15
#define MR_ERR_DOUBLE_FAIL     16
#define MR_ERR_IO_OVERFLOW     17
#define MR_ERR_NO_MIRSYS       18
#define MR_ERR_BAD_MODULUS     19
#define MR_ERR_NO_MODULUS      20
#define MR_ERR_EXP_TOO_BIG     21
#define MR_ERR_NOT_SUPPORTED   22
#define MR_ERR_NOT_DOUBLE_LEN  23
#define MR_ERR_NOT_IRREDUC     24
#define MR_ERR_NO_ROUNDING     25

               /* some useful definitions */



#define forever for(;;)   

#ifndef TRUE
  #define TRUE 1
#endif
#ifndef FALSE
  #define FALSE 0
#endif

#define OFF 0
#define ON 1
#define PLUS 1
#define MINUS (-1)

#define MR_MAXDEPTH 24
                              /* max routine stack depth */
/* big and flash variables consist of an encoded length, *
 * and an array of mr_smalls containing the digits       */

typedef int BOOL;

#define MR_BYTE unsigned char

#ifdef MR_BITSINCHAR
 #if MR_BITSINCHAR == 8
  #define MR_TOBYTE(x) ((MR_BYTE)(x))
 #else
  #define MR_TOBYTE(x) ((MR_BYTE)((x)&0xFF))
 #endif
#else
 #define MR_TOBYTE(x) ((MR_BYTE)(x))
#endif

#ifdef MR_FP

  typedef mr_utype mr_small;
  #ifdef mr_dltype
  typedef mr_dltype mr_large;
  #endif

  #define MR_DIV(a,b)    (modf((a)/(b),&dres),dres)

  #ifdef MR_FP_ROUNDING

/* slightly dicey - the optimizer might remove the MAGIC ! */

    #define MR_LROUND(a)   ( ( (a) + MR_MAGIC ) - MR_MAGIC )
  #else
    #define MR_LROUND(a)   (modfl((a),&ldres),ldres)
  #endif

  #define MR_REMAIN(a,b) ((a)-(b)*MR_DIV((a),(b)))

#else

  typedef unsigned mr_utype mr_small;
  #ifdef mr_dltype
    typedef unsigned mr_dltype mr_large;
  #endif

  #define MR_DIV(a,b)    ((a)/(b))
  #define MR_REMAIN(a,b) ((a)%(b))
  #define MR_LROUND(a)   ((a))
#endif

struct bigtype
{
    mr_unsign32 len;
    mr_small *w;
};                

typedef struct bigtype *big;
typedef big zzn;

/* Macro to create big x on the stack - x_t and x_g must be distinct variables 
   By convention use like this. See brute.c and identity.c for examples

   BIG(x,x_t,x_g,10)
   BIG(y,y_t,y_g,10)

*/

#define BIG(x,xt,xg,s) mr_small xg[s]; struct bigtype xt={s,xg}; big x=&xt;

typedef big flash;

#define MR_MSBIT ((mr_unsign32)1<<31)
#define MR_OBITS (MR_MSBIT-1)

#if MIRACL >= MR_IBITS
#define MR_TOOBIG (1<<(MR_IBITS-2))
#else
#define MR_TOOBIG (1<<(MIRACL-1))
#endif

#ifdef  MR_FLASH
#define MR_EBITS (8*sizeof(double) - MR_FLASH)
                                  /* no of Bits per double exponent */
#define MR_BTS 16
#define MR_MSK 0xFFFF

#endif

#define MR_HASH_BYTES     20

/* Marsaglia & Zaman Random number generator */
/*         constants      alternatives       */
#define NK   37           /* 21 */
#define NJ   24           /*  6 */
#define NV   14           /*  8 */


#ifdef MR_LITTLE_ENDIAN
#define MR_TOP(x) (*(((mr_small *)&(x))+1))
#define MR_BOT(x) (*(((mr_small *)&(x))))
#endif
#ifdef MR_BIG_ENDIAN
#define MR_TOP(x) (*(((mr_small *)&(x))))
#define MR_BOT(x) (*(((mr_small *)&(x))+1))
#endif

/* chinese remainder theorem structures */

typedef struct {
big *C;
big *V;
big *M;
int NP;
} big_chinese;

typedef struct {
mr_utype *C;
mr_utype *V;
mr_utype *M;
int NP;
} small_chinese;

/* Cryptographically strong pseudo-random number generator */

typedef struct {
mr_unsign32 ira[NK];  /* random number...   */
int         rndptr;   /* ...array & pointer */
mr_unsign32 borrow;
int pool_ptr;
char pool[MR_HASH_BYTES];    /* random pool */
} csprng;

/* secure hash Algorithm structure */

typedef struct {
mr_unsign32 length[2];
mr_unsign32 h[8];
mr_unsign32 w[80];
} sha256;

typedef sha256 sha;

#ifdef mr_unsign64

typedef struct {
mr_unsign64 length[2];
mr_unsign64 h[8];
mr_unsign64 w[80];
} sha512;

typedef sha512 sha384;

#endif

/* advanced encryption algorithm structure */

#define MR_ECB   0
#define MR_CBC   1
#define MR_CFB1  2
#define MR_CFB2  3
#define MR_CFB4  5
#define MR_PCFB1 10
#define MR_PCFB2 11
#define MR_PCFB4 13
#define MR_OFB1  14
#define MR_OFB2  15
#define MR_OFB4  17
#define MR_OFB8  21
#define MR_OFB16 29

typedef struct {
int Nk,Nr;
int mode;
mr_unsign32 fkey[60];
mr_unsign32 rkey[60];
char f[16];
} aes;


               /* Elliptic curve point status */

#define MR_EPOINT_GENERAL    0
#define MR_EPOINT_NORMALIZED 1
#define MR_EPOINT_INFINITY   2

#define MR_PROJECTIVE 0
#define MR_AFFINE     1


/* Elliptic Curve epoint structure. Uses projective (X,Y,Z) co-ordinates */

typedef struct {
big X;
big Y;
big Z;
int marker;
} epoint;


/* Structure for Brickell method for finite *
   field exponentiation with precomputation */

typedef struct {
    big *table;
    big n;
    int base;
    int store;
} brick;

/* Structure for Brickell method for elliptic *
   curve  exponentiation with precomputation  */

typedef struct {
    epoint **table;
    big a,b,n;
    int base;
    int store;
} ebrick;

typedef struct {
    epoint **table;
    big a6,a2;
    int m,a,b,c;
    int base;
    int store;
} ebrick2;

/* main MIRACL instance structure */

typedef struct {
mr_small base;       /* number base     */
mr_small apbase;     /* apparent base   */
int   pack;          /* packing density */
int   lg2b;          /* bits in base    */
mr_small base2;      /* 2^mr_lg2b          */
BOOL (*user)(void);  /* pointer to user supplied function */

int   nib;           /* length of bigs  */
int   depth;                 /* error tracing ..*/
int   trace[MR_MAXDEPTH];    /* .. mechanism    */
BOOL  check;         /* overflow check  */
BOOL  fout;          /* Output to file   */
BOOL  fin;           /* Input from file  */
BOOL  active;

#ifndef MR_NO_FILE_IO

FILE  *infile;       /* Input file       */
FILE  *otfile;       /* Output file      */

#endif

mr_unsign32 ira[NK];  /* random number...   */
int         rndptr;   /* ...array & pointer */
mr_unsign32 borrow;

            /* Montgomery constants */
mr_small ndash;
big modulus;
BOOL ACTIVE;
BOOL MONTY;
                       /* Elliptic Curve details  */
BOOL SS;               /* True for Super-Singular */
big A,B,C;
int coord,Asize,Bsize;

int M,AA,BB,CC;     /* for GF(2^m) curves */

int logN;           /* constants for fast fourier fft multiplication */
int nprimes,degree;
mr_utype *prime,*cr;
mr_utype *inverse,**roots;
small_chinese chin;
mr_utype const1,const2,const3;
mr_small msw,lsw;
mr_utype **s1,**s2;   /* pre-computed tables for polynomial reduction */
mr_utype **t;         /* workspace */
mr_utype *wa;
mr_utype *wb;
mr_utype *wc;
BOOL same;
BOOL first_one;
BOOL debug;

big w0;            /* workspace bigs  */
big w1,w2,w3,w4;
big w5,w6,w7;
big w8,w9,w10,w11;
big w12,w13,w14,w15;
big w16,w17,w18;

/* User modifiables */

char *IOBUFF; /* i/o buffer    */
int  IOBSIZ;  /* size of i/o buffer */
BOOL ERCON;        /* error control   */
int  ERNUM;        /* last error code */
int  NTRY;         /* no. of tries for probablistic primality testing   */
int  IOBASE;       /* base for input and output */
BOOL EXACT;        /* exact flag      */
BOOL RPOINT;       /* =ON for radix point, =OFF for fractions in output */
BOOL TRACER;       /* turns trace tracker on/off */
int  INPLEN;       /* input length               */
int *PRIMES;       /* small primes array         */

#ifdef MR_FLASH
int   workprec;
int   stprec;        /* start precision */

int RS,RD;
double D;

double db,n,p;
int a,b,c,d,r,q,oldn,ndig;
mr_small u,v,ku,kv;

BOOL last,carryon;
flash pi;


#endif

#ifdef MR_KCM
big big_ndash;
big ws;
#endif

#ifdef MR_FP_ROUNDING
mr_large inverse_base;
#endif
int size;
char *workspace;

} miracl;


#ifndef MR_GENERIC_MT

#ifdef MR_WINDOWS_MT
#define MR_OS_THREADS
#endif

#ifdef MR_UNIX_MT
#define MR_OS_THREADS
#endif

#ifndef MR_OS_THREADS

extern miracl *mr_mip;  /* pointer to MIRACL's only global variable */

#endif

#endif


#ifdef MR_GENERIC_MT

#define _MIPT_  miracl *,
#define _MIPTO_ miracl *
#define _MIPD_  miracl *mr_mip,
#define _MIPDO_ miracl *mr_mip
#define _MIPP_  mr_mip,
#define _MIPPO_ mr_mip

#else

#define _MIPT_    
#define _MIPTO_  void  
#define _MIPD_    
#define _MIPDO_  void  
#define _MIPP_    
#define _MIPPO_    

#endif

/* Preamble and exit code for MIRACL routines. *
 * Not used if MR_STRIPPED_DOWN is defined     */ 

#ifdef MR_STRIPPED_DOWN
#define MR_OUT
#define MR_IN(N)
#else
#define MR_OUT  mr_mip->depth--;        
#define MR_IN(N) mr_mip->depth++; if (mr_mip->depth<MR_MAXDEPTH) {mr_mip->trace[mr_mip->depth]=(N); if (mr_mip->TRACER) mr_track(_MIPPO_); }
#endif

/* Function definitions  */

/* Group 0 - Internal routines */

extern void  mr_berror(_MIPT_ int);
extern mr_small mr_shiftbits(mr_small,int);
extern mr_small mr_setbase(_MIPT_ mr_small);
extern void  mr_track(_MIPTO_ );
extern void  mr_lzero(big);
extern BOOL  mr_notint(flash);
extern int   mr_lent(flash);
extern void  mr_padd(_MIPT_ big,big,big);
extern void  mr_psub(_MIPT_ big,big,big);

⌨️ 快捷键说明

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