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

📄 flint.c

📁 rsa加密算法的c++实现,此程序实现利用公钥解密
💻 C
📖 第 1 页 / 共 5 页
字号:

/******************************************************************************/
/*                                                                            */
/*  Requirements:                                                             */
/*                                                                            */
/*  sizeof (ULONG)  == 4                                                      */
/*  sizeof (USHORT) == 2                                                      */
/*                                                                            */
/******************************************************************************/

#ifndef FLINT_ANSI
#define FLINT_ANSI
#endif

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

#include "flint.h"

#define NO_ASSERTS 1



#ifdef FLINT_DEBUG
#undef NO_ASSERTS
#define ASSERT_LOG_AND_QUIT
#include "_assert.h"
#include "_alloc.h"
#ifdef COVERAGE
#include "utclog.h"
#endif
#endif

#ifdef NO_ASSERTS
#define Assert(a) (void)0
#endif


/* Mute wrap up error messages of PC-lint */
/*lint -esym(14,add,sub,mul,umul,sqr)   */
/*lint -esym(15,add,sub,mul,umul,sqr)   */
/*lint -esym(515,add,sub,mul,umul,sqr)  */
/*lint -esym(516,add,sub,mul,umul,sqr)  */
/*lint -esym(532,add,sub,mul,umul,sqr)  */
/*lint -esym(533,add,sub,mul,umul,sqr)  */
/*lint -esym(1066,add,sub,mul,umul,sqr) */
/*lint -esym(534,add_l,sub_l,mul_l,sqr_l,div_l,mmul_l,msub_l,dec_l,madd_l) */
/*lint -esym(534,msqr_l,mexp_l,mexp5_l,mexpk_l,mod_l,mod2_l,mexp2_l) */


/***********  Prototypes of local functions ***********************************/

/* Private register functions */
static void
destroy_reg_l (void);
static int
allocate_reg_l (void);
/* Integer square roots from ULONG values */
static ULONG
ul_iroot (unsigned long n);

#ifdef FLINT_SECURE
#define PURGEVARS_L(X) purgevars_l X
/* Function to purge variables */
static void purgevars_l (int noofvars, ...);
#ifdef FLINT_DEBUG
#define ISPURGED_L(X) Assert(ispurged_l X)
/* Function to check, whether variables have been purged */
static int ispurged_l (int noofvars, ...);
#else
#define ISPURGED_L(X) (void)0
#endif /* FLINT_DEBUG */
#else
#define PURGEVARS_L(X) (void)0
#define ISPURGED_L(X) (void)0
#endif /* FLINT_SECURE */
/******************************************************************************/

/* CLINT-Constant Values */
clint __FLINT_API_DATA
nul_l[] = {0, 0, 0, 0, 0};
clint __FLINT_API_DATA
one_l[] = {1, 1, 0, 0, 0};
clint __FLINT_API_DATA
two_l[] = {1, 2, 0, 0, 0};



/******************************************************************************/
/*                                                                            */
/*  Function:   Initialization of FLINT/C-Library                             */
/*              If the FLINT/C functions are provided as DLL                  */
/*              the function initializing the DLL, e. g. DllMain(),           */
/*              should call this function.                                    */
/*                                                                            */
/*  Syntax:     FLINTInit_l() (void);                                         */
/*  Input:      -                                                             */
/*  Output:     -                                                             */
/*  Returns:    E_CLINT_OK if everything is O.K.                              */
/*              -1 else                                                       */
/*                                                                            */
/******************************************************************************/
int __FLINT_API
FLINTInit_l (void)
{
  int error;
  initrand64_lt();
  initrandBBS_lt();
  error = create_reg_l();

  if (!error)
    return E_CLINT_OK;
  else
    return -1;
}


/******************************************************************************/
/*                                                                            */
/*  Function:   Exit the FLINT/C-Library                                      */
/*  Syntax:     FLINTExit_l (void);                                           */
/*  Input:      -                                                             */
/*  Output:     -                                                             */
/*  Returns:    E_CLINT_OK if everything is O.K.                              */
/*              -1 else                                                       */
/*                                                                            */
/******************************************************************************/
int __FLINT_API
FLINTExit_l (void)
{
  free_reg_l();

  return E_CLINT_OK;
}


/******************************************************************************/
/*                                                                            */
/*  Function:   Copy CLINT to CLINT                                           */
/*  Syntax:     void cpy_l (CLINT dest_l, CLINT src_l);                       */
/*  Input:      CLINT src_l                                                   */
/*  Output:     CLINT dest_l                                                  */
/*  Returns:    -                                                             */
/*                                                                            */
/******************************************************************************/
void __FLINT_API
cpy_l (CLINT dest_l, CLINT src_l)
{
  clint *lastsrc_l = MSDPTR_L (src_l);
  *dest_l = *src_l;

  while ((*lastsrc_l == 0) && (*dest_l > 0))
    {
      --lastsrc_l;
      --*dest_l;
    }

  while (src_l < lastsrc_l)
    {
      *++dest_l = *++src_l;
    }
}


/******************************************************************************/
/*                                                                            */
/*  Function:  Swap two CLINT operands                                        */
/*  Syntax:    void fswap_l (CLINT a_l, CLINT b_l);                           */
/*  Input:     CLINT a_l, b_l                                                 */
/*  Output:    Swapped CLINT operands b_l, a_l                                */
/*  Returns:   -                                                              */
/*                                                                            */
/******************************************************************************/
void __FLINT_API
fswap_l (CLINT a_l, CLINT b_l)
{
  CLINT tmp_l;

  cpy_l (tmp_l, a_l);
  cpy_l (a_l, b_l);
  cpy_l (b_l, tmp_l);

  /* Purging of variables */
  PURGEVARS_L ((1, sizeof (tmp_l), tmp_l));
}


/******************************************************************************/
/*                                                                            */
/*  Function:  Test whether two CLINT operands are equal                      */
/*  Syntax:    int equ_l (CLINT a_l, CLINT b_l);                              */
/*  Input:     CLINT a_l, b_l                                                 */
/*  Output:    -                                                              */
/*  Returns:   1 : a_l have b_l equal values                                  */
/*             0 : otherwise                                                  */
/*                                                                            */
/******************************************************************************/
int __FLINT_API
equ_l (CLINT a_l, CLINT b_l)
{
  clint *msdptra_l, *msdptrb_l;
  int la = (int)DIGITS_L (a_l);
  int lb = (int)DIGITS_L (b_l);

  if (la == 0 && lb == 0)
    {
      return 1;
    }

  while (a_l[la] == 0 && la > 0)
    {
      --la;
    }

  while (b_l[lb] == 0 && lb > 0)
    {
      --lb;
    }

  if (la == 0 && lb == 0)
    {
      return 1;
    }

  if (la != lb)
    {
      return 0;
    }

  msdptra_l = a_l + la;
  msdptrb_l = b_l + lb;

  while ((*msdptra_l == *msdptrb_l) && (msdptra_l > a_l))
    {
      msdptra_l--;
      msdptrb_l--;
    }

  /* Purging of variables */
  PURGEVARS_L ((2, sizeof (la), &la,
                   sizeof (lb), &lb));

  ISPURGED_L ((2, sizeof (la), &la,
                  sizeof (lb), &lb));

  return (msdptra_l > a_l ? 0 : 1);
}


/******************************************************************************/
/*                                                                            */
/*  Function:  Test whether two CLINT operands are equal modulo m             */
/*  Syntax:    int mequ_l (CLINT a_l, CLINT b_l, CLINT m_l);                  */
/*  Input:     CLINT a_l, b_l (Values to compare), CLINT m_l (Modulus)        */
/*  Output:    -                                                              */
/*  Returns:   1          : a_l = b_l mod m_l                                 */
/*             0          : a_l != b_l mod m_l                                */
/*             E_CLINT_DBZ: division by 0                                     */
/*                                                                            */
/******************************************************************************/
int __FLINT_API
mequ_l (CLINT a_l, CLINT b_l, CLINT m_l)
{
  CLINT r_l;
  int res;

  if (EQZ_L (m_l))
    {
      return E_CLINT_DBZ;          /* Division by Zero? */
    }

  msub_l (a_l, b_l, r_l, m_l);

  res = (0 == DIGITS_L (r_l))?1:0;

  /* Purging of variables */
  PURGEVARS_L ((1, sizeof (r_l), r_l));
  ISPURGED_L ((1, sizeof (r_l), r_l));

  return res;
}


/******************************************************************************/
/*                                                                            */
/*  Function:  Comparison of two CLINT operands                               */
/*  Syntax:    int cmp_l (CLINT a_l, CLINT b_l);                              */
/*  Input:     CLINT a_l, b_l (Values to compare)                             */
/*  Output:    -                                                              */
/*  Returns:   -1: a_l < b_l,                                                 */
/*              0: a_l == b_l,                                                */
/*              1: a_l > b_l                                                  */
/*                                                                            */
/******************************************************************************/
int __FLINT_API
cmp_l (CLINT a_l, CLINT b_l)
{
  clint *msdptra_l, *msdptrb_l;
  int la = (int)DIGITS_L (a_l);
  int lb = (int)DIGITS_L (b_l);

  if (la == 0 && lb == 0)
    {
      return 0;
    }

  while (a_l[la] == 0 && la > 0)
    {
      --la;
    }

  while (b_l[lb] == 0 && lb > 0)
    {
      --lb;
    }

  if (la == 0 && lb == 0)
    {
      return 0;
    }

  if (la > lb)
    {
      PURGEVARS_L ((2, sizeof (la), &la,
                       sizeof (lb), &lb));
      ISPURGED_L  ((2, sizeof (la), &la,
                       sizeof (lb), &lb));
      return 1;
    }

  if (la < lb)
    {
      PURGEVARS_L ((2, sizeof (la), &la,
                       sizeof (lb), &lb));
      ISPURGED_L  ((2, sizeof (la), &la,
                       sizeof (lb), &lb));
      return -1;
    }

  msdptra_l = a_l + la;
  msdptrb_l = b_l + lb;

  while ((*msdptra_l == *msdptrb_l) && (msdptra_l > a_l))
    {
      msdptra_l--;
      msdptrb_l--;
    }

  PURGEVARS_L ((2, sizeof (la), &la,
                   sizeof (lb), &lb));
  ISPURGED_L  ((2, sizeof (la), &la,
                   sizeof (lb), &lb));

  if (msdptra_l == a_l)
    {
      return 0;
    }

  if (*msdptra_l > *msdptrb_l)
    {
      return 1;
    }
  else
    {
      return -1;
    }
}


/******************************************************************************/
/*                                                                            */
/*  Function:  Generation of maximum CLINT value 2^CLINTMAXBIT - 1            */
/*  Syntax:    clint * setmax_l (CLINT a_l);                                  */
/*  Input:     a_l CLINT variable                                             */
/*  Output:    a_l set to value of 2^CLINTMAXBIT - 1 = Nmax                   */
/*  Returns:   Address of CLINT variable a_l                                  */
/*                                                                            */
/******************************************************************************/
clint * __FLINT_API
setmax_l (CLINT a_l)
{
  clint *aptr_l = a_l;

⌨️ 快捷键说明

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