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

📄 flintpp.cpp

📁 flint库 RSA算法
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  else
    {
      err = umexp_l (lr.n_l, e, pot.n_l, m.n_l);
    }

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexp", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexp", err, __LINE__);
    }

  return pot;
}


LINT mexp5m (const LINT& lr, const LINT& ln, const LINT& m)
{
  LINT pot;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp5m", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp5m", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp5m", 3, __LINE__);

  err = mexp5m_l (lr.n_l, ln.n_l, pot.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_MOD:
        LINT::panic (E_LINT_MOD, "mexp5m", 3, __LINE__);
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexp5m", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexp5m", err, __LINE__);
    }

  return pot;
}


LINT mexpkm (const LINT& lr, const LINT& ln, const LINT& m)
{
  LINT pot;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexpkm", 1, __LINE__);
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexpkm", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexpkm", 3, __LINE__);

  err = mexpkm_l (lr.n_l, ln.n_l, pot.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_MOD:
        LINT::panic (E_LINT_MOD, "mexpkm", 3, __LINE__);
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexpkm", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexpkm", err, __LINE__);
    }

  return pot;
}


LINT mexp2 (const LINT& lr, USHORT e, const LINT& m)
{
  LINT pot;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 1, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 3, __LINE__);

  err = mexp2_l (lr.n_l, e, pot.n_l, m.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        pot.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mexp2", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexp2", err, __LINE__);
    }

  return pot;
}


LINT shift (const LINT& a, int noofbits)
{
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "shift", 1, __LINE__);

  LINT shft = a;
  err = shift_l (shft.n_l, noofbits);

  switch (err)
    {
      case E_CLINT_OK:
        shft.status = E_LINT_OK; 
        break;
      case E_CLINT_OFL:
        shft.status = E_LINT_OFL;
        break;
      case E_CLINT_UFL:
        shft.status = E_LINT_UFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, ">>", err, __LINE__);
    }

  return shft;
}


////////////////////////////////////////////////////////////////////////////////
//           Number-theoretic friend functions                                //
////////////////////////////////////////////////////////////////////////////////


unsigned int ld (const LINT& a)
{
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "ld", 1, __LINE__);
  return ld_l (a.n_l);
}


int isprime (const LINT& p, int noofsmallprimes, int iterations)
{
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "isprime", 1, __LINE__);
  return (prime_l (p.n_l, noofsmallprimes, iterations));
}


int iseven (const LINT& p)
{
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "iseven", 1, __LINE__);
  return (ISEVEN_L (p.n_l));
}


int isodd (const LINT& p)
{
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "isodd", 1, __LINE__);
  return (ISODD_L (p.n_l)); 
}


LINT issqr (const LINT& lr)
{
  LINT sqroot;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "issqr", 1, __LINE__);

  issqr_l (lr.n_l, sqroot.n_l);
  sqroot.status = E_LINT_OK;

  return sqroot;
}


LINT root (const LINT& lr)
{
  LINT sqroot;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "root", 1, __LINE__);

  iroot_l (lr.n_l, sqroot.n_l);
  sqroot.status = E_LINT_OK;

  return sqroot;
}


LINT gcd (const LINT& a, const LINT& b)
{
  LINT gcdiv, hlp;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "gcd", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "gcd", 2, __LINE__);

  gcd_l (a.n_l, b.n_l, gcdiv.n_l);
  gcdiv.status = E_LINT_OK;

  return gcdiv;
}


LINT lcm (const LINT& a, const LINT& b)
{
  LINT lcmult, hlp;
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "lcm", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "lcm", 2, __LINE__);

  err = lcm_l (a.n_l, b.n_l, lcmult.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        lcmult.status = E_LINT_OK;
        break;
      case E_CLINT_OFL:
        lcmult.status = E_LINT_OFL;
        break;
      default:
        LINT::panic (E_LINT_ERR, "lcm", err, __LINE__);
    }

  return lcmult;
}


LINT xgcd (const LINT& a, const LINT& b , LINT& u, int& sign_u, LINT& v, int& sign_v)
{
  LINT g, hlp;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "xgcd", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "xgcd", 2, __LINE__);

  xgcd_l (a.n_l, b.n_l, g.n_l, u.n_l, &sign_u, v.n_l, &sign_v);

  g.status = E_LINT_OK;
  u.status = E_LINT_OK;
  v.status = E_LINT_OK;

  return g;
}


LINT inv (const LINT& a, const LINT& b)
{
  LINT invers, hlp;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "inv", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "inv", 2, __LINE__);

  inv_l (a.n_l, b.n_l, hlp.n_l, invers.n_l);
  invers.status = E_LINT_OK;

  return invers;
}


int jacobi (const LINT& p, const LINT& q)
{
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "jacobi", 1, __LINE__);
  if (q.status == E_LINT_INV) LINT::panic (E_LINT_INV, "jacobi", 2, __LINE__);

  return (jacobi_l (p.n_l, q.n_l));
}


LINT root (const LINT& a, const LINT& p)
{
  LINT rt;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "root", 1, __LINE__);
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "root", 2, __LINE__);

  if (proot_l (a.n_l, p.n_l, rt.n_l) == 0)
    {
      rt.status = E_LINT_OK;
    }
  else
    {
      rt = 0;
      rt.status = E_LINT_ERR;
    }
  return rt;
}


LINT root (const LINT& a, const LINT& p, const LINT& q)
{
  LINT rt;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "root", 1, __LINE__);
  if (p.status == E_LINT_INV) LINT::panic (E_LINT_INV, "root", 2, __LINE__);
  if (q.status == E_LINT_INV) LINT::panic (E_LINT_INV, "root", 3, __LINE__);

  if (root_l (a.n_l, p.n_l, q.n_l, rt.n_l) == 0)
    {
      rt.status = E_LINT_OK;
    }
  else
    {
      rt = 0;
      rt.status = E_LINT_ERR;
    }
  return rt;
}


LINT chinrem (unsigned int noofeq, LINT** coeff)
{
  LINT x;
  clint** coeff_l = new clint* [noofeq << 1];
  if (NULL == coeff_l)
    {
      return LINT(0);
    }
  clint** k_l = coeff_l;

  for (unsigned i = 0; i < (noofeq << 1); coeff_l++, coeff++, i++)
    {
      if ((*coeff)->status == E_LINT_INV) LINT::panic (E_LINT_INV, "chinrem", i, __LINE__);
      *coeff_l = (*coeff)->n_l;
    }

  if (chinrem_l (noofeq, k_l, x.n_l) == 0)
    {
      x.status = E_LINT_OK;
    }
  else
    {
      x = 0;
      x.status = E_LINT_ERR;
    }

  delete [] k_l;
  return x;
}


LINT primroot (unsigned int noofprimes, LINT** primes)
{
  LINT x;
  clint** primes_l = new clint* [noofprimes + 1];
  if (NULL == primes_l)
    {
      return LINT(0);
    }
  clint** pr_l = primes_l;

  for (unsigned int i = 0; i <= noofprimes; primes_l++, primes++, i++)
    {
      if ((*primes)->status == E_LINT_INV) LINT::panic (E_LINT_INV, "primroot", i, __LINE__);
      *primes_l = (*primes)->n_l;
    }

  if (primroot_l (x.n_l, noofprimes, pr_l) == 0)
    {
      x.status = E_LINT_OK;
    }
  else
    {
      x = 0UL;
      x.status = E_LINT_ERR;
    }

  delete [] pr_l;
  return x;
}


int twofact (const LINT& even, LINT& odd)
{
  if (even.status == E_LINT_INV) LINT::panic (E_LINT_INV, "twofact", 1, __LINE__);
  odd.status = E_LINT_OK;
  return (twofact_l (even.n_l, odd.n_l));
}


// Determination of smallest prime p >= a  w/ gcd (p - 1, f) = 1

LINT nextprime (const LINT& a, const LINT& f)
{
  LINT p(a);
  if (p.iseven ()) ++p;
  while (!p.isprime () || (gcd (p - 1, f) != 1))
    {
      ++p;
      ++p;
    }
  return p; // Now p is the smallest prime >= a with gcd (p - 1, f) = 1
}


int mequ (const LINT& a, const LINT& b, const LINT& m)
{
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mequ", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mequ", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mequ", 3, __LINE__);

  err = mequ_l (a.n_l, b.n_l, m.n_l);

  switch (err)
    {
      case 0:
      case 1:
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "mequ", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mequ", err, __LINE__);
    }

  return err;
}


////////////////////////////////////////////////////////////////////////////////
//                    Pseudorandom number generators                          //
////////////////////////////////////////////////////////////////////////////////


// Linear congruential generator

void seedl (const LINT& seed)
{
  if (seed.status != E_LINT_INV) seed64_l (seed.n_l);
  else LINT::panic (E_LINT_INV, "seed64", 0, __LINE__);
}


LINT randl (int l)
{
  LINT random;
  rand_l (random.n_l, MIN (l, (int)CLINTMAXBIT));
  random.status = E_LINT_OK;
  return random;
}


LINT randl (const LINT& rmin, const LINT& rmax)
{
  if (rmin.status == E_LINT_INV) LINT::panic (E_LINT_INV, "randl", 1, __LINE__);
  if (rmax.status == E_LINT_INV) LINT::panic (E_LINT_INV, "randl", 2, __LINE__);
  if (rmax < rmin) LINT::panic (E_LINT_INV, "randl", 1, __LINE__);

  LINT random;
  LINT t = rmax - rmin;
  USHORT l = (ld (rmin) + ld (rmax)) >> 1;

  rand_l (random.n_l, MIN (l, (int)CLINTMAXBIT));
  random.status = E_LINT_OK;

  if (random < rmin)
    {
      random += rmin;
    }

  if (random > rmax)
    {
      random = rmin + random % (t + 1);
    }

  Assert ((random >= rmin) && (random <= rmax));

  return random;
}



////////////////////////////////////////////////////////////////////////////////
//                         Input/Output                                       //
////////////////////////////////////////////////////////////////////////////////


long LINT::flagsindex;

// Initialization of class member setup (class LintStatus == E_LINT_INV)
// The initialization of LINT::setup causes a call to the constructor 
// LintInit(), which in turn initializes the static member flagsindex as 
// a pointer to ios-internal memory: 

LintInit LINT::setup; /*lint !e1502*/


// Constructor LintInit() sets the ios-internal variable ios::iword (flagsindex)
// to the LINT default values:

LintInit::LintInit (void)
{
  // Get index to status variable of type long in class ios
  LINT::flagsindex = ios::xalloc ();

  // Set default status in cout and cerr
  cout.iword (LINT::flagsindex) = cerr.iword (LINT::flagsindex) =
           LINT::lintshowlength | LINT::linthex | LINT::lintshowbase;
}


// Read static LINT status variable in ios for standard output stream cout

long LINT::flags (void)
{
  return cout.iword (flagsindex);
}


// Read static LINT status variable in ios for output stream ostream s

long LINT::flags (ostream& s)
{
  return s.iword (flagsindex);
}


// Set static LINT status variable in ios for output stream ostream s

long LINT::setf (ostream& s, long flag)
{
  long t = s.iword (flagsindex);

  // Flags lintdec, linthex, lintbin, lintoct are mutually exclusive:
  if (flag & LINT::lintdec)
    {
      s.iword (flagsindex) = 
        (t & ~LINT::linthex & ~LINT::lintoct & ~LINT::lintbin) | LINT::lintdec;
      flag ^= LINT::lintdec;
    }

  if (flag & LINT::linthex)
    {
      s.iword (flagsindex) =
        (t & ~LINT::lintdec & ~LINT::lintoct & ~LINT::lintbin) | LINT::linthex;
      flag ^= LINT::linthex;
    }

  if (flag & LINT::lintoct)
    {
      s.iword (flagsindex) = 
        (t & ~LINT::lintdec & ~LINT::linthex & ~LINT::lintbin) | LINT::lintoct;
      flag ^= LINT:

⌨️ 快捷键说明

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