Line data Source code
1 : #include "tommath_private.h"
2 : #ifdef BN_MP_ROOT_U32_C
3 : /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 : /* SPDX-License-Identifier: Unlicense */
5 :
6 : /* find the n'th root of an integer
7 : *
8 : * Result found such that (c)**b <= a and (c+1)**b > a
9 : *
10 : * This algorithm uses Newton's approximation
11 : * x[i+1] = x[i] - f(x[i])/f'(x[i])
12 : * which will find the root in log(N) time where
13 : * each step involves a fair bit.
14 : */
15 0 : mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
16 : {
17 0 : mp_int t1, t2, t3, a_;
18 0 : mp_ord cmp;
19 0 : int ilog2;
20 0 : mp_err err;
21 :
22 : /* input must be positive if b is even */
23 0 : if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
24 0 : return MP_VAL;
25 : }
26 0 : if (b == 0) {
27 0 : return MP_VAL;
28 : }
29 :
30 0 : if ((err = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) {
31 0 : return err;
32 : }
33 :
34 : /* if a is negative fudge the sign but keep track */
35 0 : a_ = *a;
36 0 : a_.sign = MP_ZPOS;
37 :
38 : /* Compute seed: 2^(log_2(n)/b + 2)*/
39 0 : ilog2 = mp_count_bits(a);
40 :
41 : /*
42 : If "b" is larger than INT_MAX it is also larger than
43 : log_2(n) because the bit-length of the "n" is measured
44 : with an int and hence the root is always < 2 (two).
45 : */
46 0 : if (b > (uint32_t)(INT_MAX/2)) {
47 0 : mp_set(c, 1uL);
48 0 : c->sign = a->sign;
49 0 : err = MP_OKAY;
50 0 : goto LBL_ERR;
51 : }
52 :
53 : /* "b" is smaller than INT_MAX, we can cast safely */
54 0 : if (ilog2 < (int)b) {
55 0 : mp_set(c, 1uL);
56 0 : c->sign = a->sign;
57 0 : err = MP_OKAY;
58 0 : goto LBL_ERR;
59 : }
60 0 : ilog2 = ilog2 / ((int)b);
61 0 : if (ilog2 == 0) {
62 0 : mp_set(c, 1uL);
63 0 : c->sign = a->sign;
64 0 : err = MP_OKAY;
65 0 : goto LBL_ERR;
66 : }
67 : /* Start value must be larger than root */
68 0 : ilog2 += 2;
69 0 : if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY) goto LBL_ERR;
70 0 : do {
71 : /* t1 = t2 */
72 0 : if ((err = mp_copy(&t2, &t1)) != MP_OKAY) goto LBL_ERR;
73 :
74 : /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
75 :
76 : /* t3 = t1**(b-1) */
77 0 : if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY) goto LBL_ERR;
78 :
79 : /* numerator */
80 : /* t2 = t1**b */
81 0 : if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY) goto LBL_ERR;
82 :
83 : /* t2 = t1**b - a */
84 0 : if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY) goto LBL_ERR;
85 :
86 : /* denominator */
87 : /* t3 = t1**(b-1) * b */
88 0 : if ((err = mp_mul_d(&t3, b, &t3)) != MP_OKAY) goto LBL_ERR;
89 :
90 : /* t3 = (t1**b - a)/(b * t1**(b-1)) */
91 0 : if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) goto LBL_ERR;
92 :
93 0 : if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY) goto LBL_ERR;
94 :
95 : /*
96 : Number of rounds is at most log_2(root). If it is more it
97 : got stuck, so break out of the loop and do the rest manually.
98 : */
99 0 : if (ilog2-- == 0) {
100 0 : break;
101 : }
102 0 : } while (mp_cmp(&t1, &t2) != MP_EQ);
103 :
104 : /* result can be off by a few so check */
105 : /* Loop beneath can overshoot by one if found root is smaller than actual root */
106 0 : for (;;) {
107 0 : if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
108 0 : cmp = mp_cmp(&t2, &a_);
109 0 : if (cmp == MP_EQ) {
110 0 : err = MP_OKAY;
111 0 : goto LBL_ERR;
112 : }
113 0 : if (cmp == MP_LT) {
114 0 : if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
115 : } else {
116 0 : break;
117 : }
118 : }
119 : /* correct overshoot from above or from recurrence */
120 0 : for (;;) {
121 0 : if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
122 0 : if (mp_cmp(&t2, &a_) == MP_GT) {
123 0 : if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
124 : } else {
125 0 : break;
126 : }
127 : }
128 :
129 : /* set the result */
130 0 : mp_exch(&t1, c);
131 :
132 : /* set the sign of the result */
133 0 : c->sign = a->sign;
134 :
135 0 : err = MP_OKAY;
136 :
137 0 : LBL_ERR:
138 0 : mp_clear_multi(&t1, &t2, &t3, NULL);
139 0 : return err;
140 : }
141 :
142 : #endif
|