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

📄 config.c

📁 比较新的功能强大的rsa算法源代码,方便使用.
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 *   MIRACL utility program to automatically generate a mirdef.h file 
 *
 *   config.c
 *
 *   Compile WITHOUT any optimization
 *
 *   Run this with your computer/compiler configuration. It will
 *   attempt to create best-fit mirdef.h file for compiling the MIRACL 
 *   library. 
 *
 *   Generated are mirdef.tst - the suggested mirdef.h file, and 
 *   miracl.lst which tells which modules should be included in the library 
 *   build.
 *
 *   Copyright (c) 1994-2006 Shamus Software Ltd.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int answer(void)
{
    char ch;
    scanf("%c",&ch);
    getchar();
    if (ch=='Y' || ch=='y') return 1;
    return 0;
}

int main()
{
    int chosen,utlen,dllen,flsh,stripped,standard,nofull,port,mant,r,userlen;
    int nbits,i,b,dlong,threaded,choice,selected,special,sb,ab;
    int chosen64,no64,step_size,double_type,rounding,lmant,dmant,static_build,maxsize;
    int maxbase,bitsinchar,llsize,nio,edwards;
    int qlong,qllen;
    long end;
    unsigned char byte;
    char *ptr;
    char longlong[20];
    double s,magic;
    double x,y,eps;
    long double lx,ly,leps;
    FILE *fp,*fpl;

    bitsinchar=0;
    byte=1;
    while (byte!=0) {byte<<=1; bitsinchar++;}

    printf("This program attempts to generate a mirdef.h file from your\n");
    printf("responses to some simple questions, and by some internal tests\n");
    printf("of its own.\n\n");
    printf("The most fundamental decision is that of the 'underlying type'\n");
    printf("that is the C data type to be used to store each digit of a\n");
    printf("big number. You input the number of bits in this type, and \n");
    printf("this program finds a suitable candidate (usually short, int or \n");
    printf("long). Typical values would be 16, 32 or perhaps 64. \n");
    printf("The bigger the better, but a good starting point would be to\n");
    printf("enter the native wordlength of your computer\n\n");
    printf("For your information:-\n");
    printf("The size of a char  is %d bits\n",bitsinchar);
    printf("The size of a short is %d bits\n",bitsinchar*sizeof(short));
    printf("The size of an int  is %d bits\n",bitsinchar*sizeof(int));
    printf("The size of a long  is %d bits\n",bitsinchar*sizeof(long));

    eps=1.0;
    for (dmant=0;;dmant++)
    { /* IMPORTANT TO FOOL OPTIMIZER!!!!!! */
        x=1.0+eps;
        y=1.0;
        if (x==y) break;
        eps/=2.0;
    }

    printf("The size of a double is %d bits, its mantissa is %d bits\n",bitsinchar*sizeof(double),dmant);

    leps=1.0; 
    for (lmant=0;;lmant++) 
    { /* IMPORTANT TO FOOL OPTIMIZER!!!!!! */ 
        lx=1.0+leps; 
        ly=1.0; 
        if (lx==ly) break; 
        leps/=2.0; 
    } 
    printf("The size of a long double is %d bits, its mantissa is %d bits\n",bitsinchar*sizeof(long double),lmant);


    fp=fopen("mirdef.tst","wt");
    fprintf(fp,"/*\n *   MIRACL compiler/hardware definitions - mirdef.h\n");
    fprintf(fp," *   Copyright (c) 1988-2006 Shamus Software Ltd.\n */\n\n");
    end=1;
    ptr=(char *)(&end);

    if ((*ptr) == 1)
    {
         printf("\n    Little-endian processor detected\n\n");
         fprintf(fp,"#define MR_LITTLE_ENDIAN\n");
    }
    else 
    {
         printf("    Big-endian processor detected\n\n");
         fprintf(fp,"#define MR_BIG_ENDIAN\n");

    }
    printf("A double can be used as the underlying type. In rare circumstances\n");
    printf(" this may be optimal. NOT recommended!\n\n");
    
    printf("Do you wish to use a double as the underlying type? (Y/N)?");
    double_type=answer();
    nofull=0;
    chosen=0;
    chosen64=0;
    llsize=-1; /* no check for long long yet */
    no64=0;
    if (double_type)
    {
      fprintf(fp,"#define MIRACL %d\n",bitsinchar*sizeof(double));
      fprintf(fp,"#define mr_utype double\n");
      fprintf(fp,"#define mr_dltype long double\n");
      fprintf(fp,"#define MR_NOFULLWIDTH\n");
      fprintf(fp,"#define MR_FP\n");
      nofull=1;
    }
    else
    {
      printf("\nNow enter number of bits in underlying type= ");
      scanf("%d",&utlen);
      getchar();
      printf("\n");
      if (utlen!=8 && utlen!=16 && utlen!=32 && utlen!=64) 
      {
        printf("Non-standard system - this program cannot help!\n");
        fclose(fp);
        exit(0);
      }

      fprintf(fp,"#define MIRACL %d\n",utlen);

      if (utlen==bitsinchar)
      {
        printf("    underlying type is a char\n");
        fprintf(fp,"#define mr_utype char\n");
        chosen=1;
      }
      if (!chosen && utlen==bitsinchar*sizeof(short))
      {
        printf("    underlying type is a short\n");
        fprintf(fp,"#define mr_utype short\n");
        chosen=1;
      }
      if (!chosen && utlen==bitsinchar*sizeof(int))
      {
        printf("    underlying type is an int\n");
        fprintf(fp,"#define mr_utype int\n");
        chosen=1;
      }  
      if (!chosen && utlen==bitsinchar*sizeof(long))
      {
        printf("    underlying type is a long\n");
        fprintf(fp,"#define mr_utype long\n");
        chosen=1;
      }
      dlong=0;
      if (!chosen && utlen==2*bitsinchar*sizeof(long))
      {
        printf("\nDoes compiler support a %d bit unsigned type? (Y/N)?", 
              utlen);
        dlong=answer();
        if (dlong)
        {
            llsize=utlen;  /* there is a long long */
            printf("Is it called long long? (Y/N)?");
            if (!answer())
            {
                printf("Enter its name : ");
                scanf("%s",longlong);
                getchar();
            }
            else strcpy(longlong,"long long");
            printf("    underlying type is a %s\n",longlong);
            fprintf(fp,"#define mr_utype %s\n",longlong);
            chosen=1;
            if (64==utlen && !chosen64)
            { 
                printf("    64 bit unsigned type is an unsigned %s\n",longlong);
                fprintf(fp,"#define mr_unsign64 unsigned %s\n",longlong);
                chosen64=1;
            }
        }
        else
        {
            llsize=0; /* there is no long long */
            if (utlen==64) no64=1; /* there is no 64-bit type */
        }
      }

      if (!chosen)
      {
        printf("No suitable underlying type could be found\n");
        fclose(fp);
        exit(0);
      }
    }

    fprintf(fp,"#define MR_IBITS %d\n",bitsinchar*sizeof(int));
    fprintf(fp,"#define MR_LBITS %d\n",bitsinchar*sizeof(long)); 

/* Now try to find 32-bit unsigned types */

    chosen=0;
    if (32==bitsinchar*sizeof(unsigned short))
    {
        printf("    32 bit unsigned type is a unsigned short\n");
        fprintf(fp,"#define mr_unsign32 unsigned short\n");
        chosen=1;
    }
    if (!chosen && 32==bitsinchar*sizeof(unsigned int))
    {
        printf("    32 bit unsigned type is an unsigned int\n");
        fprintf(fp,"#define mr_unsign32 unsigned int\n");
        chosen=1;
    }
    if (!chosen && 32==bitsinchar*sizeof(long))
    {
        printf("    32 bit unsigned type is an unsigned long\n");
        fprintf(fp,"#define mr_unsign32 unsigned long\n");
        chosen=1;
    }

    if (!chosen)
    {
        printf("No suitable 32 bit unsigned type could be found\n");
        fclose(fp);
        exit(0);
    }

/* are some of the built-in types 64-bit? */

    if (64==bitsinchar*sizeof(int))
    {
        printf("    64 bit unsigned type is an unsigned int\n");
        fprintf(fp,"#define mr_unsign64 unsigned int\n");
        chosen64=1;
    }
    if (!chosen64 && 64==bitsinchar*sizeof(long))
    {
        printf("    64 bit unsigned type is an unsigned long\n");
        fprintf(fp,"#define mr_unsign64 unsigned long\n");
        chosen64=1;
    }
    if (!chosen64 && llsize>0 && 64==llsize)
    {
        printf("    64 bit unsigned type is a %s\n",longlong);
        fprintf(fp,"#define mr_unsign64 %s\n",longlong);
        chosen64=1;
    }

/* Now try to find a double length type */
    dlong=0;

    if (!double_type)
    {
      dllen=2*utlen;
      chosen=0;
      if (!chosen && dllen==bitsinchar*sizeof(short))
      {
        printf("    double length type is a short\n");
        fprintf(fp,"#define mr_dltype short\n");
        if (sizeof(short)==sizeof(int)) fprintf(fp,"#define MR_DLTYPE_IS_INT\n");
        chosen=1;
      }
      if (!chosen && dllen==bitsinchar*sizeof(int))
      {
        printf("    double length type is an int\n");
        fprintf(fp,"#define mr_dltype int\n");
        fprintf(fp,"#define MR_DLTYPE_IS_INT\n");
        chosen=1;
      }
      if (!chosen && dllen==bitsinchar*sizeof(long))
      {
        printf("    double length type is a long\n");
        fprintf(fp,"#define mr_dltype long\n");
        fprintf(fp,"#define MR_DLTYPE_IS_LONG\n");
        chosen=1;
      }
      if (!chosen && llsize>0 && dllen==llsize)
      {
           printf("    double length type is a %s\n",longlong);
           fprintf(fp,"#define mr_dltype %s\n",longlong);
           chosen=1;
      }
      if (!chosen && dllen==2*bitsinchar*sizeof(long))
      {
        printf("\nDoes compiler support a %d bit integer type? (Y/N)?", 
              dllen);
        dlong=answer();
       
        if (dlong)
        {
            printf("Is it called long long? (Y/N)?");
            if (!answer())
            {
                printf("Enter its name : ");
                scanf("%s",longlong);
                getchar();
            }
            else strcpy(longlong,"long long");

            printf("    double length type is a %s\n",longlong);
            fprintf(fp,"#define mr_dltype %s\n",longlong);
            chosen=1;
            if (64==dllen && !chosen64)
            { 
                printf("    64 bit unsigned type is an unsigned %s\n",longlong);
                fprintf(fp,"#define mr_unsign64 unsigned %s\n",longlong);
                chosen64=1;
            }
        }
        else
        {
            if (dllen==64) no64=1; /* there is no 64-bit type */
        }
      }
    }
    else dlong=1;

/* Now try to find a double length type */
    qlong=0;

    if (!double_type)
    {
      qllen=4*utlen;
      chosen=0;

      if (!chosen && qllen==bitsinchar*sizeof(long))
      {
        printf("    quad length type is a long\n");
        fprintf(fp,"#define mr_qltype long\n");
        chosen=1;
      }
      if (!chosen && llsize>0 && dllen==llsize)
      {

⌨️ 快捷键说明

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