From 0d039117d988ac5132c86f5fff3206a44db6f98f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Thu, 4 Apr 2013 11:07:31 +0200
Subject: [PATCH] Use mpn_cnd_add_n and mpn_cnd_sub_n when available. Rearrange
 inclusions of gmp-glue.h.

---
 ChangeLog         | 11 +++++++++++
 ecc-hash.c        |  1 -
 ecc-internal.h    |  7 +------
 ecc-point-mul-g.c |  1 -
 ecc-point-mul.c   |  1 -
 ecc-point.c       |  1 -
 ecc-random.c      |  1 -
 ecc-scalar.c      |  1 -
 ecdsa-sign.c      |  1 -
 ecdsa-verify.c    |  1 -
 gmp-glue.h        |  9 +++++++++
 11 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6f964756..ba1c1a72 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2013-04-04  Niels Möller  <nisse@lysator.liu.se>
+
+	* gmp-glue.h (cnd_add_n, cnd_sub_n): Moved here, define in terms
+	of mpn_cnd_add_n and mpn_sub_n if available, otherwise in terms of
+	mpn_addmul_1 and mpn_submul_1. This seems to be an improvement for
+	subtraction, but more questionable for addition.
+
+	* ecc-internal.h: Include gmp-glue.h. Deleted corresponding
+	include in all files using ecc-internal.h.
+	(cnd_add_n, cnd_sub_n): Moved from here.
+
 2013-04-03  Niels Möller  <nisse@lysator.liu.se>
 
 	* ecc-point-mul-g.c (ecc_point_mul_g): New file and function.
diff --git a/ecc-hash.c b/ecc-hash.c
index 7fd5c578..8040974f 100644
--- a/ecc-hash.c
+++ b/ecc-hash.c
@@ -27,7 +27,6 @@
 #endif
 
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 
 /* Convert hash value to an integer. If the digest is larger than
    the ecc bit size, then we must truncate it and use the leftmost
diff --git a/ecc-internal.h b/ecc-internal.h
index 0df8c9e1..0c3c18b3 100644
--- a/ecc-internal.h
+++ b/ecc-internal.h
@@ -29,6 +29,7 @@
 
 #include "nettle-types.h"
 #include "ecc-curve.h"
+#include "gmp-glue.h"
 
 /* Name mangling */
 #define ecc_generic_modp _nettle_ecc_generic_modp
@@ -203,12 +204,6 @@ ecc_hash (const struct ecc_curve *ecc,
 	  mp_limb_t *hp,
 	  unsigned length, const uint8_t *digest);
 
-#define cnd_add_n(cnd, rp, ap, n)		\
-  mpn_addmul_1 ((rp), (ap), (n), (cnd) != 0)
-
-#define cnd_sub_n(cnd, rp, ap, n)		\
-  mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
-
 void
 cnd_copy (int cnd, mp_limb_t *rp, const mp_limb_t *ap, mp_size_t n);
 
diff --git a/ecc-point-mul-g.c b/ecc-point-mul-g.c
index aa9d6a92..e6f51f18 100644
--- a/ecc-point-mul-g.c
+++ b/ecc-point-mul-g.c
@@ -30,7 +30,6 @@
 
 #include "ecc.h"
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 #include "nettle-internal.h"
 
 void
diff --git a/ecc-point-mul.c b/ecc-point-mul.c
index 5ddc5685..2ecea1f9 100644
--- a/ecc-point-mul.c
+++ b/ecc-point-mul.c
@@ -30,7 +30,6 @@
 
 #include "ecc.h"
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 
 void
 ecc_point_mul (struct ecc_point *r, const struct ecc_scalar *n,
diff --git a/ecc-point.c b/ecc-point.c
index 9b2639ad..4d3489c4 100644
--- a/ecc-point.c
+++ b/ecc-point.c
@@ -28,7 +28,6 @@
 
 #include "ecc.h"
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 
 void
 ecc_point_init (struct ecc_point *p, const struct ecc_curve *ecc)
diff --git a/ecc-random.c b/ecc-random.c
index 5a8faefd..0866a7d4 100644
--- a/ecc-random.c
+++ b/ecc-random.c
@@ -30,7 +30,6 @@
 
 #include "ecc.h"
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 #include "nettle-internal.h"
 
 static int
diff --git a/ecc-scalar.c b/ecc-scalar.c
index f02fc0af..2b3c4176 100644
--- a/ecc-scalar.c
+++ b/ecc-scalar.c
@@ -28,7 +28,6 @@
 
 #include "ecc.h"
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 
 void
 ecc_scalar_init (struct ecc_scalar *s, const struct ecc_curve *ecc)
diff --git a/ecdsa-sign.c b/ecdsa-sign.c
index c0827bfd..fadd3424 100644
--- a/ecdsa-sign.c
+++ b/ecdsa-sign.c
@@ -31,7 +31,6 @@
 
 #include "ecdsa.h"
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 #include "nettle-internal.h"
 
 void
diff --git a/ecdsa-verify.c b/ecdsa-verify.c
index 37ed8686..7696d59d 100644
--- a/ecdsa-verify.c
+++ b/ecdsa-verify.c
@@ -31,7 +31,6 @@
 
 #include "ecdsa.h"
 #include "ecc-internal.h"
-#include "gmp-glue.h"
 
 int
 ecdsa_verify (const struct ecc_point *pub,
diff --git a/gmp-glue.h b/gmp-glue.h
index e7645716..fcc47d00 100644
--- a/gmp-glue.h
+++ b/gmp-glue.h
@@ -50,6 +50,15 @@
 #define gmp_alloc_limbs _nettle_gmp_alloc_limbs
 #define gmp_free_limbs _nettle_gmp_free_limbs
 
+/* Use only in-place operations, so we can fall back to addmul_1/submul_1 */
+#ifdef mpn_cnd_add_n
+# define cnd_add_n(cnd, rp, ap, n) mpn_cnd_add_n ((cnd), (rp), (rp), (ap), (n))
+# define cnd_sub_n(cnd, rp, ap, n) mpn_cnd_sub_n ((cnd), (rp), (rp), (ap), (n))
+#else
+# define cnd_add_n(cnd, rp, ap, n) mpn_addmul_1 ((rp), (ap), (n), (cnd) != 0)
+# define cnd_sub_n(cnd, rp, ap, n) mpn_submul_1 ((rp), (ap), (n), (cnd) != 0)
+#endif
+
 /* Some functions for interfacing between mpz and mpn code. Signs of
    the mpz numbers are generally ignored. */
 
-- 
GitLab