From 9b1ad3e554f1dda3b65d017b1f79debddff8e712 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Sun, 12 Nov 2023 13:59:37 +0100
Subject: [PATCH] Fix is_zero_limb to work with in c89 and mini-gmp builds.

---
 ChangeLog      | 8 ++++++++
 ecc-internal.h | 7 -------
 gmp-glue.c     | 2 +-
 gmp-glue.h     | 9 +++++++++
 4 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4d53b896..be68bab2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-11-12  Niels Möller  <nisse@lysator.liu.se>
+
+	* gmp-glue.h (GMP_LIMB_BITS) [NETTLE_USE_MINI_GMP]: Define as alias for
+	GMP_NUMB_BITS.
+	(is_zero_limb): Move inline function here. Add static, for
+	compatibility with c89. and mini-gmp builds.
+	* gmp-glue.c (sec_zero_p): Use is_zero_limb.
+
 2023-11-06  Niels Möller  <nisse@lysator.liu.se>
 
 	Avoid comparison like cnd = (x == 0) in code intended to be
diff --git a/ecc-internal.h b/ecc-internal.h
index 2a5e3ae1..53359b57 100644
--- a/ecc-internal.h
+++ b/ecc-internal.h
@@ -85,13 +85,6 @@
 #define curve25519_eh_to_x _nettle_curve25519_eh_to_x
 #define curve448_eh_to_x _nettle_curve448_eh_to_x
 
-inline int
-is_zero_limb (mp_limb_t x)
-{
-  x |= (x << 1);
-  return ((x >> 1) - 1) >> (GMP_LIMB_BITS - 1);
-}
-
 extern const struct ecc_curve _nettle_secp_192r1;
 extern const struct ecc_curve _nettle_secp_224r1;
 extern const struct ecc_curve _nettle_secp_256r1;
diff --git a/gmp-glue.c b/gmp-glue.c
index ffce6c30..45189cdc 100644
--- a/gmp-glue.c
+++ b/gmp-glue.c
@@ -130,7 +130,7 @@ sec_zero_p (const mp_limb_t *ap, mp_size_t n)
   for (i = 0, w = 0; i < n; i++)
     w |= ap[i];
 
-  return w == 0;
+  return is_zero_limb (w);
 }
 
 /* Additional convenience functions. */
diff --git a/gmp-glue.h b/gmp-glue.h
index dc0ede2a..afe94635 100644
--- a/gmp-glue.h
+++ b/gmp-glue.h
@@ -56,6 +56,8 @@
 #define TMP_GMP_FREE(name) (gmp_free(name, tmp_##name##_size))
 
 #if NETTLE_USE_MINI_GMP
+#define GMP_LIMB_BITS GMP_NUMB_BITS
+
 mp_limb_t
 mpn_cnd_add_n (mp_limb_t cnd, mp_limb_t *rp,
 	       const mp_limb_t *ap, const mp_limb_t *bp, mp_size_t n);
@@ -72,6 +74,13 @@ mpn_sec_tabselect (volatile mp_limb_t *rp, volatile const mp_limb_t *table,
 		   mp_size_t rn, unsigned tn, unsigned k);
 #endif
 
+static inline int
+is_zero_limb (mp_limb_t x)
+{
+  x |= (x << 1);
+  return ((x >> 1) - 1) >> (GMP_LIMB_BITS - 1);
+}
+
 /* Side-channel silent variant of mpn_zero_p. */
 int
 sec_zero_p (const mp_limb_t *ap, mp_size_t n);
-- 
GitLab