From ce70362aceb2e6cd71a7ddfdc8d190ea8d2821b6 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Wed, 25 Nov 2015 21:24:13 +0100 Subject: [PATCH 1/3] Rename parameter p to n --- bn_mp_jacobi.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c index 160ac8f..76596a6 100644 --- a/bn_mp_jacobi.c +++ b/bn_mp_jacobi.c @@ -18,14 +18,14 @@ /* computes the jacobi c = (a | n) (or Legendre if n is prime) * HAC pp. 73 Algorithm 2.149 */ -int mp_jacobi (mp_int * a, mp_int * p, 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; - /* if p <= 0 return MP_VAL */ - if (mp_cmp_d(p, 0) != MP_GT) { + /* if n <= 0 return MP_VAL */ + if (mp_cmp_d(n, 0) != MP_GT) { return MP_VAL; } @@ -64,7 +64,7 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c) s = 1; } else { /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */ - residue = p->dp[0] & 7; + residue = n->dp[0] & 7; if (residue == 1 || residue == 7) { s = 1; @@ -74,7 +74,7 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c) } /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */ - if ( ((p->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) { + if ( ((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) { s = -s; } @@ -83,7 +83,7 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c) *c = s; } else { /* n1 = n mod a1 */ - if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) { + if ((res = mp_mod (n, &a1, &p1)) != MP_OKAY) { goto LBL_P1; } if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) { From 34e6b723ffeceebe87f9cf89f4e73ba93d566d51 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Wed, 25 Nov 2015 21:24:21 +0100 Subject: [PATCH 2/3] Implement handling of special case (0 | 1) --- bn_mp_jacobi.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c index 76596a6..6a1155d 100644 --- a/bn_mp_jacobi.c +++ b/bn_mp_jacobi.c @@ -17,6 +17,8 @@ /* computes the jacobi c = (a | n) (or Legendre if n is prime) * HAC pp. 73 Algorithm 2.149 + * 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) { @@ -29,10 +31,15 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c) return MP_VAL; } - /* step 1. if a == 0, return 0 */ + /* step 1. handle case of a == 0 */ if (mp_iszero (a) == MP_YES) { - *c = 0; - return MP_OKAY; + /* 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 */ From 7176a8777a23c27e9e9adecde3e02dea1ad66994 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Sun, 29 Nov 2015 22:43:52 +0100 Subject: [PATCH 3/3] Add tests for mp_jacobi() --- demo/demo.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/demo/demo.c b/demo/demo.c index a5ac674..45be283 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -12,7 +12,7 @@ * Configuration */ #ifndef LTM_DEMO_TEST_VS_MTEST -#define LTM_DEMO_TEST_VS_MTEST 1 +#define LTM_DEMO_TEST_VS_MTEST 0 #endif #ifndef LTM_DEMO_TEST_REDUCE_2K_L @@ -114,6 +114,16 @@ struct mp_sqrtmod_prime_st sqrtmod_prime[] = { { 7, 9, 4 }, { 113, 2, 62 } }; +struct mp_jacobi_st { + unsigned long n; + int c[16]; +}; +struct mp_jacobi_st jacobi[] = { + { 3, { 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 } }, + { 5, { 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0 } }, + { 7, { 1, -1, 1, -1, -1, 0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1 } }, + { 9, { -1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 } }, +}; char cmd[4096], buf[4096]; int main(void) @@ -186,6 +196,35 @@ int main(void) mp_add_d(&a, 1, &b); mp_add_d(&a, 6, &b); + + mp_set_int(&a, 0); + mp_set_int(&b, 1); + if ((ix = mp_jacobi(&a, &b, &i)) != MP_OKAY) { + printf("Failed executing mp_jacobi(0 | 1) %s.\n", mp_error_to_string(ix)); + return EXIT_FAILURE; + } + if (i != 1) { + printf("Failed trivial mp_jacobi(0 | 1) %d != 1\n", i); + return EXIT_FAILURE; + } + for (cnt = 0; cnt < (int)(sizeof(jacobi)/sizeof(jacobi[0])); ++cnt) { + mp_set_int(&b, jacobi[cnt].n); + /* only test positive values of a */ +// for (n = -5; n <= 10; ++n) { + for (n = 0; n <= 10; ++n) { + mp_set_int(&a, abs(n)); + if (n < 0) mp_neg(&a, &a); + if ((ix = mp_jacobi(&a, &b, &i)) != MP_OKAY) { + printf("Failed executing mp_jacobi(%d | %lu) %s.\n", n, jacobi[cnt].n, mp_error_to_string(ix)); + return EXIT_FAILURE; + } + if (i != jacobi[cnt].c[n + 5]) { + printf("Failed trivial mp_jacobi(%d | %lu) %d != %d\n", n, jacobi[cnt].n, i, jacobi[cnt].c[n + 5]); + return EXIT_FAILURE; + } + } + } + // test montgomery printf("Testing: montgomery...\n"); for (i = 1; i <= 10; i++) {