Commit ab7bcec57becb83d7c55b92ad94a4d7b289aa545

czurnieden 2021-12-13T21:08:49

Introduced 0 (zero) to mp_is_square as a perfect square

diff --git a/demo/test.c b/demo/test.c
index 4ce8131..b9ef509 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -677,6 +677,17 @@ static int test_mp_is_square(void)
 
    DOR(mp_init_multi(&a, &b, NULL));
 
+
+   /* Domain is {x \in \mathbb{Z} : x \le 0}  */
+   mp_set_l(&a, -1);
+   EXPECT(mp_is_square(&a, &res) == MP_VAL);
+   EXPECT(!res);
+
+   /* Zero is a perfect square, too */
+   mp_zero(&a);
+   DO(mp_is_square(&a, &res));
+   EXPECT(res);
+
    for (i = 0; i < 1000; ++i) {
       printf("%6d\r", i);
       fflush(stdout);
diff --git a/mp_is_square.c b/mp_is_square.c
index db1cb3f..d2856e2 100644
--- a/mp_is_square.c
+++ b/mp_is_square.c
@@ -41,6 +41,7 @@ mp_err mp_is_square(const mp_int *arg, bool *ret)
    }
 
    if (mp_iszero(arg)) {
+      *ret = true;
       return MP_OKAY;
    }