diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2024-03-28 12:51:50 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2024-03-28 12:51:50 (GMT) |
commit | 402e47970bb3d0b07bd25cb83947972ff1e822ff (patch) | |
tree | f33f1ae9067da8fa725475047cc7776722833023 | |
parent | 075caaccba6d04a49964805c51f76d3ac141b7c9 (diff) | |
download | tcl-402e47970bb3d0b07bd25cb83947972ff1e822ff.zip tcl-402e47970bb3d0b07bd25cb83947972ff1e822ff.tar.gz tcl-402e47970bb3d0b07bd25cb83947972ff1e822ff.tar.bz2 |
C++ improvements/typo's
-rw-r--r-- | libtommath/bn_mp_div.c | 4 | ||||
-rw-r--r-- | libtommath/bn_mp_exptmod.c | 4 | ||||
-rw-r--r-- | libtommath/bn_mp_exteuclid.c | 4 | ||||
-rw-r--r-- | libtommath/bn_mp_lcm.c | 4 | ||||
-rw-r--r-- | libtommath/bn_mp_mul.c | 2 | ||||
-rw-r--r-- | libtommath/bn_mp_prime_frobenius_underwood.c | 4 | ||||
-rw-r--r-- | libtommath/bn_mp_prime_strong_lucas_selfridge.c | 4 | ||||
-rw-r--r-- | libtommath/bn_mp_root_n.c | 4 | ||||
-rw-r--r-- | libtommath/bn_mp_sqrt.c | 73 | ||||
-rw-r--r-- | libtommath/bn_mp_sqrtmod_prime.c | 4 | ||||
-rw-r--r-- | libtommath/bn_s_mp_balance_mul.c | 4 | ||||
-rw-r--r-- | libtommath/bn_s_mp_invmod_fast.c | 4 | ||||
-rw-r--r-- | libtommath/bn_s_mp_invmod_slow.c | 4 | ||||
-rw-r--r-- | libtommath/bn_s_mp_log.c | 4 | ||||
-rw-r--r-- | libtommath/bn_s_mp_mul_high_digs_fast.c | 4 | ||||
-rw-r--r-- | libtommath/bn_s_mp_rand_jenkins.c | 4 | ||||
-rw-r--r-- | libtommath/bn_s_mp_toom_mul.c | 4 | ||||
-rw-r--r-- | libtommath/changes.txt | 2 |
18 files changed, 105 insertions, 32 deletions
diff --git a/libtommath/bn_mp_div.c b/libtommath/bn_mp_div.c index 71de55b..bca227d 100644 --- a/libtommath/bn_mp_div.c +++ b/libtommath/bn_mp_div.c @@ -31,7 +31,7 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) } /* init our temps */ - if ((err = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&ta, &tb, &tq, &q, (void *)NULL)) != MP_OKAY) { return err; } @@ -64,7 +64,7 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) d->sign = MP_IS_ZERO(d) ? MP_ZPOS : n; } LBL_ERR: - mp_clear_multi(&ta, &tb, &tq, &q, NULL); + mp_clear_multi(&ta, &tb, &tq, &q, (void *)NULL); return err; } diff --git a/libtommath/bn_mp_exptmod.c b/libtommath/bn_mp_exptmod.c index 5f811eb..1ca2e93 100644 --- a/libtommath/bn_mp_exptmod.c +++ b/libtommath/bn_mp_exptmod.c @@ -26,7 +26,7 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) return MP_VAL; } - if ((err = mp_init_multi(&tmpG, &tmpX, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&tmpG, &tmpX, (void *)NULL)) != MP_OKAY) { return err; } @@ -43,7 +43,7 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) /* and now compute (1/G)**|X| instead of G**X [X < 0] */ err = mp_exptmod(&tmpG, &tmpX, P, Y); LBL_ERR: - mp_clear_multi(&tmpG, &tmpX, NULL); + mp_clear_multi(&tmpG, &tmpX, (void *)NULL); return err; } diff --git a/libtommath/bn_mp_exteuclid.c b/libtommath/bn_mp_exteuclid.c index faf47ba..fbac454 100644 --- a/libtommath/bn_mp_exteuclid.c +++ b/libtommath/bn_mp_exteuclid.c @@ -11,7 +11,7 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp; mp_err err; - if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, (void *)NULL)) != MP_OKAY) { return err; } @@ -67,7 +67,7 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp err = MP_OKAY; LBL_ERR: - mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL); + mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, (void *)NULL); return err; } #endif diff --git a/libtommath/bn_mp_lcm.c b/libtommath/bn_mp_lcm.c index c32b269..5be3fc6 100644 --- a/libtommath/bn_mp_lcm.c +++ b/libtommath/bn_mp_lcm.c @@ -10,7 +10,7 @@ mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) mp_int t1, t2; - if ((err = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&t1, &t2, (void *)NULL)) != MP_OKAY) { return err; } @@ -38,7 +38,7 @@ mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) c->sign = MP_ZPOS; LBL_T: - mp_clear_multi(&t1, &t2, NULL); + mp_clear_multi(&t1, &t2, (void *)NULL); return err; } #endif diff --git a/libtommath/bn_mp_mul.c b/libtommath/bn_mp_mul.c index 561913a..91707cd 100644 --- a/libtommath/bn_mp_mul.c +++ b/libtommath/bn_mp_mul.c @@ -17,7 +17,7 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) * The bigger one needs to be at least about one MP_KARATSUBA_MUL_CUTOFF bigger * to make some sense, but it depends on architecture, OS, position of the * stars... so YMMV. - * Using it to cut the input into slices small enough for fast_s_mp_mul_digs + * Using it to cut the input into slices small enough for s_mp_mul_digs_fast * was actually slower on the author's machine, but YMMV. */ (min_len >= MP_KARATSUBA_MUL_CUTOFF) && diff --git a/libtommath/bn_mp_prime_frobenius_underwood.c b/libtommath/bn_mp_prime_frobenius_underwood.c index 253e8d5..be4bd08 100644 --- a/libtommath/bn_mp_prime_frobenius_underwood.c +++ b/libtommath/bn_mp_prime_frobenius_underwood.c @@ -32,7 +32,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) *result = MP_NO; - if ((err = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, (void *)NULL)) != MP_OKAY) { return err; } @@ -124,7 +124,7 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) } LBL_FU_ERR: - mp_clear_multi(&tz, &sz, &Np1z, &T2z, &T1z, NULL); + mp_clear_multi(&tz, &sz, &Np1z, &T2z, &T1z, (void *)NULL); return err; } diff --git a/libtommath/bn_mp_prime_strong_lucas_selfridge.c b/libtommath/bn_mp_prime_strong_lucas_selfridge.c index b50bbcd..617648c 100644 --- a/libtommath/bn_mp_prime_strong_lucas_selfridge.c +++ b/libtommath/bn_mp_prime_strong_lucas_selfridge.c @@ -73,7 +73,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) */ if ((err = mp_init_multi(&Dz, &gcd, &Np1, &Uz, &Vz, &U2mz, &V2mz, &Qmz, &Q2mz, &Qkdz, &T1z, &T2z, &T3z, &T4z, &Q2kdz, - NULL)) != MP_OKAY) { + (void *)NULL)) != MP_OKAY) { return err; } @@ -281,7 +281,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) } } LBL_LS_ERR: - mp_clear_multi(&Q2kdz, &T4z, &T3z, &T2z, &T1z, &Qkdz, &Q2mz, &Qmz, &V2mz, &U2mz, &Vz, &Uz, &Np1, &gcd, &Dz, NULL); + mp_clear_multi(&Q2kdz, &T4z, &T3z, &T2z, &T1z, &Qkdz, &Q2mz, &Qmz, &V2mz, &U2mz, &Vz, &Uz, &Np1, &gcd, &Dz, (void *)NULL); return err; } #endif diff --git a/libtommath/bn_mp_root_n.c b/libtommath/bn_mp_root_n.c index 5b92ff5..51ad759 100644 --- a/libtommath/bn_mp_root_n.c +++ b/libtommath/bn_mp_root_n.c @@ -27,7 +27,7 @@ mp_err mp_root_n(const mp_int *a, int b, mp_int *c) return MP_VAL; } - if ((err = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&t1, &t2, &t3, (void *)NULL)) != MP_OKAY) { return err; } @@ -134,7 +134,7 @@ mp_err mp_root_n(const mp_int *a, int b, mp_int *c) c->sign = a->sign; LBL_ERR: - mp_clear_multi(&t1, &t2, &t3, NULL); + mp_clear_multi(&t1, &t2, &t3, (void *)NULL); return err; } diff --git a/libtommath/bn_mp_sqrt.c b/libtommath/bn_mp_sqrt.c index 82d6824..dcf28fd 100644 --- a/libtommath/bn_mp_sqrt.c +++ b/libtommath/bn_mp_sqrt.c @@ -3,11 +3,24 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ +#ifndef NO_FLOATING_POINT +#include <float.h> +#include <math.h> +#if (MP_DIGIT_BIT != 28) || (FLT_RADIX != 2) || (DBL_MANT_DIG != 53) || (DBL_MAX_EXP != 1024) +#define NO_FLOATING_POINT +#endif +#endif + /* this function is less generic than mp_n_root, simpler and faster */ mp_err mp_sqrt(const mp_int *arg, mp_int *ret) { mp_err err; mp_int t1, t2; +#ifndef NO_FLOATING_POINT + int i, j, k; + volatile double d; + mp_digit dig; +#endif /* must be positive */ if (arg->sign == MP_NEG) { @@ -20,6 +33,64 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret) return MP_OKAY; } +#ifndef NO_FLOATING_POINT + + i = (arg->used / 2) - 1; + j = 2 * i; + if ((err = mp_init_size(&t1, i+2)) != MP_OKAY) { + return err; + } + + if ((err = mp_init(&t2)) != MP_OKAY) { + goto E2; + } + + for (k = 0; k < i; ++k) { + t1.dp[k] = (mp_digit) 0; + } + + /* Estimate the square root using the hardware floating point unit. */ + + d = 0.0; + for (k = arg->used-1; k >= j; --k) { + d = ldexp(d, MP_DIGIT_BIT) + (double)(arg->dp[k]); + } + + /* + * At this point, d is the nearest floating point number to the most + * significant 1 or 2 mp_digits of arg. Extract its square root. + */ + + d = sqrt(d); + + /* dig is the most significant mp_digit of the square root */ + + dig = (mp_digit) ldexp(d, -MP_DIGIT_BIT); + + /* + * If the most significant digit is nonzero, find the next digit down + * by subtracting MP_DIGIT_BIT times thie most significant digit. + * Subtract one from the result so that our initial estimate is always + * low. + */ + + if (dig) { + t1.used = i+2; + d -= ldexp((double) dig, MP_DIGIT_BIT); + if (d >= 1.0) { + t1.dp[i+1] = dig; + t1.dp[i] = ((mp_digit) d) - 1; + } else { + t1.dp[i+1] = dig-1; + t1.dp[i] = MP_DIGIT_MAX; + } + } else { + t1.used = i+1; + t1.dp[i] = ((mp_digit) d) - 1; + } + +#else + if ((err = mp_init_copy(&t1, arg)) != MP_OKAY) { return err; } @@ -31,6 +102,8 @@ mp_err mp_sqrt(const mp_int *arg, mp_int *ret) /* First approx. (not very bad for large arg) */ mp_rshd(&t1, t1.used/2); +#endif + /* t1 > 0 */ if ((err = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) { goto E1; diff --git a/libtommath/bn_mp_sqrtmod_prime.c b/libtommath/bn_mp_sqrtmod_prime.c index a833ed7..425ae17 100644 --- a/libtommath/bn_mp_sqrtmod_prime.c +++ b/libtommath/bn_mp_sqrtmod_prime.c @@ -25,7 +25,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) if ((err = mp_kronecker(n, prime, &legendre)) != MP_OKAY) return err; if (legendre == -1) return MP_VAL; /* quadratic non-residue mod prime */ - if ((err = mp_init_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, (void *)NULL)) != MP_OKAY) { return err; } @@ -111,7 +111,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) } cleanup: - mp_clear_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, NULL); + mp_clear_multi(&t1, &C, &Q, &S, &Z, &M, &T, &R, &two, (void *)NULL); return err; } diff --git a/libtommath/bn_s_mp_balance_mul.c b/libtommath/bn_s_mp_balance_mul.c index 7ece5d7..557cc1d 100644 --- a/libtommath/bn_s_mp_balance_mul.c +++ b/libtommath/bn_s_mp_balance_mul.c @@ -19,7 +19,7 @@ mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) if ((err = mp_init_size(&a0, bsize + 2)) != MP_OKAY) { return err; } - if ((err = mp_init_multi(&tmp, &r, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&tmp, &r, (void *)NULL)) != MP_OKAY) { mp_clear(&a0); return err; } @@ -75,7 +75,7 @@ mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) mp_exch(&r,c); LBL_ERR: - mp_clear_multi(&a0, &tmp, &r,NULL); + mp_clear_multi(&a0, &tmp, &r, (void *)NULL); return err; } #endif diff --git a/libtommath/bn_s_mp_invmod_fast.c b/libtommath/bn_s_mp_invmod_fast.c index 677d7ab..e76c604 100644 --- a/libtommath/bn_s_mp_invmod_fast.c +++ b/libtommath/bn_s_mp_invmod_fast.c @@ -21,7 +21,7 @@ mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) } /* init all our temps */ - if ((err = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&x, &y, &u, &v, &B, &D, (void *)NULL)) != MP_OKAY) { return err; } @@ -112,7 +112,7 @@ top: err = MP_OKAY; LBL_ERR: - mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL); + mp_clear_multi(&x, &y, &u, &v, &B, &D, (void *)NULL); return err; } #endif diff --git a/libtommath/bn_s_mp_invmod_slow.c b/libtommath/bn_s_mp_invmod_slow.c index 4c5db33..6079d4b 100644 --- a/libtommath/bn_s_mp_invmod_slow.c +++ b/libtommath/bn_s_mp_invmod_slow.c @@ -16,7 +16,7 @@ mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) /* init temps */ if ((err = mp_init_multi(&x, &y, &u, &v, - &A, &B, &C, &D, NULL)) != MP_OKAY) { + &A, &B, &C, &D, (void *)NULL)) != MP_OKAY) { return err; } @@ -113,7 +113,7 @@ top: mp_exch(&C, c); err = MP_OKAY; LBL_ERR: - mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL); + mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, (void *)NULL); return err; } #endif diff --git a/libtommath/bn_s_mp_log.c b/libtommath/bn_s_mp_log.c index a75212a..6ead7d9 100644 --- a/libtommath/bn_s_mp_log.c +++ b/libtommath/bn_s_mp_log.c @@ -17,7 +17,7 @@ mp_err s_mp_log(const mp_int *a, mp_digit base, int *c) if ((err = mp_init_multi(&bracket_low, &bracket_high, - &bracket_mid, &t, &bi_base, NULL)) != MP_OKAY) { + &bracket_mid, &t, &bi_base, (void *)NULL)) != MP_OKAY) { return err; } @@ -73,7 +73,7 @@ mp_err s_mp_log(const mp_int *a, mp_digit base, int *c) LBL_END: mp_clear_multi(&bracket_low, &bracket_high, &bracket_mid, - &t, &bi_base, NULL); + &t, &bi_base, (void *)NULL); return err; } diff --git a/libtommath/bn_s_mp_mul_high_digs_fast.c b/libtommath/bn_s_mp_mul_high_digs_fast.c index 4ce7f59..04c74a8 100644 --- a/libtommath/bn_s_mp_mul_high_digs_fast.c +++ b/libtommath/bn_s_mp_mul_high_digs_fast.c @@ -3,8 +3,8 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* this is a modified version of fast_s_mul_digs that only produces - * output digits *above* digs. See the comments for fast_s_mul_digs +/* this is a modified version of s_mp_mul_digs_fast that only produces + * output digits *above* digs. See the comments for s_mp_mul_digs_fast * to see how it works. * * This is used in the Barrett reduction since for one of the multiplications diff --git a/libtommath/bn_s_mp_rand_jenkins.c b/libtommath/bn_s_mp_rand_jenkins.c index da0771c..c64afac 100644 --- a/libtommath/bn_s_mp_rand_jenkins.c +++ b/libtommath/bn_s_mp_rand_jenkins.c @@ -27,10 +27,10 @@ static uint64_t s_rand_jenkins_val(void) void s_mp_rand_jenkins_init(uint64_t seed) { - uint64_t i; + int i; jenkins_x.a = 0xf1ea5eedULL; jenkins_x.b = jenkins_x.c = jenkins_x.d = seed; - for (i = 0uLL; i < 20uLL; ++i) { + for (i = 0; i < 20; ++i) { (void)s_rand_jenkins_val(); } } diff --git a/libtommath/bn_s_mp_toom_mul.c b/libtommath/bn_s_mp_toom_mul.c index eefce6c..fd574a2 100644 --- a/libtommath/bn_s_mp_toom_mul.c +++ b/libtommath/bn_s_mp_toom_mul.c @@ -36,7 +36,7 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) mp_err err; /* init temps */ - if ((err = mp_init_multi(&S1, &S2, &T1, NULL)) != MP_OKAY) { + if ((err = mp_init_multi(&S1, &S2, &T1, (void *)NULL)) != MP_OKAY) { return err; } @@ -208,7 +208,7 @@ LBL_ERRa2: LBL_ERRa1: mp_clear(&a0); LBL_ERRa0: - mp_clear_multi(&S1, &S2, &T1, NULL); + mp_clear_multi(&S1, &S2, &T1, (void *)NULL); return err; } diff --git a/libtommath/changes.txt b/libtommath/changes.txt index 80ff7dd..ac9e49a 100644 --- a/libtommath/changes.txt +++ b/libtommath/changes.txt @@ -423,7 +423,7 @@ v0.13 -- tons of minor speed-ups in low level add, sub, mul_2 and div_2 which p Jan 17th, 2003 v0.12 -- re-wrote the majority of the makefile so its more portable and will install via "make install" on most *nix platforms - -- Re-packaged all the source as seperate files. Means the library a single + -- Re-packaged all the source as separate files. Means the library a single file packagage any more. Instead of just adding "bn.c" you have to add libtommath.a -- Renamed "bn.h" to "tommath.h" |