Skip to content

remove W array from s_mp_mul_comba and s_mp_sqr_comba #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ static int test_s_mp_mul_balance(void)
return EXIT_FAILURE;
}

#define s_mp_mul_full(a, b, c) s_mp_mul(a, b, c, (a)->used + (b)->used + 1)
#define s_mp_mul_full(a, b, c) s_mp_mul_comba(a, b, c, (a)->used + (b)->used + 1)
static int test_s_mp_mul_karatsuba(void)
{
mp_int a, b, c, d;
Expand Down Expand Up @@ -1929,7 +1929,7 @@ static int test_s_mp_sqr_karatsuba(void)
for (size = MP_SQR_KARATSUBA_CUTOFF; size < (MP_SQR_KARATSUBA_CUTOFF + 20); size++) {
DO(mp_rand(&a, size));
DO(s_mp_sqr_karatsuba(&a, &b));
DO(s_mp_sqr(&a, &c));
DO(s_mp_sqr_comba(&a, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
fprintf(stderr, "Karatsuba squaring failed at size %d\n", size);
goto LBL_ERR;
Expand Down Expand Up @@ -2002,7 +2002,7 @@ static int test_s_mp_sqr_toom(void)
for (size = MP_SQR_TOOM_CUTOFF; size < (MP_SQR_TOOM_CUTOFF + 20); size++) {
DO(mp_rand(&a, size));
DO(s_mp_sqr_toom(&a, &b));
DO(s_mp_sqr(&a, &c));
DO(s_mp_sqr_comba(&a, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
fprintf(stderr, "Toom-Cook 3-way squaring failed at size %d\n", size);
goto LBL_ERR;
Expand Down
10 changes: 7 additions & 3 deletions etc/tune.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ static int s_number_of_test_loops;
static int s_stabilization_extra;
static int s_offset = 1;

#define s_mp_mul_full(a, b, c) s_mp_mul(a, b, c, (a)->used + (b)->used + 1)
static mp_err s_mul_full(const mp_int *a, const mp_int *b, mp_int *c)
{
return s_mp_mul_comba(a, b, c, a->used + b->used + 1);
}

static uint64_t s_time_mul(int size)
{
int x;
Expand Down Expand Up @@ -87,7 +91,7 @@ static uint64_t s_time_mul(int size)
goto LBL_ERR;
}
if (s_check_result == 1) {
if ((e = s_mp_mul_full(&a,&b,&d)) != MP_OKAY) {
if ((e = s_mul_full(&a,&b,&d)) != MP_OKAY) {
t1 = UINT64_MAX;
goto LBL_ERR;
}
Expand Down Expand Up @@ -129,7 +133,7 @@ static uint64_t s_time_sqr(int size)
goto LBL_ERR;
}
if (s_check_result == 1) {
if ((e = s_mp_sqr(&a,&c)) != MP_OKAY) {
if ((e = s_mp_sqr_comba(&a,&c)) != MP_OKAY) {
t1 = UINT64_MAX;
goto LBL_ERR;
}
Expand Down
8 changes: 0 additions & 8 deletions libtommath_VS2008.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,6 @@
RelativePath="s_mp_montgomery_reduce_comba.c"
>
</File>
<File
RelativePath="s_mp_mul.c"
>
</File>
<File
RelativePath="s_mp_mul_balance.c"
>
Expand Down Expand Up @@ -916,10 +912,6 @@
RelativePath="s_mp_rand_platform.c"
>
</File>
<File
RelativePath="s_mp_sqr.c"
>
</File>
<File
RelativePath="s_mp_sqr_comba.c"
>
Expand Down
6 changes: 3 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_r
mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
s_mp_zero_buf.o s_mp_zero_digs.o
s_mp_rand_platform.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o \
s_mp_zero_digs.o

#END_INS

Expand Down
6 changes: 3 additions & 3 deletions makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_r
mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
s_mp_zero_buf.o s_mp_zero_digs.o
s_mp_rand_platform.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o \
s_mp_zero_digs.o

HEADERS_PUB=tommath.h
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
Expand Down
6 changes: 3 additions & 3 deletions makefile.msvc
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ mp_sqrmod.obj mp_sqrt.obj mp_sqrtmod_prime.obj mp_sub.obj mp_sub_d.obj mp_submod
mp_to_ubin.obj mp_ubin_size.obj mp_unpack.obj mp_xor.obj mp_zero.obj s_mp_add.obj s_mp_copy_digs.obj s_mp_div_3.obj \
s_mp_div_recursive.obj s_mp_div_school.obj s_mp_div_small.obj s_mp_exptmod.obj s_mp_exptmod_fast.obj s_mp_get_bit.obj \
s_mp_invmod.obj s_mp_invmod_odd.obj s_mp_log.obj s_mp_log_d.obj s_mp_log_pow2.obj s_mp_montgomery_reduce_comba.obj \
s_mp_mul.obj s_mp_mul_balance.obj s_mp_mul_comba.obj s_mp_mul_high.obj s_mp_mul_high_comba.obj s_mp_mul_karatsuba.obj \
s_mp_mul_balance.obj s_mp_mul_comba.obj s_mp_mul_high.obj s_mp_mul_high_comba.obj s_mp_mul_karatsuba.obj \
s_mp_mul_toom.obj s_mp_prime_is_divisible.obj s_mp_prime_tab.obj s_mp_radix_map.obj s_mp_rand_jenkins.obj \
s_mp_rand_platform.obj s_mp_sqr.obj s_mp_sqr_comba.obj s_mp_sqr_karatsuba.obj s_mp_sqr_toom.obj s_mp_sub.obj \
s_mp_zero_buf.obj s_mp_zero_digs.obj
s_mp_rand_platform.obj s_mp_sqr_comba.obj s_mp_sqr_karatsuba.obj s_mp_sqr_toom.obj s_mp_sub.obj s_mp_zero_buf.obj \
s_mp_zero_digs.obj

HEADERS_PUB=tommath.h
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
Expand Down
6 changes: 3 additions & 3 deletions makefile.shared
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_r
mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
s_mp_zero_buf.o s_mp_zero_digs.o
s_mp_rand_platform.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o \
s_mp_zero_digs.o

#END_INS

Expand Down
6 changes: 3 additions & 3 deletions makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_r
mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_get_bit.o \
s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_d.o s_mp_log_pow2.o s_mp_montgomery_reduce_comba.o \
s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o \
s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \
s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \
s_mp_zero_buf.o s_mp_zero_digs.o
s_mp_rand_platform.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o \
s_mp_zero_digs.o

HEADERS_PUB=tommath.h
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
Expand Down
19 changes: 2 additions & 17 deletions mp_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
(a->used >= MP_SQR_KARATSUBA_CUTOFF)) {
err = s_mp_sqr_karatsuba(a, c);
} else if ((a == b) &&
MP_HAS(S_MP_SQR_COMBA) && /* can we use the fast comba multiplier? */
(((a->used * 2) + 1) < MP_WARRAY) &&
(a->used < (MP_MAX_COMBA / 2))) {
MP_HAS(S_MP_SQR_COMBA)) {
err = s_mp_sqr_comba(a, c);
} else if ((a == b) &&
MP_HAS(S_MP_SQR)) {
err = s_mp_sqr(a, c);
} else if (MP_HAS(S_MP_MUL_BALANCE) &&
/* 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_MUL_KARATSUBA_CUTOFF bigger
Expand All @@ -47,18 +42,8 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
} else if (MP_HAS(S_MP_MUL_KARATSUBA) &&
(min >= MP_MUL_KARATSUBA_CUTOFF)) {
err = s_mp_mul_karatsuba(a, b, c);
} else if (MP_HAS(S_MP_MUL_COMBA) &&
/* can we use the fast multiplier?
*
* The fast multiplier can be used if the output will
* have less than MP_WARRAY digits and the number of
* digits won't affect carry propagation
*/
(digs < MP_WARRAY) &&
(min <= MP_MAX_COMBA)) {
} else if (MP_HAS(S_MP_MUL_COMBA)) {
err = s_mp_mul_comba(a, b, c, digs);
} else if (MP_HAS(S_MP_MUL)) {
err = s_mp_mul(a, b, c, digs);
} else {
err = MP_VAL;
}
Expand Down
39 changes: 10 additions & 29 deletions mp_reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)

/* according to HAC this optimization is ok */
if ((mp_digit)um > ((mp_digit)1 << (MP_DIGIT_BIT - 1))) {
if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) {
goto LBL_ERR;
}
} else if (MP_HAS(S_MP_MUL_HIGH)) {
if ((err = s_mp_mul_high(&q, mu, &q, um)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) goto LBL_ERR;
} else if (MP_HAS(S_MP_MUL_HIGH_COMBA)) {
if ((err = s_mp_mul_high_comba(&q, mu, &q, um)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = s_mp_mul_high_comba(&q, mu, &q, um)) != MP_OKAY) goto LBL_ERR;
} else if (MP_HAS(S_MP_MUL_HIGH)) {
if ((err = s_mp_mul_high(&q, mu, &q, um)) != MP_OKAY) goto LBL_ERR;
} else {
err = MP_VAL;
goto LBL_ERR;
Expand All @@ -43,41 +37,28 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
mp_rshd(&q, um + 1);

/* x = x mod b**(k+1), quick (no division) */
if ((err = mp_mod_2d(x, MP_DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_mod_2d(x, MP_DIGIT_BIT * (um + 1), x)) != MP_OKAY) goto LBL_ERR;

/* q = q * m mod b**(k+1), quick (no division) */
if ((err = s_mp_mul(&q, m, &q, um + 1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = s_mp_mul_comba(&q, m, &q, um + 1)) != MP_OKAY) goto LBL_ERR;

/* x = x - q */
if ((err = mp_sub(x, &q, x)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_sub(x, &q, x)) != MP_OKAY) goto LBL_ERR;

/* If x < 0, add b**(k+1) to it */
if (mp_cmp_d(x, 0uL) == MP_LT) {
mp_set(&q, 1uL);
if ((err = mp_lshd(&q, um + 1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_add(x, &q, x)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_lshd(&q, um + 1)) != MP_OKAY) goto LBL_ERR;
if ((err = mp_add(x, &q, x)) != MP_OKAY) goto LBL_ERR;
}

/* Back off if it's too big */
while (mp_cmp(x, m) != MP_LT) {
if ((err = s_mp_sub(x, m, x)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = s_mp_sub(x, m, x)) != MP_OKAY) goto LBL_ERR;
}

LBL_ERR:
mp_clear(&q);

return err;
}
#endif
61 changes: 0 additions & 61 deletions s_mp_mul.c

This file was deleted.

Loading