LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/hcrypto/libtommath - bn_s_mp_invmod_fast.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 41 48 85.4 %
Date: 2024-04-21 15:09:00 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "tommath_private.h"
       2             : #ifdef BN_S_MP_INVMOD_FAST_C
       3             : /* LibTomMath, multiple-precision integer library -- Tom St Denis */
       4             : /* SPDX-License-Identifier: Unlicense */
       5             : 
       6             : /* computes the modular inverse via binary extended euclidean algorithm,
       7             :  * that is c = 1/a mod b
       8             :  *
       9             :  * Based on slow invmod except this is optimized for the case where b is
      10             :  * odd as per HAC Note 14.64 on pp. 610
      11             :  */
      12         139 : mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c)
      13             : {
      14           8 :    mp_int  x, y, u, v, B, D;
      15           8 :    mp_sign neg;
      16           8 :    mp_err  err;
      17             : 
      18             :    /* 2. [modified] b must be odd   */
      19         139 :    if (MP_IS_EVEN(b)) {
      20           0 :       return MP_VAL;
      21             :    }
      22             : 
      23             :    /* init all our temps */
      24         139 :    if ((err = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
      25           0 :       return err;
      26             :    }
      27             : 
      28             :    /* x == modulus, y == value to invert */
      29         139 :    if ((err = mp_copy(b, &x)) != MP_OKAY)                         goto LBL_ERR;
      30             : 
      31             :    /* we need y = |a| */
      32         139 :    if ((err = mp_mod(a, b, &y)) != MP_OKAY)                       goto LBL_ERR;
      33             : 
      34             :    /* if one of x,y is zero return an error! */
      35         139 :    if (MP_IS_ZERO(&x) || MP_IS_ZERO(&y)) {
      36           0 :       err = MP_VAL;
      37           0 :       goto LBL_ERR;
      38             :    }
      39             : 
      40             :    /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
      41         139 :    if ((err = mp_copy(&x, &u)) != MP_OKAY)                        goto LBL_ERR;
      42         139 :    if ((err = mp_copy(&y, &v)) != MP_OKAY)                        goto LBL_ERR;
      43         139 :    mp_set(&D, 1uL);
      44             : 
      45      320335 : top:
      46             :    /* 4.  while u is even do */
      47      687667 :    while (MP_IS_EVEN(&u)) {
      48             :       /* 4.1 u = u/2 */
      49      344197 :       if ((err = mp_div_2(&u, &u)) != MP_OKAY)                    goto LBL_ERR;
      50             : 
      51             :       /* 4.2 if B is odd then */
      52      344197 :       if (MP_IS_ODD(&B)) {
      53      172593 :          if ((err = mp_sub(&B, &x, &B)) != MP_OKAY)               goto LBL_ERR;
      54             :       }
      55             :       /* B = B/2 */
      56      344197 :       if ((err = mp_div_2(&B, &B)) != MP_OKAY)                    goto LBL_ERR;
      57             :    }
      58             : 
      59             :    /* 5.  while v is even do */
      60      688193 :    while (MP_IS_EVEN(&v)) {
      61             :       /* 5.1 v = v/2 */
      62      344723 :       if ((err = mp_div_2(&v, &v)) != MP_OKAY)                    goto LBL_ERR;
      63             : 
      64             :       /* 5.2 if D is odd then */
      65      344723 :       if (MP_IS_ODD(&D)) {
      66             :          /* D = (D-x)/2 */
      67      172169 :          if ((err = mp_sub(&D, &x, &D)) != MP_OKAY)               goto LBL_ERR;
      68             :       }
      69             :       /* D = D/2 */
      70      344723 :       if ((err = mp_div_2(&D, &D)) != MP_OKAY)                    goto LBL_ERR;
      71             :    }
      72             : 
      73             :    /* 6.  if u >= v then */
      74      343470 :    if (mp_cmp(&u, &v) != MP_LT) {
      75             :       /* u = u - v, B = B - D */
      76      171444 :       if ((err = mp_sub(&u, &v, &u)) != MP_OKAY)                  goto LBL_ERR;
      77             : 
      78      171444 :       if ((err = mp_sub(&B, &D, &B)) != MP_OKAY)                  goto LBL_ERR;
      79             :    } else {
      80             :       /* v - v - u, D = D - B */
      81      172026 :       if ((err = mp_sub(&v, &u, &v)) != MP_OKAY)                  goto LBL_ERR;
      82             : 
      83      172026 :       if ((err = mp_sub(&D, &B, &D)) != MP_OKAY)                  goto LBL_ERR;
      84             :    }
      85             : 
      86             :    /* if not zero goto step 4 */
      87      343470 :    if (!MP_IS_ZERO(&u)) {
      88      343331 :       goto top;
      89             :    }
      90             : 
      91             :    /* now a = C, b = D, gcd == g*v */
      92             : 
      93             :    /* if v != 1 then there is no inverse */
      94         139 :    if (mp_cmp_d(&v, 1uL) != MP_EQ) {
      95           0 :       err = MP_VAL;
      96           0 :       goto LBL_ERR;
      97             :    }
      98             : 
      99             :    /* b is now the inverse */
     100         139 :    neg = a->sign;
     101         261 :    while (D.sign == MP_NEG) {
     102         122 :       if ((err = mp_add(&D, b, &D)) != MP_OKAY)                   goto LBL_ERR;
     103             :    }
     104             : 
     105             :    /* too big */
     106         139 :    while (mp_cmp_mag(&D, b) != MP_LT) {
     107           0 :       if ((err = mp_sub(&D, b, &D)) != MP_OKAY)                   goto LBL_ERR;
     108             :    }
     109             : 
     110         139 :    mp_exch(&D, c);
     111         139 :    c->sign = neg;
     112         139 :    err = MP_OKAY;
     113             : 
     114         139 : LBL_ERR:
     115         139 :    mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
     116         139 :    return err;
     117             : }
     118             : #endif

Generated by: LCOV version 1.14