]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
New function ecc_mod_equal_p, based on patch by Wim Lewis.
authorNiels Möller <nisse@lysator.liu.se>
Mon, 8 Nov 2021 19:10:32 +0000 (20:10 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 8 Nov 2021 19:10:32 +0000 (20:10 +0100)
ChangeLog
ecc-internal.h
ecc-mod-arith.c
testsuite/ecc-modinv-test.c

index 4cc12d7a94951b5da28ede3ad96bc5e41e5990cb..6c59e38d444531a0c0221efc25e116df0e0cc6c1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
        (struct ecc_modulo): Renamed corresponding function pointer to
        sqrt_ratio. Updated all uses.
 
+2021-10-28  Niels Möller  <nisse@lysator.liu.se>
+
+       * ecc-mod-arith.c (ecc_mod_equal_p): New function, moved from
+       ecc-modinv-test.c. Based on patch by Wim Lewis.
+       * testsuite/ecc-modinv-test.c (mod_eq_p): Deleted, replaced with ecc_mod_equal_p.
+
 2021-10-26  Niels Möller  <nisse@lysator.liu.se>
 
        * ecc-mod-arith.c (ecc_mod_zero_p): New function.
index 277f5a02af6066cbaaa42545d0a8227de80990a6..2ea553b52692ab12cfb4d804e8ca72bec618ad0a 100644 (file)
@@ -43,6 +43,7 @@
 #define ecc_pp1_redc _nettle_ecc_pp1_redc
 #define ecc_pm1_redc _nettle_ecc_pm1_redc
 #define ecc_mod_zero_p _nettle_ecc_mod_zero_p
+#define ecc_mod_equal_p _nettle_ecc_mod_equal_p
 #define ecc_mod_add _nettle_ecc_mod_add
 #define ecc_mod_sub _nettle_ecc_mod_sub
 #define ecc_mod_mul_1 _nettle_ecc_mod_mul_1
@@ -248,6 +249,12 @@ ecc_mod_inv_func ecc_mod_inv;
 int
 ecc_mod_zero_p (const struct ecc_modulo *m, const mp_limb_t *xp);
 
+/* Requires that a < 2m, and ref < m, needs m->size limbs of scratch
+   space. Overlap, a == scratch or ref == scratch, is allowed. */
+int
+ecc_mod_equal_p (const struct ecc_modulo *m, const mp_limb_t *a,
+                const mp_limb_t *ref, mp_limb_t *scratch);
+
 void
 ecc_mod_add (const struct ecc_modulo *m, mp_limb_t *rp,
             const mp_limb_t *ap, const mp_limb_t *bp);
index 0b0631af7bc37d0a13e9c2ff061cb36473f60e4f..310cbb1d68b1699f26f1fb3b2be2dde2f17720e9 100644 (file)
@@ -58,6 +58,16 @@ ecc_mod_zero_p (const struct ecc_modulo *m, const mp_limb_t *xp_in)
   return (is_non_zero == 0) | (is_not_p == 0);
 }
 
+int
+ecc_mod_equal_p (const struct ecc_modulo *m, const mp_limb_t *a,
+                const mp_limb_t *ref, mp_limb_t *scratch)
+{
+  mp_limb_t cy;
+  cy = mpn_sub_n (scratch, a, ref, m->size);
+  /* If cy > 0, i.e., a < ref, then they can't be equal mod m. */
+  return (cy == 0) & ecc_mod_zero_p (m, scratch);
+}
+
 void
 ecc_mod_add (const struct ecc_modulo *m, mp_limb_t *rp,
             const mp_limb_t *ap, const mp_limb_t *bp)
index b9993e02b57d5a8ef4829d289d254eed23189b31..a30f182e5d8621f36007d11ec2627eccf1abd2f8 100644 (file)
@@ -38,18 +38,6 @@ ref_modinv (mp_limb_t *rp, const mp_limb_t *ap,
   return res;
 }
 
-/* Requires that a < 2m, and ref < m. */
-static int
-mod_eq_p (const struct ecc_modulo *m, const mp_limb_t *a, const mp_limb_t *ref,
-         mp_limb_t *scratch) {
-  mp_limb_t cy;
-  assert (mpn_cmp (ref, m->m, m->size) < 0);
-  cy = mpn_sub_n (scratch, a, ref, m->size);
-  /* If cy > 0, i.e., a < ref, then they can't be equal mod m. */
-  return (cy == 0) & ecc_mod_zero_p (m, scratch);
-
-}
-
 #define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
 #define COUNT 500
 
@@ -120,7 +108,7 @@ test_modulo (gmp_randstate_t rands, const char *name,
          continue;
        }
       m->invert (m, ai, a, scratch);
-      if (!mod_eq_p (m, ai, ref, scratch))
+      if (!ecc_mod_equal_p (m, ai, ref, scratch))
        {
          fprintf (stderr, "%s->invert failed (test %u, bit size %u):\n",
                   name, j, m->bit_size);