Skip to content

Commit a1417cf

Browse files
committed
Repaired tuning
1 parent 37ba942 commit a1417cf

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

etc/tune.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,21 @@ static uint64_t s_time_radix_conversion_read(int size)
188188
uint64_t t1;
189189

190190
/* "size" is given as "number of limbs" and starts at 8 */
191-
length = ((size_t)size - 7u) * MP_DIGIT_BIT;
192-
193-
/* Over-estimate number of base 10 digits */
194-
/* TODO: can overflow with small INT_MAX */
195-
length = (length * 28u) / 93u + 2u;
191+
length = (size_t)(size * MP_DIGIT_BIT);
192+
193+
/* Over-estimate number of base 10 digits
194+
Magick number: 28/93 = CF(log_10(2))_(p_3, q_3)
195+
*/
196+
written = (length * 28u);
197+
/* May happen e.g. if size > 2184 with MP_16BIT
198+
but cutoff should be about a couple of thousand bits
199+
at most (around or above Karatsuba cutoff).
200+
*/
201+
if (length != written / 28u) {
202+
t1 = UINT64_MAX;
203+
goto LBL_ERR_1;
204+
}
205+
length = written / 93u + 2u;
196206

197207
if ((err = random_number(&str_a, length)) != MP_OKAY) {
198208
t1 = UINT64_MAX;
@@ -659,9 +669,11 @@ int main(int argc, char **argv)
659669
if (test[n].fn != NULL) {
660670
s_run(test[n].name, test[n].fn, test[n].cutoff);
661671
/* TODO: can overflow for small INT_MAX */
662-
*test[n].update = ((*test[n].cutoff) * MP_DIGIT_BIT * 93)/28;
672+
*test[n].update = (*test[n].cutoff) * MP_DIGIT_BIT;
673+
*test[n].cutoff = INT_MAX;
663674
}
664675
}
676+
665677
}
666678
if (args.terse == 1) {
667679
printf("%d %d %d %d %d %d\n",

s_mp_faster_to_radix.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ static mp_err s_mp_to_radix_recursive(const mp_int *a, char **str, size_t *part_
6161
}
6262
/* Q = floor(A1 * I / 2^Beta) */
6363
/* I = floor( (2^(2*Beta)) / B) Here we have R[t] = I, P[t] = B */
64-
/* TODO: we don't need the full "a" only the upper part: a = a_1\beta + a_0 with 0 < a_0 < \beta
65-
(high short-product with s_mp_mul_high).
66-
Problem: s_mp_mul_high does not make use of fast multiplication except COMBA.
67-
(You can do it with Toom-Cook algorithms, but that is work for another day.) */#
68-
/*if( (err = s_mp_mul_high(a, &R[t], &q, (a->used/2) + 1) ) != MP_OKAY) goto LTM_ERR;*/
64+
/* TODO: We don't need the full "a" only the upper part: a = a_1\beta + a_0 with 0 < a_0 < \beta
65+
The cutoff with s_mp_mul_high is so low that the gap between that and the general cutoff
66+
is too small to be worth the hassle.
67+
But if somebody implements Thom Mulder's short products...
68+
(There are successors. See e.g. D. Harvey and P. Zimmermann "Short Division of Long Integers",
69+
Laszlo Hars "Fast Truncated Multiplication for Cryptographic Applications", Daniel Lemire "Exact Short
70+
Products From Truncated Multipliers", and many^Wsome more.
71+
*/
6972
if ((err = mp_mul(a, &R[t], &q)) != MP_OKAY) goto LTM_ERR;
7073
if ((err = mp_div_2d(&q, Beta, &q, NULL)) != MP_OKAY) goto LTM_ERR;
7174

0 commit comments

Comments
 (0)