summaryrefslogtreecommitdiffstats
path: root/libtommath/bn_mp_jacobi.c
diff options
context:
space:
mode:
authorjan.nijtmans <nijtmans@users.sourceforge.net>2017-09-15 14:44:00 (GMT)
committerjan.nijtmans <nijtmans@users.sourceforge.net>2017-09-15 14:44:00 (GMT)
commit30f7e69b182d1267056ee2628a860891f6555aa3 (patch)
tree4598f9d64ea232476fcf18bbef6bd413444c0c07 /libtommath/bn_mp_jacobi.c
parentb167e2845e3a2d5b2ffe3a877cb19c3f430aeacf (diff)
downloadtcl-30f7e69b182d1267056ee2628a860891f6555aa3.zip
tcl-30f7e69b182d1267056ee2628a860891f6555aa3.tar.gz
tcl-30f7e69b182d1267056ee2628a860891f6555aa3.tar.bz2
re-format everything through astyle. Taken from libtom/libtommath (pull request [https://github.com/libtom/libtommath/pull/85|85])
Diffstat (limited to 'libtommath/bn_mp_jacobi.c')
-rw-r--r--libtommath/bn_mp_jacobi.c152
1 files changed, 77 insertions, 75 deletions
diff --git a/libtommath/bn_mp_jacobi.c b/libtommath/bn_mp_jacobi.c
index 5fc8593..8981393 100644
--- a/libtommath/bn_mp_jacobi.c
+++ b/libtommath/bn_mp_jacobi.c
@@ -20,95 +20,97 @@
* HAC is wrong here, as the special case of (0 | 1) is not
* handled correctly.
*/
-int mp_jacobi (mp_int * a, mp_int * n, int *c)
+int mp_jacobi(mp_int *a, mp_int *n, int *c)
{
- mp_int a1, p1;
- int k, s, r, res;
- mp_digit residue;
+ mp_int a1, p1;
+ int k, s, r, res;
+ mp_digit residue;
- /* if a < 0 return MP_VAL */
- if (mp_isneg(a) == MP_YES) {
- return MP_VAL;
- }
+ /* if a < 0 return MP_VAL */
+ if (mp_isneg(a) == MP_YES) {
+ return MP_VAL;
+ }
- /* if n <= 0 return MP_VAL */
- if (mp_cmp_d(n, 0) != MP_GT) {
- return MP_VAL;
- }
+ /* if n <= 0 return MP_VAL */
+ if (mp_cmp_d(n, 0) != MP_GT) {
+ return MP_VAL;
+ }
- /* step 1. handle case of a == 0 */
- if (mp_iszero (a) == MP_YES) {
- /* special case of a == 0 and n == 1 */
- if (mp_cmp_d (n, 1) == MP_EQ) {
- *c = 1;
- } else {
- *c = 0;
- }
- return MP_OKAY;
- }
+ /* step 1. handle case of a == 0 */
+ if (mp_iszero(a) == MP_YES) {
+ /* special case of a == 0 and n == 1 */
+ if (mp_cmp_d(n, 1) == MP_EQ) {
+ *c = 1;
+ } else {
+ *c = 0;
+ }
+ return MP_OKAY;
+ }
- /* step 2. if a == 1, return 1 */
- if (mp_cmp_d (a, 1) == MP_EQ) {
- *c = 1;
- return MP_OKAY;
- }
+ /* step 2. if a == 1, return 1 */
+ if (mp_cmp_d(a, 1) == MP_EQ) {
+ *c = 1;
+ return MP_OKAY;
+ }
- /* default */
- s = 0;
+ /* default */
+ s = 0;
- /* step 3. write a = a1 * 2**k */
- if ((res = mp_init_copy (&a1, a)) != MP_OKAY) {
- return res;
- }
+ /* step 3. write a = a1 * 2**k */
+ if ((res = mp_init_copy(&a1, a)) != MP_OKAY) {
+ return res;
+ }
- if ((res = mp_init (&p1)) != MP_OKAY) {
- goto LBL_A1;
- }
+ if ((res = mp_init(&p1)) != MP_OKAY) {
+ goto LBL_A1;
+ }
- /* divide out larger power of two */
- k = mp_cnt_lsb(&a1);
- if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) {
- goto LBL_P1;
- }
-
- /* step 4. if e is even set s=1 */
- if ((k & 1) == 0) {
- s = 1;
- } else {
- /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
- residue = n->dp[0] & 7;
+ /* divide out larger power of two */
+ k = mp_cnt_lsb(&a1);
+ if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) {
+ goto LBL_P1;
+ }
- if ((residue == 1) || (residue == 7)) {
+ /* step 4. if e is even set s=1 */
+ if ((k & 1) == 0) {
s = 1;
- } else if ((residue == 3) || (residue == 5)) {
- s = -1;
- }
- }
+ } else {
+ /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
+ residue = n->dp[0] & 7;
- /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
- if ( ((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
- s = -s;
- }
+ if ((residue == 1) || (residue == 7)) {
+ s = 1;
+ } else if ((residue == 3) || (residue == 5)) {
+ s = -1;
+ }
+ }
- /* if a1 == 1 we're done */
- if (mp_cmp_d (&a1, 1) == MP_EQ) {
- *c = s;
- } else {
- /* n1 = n mod a1 */
- if ((res = mp_mod (n, &a1, &p1)) != MP_OKAY) {
- goto LBL_P1;
- }
- if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) {
- goto LBL_P1;
- }
- *c = s * r;
- }
+ /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
+ if (((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
+ s = -s;
+ }
+
+ /* if a1 == 1 we're done */
+ if (mp_cmp_d(&a1, 1) == MP_EQ) {
+ *c = s;
+ } else {
+ /* n1 = n mod a1 */
+ if ((res = mp_mod(n, &a1, &p1)) != MP_OKAY) {
+ goto LBL_P1;
+ }
+ if ((res = mp_jacobi(&p1, &a1, &r)) != MP_OKAY) {
+ goto LBL_P1;
+ }
+ *c = s * r;
+ }
- /* done */
- res = MP_OKAY;
-LBL_P1:mp_clear (&p1);
-LBL_A1:mp_clear (&a1);
- return res;
+ /* done */
+ res = MP_OKAY;
+LBL_P1:
+ mp_clear(&p1);
+LBL_A1:
+ mp_clear(&a1);
+ return res;
}
#endif