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

          Line data    Source code
       1             : #include <tommath.h>
       2             : #ifdef BN_MP_MONTGOMERY_SETUP_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             : /* setups the montgomery reduction stuff */
      19             : int
      20         872 : mp_montgomery_setup (mp_int * n, mp_digit * rho)
      21             : {
      22             :   mp_digit x, b;
      23             : 
      24             : /* fast inversion mod 2**k
      25             :  *
      26             :  * Based on the fact that
      27             :  *
      28             :  * XA = 1 (mod 2**n)  =>  (X(2-XA)) A = 1 (mod 2**2n)
      29             :  *                    =>  2*X*A - X*X*A*A = 1
      30             :  *                    =>  2*(1) - (1)     = 1
      31             :  */
      32         872 :   b = n->dp[0];
      33             : 
      34         872 :   if ((b & 1) == 0) {
      35           0 :     return MP_VAL;
      36             :   }
      37             : 
      38         872 :   x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
      39         872 :   x *= 2 - b * x;               /* here x*a==1 mod 2**8 */
      40             : #if !defined(MP_8BIT)
      41         872 :   x *= 2 - b * x;               /* here x*a==1 mod 2**16 */
      42             : #endif
      43             : #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
      44         872 :   x *= 2 - b * x;               /* here x*a==1 mod 2**32 */
      45             : #endif
      46             : #ifdef MP_64BIT
      47         872 :   x *= 2 - b * x;               /* here x*a==1 mod 2**64 */
      48             : #endif
      49             : 
      50             :   /* rho = -1/m mod b */
      51         872 :   *rho = (unsigned long)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
      52             : 
      53         872 :   return MP_OKAY;
      54             : }
      55             : #endif
      56             : 
      57             : /* $Source: /cvs/libtom/libtommath/bn_mp_montgomery_setup.c,v $ */
      58             : /* $Revision: 1.5 $ */
      59             : /* $Date: 2006/12/28 01:25:13 $ */

Generated by: LCOV version 1.13