aboutsummaryrefslogtreecommitdiff
path: root/modexp_fpga_model_montgomery.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modexp_fpga_model_montgomery.cpp')
-rw-r--r--modexp_fpga_model_montgomery.cpp47
1 files changed, 32 insertions, 15 deletions
diff --git a/modexp_fpga_model_montgomery.cpp b/modexp_fpga_model_montgomery.cpp
index 5bc5ba4..92a5e47 100644
--- a/modexp_fpga_model_montgomery.cpp
+++ b/modexp_fpga_model_montgomery.cpp
@@ -132,22 +132,35 @@ void montgomery_exponentiate(const FPGA_WORD *A, const FPGA_WORD *B, const FPGA_
bool flag_update_r; // flag
- FPGA_WORD P[MAX_OPERAND_WORDS]; // power of A
- FPGA_WORD mask; // mask
+ FPGA_WORD T0[MAX_OPERAND_WORDS]; //
+ FPGA_WORD T1[MAX_OPERAND_WORDS]; //
+ FPGA_WORD T2[MAX_OPERAND_WORDS]; //
+
+ FPGA_WORD P1[MAX_OPERAND_WORDS]; //
+ FPGA_WORD P2[MAX_OPERAND_WORDS]; //
+ FPGA_WORD P3[MAX_OPERAND_WORDS]; //
+
+ FPGA_WORD mask; //
// R = 1, P = 1
for (word_cnt=0; word_cnt<len; word_cnt++)
- R[word_cnt] = (word_cnt > 0) ? 0 : 1,
- P[word_cnt] = A[word_cnt];
+ T1[word_cnt] = (word_cnt > 0) ? 0 : 1,
+ T2[word_cnt] = (word_cnt > 0) ? 0 : 1,
+ P1[word_cnt] = A[word_cnt],
+ P2[word_cnt] = A[word_cnt],
+ P3[word_cnt] = A[word_cnt];
- FPGA_WORD M_PP[MAX_OPERAND_WORDS]; // intermediate buffer for next power
- FPGA_WORD M_RP[MAX_OPERAND_WORDS]; // intermediate buffer for next result
+ FPGA_WORD PP[MAX_OPERAND_WORDS]; // intermediate buffer for next power
+ FPGA_WORD TP[MAX_OPERAND_WORDS]; // intermediate buffer for next result
// scan all bits of the exponent
for (bit_cnt=0; bit_cnt<(len * CHAR_BIT * sizeof(FPGA_WORD)); bit_cnt++)
{
- montgomery_multiply(P, P, N, N_COEFF, M_PP, len, false); // M_PP = P * P
- montgomery_multiply(R, P, N, N_COEFF, M_RP, len, false); // M_RP = R * P
+ for (word_cnt=0; word_cnt<len; word_cnt++)
+ T0[word_cnt] = T1[word_cnt] ^ POWER_MASK;
+
+ montgomery_multiply(P1, P2, N, N_COEFF, PP, len, false); // PP = P1 * P2
+ montgomery_multiply(T2, P3, N, N_COEFF, TP, len, false); // TP = T * P3
word_index = bit_cnt / (CHAR_BIT * sizeof(FPGA_WORD));
bit_index = bit_cnt & ((CHAR_BIT * sizeof(FPGA_WORD)) - 1);
@@ -159,15 +172,19 @@ void montgomery_exponentiate(const FPGA_WORD *A, const FPGA_WORD *B, const FPGA_
// always update P
for (word_cnt=0; word_cnt<len; word_cnt++)
- P[word_cnt] = M_PP[word_cnt];
+ P1[word_cnt] = PP[word_cnt],
+ P2[word_cnt] = PP[word_cnt],
+ P3[word_cnt] = PP[word_cnt];
- // only update R when necessary
- if (flag_update_r)
- {
- for (word_cnt=0; word_cnt<len; word_cnt++)
- R[word_cnt] = M_RP[word_cnt];
- }
+ // update T
+ for (word_cnt=0; word_cnt<len; word_cnt++)
+ T1[word_cnt] = flag_update_r ? TP[word_cnt] : T0[word_cnt] ^ POWER_MASK,
+ T2[word_cnt] = flag_update_r ? TP[word_cnt] : T0[word_cnt] ^ POWER_MASK;
}
+
+ // store result
+ for (word_cnt=0; word_cnt<len; word_cnt++)
+ R[word_cnt] = T1[word_cnt];
}