LCOV - code coverage report
Current view: top level - source4/heimdal/lib/hcrypto/libtommath - bn_mp_sqrt.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 0 28 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_SQRT_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             : /* this function is less generic than mp_n_root, simpler and faster */
      19           0 : int mp_sqrt(mp_int *arg, mp_int *ret)
      20             : {
      21             :   int res;
      22             :   mp_int t1,t2;
      23             : 
      24             :   /* must be positive */
      25           0 :   if (arg->sign == MP_NEG) {
      26           0 :     return MP_VAL;
      27             :   }
      28             : 
      29             :   /* easy out */
      30           0 :   if (mp_iszero(arg) == MP_YES) {
      31           0 :     mp_zero(ret);
      32           0 :     return MP_OKAY;
      33             :   }
      34             : 
      35           0 :   if ((res = mp_init_copy(&t1, arg)) != MP_OKAY) {
      36           0 :     return res;
      37             :   }
      38             : 
      39           0 :   if ((res = mp_init(&t2)) != MP_OKAY) {
      40           0 :     goto E2;
      41             :   }
      42             : 
      43             :   /* First approx. (not very bad for large arg) */
      44           0 :   mp_rshd (&t1,t1.used/2);
      45             : 
      46             :   /* t1 > 0  */
      47           0 :   if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
      48           0 :     goto E1;
      49             :   }
      50           0 :   if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) {
      51           0 :     goto E1;
      52             :   }
      53           0 :   if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) {
      54           0 :     goto E1;
      55             :   }
      56             :   /* And now t1 > sqrt(arg) */
      57             :   do {
      58           0 :     if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
      59           0 :       goto E1;
      60             :     }
      61           0 :     if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) {
      62           0 :       goto E1;
      63             :     }
      64           0 :     if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) {
      65           0 :       goto E1;
      66             :     }
      67             :     /* t1 >= sqrt(arg) >= t2 at this point */
      68           0 :   } while (mp_cmp_mag(&t1,&t2) == MP_GT);
      69             : 
      70           0 :   mp_exch(&t1,ret);
      71             : 
      72           0 : E1: mp_clear(&t2);
      73           0 : E2: mp_clear(&t1);
      74           0 :   return res;
      75             : }
      76             : 
      77             : #endif
      78             : 
      79             : /* $Source: /cvs/libtom/libtommath/bn_mp_sqrt.c,v $ */
      80             : /* $Revision: 1.4 $ */
      81             : /* $Date: 2006/12/28 01:25:13 $ */

Generated by: LCOV version 1.13