LCOV - code coverage report
Current view: top level - source4/heimdal/lib/hcrypto/libtommath - bn_mp_exteuclid.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 0 31 0.0 %
Date: 2021-09-23 10:06:22 Functions: 0 1 0.0 %

          Line data    Source code
       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://libtom.org
      16             :  */
      17             : 
      18             : /* Extended euclidean algorithm of (a, b) produces
      19             :    a*u1 + b*u2 = u3
      20             :  */
      21           0 : int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
      22             : {
      23             :    mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp;
      24             :    int err;
      25             : 
      26           0 :    if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) {
      27           0 :       return err;
      28             :    }
      29             : 
      30             :    /* initialize, (u1,u2,u3) = (1,0,a) */
      31           0 :    mp_set(&u1, 1);
      32           0 :    if ((err = mp_copy(a, &u3)) != MP_OKAY)                                        { goto _ERR; }
      33             : 
      34             :    /* initialize, (v1,v2,v3) = (0,1,b) */
      35           0 :    mp_set(&v2, 1);
      36           0 :    if ((err = mp_copy(b, &v3)) != MP_OKAY)                                        { goto _ERR; }
      37             : 
      38             :    /* loop while v3 != 0 */
      39           0 :    while (mp_iszero(&v3) == MP_NO) {
      40             :        /* q = u3/v3 */
      41           0 :        if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY)                         { goto _ERR; }
      42             : 
      43             :        /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
      44           0 :        if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY)                              { goto _ERR; }
      45           0 :        if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY)                             { goto _ERR; }
      46           0 :        if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY)                              { goto _ERR; }
      47           0 :        if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY)                             { goto _ERR; }
      48           0 :        if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY)                              { goto _ERR; }
      49           0 :        if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY)                             { goto _ERR; }
      50             : 
      51             :        /* (u1,u2,u3) = (v1,v2,v3) */
      52           0 :        if ((err = mp_copy(&v1, &u1)) != MP_OKAY)                                  { goto _ERR; }
      53           0 :        if ((err = mp_copy(&v2, &u2)) != MP_OKAY)                                  { goto _ERR; }
      54           0 :        if ((err = mp_copy(&v3, &u3)) != MP_OKAY)                                  { goto _ERR; }
      55             : 
      56             :        /* (v1,v2,v3) = (t1,t2,t3) */
      57           0 :        if ((err = mp_copy(&t1, &v1)) != MP_OKAY)                                  { goto _ERR; }
      58           0 :        if ((err = mp_copy(&t2, &v2)) != MP_OKAY)                                  { goto _ERR; }
      59           0 :        if ((err = mp_copy(&t3, &v3)) != MP_OKAY)                                  { goto _ERR; }
      60             :    }
      61             : 
      62             :    /* make sure U3 >= 0 */
      63           0 :    if (u3.sign == MP_NEG) {
      64           0 :       mp_neg(&u1, &u1);
      65           0 :       mp_neg(&u2, &u2);
      66           0 :       mp_neg(&u3, &u3);
      67             :    }
      68             : 
      69             :    /* copy result out */
      70           0 :    if (U1 != NULL) { mp_exch(U1, &u1); }
      71           0 :    if (U2 != NULL) { mp_exch(U2, &u2); }
      72           0 :    if (U3 != NULL) { mp_exch(U3, &u3); }
      73             : 
      74           0 :    err = MP_OKAY;
      75           0 : _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
      76           0 :    return err;
      77             : }
      78             : #endif
      79             : 
      80             : /* $Source: /cvs/libtom/libtommath/bn_mp_exteuclid.c,v $ */
      81             : /* $Revision: 1.4 $ */
      82             : /* $Date: 2006/12/28 01:25:13 $ */

Generated by: LCOV version 1.13