[PATCH] Add gcry_mpi_ec_sub.

Markus Teich markus.teich at stusta.mhn.de
Mon Jul 21 19:52:27 CEST 2014


This function subtracts two points on the curve. Only Twisted Edwards curves are
supported with this change.
---


Heyho,

I tried to implement it correctly. Beware, this is my first libgcrypt patch.
Please review carefully for any bugs.

--Markus


 mpi/ec.c         | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcrypt.h.in  |  4 ++++
 src/visibility.c |  8 +++++++
 src/visibility.h |  1 +
 4 files changed, 78 insertions(+)

diff --git a/mpi/ec.c b/mpi/ec.c
index 4f35de0..5cfc208 100644
--- a/mpi/ec.c
+++ b/mpi/ec.c
@@ -1094,6 +1094,71 @@ _gcry_mpi_ec_add_points (mpi_point_t result,
 }
 
 
+/* RESULT = P1 - P2  (Weierstrass version).*/
+static void
+sub_points_weierstrass (mpi_point_t result,
+                        mpi_point_t p1, mpi_point_t p2,
+                        mpi_ec_t ctx)
+{
+  (void)result;
+  (void)p1;
+  (void)p2;
+  (void)ctx;
+  log_fatal ("%s: %s not yet supported\n",
+             "_gcry_mpi_ec_sub_points", "Weierstrass");
+}
+
+
+/* RESULT = P1 - P2  (Montgomery version).*/
+static void
+sub_points_montgomery (mpi_point_t result,
+                       mpi_point_t p1, mpi_point_t p2,
+                       mpi_ec_t ctx)
+{
+  (void)result;
+  (void)p1;
+  (void)p2;
+  (void)ctx;
+  log_fatal ("%s: %s not yet supported\n",
+             "_gcry_mpi_ec_sub_points", "Montgomery");
+}
+
+
+/* RESULT = P1 - P2  (Twisted Edwards version).*/
+static void
+sub_points_edwards (mpi_point_t result,
+                    mpi_point_t p1, mpi_point_t p2,
+                    mpi_ec_t ctx)
+{
+  mpi_point_t p2i = _gcry_mpi_point_new (0);
+  point_set (p2i, p2);
+  _gcry_mpi_neg (p2i->y, p2i->y);
+  add_points_edwards (result, p1, p2i, ctx);
+  _gcry_mpi_point_release (p2i);
+}
+
+
+/* RESULT = P1 - P2 */
+void
+_gcry_mpi_ec_sub_points (mpi_point_t result,
+                         mpi_point_t p1, mpi_point_t p2,
+                         mpi_ec_t ctx)
+{
+  switch (ctx->model)
+    {
+    case MPI_EC_WEIERSTRASS:
+      sub_points_weierstrass (result, p1, p2, ctx);
+      break;
+    case MPI_EC_MONTGOMERY:
+      sub_points_montgomery (result, p1, p2, ctx);
+      break;
+    case MPI_EC_EDWARDS:
+      sub_points_edwards (result, p1, p2, ctx);
+      break;
+    }
+}
+
+
 /* Scalar point multiplication - the main function for ECC.  If takes
    an integer SCALAR and a POINT as well as the usual context CTX.
    RESULT will be set to the resulting point. */
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index a5f8350..7dbad07 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -703,6 +703,10 @@ void gcry_mpi_ec_dup (gcry_mpi_point_t w, gcry_mpi_point_t u, gcry_ctx_t ctx);
 void gcry_mpi_ec_add (gcry_mpi_point_t w,
                       gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx);
 
+/* W = U - V.  */
+void gcry_mpi_ec_sub (gcry_mpi_point_t w,
+                      gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx);
+
 /* W = N * U.  */
 void gcry_mpi_ec_mul (gcry_mpi_point_t w, gcry_mpi_t n, gcry_mpi_point_t u,
                       gcry_ctx_t ctx);
diff --git a/src/visibility.c b/src/visibility.c
index 6ed57ca..fa23e53 100644
--- a/src/visibility.c
+++ b/src/visibility.c
@@ -567,6 +567,14 @@ gcry_mpi_ec_add (gcry_mpi_point_t w,
 }
 
 void
+gcry_mpi_ec_sub (gcry_mpi_point_t w,
+                 gcry_mpi_point_t u, gcry_mpi_point_t v, gcry_ctx_t ctx)
+{
+  _gcry_mpi_ec_sub_points (w, u, v,
+                           _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC));
+}
+
+void
 gcry_mpi_ec_mul (gcry_mpi_point_t w, gcry_mpi_t n, gcry_mpi_point_t u,
                  gcry_ctx_t ctx)
 {
diff --git a/src/visibility.h b/src/visibility.h
index 96b5235..f7b5ace 100644
--- a/src/visibility.h
+++ b/src/visibility.h
@@ -486,6 +486,7 @@ MARK_VISIBLEX (_gcry_mpi_get_const)
 
 #define gcry_mpi_abs                _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mpi_ec_add             _gcry_USE_THE_UNDERSCORED_FUNCTION
+#define gcry_mpi_ec_sub             _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mpi_ec_curve_point     _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mpi_ec_dup             _gcry_USE_THE_UNDERSCORED_FUNCTION
 #define gcry_mpi_ec_get_affine      _gcry_USE_THE_UNDERSCORED_FUNCTION
-- 
1.8.5.5




More information about the Gcrypt-devel mailing list