also patch fp_mul() as proposed by tom on the ML [1]

[1] https://groups.google.com/forum/#!topic/libtom/MdbS1vcLhCU
This commit is contained in:
Steffen Jaeckel 2014-06-12 18:06:26 +02:00
parent a7804acf42
commit 2d5b8206fa

View File

@ -12,16 +12,23 @@
/* c = a * b */ /* c = a * b */
void fp_mul(fp_int *A, fp_int *B, fp_int *C) void fp_mul(fp_int *A, fp_int *B, fp_int *C)
{ {
int y, yy; int y, old_used;
#if FP_SIZE >= 48
int yy;
#endif
old_used = C->used;
/* call generic if we're out of range */ /* call generic if we're out of range */
if (A->used + B->used > FP_SIZE) { if (A->used + B->used > FP_SIZE) {
fp_mul_comba(A, B, C); fp_mul_comba(A, B, C);
return ; goto clean;
} }
y = MAX(A->used, B->used); y = MAX(A->used, B->used);
#if FP_SIZE >= 48
yy = MIN(A->used, B->used); yy = MIN(A->used, B->used);
#endif
/* pick a comba (unrolled 4/8/16/32 x or rolled) based on the size /* pick a comba (unrolled 4/8/16/32 x or rolled) based on the size
of the largest input. We also want to avoid doing excess mults if the of the largest input. We also want to avoid doing excess mults if the
inputs are not close to the next power of two. That is, for example, inputs are not close to the next power of two. That is, for example,
@ -31,95 +38,99 @@ void fp_mul(fp_int *A, fp_int *B, fp_int *C)
#if defined(TFM_MUL3) && FP_SIZE >= 6 #if defined(TFM_MUL3) && FP_SIZE >= 6
if (y <= 3) { if (y <= 3) {
fp_mul_comba3(A,B,C); fp_mul_comba3(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL4) && FP_SIZE >= 8 #if defined(TFM_MUL4) && FP_SIZE >= 8
if (y == 4) { if (y == 4) {
fp_mul_comba4(A,B,C); fp_mul_comba4(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL6) && FP_SIZE >= 12 #if defined(TFM_MUL6) && FP_SIZE >= 12
if (y <= 6) { if (y <= 6) {
fp_mul_comba6(A,B,C); fp_mul_comba6(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL7) && FP_SIZE >= 14 #if defined(TFM_MUL7) && FP_SIZE >= 14
if (y == 7) { if (y == 7) {
fp_mul_comba7(A,B,C); fp_mul_comba7(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL8) && FP_SIZE >= 16 #if defined(TFM_MUL8) && FP_SIZE >= 16
if (y == 8) { if (y == 8) {
fp_mul_comba8(A,B,C); fp_mul_comba8(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL9) && FP_SIZE >= 18 #if defined(TFM_MUL9) && FP_SIZE >= 18
if (y == 9) { if (y == 9) {
fp_mul_comba9(A,B,C); fp_mul_comba9(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL12) && FP_SIZE >= 24 #if defined(TFM_MUL12) && FP_SIZE >= 24
if (y <= 12) { if (y <= 12) {
fp_mul_comba12(A,B,C); fp_mul_comba12(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL17) && FP_SIZE >= 34 #if defined(TFM_MUL17) && FP_SIZE >= 34
if (y <= 17) { if (y <= 17) {
fp_mul_comba17(A,B,C); fp_mul_comba17(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_SMALL_SET) && FP_SIZE >= 32 #if defined(TFM_SMALL_SET) && FP_SIZE >= 32
if (y <= 16) { if (y <= 16) {
fp_mul_comba_small(A,B,C); fp_mul_comba_small(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL20) && FP_SIZE >= 40 #if defined(TFM_MUL20) && FP_SIZE >= 40
if (y <= 20) { if (y <= 20) {
fp_mul_comba20(A,B,C); fp_mul_comba20(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL24) && FP_SIZE >= 48 #if defined(TFM_MUL24) && FP_SIZE >= 48
if (yy >= 16 && y <= 24) { if (yy >= 16 && y <= 24) {
fp_mul_comba24(A,B,C); fp_mul_comba24(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL28) && FP_SIZE >= 56 #if defined(TFM_MUL28) && FP_SIZE >= 56
if (yy >= 20 && y <= 28) { if (yy >= 20 && y <= 28) {
fp_mul_comba28(A,B,C); fp_mul_comba28(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL32) && FP_SIZE >= 64 #if defined(TFM_MUL32) && FP_SIZE >= 64
if (yy >= 24 && y <= 32) { if (yy >= 24 && y <= 32) {
fp_mul_comba32(A,B,C); fp_mul_comba32(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL48) && FP_SIZE >= 96 #if defined(TFM_MUL48) && FP_SIZE >= 96
if (yy >= 40 && y <= 48) { if (yy >= 40 && y <= 48) {
fp_mul_comba48(A,B,C); fp_mul_comba48(A,B,C);
return; goto clean;
} }
#endif #endif
#if defined(TFM_MUL64) && FP_SIZE >= 128 #if defined(TFM_MUL64) && FP_SIZE >= 128
if (yy >= 56 && y <= 64) { if (yy >= 56 && y <= 64) {
fp_mul_comba64(A,B,C); fp_mul_comba64(A,B,C);
return; goto clean;
} }
#endif #endif
fp_mul_comba(A,B,C); fp_mul_comba(A,B,C);
clean:
for (y = C->used; y < old_used; y++) {
C->dp[y] = 0;
}
} }