Mercurial > dropbear
comparison bn_mp_exptmod.c @ 142:d29b64170cf0 libtommath-orig
import of libtommath 0.32
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 19 Dec 2004 11:33:56 +0000 |
parents | 86e0b50a9b58 |
children | a96ff234ff19 d8254fc979e9 |
comparison
equal
deleted
inserted
replaced
19:e1037a1e12e7 | 142:d29b64170cf0 |
---|---|
1 #include <tommath.h> | |
2 #ifdef BN_MP_EXPTMOD_C | |
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis | 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
2 * | 4 * |
3 * LibTomMath is a library that provides multiple-precision | 5 * LibTomMath is a library that provides multiple-precision |
4 * integer arithmetic as well as number theoretic functionality. | 6 * integer arithmetic as well as number theoretic functionality. |
5 * | 7 * |
10 * The library is free for all purposes without any express | 12 * The library is free for all purposes without any express |
11 * guarantee it works. | 13 * guarantee it works. |
12 * | 14 * |
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org | 15 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
14 */ | 16 */ |
15 #include <tommath.h> | |
16 | 17 |
17 | 18 |
18 /* this is a shell function that calls either the normal or Montgomery | 19 /* this is a shell function that calls either the normal or Montgomery |
19 * exptmod functions. Originally the call to the montgomery code was | 20 * exptmod functions. Originally the call to the montgomery code was |
20 * embedded in the normal function but that wasted alot of stack space | 21 * embedded in the normal function but that wasted alot of stack space |
29 return MP_VAL; | 30 return MP_VAL; |
30 } | 31 } |
31 | 32 |
32 /* if exponent X is negative we have to recurse */ | 33 /* if exponent X is negative we have to recurse */ |
33 if (X->sign == MP_NEG) { | 34 if (X->sign == MP_NEG) { |
35 #ifdef BN_MP_INVMOD_C | |
34 mp_int tmpG, tmpX; | 36 mp_int tmpG, tmpX; |
35 int err; | 37 int err; |
36 | 38 |
37 /* first compute 1/G mod P */ | 39 /* first compute 1/G mod P */ |
38 if ((err = mp_init(&tmpG)) != MP_OKAY) { | 40 if ((err = mp_init(&tmpG)) != MP_OKAY) { |
55 | 57 |
56 /* and now compute (1/G)**|X| instead of G**X [X < 0] */ | 58 /* and now compute (1/G)**|X| instead of G**X [X < 0] */ |
57 err = mp_exptmod(&tmpG, &tmpX, P, Y); | 59 err = mp_exptmod(&tmpG, &tmpX, P, Y); |
58 mp_clear_multi(&tmpG, &tmpX, NULL); | 60 mp_clear_multi(&tmpG, &tmpX, NULL); |
59 return err; | 61 return err; |
62 #else | |
63 /* no invmod */ | |
64 return MP_VAL | |
65 #endif | |
60 } | 66 } |
61 | 67 |
68 #ifdef BN_MP_DR_IS_MODULUS_C | |
62 /* is it a DR modulus? */ | 69 /* is it a DR modulus? */ |
63 dr = mp_dr_is_modulus(P); | 70 dr = mp_dr_is_modulus(P); |
71 #else | |
72 dr = 0; | |
73 #endif | |
64 | 74 |
75 #ifdef BN_MP_REDUCE_IS_2K_C | |
65 /* if not, is it a uDR modulus? */ | 76 /* if not, is it a uDR modulus? */ |
66 if (dr == 0) { | 77 if (dr == 0) { |
67 dr = mp_reduce_is_2k(P) << 1; | 78 dr = mp_reduce_is_2k(P) << 1; |
68 } | 79 } |
80 #endif | |
69 | 81 |
70 /* if the modulus is odd or dr != 0 use the fast method */ | 82 /* if the modulus is odd or dr != 0 use the fast method */ |
83 #ifdef BN_MP_EXPTMOD_FAST_C | |
71 if (mp_isodd (P) == 1 || dr != 0) { | 84 if (mp_isodd (P) == 1 || dr != 0) { |
72 return mp_exptmod_fast (G, X, P, Y, dr); | 85 return mp_exptmod_fast (G, X, P, Y, dr); |
73 } else { | 86 } else { |
87 #endif | |
88 #ifdef BN_S_MP_EXPTMOD_C | |
74 /* otherwise use the generic Barrett reduction technique */ | 89 /* otherwise use the generic Barrett reduction technique */ |
75 return s_mp_exptmod (G, X, P, Y); | 90 return s_mp_exptmod (G, X, P, Y); |
91 #else | |
92 /* no exptmod for evens */ | |
93 return MP_VAL; | |
94 #endif | |
95 #ifdef BN_MP_EXPTMOD_FAST_C | |
76 } | 96 } |
97 #endif | |
77 } | 98 } |
78 | 99 |
100 #endif |