diff options
Diffstat (limited to 'src/model/c/src/montgomery_array.c')
-rw-r--r-- | src/model/c/src/montgomery_array.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/model/c/src/montgomery_array.c b/src/model/c/src/montgomery_array.c index c3579a9..259dcff 100644 --- a/src/model/c/src/montgomery_array.c +++ b/src/model/c/src/montgomery_array.c @@ -102,6 +102,53 @@ void mont_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, } +// Experimental version where we add explicit lengths. +void mont_exp_array2(uint32_t explength, uint32_t modlength, uint32_t *X, uint32_t *E, uint32_t *M, + uint32_t *Nr, uint32_t *P, uint32_t *ONE, uint32_t *temp, + uint32_t *temp2, uint32_t *Z) { + //debugArray("X ", length, X); + //debugArray("E ", length, E); + //debugArray("M ", length, M); + + // 1. Nr := 2 ** 2N mod M + const uint32_t N = 32 * modlength; + m_residue_2_2N_array(modlength, N, M, temp, Nr); + //debugArray("Nr", length, Nr); + + // 2. Z0 := MontProd( 1, Nr, M ) + zero_array(modlength, ONE); + ONE[modlength - 1] = 1; + mont_prod_array(modlength, ONE, Nr, M, Z); + //debugArray("Z0", length, Z); + + // 3. P0 := MontProd( X, Nr, M ); + mont_prod_array(modlength, X, Nr, M, P); + //debugArray("P0", length, P); + + // 4. for i = 0 to explength - 1 loop + for (uint32_t i = 0; i < (explength * 32); i++) { + uint32_t ei_ = E[explength - 1 - (i / 32)]; + uint32_t ei = (ei_ >> (i % 32)) & 1; + // 6. if (ei = 1) then Zi+1 := MontProd ( Zi, Pi, M) else Zi+1 := Zi + if (ei == 1) { + mont_prod_array(modlength, Z, P, M, temp2); + copy_array(modlength, temp2, Z); + //debugArray("Z ", length, Z); + } + // 5. Pi+1 := MontProd( Pi, Pi, M ); + mont_prod_array(modlength, P, P, M, temp2); + copy_array(modlength, temp2, P); + //debugArray("P ", length, P); + // 7. end for + } + // 8. Zn := MontProd( 1, Zn, M ); + mont_prod_array(modlength, ONE, Z, M, temp2); + copy_array(modlength, temp2, Z); + //debugArray("Z ", length, Z); + // 9. RETURN Zn + +} + void die(const char *c) { printf("Fatal error: %s\n", c); exit(1); @@ -125,3 +172,23 @@ void mod_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, uint3 free(temp); free(temp2); } + +// Experimental version with explicit explength separate from modlength. +void mod_exp_array2(uint32_t explength, uint32_t modlength, uint32_t *X, uint32_t *E, uint32_t *M, uint32_t *Z) { + uint32_t *Nr = calloc(modlength, sizeof(uint32_t)); + uint32_t *P = calloc(modlength, sizeof(uint32_t)); + uint32_t *ONE = calloc(modlength, sizeof(uint32_t)); + uint32_t *temp = calloc(modlength, sizeof(uint32_t)); + uint32_t *temp2 = calloc(modlength, sizeof(uint32_t)); + if (Nr == NULL) die("calloc"); + if (P == NULL) die("calloc"); + if (ONE == NULL) die("calloc"); + if (temp == NULL) die("calloc"); + if (temp2 == NULL) die("calloc"); + mont_exp_array2(explength, modlength, X, E, M, Nr, P, ONE, temp, temp2, Z); + free(Nr); + free(P); + free(ONE); + free(temp); + free(temp2); +} |