bn_mp_exteuclid.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <tommath.h>
  2. #ifdef BN_MP_EXTEUCLID_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  4. *
  5. * LibTomMath is a library that provides multiple-precision
  6. * integer arithmetic as well as number theoretic functionality.
  7. *
  8. * The library was designed directly after the MPI library by
  9. * Michael Fromberger but has been written from scratch with
  10. * additional optimizations in place.
  11. *
  12. * The library is free for all purposes without any express
  13. * guarantee it works.
  14. *
  15. * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
  16. */
  17. /* Extended euclidean algorithm of (a, b) produces
  18. a*u1 + b*u2 = u3
  19. */
  20. int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
  21. {
  22. mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp;
  23. int err;
  24. if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) {
  25. return err;
  26. }
  27. /* initialize, (u1,u2,u3) = (1,0,a) */
  28. mp_set(&u1, 1);
  29. if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto _ERR; }
  30. /* initialize, (v1,v2,v3) = (0,1,b) */
  31. mp_set(&v2, 1);
  32. if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto _ERR; }
  33. /* loop while v3 != 0 */
  34. while (mp_iszero(&v3) == MP_NO) {
  35. /* q = u3/v3 */
  36. if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto _ERR; }
  37. /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
  38. if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto _ERR; }
  39. if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto _ERR; }
  40. if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto _ERR; }
  41. if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto _ERR; }
  42. if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto _ERR; }
  43. if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto _ERR; }
  44. /* (u1,u2,u3) = (v1,v2,v3) */
  45. if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto _ERR; }
  46. if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto _ERR; }
  47. if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto _ERR; }
  48. /* (v1,v2,v3) = (t1,t2,t3) */
  49. if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto _ERR; }
  50. if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto _ERR; }
  51. if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto _ERR; }
  52. }
  53. /* make sure U3 >= 0 */
  54. if (u3.sign == MP_NEG) {
  55. mp_neg(&u1, &u1);
  56. mp_neg(&u2, &u2);
  57. mp_neg(&u3, &u3);
  58. }
  59. /* copy result out */
  60. if (U1 != NULL) { mp_exch(U1, &u1); }
  61. if (U2 != NULL) { mp_exch(U2, &u2); }
  62. if (U3 != NULL) { mp_exch(U3, &u3); }
  63. err = MP_OKAY;
  64. _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
  65. return err;
  66. }
  67. #endif
  68. /* $Source: /cvs/libtom/libtommath/bn_mp_exteuclid.c,v $ */
  69. /* $Revision: 1.3 $ */
  70. /* $Date: 2006/03/31 14:18:44 $ */