Add tests for mp_jacobi()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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++) {