2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-30 16:26:27 +00:00
|
|
|
#ifdef S_MP_SQR_COMBA_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
/* the jist of squaring...
|
2017-08-30 03:51:11 +00:00
|
|
|
* you do like mult except the offset of the tmpx [one that
|
|
|
|
* starts closer to zero] can't equal the offset of tmpy.
|
2005-03-12 11:55:11 +00:00
|
|
|
* So basically you set up iy like before then you min it with
|
2017-08-30 03:51:11 +00:00
|
|
|
* (ty-tx) so that it never happens. You double all those
|
2005-03-12 11:55:11 +00:00
|
|
|
* you add in the inner loop
|
2004-10-29 22:07:18 +00:00
|
|
|
|
|
|
|
After that loop you do the squares and add them in.
|
|
|
|
*/
|
|
|
|
|
2019-10-30 16:26:27 +00:00
|
|
|
mp_err s_mp_sqr_comba(const mp_int *a, mp_int *b)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2019-10-29 19:06:20 +00:00
|
|
|
int oldused, pa, ix;
|
|
|
|
mp_digit W[MP_WARRAY];
|
2017-08-29 20:23:48 +00:00
|
|
|
mp_word W1;
|
2019-10-29 21:38:49 +00:00
|
|
|
mp_err err;
|
2017-08-29 20:23:48 +00:00
|
|
|
|
|
|
|
/* grow the destination as required */
|
|
|
|
pa = a->used + a->used;
|
2019-10-29 21:38:49 +00:00
|
|
|
if ((err = mp_grow(b, pa)) != MP_OKAY) {
|
|
|
|
return err;
|
2017-08-29 20:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* number of output digits to produce */
|
|
|
|
W1 = 0;
|
|
|
|
for (ix = 0; ix < pa; ix++) {
|
2019-10-29 19:06:20 +00:00
|
|
|
int tx, ty, iy, iz;
|
2004-10-29 22:07:18 +00:00
|
|
|
mp_word _W;
|
|
|
|
|
|
|
|
/* clear counter */
|
|
|
|
_W = 0;
|
|
|
|
|
|
|
|
/* get offsets into the two bignums */
|
2019-04-09 09:08:26 +00:00
|
|
|
ty = MP_MIN(a->used-1, ix);
|
2004-10-29 22:07:18 +00:00
|
|
|
tx = ix - ty;
|
|
|
|
|
2005-03-12 11:55:11 +00:00
|
|
|
/* this is the number of times the loop will iterrate, essentially
|
2004-10-29 22:07:18 +00:00
|
|
|
while (tx++ < a->used && ty-- >= 0) { ... }
|
|
|
|
*/
|
2019-04-09 09:08:26 +00:00
|
|
|
iy = MP_MIN(a->used-tx, ty+1);
|
2004-10-29 22:07:18 +00:00
|
|
|
|
2017-08-30 03:51:11 +00:00
|
|
|
/* now for squaring tx can never equal ty
|
2004-10-29 22:07:18 +00:00
|
|
|
* we halve the distance since they approach at a rate of 2x
|
|
|
|
* and we have to round because odd cases need to be executed
|
|
|
|
*/
|
2019-04-09 09:08:26 +00:00
|
|
|
iy = MP_MIN(iy, ((ty-tx)+1)>>1);
|
2004-10-29 22:07:18 +00:00
|
|
|
|
|
|
|
/* execute loop */
|
|
|
|
for (iz = 0; iz < iy; iz++) {
|
2019-10-29 19:06:20 +00:00
|
|
|
_W += (mp_word)a->dp[tx + iz] * (mp_word)a->dp[ty - iz];
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
|
|
|
|
/* double the inner product and add carry */
|
|
|
|
_W = _W + _W + W1;
|
|
|
|
|
|
|
|
/* even columns have the square term in them */
|
2017-10-15 17:57:12 +00:00
|
|
|
if (((unsigned)ix & 1u) == 0u) {
|
2017-10-15 17:58:35 +00:00
|
|
|
_W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1];
|
2004-10-29 22:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* store it */
|
2019-03-26 10:32:33 +00:00
|
|
|
W[ix] = (mp_digit)_W & MP_MASK;
|
2004-10-29 22:07:18 +00:00
|
|
|
|
|
|
|
/* make next carry */
|
2019-04-13 06:46:57 +00:00
|
|
|
W1 = _W >> (mp_word)MP_DIGIT_BIT;
|
2017-08-29 20:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup dest */
|
2019-10-29 19:06:20 +00:00
|
|
|
oldused = b->used;
|
2017-08-29 20:23:48 +00:00
|
|
|
b->used = a->used+a->used;
|
|
|
|
|
2019-10-29 19:06:20 +00:00
|
|
|
for (ix = 0; ix < pa; ix++) {
|
|
|
|
b->dp[ix] = W[ix] & MP_MASK;
|
2017-08-29 20:23:48 +00:00
|
|
|
}
|
2019-10-29 19:06:20 +00:00
|
|
|
|
|
|
|
/* clear unused digits [that existed in the old copy of c] */
|
2019-10-29 20:48:50 +00:00
|
|
|
s_mp_zero_digs(b->dp + b->used, oldused - b->used);
|
2019-10-29 19:06:20 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
mp_clamp(b);
|
|
|
|
return MP_OKAY;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|