Skip to content

Commit

Permalink
Bug 1194073 - removing mp_sqrt, r=rbarnes
Browse files Browse the repository at this point in the history
try: -b do -p linux64-asan -u cipher

--HG--
extra : rebase_source : d2262d0dc6ca0ed3ca1a153a4ef6a9769612758c
  • Loading branch information
franziskuskiefer committed Sep 27, 2016
1 parent 32032dc commit 10d2159
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 133 deletions.
1 change: 0 additions & 1 deletion lib/freebl/mpi/README
Expand Up @@ -234,7 +234,6 @@ mp_div(a, b, q, r) - computes q, r such that a = bq + r
mp_div_2d(a, d, q, r) - computes q = a / 2^d, r = a % 2^d
mp_expt(a, b, c) - computes c = a ** b
mp_2expt(a, k) - computes a = 2^k
mp_sqrt(a, c) - computes c = floor(sqrt(a))

The mp_div_2d() function efficiently computes division by powers of
two. Either the q or r parameter may be NULL, in which case that
Expand Down
33 changes: 0 additions & 33 deletions lib/freebl/mpi/mpi-test.c
Expand Up @@ -1181,39 +1181,6 @@ test_2expt(void)

/*------------------------------------------------------------------------*/

int
test_sqrt(void)
{
mp_int a;
int res = 0;

mp_init(&a);
mp_read_radix(&a, mp9, 16);
mp_sqrt(&a, &a);
mp_toradix(&a, g_intbuf, 16);

if (strcmp(g_intbuf, t_mp9) != 0) {
reason("error: computed %s, expected %s\n", g_intbuf, t_mp9);
res = 1;
goto CLEANUP;
}

mp_read_radix(&a, mp15, 16);
mp_sqrt(&a, &a);
mp_toradix(&a, g_intbuf, 16);

if (strcmp(g_intbuf, t_mp15) != 0) {
reason("error: computed %s, expected %s\n", g_intbuf, t_mp15);
res = 1;
}

CLEANUP:
mp_clear(&a);
return res;
}

/*------------------------------------------------------------------------*/

int
test_mod_d(void)
{
Expand Down
82 changes: 0 additions & 82 deletions lib/freebl/mpi/mpi.c
Expand Up @@ -1297,88 +1297,6 @@ mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c)

/* }}} */

/* {{{ mp_sqrt(a, b) */

/*
mp_sqrt(a, b)
Compute the integer square root of a, and store the result in b.
Uses an integer-arithmetic version of Newton's iterative linear
approximation technique to determine this value; the result has the
following two properties:
b^2 <= a
(b+1)^2 >= a
It is a range error to pass a negative value.
*/
mp_err
mp_sqrt(const mp_int *a, mp_int *b)
{
mp_int x, t;
mp_err res;
mp_size used;

ARGCHK(a != NULL && b != NULL, MP_BADARG);

/* Cannot take square root of a negative value */
if (SIGN(a) == NEG)
return MP_RANGE;

/* Special cases for zero and one, trivial */
if (mp_cmp_d(a, 1) <= 0)
return mp_copy(a, b);

/* Initialize the temporaries we'll use below */
if ((res = mp_init_size(&t, USED(a))) != MP_OKAY)
return res;

/* Compute an initial guess for the iteration as a itself */
if ((res = mp_init_copy(&x, a)) != MP_OKAY)
goto X;

used = MP_USED(&x);
if (used > 1) {
s_mp_rshd(&x, used / 2);
}

for (;;) {
/* t = (x * x) - a */
if ((res = mp_copy(&x, &t)) != MP_OKAY ||
(res = mp_sqr(&t, &t)) != MP_OKAY ||
(res = mp_sub(&t, a, &t)) != MP_OKAY)
goto CLEANUP;

/* t = t / 2x */
s_mp_mul_2(&x);
if ((res = mp_div(&t, &x, &t, NULL)) != MP_OKAY)
goto CLEANUP;
s_mp_div_2(&x);

/* Terminate the loop, if the quotient is zero */
if (mp_cmp_z(&t) == MP_EQ)
break;

/* x = x - t */
if ((res = mp_sub(&x, &t, &x)) != MP_OKAY)
goto CLEANUP;
}

/* Copy result to output parameter */
MP_CHECKOK(mp_sub_d(&x, 1, &x));
s_mp_exch(&x, b);

CLEANUP:
mp_clear(&x);
X:
mp_clear(&t);

return res;

} /* end mp_sqrt() */

/* }}} */

/* }}} */

/*------------------------------------------------------------------------*/
Expand Down
1 change: 0 additions & 1 deletion lib/freebl/mpi/mpi.h
Expand Up @@ -196,7 +196,6 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r);
mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r);
mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c);
mp_err mp_2expt(mp_int *a, mp_digit k);
mp_err mp_sqrt(const mp_int *a, mp_int *b);

/* Modular arithmetic */
#if MP_MODARITH
Expand Down
16 changes: 0 additions & 16 deletions lib/freebl/mpi/tests/mptest-3.c
Expand Up @@ -17,7 +17,6 @@

#include "mpi.h"

#define SQRT 1 /* define nonzero to get square-root test */
#define EXPT 0 /* define nonzero to get exponentiate test */

int
Expand Down Expand Up @@ -97,21 +96,6 @@ main(int argc, char *argv[])
mp_print(&c, stdout);
fputc('\n', stdout);

#if SQRT
printf("\nc = sqrt(a)\n");
if ((res = mp_sqrt(&a, &c)) != MP_OKAY) {
printf("mp_sqrt: %s\n", mp_strerror(res));
} else {
printf("c = ");
mp_print(&c, stdout);
fputc('\n', stdout);
mp_sqr(&c, &c);
printf("c^2 = ");
mp_print(&c, stdout);
fputc('\n', stdout);
}
#endif

mp_clear(&d);
mp_clear(&c);
mp_clear(&b);
Expand Down

0 comments on commit 10d2159

Please sign in to comment.