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

📄 mpi.c

📁 该压缩包中包括 tom的加密函数库及pdf说明 ,以及Rinick s ECC:椭圆曲线非对称加密密钥生成器
💻 C
📖 第 1 页 / 共 5 页
字号:
      /* make next carry */      _W = _W >> ((mp_word)DIGIT_BIT);  }    /* store final carry */  W[ix] = (mp_digit)(_W & MP_MASK);  /* setup dest */  olduse  = c->used;  c->used = pa;  {    register mp_digit *tmpc;    tmpc = c->dp + digs;    for (ix = digs; ix <= pa; ix++) {      /* now extract the previous digit [below the carry] */      *tmpc++ = W[ix];    }    /* clear unused digits [that existed in the old copy of c] */    for (; ix < olduse; ix++) {      *tmpc++ = 0;    }  }  mp_clamp (c);  return MP_OKAY;}#endif/* End: bn_fast_s_mp_mul_high_digs.c *//* Start: bn_fast_s_mp_sqr.c */#include <ltc_tommath.h>#ifdef BN_FAST_S_MP_SQR_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* the jist of squaring... * you do like mult except the offset of the tmpx [one that  * starts closer to zero] can't equal the offset of tmpy.   * So basically you set up iy like before then you min it with * (ty-tx) so that it never happens.  You double all those  * you add in the inner loopAfter that loop you do the squares and add them in.*/int fast_s_mp_sqr (mp_int * a, mp_int * b){  int       olduse, res, pa, ix, iz;  mp_digit   W[MP_WARRAY], *tmpx;  mp_word   W1;  /* grow the destination as required */  pa = a->used + a->used;  if (b->alloc < pa) {    if ((res = mp_grow (b, pa)) != MP_OKAY) {      return res;    }  }  /* number of output digits to produce */  W1 = 0;  for (ix = 0; ix < pa; ix++) {       int      tx, ty, iy;      mp_word  _W;      mp_digit *tmpy;      /* clear counter */      _W = 0;      /* get offsets into the two bignums */      ty = MIN(a->used-1, ix);      tx = ix - ty;      /* setup temp aliases */      tmpx = a->dp + tx;      tmpy = a->dp + ty;      /* this is the number of times the loop will iterrate, essentially         while (tx++ < a->used && ty-- >= 0) { ... }       */      iy = MIN(a->used-tx, ty+1);      /* now for squaring tx can never equal ty        * we halve the distance since they approach at a rate of 2x       * and we have to round because odd cases need to be executed       */      iy = MIN(iy, (ty-tx+1)>>1);      /* execute loop */      for (iz = 0; iz < iy; iz++) {         _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);      }      /* double the inner product and add carry */      _W = _W + _W + W1;      /* even columns have the square term in them */      if ((ix&1) == 0) {         _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);      }      /* store it */      W[ix] = (mp_digit)(_W & MP_MASK);      /* make next carry */      W1 = _W >> ((mp_word)DIGIT_BIT);  }  /* setup dest */  olduse  = b->used;  b->used = a->used+a->used;  {    mp_digit *tmpb;    tmpb = b->dp;    for (ix = 0; ix < pa; ix++) {      *tmpb++ = W[ix] & MP_MASK;    }    /* clear unused digits [that existed in the old copy of c] */    for (; ix < olduse; ix++) {      *tmpb++ = 0;    }  }  mp_clamp (b);  return MP_OKAY;}#endif/* End: bn_fast_s_mp_sqr.c *//* Start: bn_mp_2expt.c */#include <ltc_tommath.h>#ifdef BN_MP_2EXPT_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* computes a = 2**b  * * Simple algorithm which zeroes the int, grows it then just sets one bit * as required. */intmp_2expt (mp_int * a, int b){  int     res;  /* zero a as per default */  mp_zero (a);  /* grow a to accomodate the single bit */  if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {    return res;  }  /* set the used count of where the bit will go */  a->used = b / DIGIT_BIT + 1;  /* put the single bit in its place */  a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);  return MP_OKAY;}#endif/* End: bn_mp_2expt.c *//* Start: bn_mp_abs.c */#include <ltc_tommath.h>#ifdef BN_MP_ABS_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* b = |a|  * * Simple function copies the input and fixes the sign to positive */intmp_abs (mp_int * a, mp_int * b){  int     res;  /* copy a to b */  if (a != b) {     if ((res = mp_copy (a, b)) != MP_OKAY) {       return res;     }  }  /* force the sign of b to positive */  b->sign = MP_ZPOS;  return MP_OKAY;}#endif/* End: bn_mp_abs.c *//* Start: bn_mp_add.c */#include <ltc_tommath.h>#ifdef BN_MP_ADD_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* high level addition (handles signs) */int mp_add (mp_int * a, mp_int * b, mp_int * c){  int     sa, sb, res;  /* get sign of both inputs */  sa = a->sign;  sb = b->sign;  /* handle two cases, not four */  if (sa == sb) {    /* both positive or both negative */    /* add their magnitudes, copy the sign */    c->sign = sa;    res = s_mp_add (a, b, c);  } else {    /* one positive, the other negative */    /* subtract the one with the greater magnitude from */    /* the one of the lesser magnitude.  The result gets */    /* the sign of the one with the greater magnitude. */    if (mp_cmp_mag (a, b) == MP_LT) {      c->sign = sb;      res = s_mp_sub (b, a, c);    } else {      c->sign = sa;      res = s_mp_sub (a, b, c);    }  }  return res;}#endif/* End: bn_mp_add.c *//* Start: bn_mp_add_d.c */#include <ltc_tommath.h>#ifdef BN_MP_ADD_D_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* single digit addition */intmp_add_d (mp_int * a, mp_digit b, mp_int * c){  int     res, ix, oldused;  mp_digit *tmpa, *tmpc, mu;  /* grow c as required */  if (c->alloc < a->used + 1) {     if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {        return res;     }  }  /* if a is negative and |a| >= b, call c = |a| - b */  if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) {     /* temporarily fix sign of a */     a->sign = MP_ZPOS;     /* c = |a| - b */     res = mp_sub_d(a, b, c);     /* fix sign  */     a->sign = c->sign = MP_NEG;     return res;  }  /* old number of used digits in c */  oldused = c->used;  /* sign always positive */  c->sign = MP_ZPOS;  /* source alias */  tmpa    = a->dp;  /* destination alias */  tmpc    = c->dp;  /* if a is positive */  if (a->sign == MP_ZPOS) {     /* add digit, after this we're propagating      * the carry.      */     *tmpc   = *tmpa++ + b;     mu      = *tmpc >> DIGIT_BIT;     *tmpc++ &= MP_MASK;     /* now handle rest of the digits */     for (ix = 1; ix < a->used; ix++) {        *tmpc   = *tmpa++ + mu;        mu      = *tmpc >> DIGIT_BIT;        *tmpc++ &= MP_MASK;     }     /* set final carry */     ix++;     *tmpc++  = mu;     /* setup size */     c->used = a->used + 1;  } else {     /* a was negative and |a| < b */     c->used  = 1;     /* the result is a single digit */     if (a->used == 1) {        *tmpc++  =  b - a->dp[0];     } else {        *tmpc++  =  b;     }     /* setup count so the clearing of oldused      * can fall through correctly      */     ix       = 1;  }  /* now zero to oldused */  while (ix++ < oldused) {     *tmpc++ = 0;  }  mp_clamp(c);  return MP_OKAY;}#endif/* End: bn_mp_add_d.c *//* Start: bn_mp_addmod.c */#include <ltc_tommath.h>#ifdef BN_MP_ADDMOD_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* d = a + b (mod c) */intmp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d){  int     res;  mp_int  t;  if ((res = mp_init (&t)) != MP_OKAY) {    return res;  }  if ((res = mp_add (a, b, &t)) != MP_OKAY) {    mp_clear (&t);    return res;  }  res = mp_mod (&t, c, d);  mp_clear (&t);  return res;}#endif/* End: bn_mp_addmod.c *//* Start: bn_mp_and.c */#include <ltc_tommath.h>#ifdef BN_MP_AND_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* AND two ints together */intmp_and (mp_int * a, mp_int * b, mp_int * c){  int     res, ix, px;  mp_int  t, *x;  if (a->used > b->used) {    if ((res = mp_init_copy (&t, a)) != MP_OKAY) {      return res;    }    px = b->used;    x = b;  } else {    if ((res = mp_init_copy (&t, b)) != MP_OKAY) {      return res;    }    px = a->used;    x = a;  }  for (ix = 0; ix < px; ix++) {    t.dp[ix] &= x->dp[ix];  }  /* zero digits above the last from the smallest mp_int */  for (; ix < t.used; ix++) {    t.dp[ix] = 0;  }  mp_clamp (&t);  mp_exch (c, &t);  mp_clear (&t);  return MP_OKAY;}#endif/* End: bn_mp_and.c *//* Start: bn_mp_clamp.c */#include <ltc_tommath.h>#ifdef BN_MP_CLAMP_C/* LibTomMath, multiple-precision integer library -- Tom St Denis * * LibTomMath is a library that provides multiple-precision * integer arithmetic as well as number theoretic functionality. * * The library was designed directly after the MPI library by * Michael Fromberger but has been written from scratch with * additional optimizations in place. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.org *//* trim unused digits  * * This is used to ensure that leading zero digits are * trimed and the leading "used" digit will be non-zero * Typically very fast.  Also fixes the sign if there * are no more leading digits */voidmp_clamp (mp_int * a){  /* decrease used while the most significant digit is   * zero.   */  while (a->used > 0 && a->dp[a->used - 1] == 0) {    --(a->used);  }  /* reset the sign flag if used == 0 */  if (a->used == 0) {    a->sign = MP_ZPOS;  }}#endif/* End: bn_mp_clamp.c *//* Start: bn_mp_clear.c */#include <ltc_tommath.h>

⌨️ 快捷键说明

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