From 9eeefad066a16a6e9a3407178f7629a8cd4c0e61 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 13 Dec 2019 21:16:44 +0000 Subject: Optimize mp_mul for the case a==b --- generic/tclTomMathDecls.h | 3 +++ libtommath/bn_mp_mul.c | 6 ++++-- libtommath/tommath_private.h | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/generic/tclTomMathDecls.h b/generic/tclTomMathDecls.h index f199a2a..52ac5da 100644 --- a/generic/tclTomMathDecls.h +++ b/generic/tclTomMathDecls.h @@ -740,6 +740,9 @@ extern const TclTomMathStubs *tclTomMathStubsPtr; #undef mp_isodd #define mp_iseven(a) (!mp_isodd(a)) #define mp_isodd(a) (((a)->used != 0 && (((a)->dp[0] & 1) != 0)) ? MP_YES : MP_NO) +#undef mp_sqr +#define mp_sqr(a,b) mp_mul(a,a,b) + #undef TCL_STORAGE_CLASS #define TCL_STORAGE_CLASS DLLIMPORT diff --git a/libtommath/bn_mp_mul.c b/libtommath/bn_mp_mul.c index 91707cd..b00334d 100644 --- a/libtommath/bn_mp_mul.c +++ b/libtommath/bn_mp_mul.c @@ -12,12 +12,14 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) digs = a->used + b->used + 1; mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; - if (MP_HAS(S_MP_BALANCE_MUL) && + if (a == b) { + return mp_sqr(a,c); + } else if (MP_HAS(S_MP_BALANCE_MUL) && /* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off. * 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 s_mp_mul_digs_fast + * Using it to cut the input into slices small enough for fast_s_mp_mul_digs * was actually slower on the author's machine, but YMMV. */ (min_len >= MP_KARATSUBA_MUL_CUTOFF) && diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h index 7cef443..60c8838 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -249,6 +249,9 @@ MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b); MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len); +#undef mp_sqr +#define mp_sqr TclBN_mp_sqr + #define MP_GET_ENDIANNESS(x) \ do{\ int16_t n = 0x1; \ -- cgit v0.12 From de326e5c5ed447c99c7be6991f76cc9d5e774698 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 13 Dec 2019 21:48:15 +0000 Subject: Don't export mp_sqr() directly from libtcl --- libtommath/tommath_private.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h index 60c8838..93724a4 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -249,6 +249,11 @@ MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b); MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len); +#ifndef TCL_WITH_EXTERNAL_TOMMATH +#undef mp_sqr +#define mp_sqr TclBN_mp_sqr +#endif + #undef mp_sqr #define mp_sqr TclBN_mp_sqr -- cgit v0.12