Mercurial > dropbear
comparison libtommath/bn_mp_jacobi.c @ 1436:60fc6476e044
Update to libtommath v1.0
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 24 Jun 2017 22:37:14 +0800 |
parents | 5ff8218bcee9 |
children | 8bba51a55704 |
comparison
equal
deleted
inserted
replaced
1435:f849a5ca2efc | 1436:60fc6476e044 |
---|---|
1 #include <tommath.h> | 1 #include <tommath_private.h> |
2 #ifdef BN_MP_JACOBI_C | 2 #ifdef BN_MP_JACOBI_C |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
4 * | 4 * |
5 * LibTomMath is a library that provides multiple-precision | 5 * LibTomMath is a library that provides multiple-precision |
6 * integer arithmetic as well as number theoretic functionality. | 6 * integer arithmetic as well as number theoretic functionality. |
10 * additional optimizations in place. | 10 * additional optimizations in place. |
11 * | 11 * |
12 * The library is free for all purposes without any express | 12 * The library is free for all purposes without any express |
13 * guarantee it works. | 13 * guarantee it works. |
14 * | 14 * |
15 * Tom St Denis, [email protected], http://math.libtomcrypt.com | 15 * Tom St Denis, [email protected], http://libtom.org |
16 */ | 16 */ |
17 | 17 |
18 /* computes the jacobi c = (a | n) (or Legendre if n is prime) | 18 /* computes the jacobi c = (a | n) (or Legendre if n is prime) |
19 * HAC pp. 73 Algorithm 2.149 | 19 * HAC pp. 73 Algorithm 2.149 |
20 * HAC is wrong here, as the special case of (0 | 1) is not | |
21 * handled correctly. | |
20 */ | 22 */ |
21 int mp_jacobi (mp_int * a, mp_int * p, int *c) | 23 int mp_jacobi (mp_int * a, mp_int * n, int *c) |
22 { | 24 { |
23 mp_int a1, p1; | 25 mp_int a1, p1; |
24 int k, s, r, res; | 26 int k, s, r, res; |
25 mp_digit residue; | 27 mp_digit residue; |
26 | 28 |
27 /* if p <= 0 return MP_VAL */ | 29 /* if a < 0 return MP_VAL */ |
28 if (mp_cmp_d(p, 0) != MP_GT) { | 30 if (mp_isneg(a) == MP_YES) { |
29 return MP_VAL; | 31 return MP_VAL; |
30 } | 32 } |
31 | 33 |
32 /* step 1. if a == 0, return 0 */ | 34 /* if n <= 0 return MP_VAL */ |
33 if (mp_iszero (a) == 1) { | 35 if (mp_cmp_d(n, 0) != MP_GT) { |
34 *c = 0; | 36 return MP_VAL; |
35 return MP_OKAY; | 37 } |
38 | |
39 /* step 1. handle case of a == 0 */ | |
40 if (mp_iszero (a) == MP_YES) { | |
41 /* special case of a == 0 and n == 1 */ | |
42 if (mp_cmp_d (n, 1) == MP_EQ) { | |
43 *c = 1; | |
44 } else { | |
45 *c = 0; | |
46 } | |
47 return MP_OKAY; | |
36 } | 48 } |
37 | 49 |
38 /* step 2. if a == 1, return 1 */ | 50 /* step 2. if a == 1, return 1 */ |
39 if (mp_cmp_d (a, 1) == MP_EQ) { | 51 if (mp_cmp_d (a, 1) == MP_EQ) { |
40 *c = 1; | 52 *c = 1; |
62 /* step 4. if e is even set s=1 */ | 74 /* step 4. if e is even set s=1 */ |
63 if ((k & 1) == 0) { | 75 if ((k & 1) == 0) { |
64 s = 1; | 76 s = 1; |
65 } else { | 77 } else { |
66 /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */ | 78 /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */ |
67 residue = p->dp[0] & 7; | 79 residue = n->dp[0] & 7; |
68 | 80 |
69 if (residue == 1 || residue == 7) { | 81 if ((residue == 1) || (residue == 7)) { |
70 s = 1; | 82 s = 1; |
71 } else if (residue == 3 || residue == 5) { | 83 } else if ((residue == 3) || (residue == 5)) { |
72 s = -1; | 84 s = -1; |
73 } | 85 } |
74 } | 86 } |
75 | 87 |
76 /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */ | 88 /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */ |
77 if ( ((p->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) { | 89 if ( ((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) { |
78 s = -s; | 90 s = -s; |
79 } | 91 } |
80 | 92 |
81 /* if a1 == 1 we're done */ | 93 /* if a1 == 1 we're done */ |
82 if (mp_cmp_d (&a1, 1) == MP_EQ) { | 94 if (mp_cmp_d (&a1, 1) == MP_EQ) { |
83 *c = s; | 95 *c = s; |
84 } else { | 96 } else { |
85 /* n1 = n mod a1 */ | 97 /* n1 = n mod a1 */ |
86 if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) { | 98 if ((res = mp_mod (n, &a1, &p1)) != MP_OKAY) { |
87 goto LBL_P1; | 99 goto LBL_P1; |
88 } | 100 } |
89 if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) { | 101 if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) { |
90 goto LBL_P1; | 102 goto LBL_P1; |
91 } | 103 } |
98 LBL_A1:mp_clear (&a1); | 110 LBL_A1:mp_clear (&a1); |
99 return res; | 111 return res; |
100 } | 112 } |
101 #endif | 113 #endif |
102 | 114 |
103 /* $Source: /cvs/libtom/libtommath/bn_mp_jacobi.c,v $ */ | 115 /* $Source$ */ |
104 /* $Revision: 1.3 $ */ | 116 /* $Revision$ */ |
105 /* $Date: 2006/03/31 14:18:44 $ */ | 117 /* $Date$ */ |