comparison libtommath/bn_mp_root_u32.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
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 mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
16 {
17 mp_int t1, t2, t3, a_;
18 mp_ord cmp;
19 int ilog2;
20 mp_err err;
21
22 /* input must be positive if b is even */
23 if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
24 return MP_VAL;
25 }
26
27 if ((err = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) {
28 return err;
29 }
30
31 /* if a is negative fudge the sign but keep track */
32 a_ = *a;
33 a_.sign = MP_ZPOS;
34
35 /* Compute seed: 2^(log_2(n)/b + 2)*/
36 ilog2 = mp_count_bits(a);
37
38 /*
39 If "b" is larger than INT_MAX it is also larger than
40 log_2(n) because the bit-length of the "n" is measured
41 with an int and hence the root is always < 2 (two).
42 */
43 if (b > (uint32_t)(INT_MAX/2)) {
44 mp_set(c, 1uL);
45 c->sign = a->sign;
46 err = MP_OKAY;
47 goto LBL_ERR;
48 }
49
50 /* "b" is smaller than INT_MAX, we can cast safely */
51 if (ilog2 < (int)b) {
52 mp_set(c, 1uL);
53 c->sign = a->sign;
54 err = MP_OKAY;
55 goto LBL_ERR;
56 }
57 ilog2 = ilog2 / ((int)b);
58 if (ilog2 == 0) {
59 mp_set(c, 1uL);
60 c->sign = a->sign;
61 err = MP_OKAY;
62 goto LBL_ERR;
63 }
64 /* Start value must be larger than root */
65 ilog2 += 2;
66 if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY) goto LBL_ERR;
67 do {
68 /* t1 = t2 */
69 if ((err = mp_copy(&t2, &t1)) != MP_OKAY) goto LBL_ERR;
70
71 /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
72
73 /* t3 = t1**(b-1) */
74 if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY) goto LBL_ERR;
75
76 /* numerator */
77 /* t2 = t1**b */
78 if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY) goto LBL_ERR;
79
80 /* t2 = t1**b - a */
81 if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY) goto LBL_ERR;
82
83 /* denominator */
84 /* t3 = t1**(b-1) * b */
85 if ((err = mp_mul_d(&t3, b, &t3)) != MP_OKAY) goto LBL_ERR;
86
87 /* t3 = (t1**b - a)/(b * t1**(b-1)) */
88 if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) goto LBL_ERR;
89
90 if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY) goto LBL_ERR;
91
92 /*
93 Number of rounds is at most log_2(root). If it is more it
94 got stuck, so break out of the loop and do the rest manually.
95 */
96 if (ilog2-- == 0) {
97 break;
98 }
99 } while (mp_cmp(&t1, &t2) != MP_EQ);
100
101 /* result can be off by a few so check */
102 /* Loop beneath can overshoot by one if found root is smaller than actual root */
103 for (;;) {
104 if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
105 cmp = mp_cmp(&t2, &a_);
106 if (cmp == MP_EQ) {
107 err = MP_OKAY;
108 goto LBL_ERR;
109 }
110 if (cmp == MP_LT) {
111 if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
112 } else {
113 break;
114 }
115 }
116 /* correct overshoot from above or from recurrence */
117 for (;;) {
118 if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
119 if (mp_cmp(&t2, &a_) == MP_GT) {
120 if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
121 } else {
122 break;
123 }
124 }
125
126 /* set the result */
127 mp_exch(&t1, c);
128
129 /* set the sign of the result */
130 c->sign = a->sign;
131
132 err = MP_OKAY;
133
134 LBL_ERR:
135 mp_clear_multi(&t1, &t2, &t3, NULL);
136 return err;
137 }
138
139 #endif