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

          Line data    Source code
       1             : #include "tommath_private.h"
       2             : #ifdef BN_MP_REDUCE_C
       3             : /* LibTomMath, multiple-precision integer library -- Tom St Denis */
       4             : /* SPDX-License-Identifier: Unlicense */
       5             : 
       6             : /* reduces x mod m, assumes 0 < x < m**2, mu is
       7             :  * precomputed via mp_reduce_setup.
       8             :  * From HAC pp.604 Algorithm 14.42
       9             :  */
      10           0 : mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
      11             : {
      12           0 :    mp_int  q;
      13           0 :    mp_err  err;
      14           0 :    int     um = m->used;
      15             : 
      16             :    /* q = x */
      17           0 :    if ((err = mp_init_copy(&q, x)) != MP_OKAY) {
      18           0 :       return err;
      19             :    }
      20             : 
      21             :    /* q1 = x / b**(k-1)  */
      22           0 :    mp_rshd(&q, um - 1);
      23             : 
      24             :    /* according to HAC this optimization is ok */
      25           0 :    if ((mp_digit)um > ((mp_digit)1 << (MP_DIGIT_BIT - 1))) {
      26           0 :       if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) {
      27           0 :          goto CLEANUP;
      28             :       }
      29           0 :    } else if (MP_HAS(S_MP_MUL_HIGH_DIGS)) {
      30           0 :       if ((err = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
      31           0 :          goto CLEANUP;
      32             :       }
      33             :    } else if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)) {
      34             :       if ((err = s_mp_mul_high_digs_fast(&q, mu, &q, um)) != MP_OKAY) {
      35             :          goto CLEANUP;
      36             :       }
      37             :    } else {
      38             :       err = MP_VAL;
      39             :       goto CLEANUP;
      40             :    }
      41             : 
      42             :    /* q3 = q2 / b**(k+1) */
      43           0 :    mp_rshd(&q, um + 1);
      44             : 
      45             :    /* x = x mod b**(k+1), quick (no division) */
      46           0 :    if ((err = mp_mod_2d(x, MP_DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
      47           0 :       goto CLEANUP;
      48             :    }
      49             : 
      50             :    /* q = q * m mod b**(k+1), quick (no division) */
      51           0 :    if ((err = s_mp_mul_digs(&q, m, &q, um + 1)) != MP_OKAY) {
      52           0 :       goto CLEANUP;
      53             :    }
      54             : 
      55             :    /* x = x - q */
      56           0 :    if ((err = mp_sub(x, &q, x)) != MP_OKAY) {
      57           0 :       goto CLEANUP;
      58             :    }
      59             : 
      60             :    /* If x < 0, add b**(k+1) to it */
      61           0 :    if (mp_cmp_d(x, 0uL) == MP_LT) {
      62           0 :       mp_set(&q, 1uL);
      63           0 :       if ((err = mp_lshd(&q, um + 1)) != MP_OKAY) {
      64           0 :          goto CLEANUP;
      65             :       }
      66           0 :       if ((err = mp_add(x, &q, x)) != MP_OKAY) {
      67           0 :          goto CLEANUP;
      68             :       }
      69             :    }
      70             : 
      71             :    /* Back off if it's too big */
      72           0 :    while (mp_cmp(x, m) != MP_LT) {
      73           0 :       if ((err = s_mp_sub(x, m, x)) != MP_OKAY) {
      74           0 :          goto CLEANUP;
      75             :       }
      76             :    }
      77             : 
      78           0 : CLEANUP:
      79           0 :    mp_clear(&q);
      80             : 
      81           0 :    return err;
      82             : }
      83             : #endif

Generated by: LCOV version 1.14