- fix coverity
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct-ecc_alice.c
index 4bb67c10ae24594239ded024998b6412f53a513b..bf5ee124193e7b09e317abda2477d3a82a43c3ec 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2013-2015 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013-2015 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 
 /**
  * Maximum allowed result value for the scalarproduct computation.
- * DLOG will fail if the result is bigger.
+ * DLOG will fail if the result is bigger.  At 1 million, the
+ * precomputation takes about 2s on a fast machine.
  */
 #define MAX_RESULT (1024 * 1024)
 
+/**
+ * How many values should DLOG store in memory (determines baseline
+ * RAM consumption, roughly 100 bytes times the value given here).
+ * Should be about SQRT (MAX_RESULT), larger values will make the
+ * online computation faster.
+ */
+#define MAX_RAM (1024)
+
 /**
  * An encrypted element key-value pair.
  */
@@ -61,11 +70,6 @@ struct MpiElement
    */
   gcry_mpi_t value;
 
-  /**
-   * r_i value, chosen at random, not disclosed to Bob.
-   */
-  gcry_mpi_t r_i;
-
 };
 
 
@@ -307,6 +311,8 @@ prepare_client_end_notification (struct AliceServiceSession *session)
   struct ClientResponseMessage *msg;
   struct GNUNET_MQ_Envelope *e;
 
+  if (NULL == session->client_mq)
+    return; /* no client left to be notified */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Sending session-end notification with status %d to client for session %s\n",
               session->status,
@@ -466,7 +472,7 @@ compute_scalar_product (struct AliceServiceSession *session,
   ai_bi = GNUNET_CRYPTO_ecc_dlog (edc,
                                   g_ai_bi);
   gcry_mpi_point_release (g_ai_bi);
-  if (MAX_RESULT == ai_bi)
+  if (INT_MAX == ai_bi)
   {
     /* result too big */
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -474,7 +480,15 @@ compute_scalar_product (struct AliceServiceSession *session,
     return NULL;
   }
   ret = gcry_mpi_new (0);
-  gcry_mpi_set_ui (ret, ai_bi);
+  if (ai_bi > 0)
+  {
+    gcry_mpi_set_ui (ret, ai_bi);
+  }
+  else
+  {
+    gcry_mpi_set_ui (ret, - ai_bi);
+    gcry_mpi_neg (ret, ret);
+  }
   return ret;
 }
 
@@ -576,8 +590,6 @@ copy_element_cb (void *cls,
   else
     gcry_mpi_add_ui (mval, mval, val);
   s->sorted_elements [s->used_element_count].value = mval;
-  s->sorted_elements [s->used_element_count].r_i
-    = GNUNET_CRYPTO_ecc_random_mod_n (edc);
   s->sorted_elements [s->used_element_count].key = &e->key;
   s->used_element_count++;
   return GNUNET_OK;
@@ -622,8 +634,6 @@ send_alices_cryptodata_message (struct AliceServiceSession *s)
   struct EccAliceCryptodataMessage *msg;
   struct GNUNET_MQ_Envelope *e;
   struct GNUNET_CRYPTO_EccPoint *payload;
-  gcry_mpi_point_t g_i;
-  gcry_mpi_point_t h_i;
   gcry_mpi_t r_ia;
   gcry_mpi_t r_ia_ai;
   unsigned int i;
@@ -664,16 +674,22 @@ send_alices_cryptodata_message (struct AliceServiceSession *s)
     r_ia_ai = gcry_mpi_new (0);
     for (i = off; i < off + todo_count; i++)
     {
+      gcry_mpi_t r_i;
+      gcry_mpi_point_t g_i;
+      gcry_mpi_point_t h_i;
+
+      r_i = GNUNET_CRYPTO_ecc_random_mod_n (edc);
       g_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
-                                        s->sorted_elements [i].r_i);
+                                        r_i);
       /* r_ia = r_i * a */
-      gcry_mpi_mul (s->sorted_elements[i].r_i,
-                    my_privkey,
-                    r_ia);
+      gcry_mpi_mul (r_ia,
+                    r_i,
+                    my_privkey);
+      gcry_mpi_release (r_i);
       /* r_ia_ai = r_ia + a_i */
       gcry_mpi_add (r_ia_ai,
-                    s->sorted_elements[i].value,
-                    r_ia);
+                    r_ia,
+                    s->sorted_elements[i].value);
       h_i = GNUNET_CRYPTO_ecc_dexp_mpi (edc,
                                         r_ia_ai);
       GNUNET_CRYPTO_ecc_point_to_bin (edc,
@@ -1078,8 +1094,7 @@ GSS_handle_alice_client_message (void *cls,
  * @param tc unused
  */
 static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Shutting down, initiating cleanup.\n");
@@ -1157,8 +1172,8 @@ run (void *cls,
   };
 
   cfg = c;
-  edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_RESULT /* max value */,
-                                        1024 /* RAM */);
+  edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_RESULT,
+                                        MAX_RAM);
   /* Select a random 'a' value for Alice */
   GNUNET_CRYPTO_ecc_rnd_mpi (edc,
                              &my_privkey,
@@ -1180,9 +1195,8 @@ run (void *cls,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                &shutdown_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
 
 }