diff libtommath/bn_mp_is_square.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
line wrap: on
line diff
--- a/libtommath/bn_mp_is_square.c	Tue May 26 23:27:26 2020 +0800
+++ b/libtommath/bn_mp_is_square.c	Tue May 26 17:36:47 2020 +0200
@@ -1,16 +1,7 @@
 #include "tommath_private.h"
 #ifdef BN_MP_IS_SQUARE_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis
- *
- * LibTomMath is a library that provides multiple-precision
- * integer arithmetic as well as number theoretic functionality.
- *
- * The library was designed directly after the MPI library by
- * Michael Fromberger but has been written from scratch with
- * additional optimizations in place.
- *
- * SPDX-License-Identifier: Unlicense
- */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
 
 /* Check if remainders are possible squares - fast exclude non-squares */
 static const char rem_128[128] = {
@@ -35,9 +26,9 @@
 };
 
 /* Store non-zero to ret if arg is square, and zero if not */
-int mp_is_square(const mp_int *arg, int *ret)
+mp_err mp_is_square(const mp_int *arg, mp_bool *ret)
 {
-   int           res;
+   mp_err        err;
    mp_digit      c;
    mp_int        t;
    unsigned long r;
@@ -49,34 +40,33 @@
       return MP_VAL;
    }
 
-   /* digits used?  (TSD) */
-   if (arg->used == 0) {
+   if (MP_IS_ZERO(arg)) {
       return MP_OKAY;
    }
 
-   /* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
-   if (rem_128[127u & DIGIT(arg, 0)] == (char)1) {
+   /* First check mod 128 (suppose that MP_DIGIT_BIT is at least 7) */
+   if (rem_128[127u & arg->dp[0]] == (char)1) {
       return MP_OKAY;
    }
 
    /* Next check mod 105 (3*5*7) */
-   if ((res = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) {
-      return res;
+   if ((err = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) {
+      return err;
    }
    if (rem_105[c] == (char)1) {
       return MP_OKAY;
    }
 
 
-   if ((res = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
-      return res;
+   if ((err = mp_init_u32(&t, 11u*13u*17u*19u*23u*29u*31u)) != MP_OKAY) {
+      return err;
    }
-   if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
+   if ((err = mp_mod(arg, &t, &t)) != MP_OKAY) {
       goto LBL_ERR;
    }
-   r = mp_get_int(&t);
+   r = mp_get_u32(&t);
    /* Check for other prime modules, note it's not an ERROR but we must
-    * free "t" so the easiest way is to goto LBL_ERR.  We know that res
+    * free "t" so the easiest way is to goto LBL_ERR.  We know that err
     * is already equal to MP_OKAY from the mp_mod call
     */
    if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL)         goto LBL_ERR;
@@ -88,20 +78,16 @@
    if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL)    goto LBL_ERR;
 
    /* Final check - is sqr(sqrt(arg)) == arg ? */
-   if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
+   if ((err = mp_sqrt(arg, &t)) != MP_OKAY) {
       goto LBL_ERR;
    }
-   if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
+   if ((err = mp_sqr(&t, &t)) != MP_OKAY) {
       goto LBL_ERR;
    }
 
    *ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
 LBL_ERR:
    mp_clear(&t);
-   return res;
+   return err;
 }
 #endif
-
-/* ref:         HEAD -> master, tag: v1.1.0 */
-/* git commit:  08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
-/* commit time: 2019-01-28 20:32:32 +0100 */