diff options
author | lloyd <[email protected]> | 2008-09-07 16:46:51 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-07 16:46:51 +0000 |
commit | 26661fa5d2ecda183441a1506c6ab457da2849a2 (patch) | |
tree | 2738b5e373ce6cf6fc60114d167a6b05756cd8d3 | |
parent | b357bc7ab072cfac8c9a77a1f30406aa9883f5e0 (diff) |
Rewrite without gotos
-rw-r--r-- | src/mp_monty.cpp | 146 |
1 files changed, 11 insertions, 135 deletions
diff --git a/src/mp_monty.cpp b/src/mp_monty.cpp index bafa679c9..5edee3e2d 100644 --- a/src/mp_monty.cpp +++ b/src/mp_monty.cpp @@ -8,9 +8,6 @@ #include <botan/mp_asm.h> #include <botan/mp_asmi.h> -#include <assert.h> -#include <stdio.h> - namespace Botan { extern "C" { @@ -48,140 +45,24 @@ void bigint_monty_redc(word z[], u32bit z_size, } } -#if 0 - if(bigint_cmp(z + x_size, x_size + 1, x, x_size) >= 0) - bigint_sub2(z + x_size, x_size + 1, x, x_size); -#else - /* - -s32bit bigint_cmp(const word x[], u32bit x_size, - const word y[], u32bit y_size) - { - if(x_size < y_size) { return (-bigint_cmp(y, y_size, x, x_size)); } - - while(x_size > y_size) - { - if(x[x_size-1]) - return 1; - x_size--; - } - for(u32bit j = x_size; j > 0; --j) - { - if(x[j-1] > y[j-1]) return 1; - if(x[j-1] < y[j-1]) return -1; - } - return 0; - } - - */ - - /* - - if((x_size+1) < x_size) { return (-bigint_cmp(y, x_size, x, (x_size+1))); } - - while((x_size+1) > x_size) - { - if(x[(x_size+1)-1]) - return 1; - (x_size+1)--; - } - for(u32bit j = (x_size+1); j > 0; --j) - { - if(x[j-1] > y[j-1]) return 1; - if(x[j-1] < y[j-1]) return -1; - } - return 0; - - -> - - //can't happen: if((x_size+1) < x_size) { return (-bigint_cmp(y, x_size, x, (x_size+1))); } - - // always true: while((x_size+1) > x_size) - // { - if(x[x_size]) - return do_sub(); - //rewrite as x_size: (x_size+1)--; - } - for(u32bit j = x_size; j > 0; --j) - { - if(x[j-1] > y[j-1]) - return do_sub(); - if(x[j-1] < y[j-1]) - return; - } - return do_sub(); - - -> - - cleanup: - - if(x[x_size]) - return do_sub(); - - for(u32bit j = x_size; j > 0; --j) - { - if(x[j-1] > y[j-1]) - return do_sub(); - if(x[j-1] < y[j-1]) - return; - } - return do_sub(); - - -> arg rewrite - - bigint_cmp(z + x_size, x_size + 1, x, x_size) - - x = z + x_size - x_size = x_size + 1 - y = x - y_size = x_size - ^ !!! - - if(z[x_size + x_size + 1]) - return do_sub(); - - for(u32bit j = x_size; j > 0; --j) - { - if(z[x_size+j-1] > x[j-1]) - return do_sub(); - if(z[x_size+j-1] < x[j-1]) - return; - } - return do_sub(); - + /* Check if z[x_size...x_size+1] >= x[0...x_size] + This is bigint_cmp, inlined */ - - word carry = 0; - const u32bit blocks = x_size - (x_size % 8); - - if(z[x_size + x_size]) - { - //assert((bigint_cmp(z + x_size, x_size + 1, x, x_size) > 0); - goto do_sub; - } - - for(u32bit j = x_size; j > 0; --j) + if(!z[x_size + x_size]) { - if(z[x_size + j - 1] > x[j-1]) + for(u32bit j = x_size; j > 0; --j) { - //assert((bigint_cmp(z + x_size, x_size + 1, x, x_size) > 0); - goto do_sub; - } + if(z[x_size + j - 1] > x[j-1]) + break; - if(z[x_size + j - 1] < x[j-1]) - { - //assert((bigint_cmp(z + x_size, x_size + 1, x, x_size) < 0); - goto done; + if(z[x_size + j - 1] < x[j-1]) + return; } } - // default to subtraction (equal) - - //assert(m(bigint_cmp(z + x_size, x_size + 1, x, x_size) == 0); - -do_sub: - - //bigint_sub2(z + x_size, x_size + 1, x, x_size); + /* If the compare above is true, subtract using bigint_sub2 (inlined) */ + word carry = 0; + const u32bit blocks = x_size - (x_size % 8); for(u32bit j = 0; j != blocks; j += 8) carry = word8_sub2(z + x_size + j, x + j, carry); @@ -191,11 +72,6 @@ do_sub: if(carry) --z[x_size+x_size]; - -done: - return; - -#endif } } |