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

📄 flintpp.cpp

📁 flint库 RSA算法
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  LINT sqroot;
  if (status == E_LINT_INV) panic (E_LINT_INV, "root", 0, __LINE__);

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

  return sqroot;
}


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

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

  return gcdiv;
}


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

  err = lcm_l (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 LINT::xgcd (const LINT& b , LINT& u, int& sign_u, LINT& v, int& sign_v) const
{
  LINT g, hlp;
  if (status == E_LINT_INV) LINT::panic (E_LINT_INV, "xgcd", 0, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "xgcd", 1, __LINE__);

  xgcd_l (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 LINT::inv (const LINT& b) const
{
  LINT invers, hlp;
  if (status == E_LINT_INV) panic (E_LINT_INV, "inv", 0, __LINE__);
  if (b.status == E_LINT_INV) panic (E_LINT_INV, "inv", 1, __LINE__);

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

  return invers;
}


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

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


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

  if (root_l (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 LINT::root (const LINT& p) const
{
  LINT rt;
  if (status == E_LINT_INV) panic (E_LINT_INV, "root", 0, __LINE__);
  if (p.status == E_LINT_INV) panic (E_LINT_INV, "root", 1, __LINE__);

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


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


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

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

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

  return err;
}


LINT LINT::chinrem (const LINT& m, const LINT& b, const LINT& u) const
{
  LINT x;
  if (status == E_LINT_INV) LINT::panic (E_LINT_INV, "chinrem", 0, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "chinrem", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "chinrem", 2, __LINE__);
  if (u.status == E_LINT_INV) LINT::panic (E_LINT_INV, "chinrem", 3, __LINE__);

  clint* coeff_l[4];
  coeff_l[0] = n_l;
  coeff_l[1] = m.n_l;
  coeff_l[2] = b.n_l;
  coeff_l[3] = u.n_l;

  if (chinrem_l (2, coeff_l, x.n_l) == 0)
    {
      x.status = E_LINT_OK;
    }
  else
    {
      x = 0;
      x.status = E_LINT_ERR;
    }
  return x;
}


////////////////////////////////////////////////////////////////////////////////
//          Arithmetic friend functions                                       //
////////////////////////////////////////////////////////////////////////////////

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

  err = add_l (a.n_l, b.n_l, sum.n_l);

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

  return sum;
}


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

  err = sub_l (a.n_l, b.n_l, dif.n_l);

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

  return dif;
}


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

  if (&a == &b) //lint !e506
      err = sqr_l (a.n_l, p.n_l);
  else
      err = mul_l (a.n_l, b.n_l, p.n_l);

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

  return p;
}


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

  err = sqr_l (a.n_l, p.n_l);

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

  return p;
}


LINT divr (const LINT& a, const LINT& b, LINT& r)
{
  LINT q;
  int err;
  if (a.status == E_LINT_INV) LINT::panic (E_LINT_INV, "divr", 1, __LINE__);
  if (b.status == E_LINT_INV) LINT::panic (E_LINT_INV, "divr", 2, __LINE__);

  err = div_l (a.n_l, b.n_l, q.n_l, r.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        q.status = E_LINT_OK;
        r.status = E_LINT_OK;
        break;
      case E_CLINT_DBZ:
        LINT::panic (E_LINT_DBZ, "divr", 2, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "div", err, __LINE__);
    }

  return q;
}


LINT mod (const LINT& a, const LINT& n)
{
  LINT r, junk;
  int err;
  if (a.status == E_LINT_INV)  LINT::panic (E_LINT_INV, "mod", 1, __LINE__);
  if (n.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mod", 2, __LINE__);

  err = div_l (a.n_l, n.n_l, junk.n_l, r.n_l);

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

  return r;
}


LINT mod2 (const LINT& a, USHORT k)
{
  LINT r;
  int err;
  if (a.status == E_LINT_INV)  LINT::panic (E_LINT_INV, "mod2", 1, __LINE__);

  err = mod2_l (a.n_l, k, r.n_l);

  switch (err)
    {
      case E_CLINT_OK:
        r.status = E_LINT_OK;
        break;
      default:
        LINT::panic (E_LINT_ERR, "mod2", err, __LINE__);
    }

  return r;
}


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

  err = madd_l (lr.n_l, ln.n_l, sum.n_l, m.n_l);

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

  return sum;
}


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

  err = msub_l (lr.n_l, ln.n_l, dif.n_l, m.n_l);

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

  return dif;
}


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

  if (&lr == &ln) //lint !e506
    {
      err = msqr_l (lr.n_l, p.n_l, m.n_l);
    }
  else
    {
      err = mmul_l (lr.n_l, ln.n_l, p.n_l, m.n_l);
    }

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

  return p;
}


LINT msqr (const LINT& lr, const LINT& m)
{
  LINT p;
  int err;
  if (lr.status == E_LINT_INV) LINT::panic (E_LINT_INV, "msqr", 1, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "msqr", 2, __LINE__);

  err = msqr_l (lr.n_l, p.n_l, m.n_l);

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

  return p;
}


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

  err = mexp_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_DBZ:
        LINT::panic (E_LINT_DBZ, "mexp", 3, __LINE__);
        break;
      default:
        LINT::panic (E_LINT_ERR, "mexp", err, __LINE__);
    }

  return pot;
}


LINT mexp (USHORT b, const LINT& ln, const LINT& m)
{
  LINT pot;
  int err;
  if (ln.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 2, __LINE__);
  if (m.status == E_LINT_INV) LINT::panic (E_LINT_INV, "mexp", 3, __LINE__);
  
  if (m.isodd ())
    {
      err = wmexpm_l (b, ln.n_l, pot.n_l, m.n_l);
    }
  else
    {
      err = wmexp_l (b, ln.n_l, 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 mexp (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__);

  if (m.isodd())
    {
      err = umexpm_l (lr.n_l, e, pot.n_l, m.n_l);
    }

⌨️ 快捷键说明

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