change some parameters in the library to meet the needs of the website httpbin.org
Fork of MiniTLS-GPL by
math/mont/fp_montgomery_setup.c@0:35aa5be3b78d, 2014-06-06 (annotated)
- Committer:
- MiniTLS
- Date:
- Fri Jun 06 10:49:02 2014 +0000
- Revision:
- 0:35aa5be3b78d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MiniTLS | 0:35aa5be3b78d | 1 | /* TomsFastMath, a fast ISO C bignum library. |
MiniTLS | 0:35aa5be3b78d | 2 | * |
MiniTLS | 0:35aa5be3b78d | 3 | * This project is meant to fill in where LibTomMath |
MiniTLS | 0:35aa5be3b78d | 4 | * falls short. That is speed ;-) |
MiniTLS | 0:35aa5be3b78d | 5 | * |
MiniTLS | 0:35aa5be3b78d | 6 | * This project is public domain and free for all purposes. |
MiniTLS | 0:35aa5be3b78d | 7 | * |
MiniTLS | 0:35aa5be3b78d | 8 | * Tom St Denis, tomstdenis@gmail.com |
MiniTLS | 0:35aa5be3b78d | 9 | */ |
MiniTLS | 0:35aa5be3b78d | 10 | #include <tfm.h> |
MiniTLS | 0:35aa5be3b78d | 11 | |
MiniTLS | 0:35aa5be3b78d | 12 | /* setups the montgomery reduction */ |
MiniTLS | 0:35aa5be3b78d | 13 | int fp_montgomery_setup(fp_int *a, fp_digit *rho) |
MiniTLS | 0:35aa5be3b78d | 14 | { |
MiniTLS | 0:35aa5be3b78d | 15 | fp_digit x, b; |
MiniTLS | 0:35aa5be3b78d | 16 | |
MiniTLS | 0:35aa5be3b78d | 17 | /* fast inversion mod 2**k |
MiniTLS | 0:35aa5be3b78d | 18 | * |
MiniTLS | 0:35aa5be3b78d | 19 | * Based on the fact that |
MiniTLS | 0:35aa5be3b78d | 20 | * |
MiniTLS | 0:35aa5be3b78d | 21 | * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n) |
MiniTLS | 0:35aa5be3b78d | 22 | * => 2*X*A - X*X*A*A = 1 |
MiniTLS | 0:35aa5be3b78d | 23 | * => 2*(1) - (1) = 1 |
MiniTLS | 0:35aa5be3b78d | 24 | */ |
MiniTLS | 0:35aa5be3b78d | 25 | b = a->dp[0]; |
MiniTLS | 0:35aa5be3b78d | 26 | |
MiniTLS | 0:35aa5be3b78d | 27 | if ((b & 1) == 0) { |
MiniTLS | 0:35aa5be3b78d | 28 | return FP_VAL; |
MiniTLS | 0:35aa5be3b78d | 29 | } |
MiniTLS | 0:35aa5be3b78d | 30 | |
MiniTLS | 0:35aa5be3b78d | 31 | x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ |
MiniTLS | 0:35aa5be3b78d | 32 | x *= 2 - b * x; /* here x*a==1 mod 2**8 */ |
MiniTLS | 0:35aa5be3b78d | 33 | x *= 2 - b * x; /* here x*a==1 mod 2**16 */ |
MiniTLS | 0:35aa5be3b78d | 34 | x *= 2 - b * x; /* here x*a==1 mod 2**32 */ |
MiniTLS | 0:35aa5be3b78d | 35 | #ifdef FP_64BIT |
MiniTLS | 0:35aa5be3b78d | 36 | x *= 2 - b * x; /* here x*a==1 mod 2**64 */ |
MiniTLS | 0:35aa5be3b78d | 37 | #endif |
MiniTLS | 0:35aa5be3b78d | 38 | |
MiniTLS | 0:35aa5be3b78d | 39 | /* rho = -1/m mod b */ |
MiniTLS | 0:35aa5be3b78d | 40 | *rho = (((fp_word) 1 << ((fp_word) DIGIT_BIT)) - ((fp_word)x)); |
MiniTLS | 0:35aa5be3b78d | 41 | |
MiniTLS | 0:35aa5be3b78d | 42 | return FP_OKAY; |
MiniTLS | 0:35aa5be3b78d | 43 | } |
MiniTLS | 0:35aa5be3b78d | 44 | |
MiniTLS | 0:35aa5be3b78d | 45 | |
MiniTLS | 0:35aa5be3b78d | 46 | /* $Source: /cvs/libtom/tomsfastmath/src/mont/fp_montgomery_setup.c,v $ */ |
MiniTLS | 0:35aa5be3b78d | 47 | /* $Revision: 1.1 $ */ |
MiniTLS | 0:35aa5be3b78d | 48 | /* $Date: 2006/12/31 21:25:53 $ */ |