@@ -23,17 +23,11 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
2323
2424 /* according to HAC this optimization is ok */
2525 if ((mp_digit )um > ((mp_digit )1 << (MP_DIGIT_BIT - 1 ))) {
26- if ((err = mp_mul (& q , mu , & q )) != MP_OKAY ) {
27- goto LBL_ERR ;
28- }
29- } else if (MP_HAS (S_MP_MUL_HIGH )) {
30- if ((err = s_mp_mul_high (& q , mu , & q , um )) != MP_OKAY ) {
31- goto LBL_ERR ;
32- }
26+ if ((err = mp_mul (& q , mu , & q )) != MP_OKAY ) goto LBL_ERR ;
3327 } else if (MP_HAS (S_MP_MUL_HIGH_COMBA )) {
34- if ((err = s_mp_mul_high_comba (& q , mu , & q , um )) != MP_OKAY ) {
35- goto LBL_ERR ;
36- }
28+ if ((err = s_mp_mul_high_comba (& q , mu , & q , um )) != MP_OKAY ) goto LBL_ERR ;
29+ } else if ( MP_HAS ( S_MP_MUL_HIGH )) {
30+ if (( err = s_mp_mul_high ( & q , mu , & q , um )) != MP_OKAY ) goto LBL_ERR ;
3731 } else {
3832 err = MP_VAL ;
3933 goto LBL_ERR ;
@@ -43,41 +37,33 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
4337 mp_rshd (& q , um + 1 );
4438
4539 /* x = x mod b**(k+1), quick (no division) */
46- if ((err = mp_mod_2d (x , MP_DIGIT_BIT * (um + 1 ), x )) != MP_OKAY ) {
47- goto LBL_ERR ;
48- }
40+ if ((err = mp_mod_2d (x , MP_DIGIT_BIT * (um + 1 ), x )) != MP_OKAY ) goto LBL_ERR ;
4941
5042 /* q = q * m mod b**(k+1), quick (no division) */
51- if ((err = s_mp_mul (& q , m , & q , um + 1 )) != MP_OKAY ) {
52- goto LBL_ERR ;
43+ if (MP_HAS (S_MP_MUL_COMBA )
44+ && (MP_MIN (q .used , m -> used ) < MP_MAX_COMBA )) {
45+ if ((err = s_mp_mul_comba (& q , m , & q , um + 1 )) != MP_OKAY ) goto LBL_ERR ;
46+ } else {
47+ if ((err = s_mp_mul (& q , m , & q , um + 1 )) != MP_OKAY ) goto LBL_ERR ;
5348 }
5449
5550 /* x = x - q */
56- if ((err = mp_sub (x , & q , x )) != MP_OKAY ) {
57- goto LBL_ERR ;
58- }
51+ if ((err = mp_sub (x , & q , x )) != MP_OKAY ) goto LBL_ERR ;
5952
6053 /* If x < 0, add b**(k+1) to it */
6154 if (mp_cmp_d (x , 0uL ) == MP_LT ) {
6255 mp_set (& q , 1uL );
63- if ((err = mp_lshd (& q , um + 1 )) != MP_OKAY ) {
64- goto LBL_ERR ;
65- }
66- if ((err = mp_add (x , & q , x )) != MP_OKAY ) {
67- goto LBL_ERR ;
68- }
56+ if ((err = mp_lshd (& q , um + 1 )) != MP_OKAY ) goto LBL_ERR ;
57+ if ((err = mp_add (x , & q , x )) != MP_OKAY ) goto LBL_ERR ;
6958 }
7059
7160 /* Back off if it's too big */
7261 while (mp_cmp (x , m ) != MP_LT ) {
73- if ((err = s_mp_sub (x , m , x )) != MP_OKAY ) {
74- goto LBL_ERR ;
75- }
62+ if ((err = s_mp_sub (x , m , x )) != MP_OKAY ) goto LBL_ERR ;
7663 }
7764
7865LBL_ERR :
7966 mp_clear (& q );
80-
8167 return err ;
8268}
8369#endif
0 commit comments