Use more-or-equal (some machines are fast enough)
[oweals/gnunet.git] / src / scalarproduct / gnunet-service-scalarproduct.c
index 38a22b1f6810ddb9576b4b60220b07b361a5f98b..44d80ed1f06c84c0e904707f9aab622b2e20989a 100644 (file)
@@ -486,7 +486,8 @@ encrypt_element (gcry_mpi_t c, gcry_mpi_t m, gcry_mpi_t g, gcry_mpi_t n, gcry_mp
 
   GNUNET_assert (tmp = gcry_mpi_new (0));
 
-  while (0 >= gcry_mpi_cmp_ui (tmp, 1)) {
+  while (0 >= gcry_mpi_cmp_ui (tmp, 1))
+  {
     gcry_mpi_randomize (tmp, KEYBITS / 3, GCRY_WEAK_RANDOM);
     // r must be 1 < r < n
   }
@@ -913,7 +914,7 @@ prepare_client_response (void *cls,
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) +product_length;
   msg = GNUNET_malloc (msg_length);
   msg->key = session->key;
-  &msg->peer = session->peer;
+  msg->peer = session->peer;
   if (product_exported != NULL)
   {
     memcpy (&msg[1], product_exported, product_length);
@@ -1235,11 +1236,14 @@ compute_service_response (struct ServiceSession * request,
   rand_pi_prime = GNUNET_malloc (sizeof (gcry_mpi_t) * count);
 
   // convert responder session to from long to mpi
-  for (i = 0, j = 0; i < response->total && j < count; i++) {
-    if (request->mask[i / 8] & (1 << (i % 8))) {
+  for (i = 0, j = 0; i < response->total && j < count; i++)
+  {
+    if (request->mask[i / 8] & (1 << (i % 8)))
+    {
       value = response->vector[i] >= 0 ? response->vector[i] : -response->vector[i];
       // long to gcry_mpi_t
-      if (0 > response->vector[i]) {
+      if (0 > response->vector[i])
+      {
         b[j] = gcry_mpi_new (0);
         gcry_mpi_sub_ui (b[j], b[j], value);
       }
@@ -1251,16 +1255,19 @@ compute_service_response (struct ServiceSession * request,
   }
   GNUNET_free (response->vector);
   response->vector = NULL;
-
+  q = NULL;
+  p = NULL;
   tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "n", 0);
-  if (!tmp_exp) {
+  if (!tmp_exp)
+  {
     GNUNET_break_op (0);
     gcry_sexp_release (request->remote_pubkey);
     request->remote_pubkey = NULL;
     goto except;
   }
   remote_n = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
-  if (!remote_n) {
+  if (!remote_n)
+  {
     GNUNET_break (0);
     gcry_sexp_release (tmp_exp);
     goto except;
@@ -1271,13 +1278,15 @@ compute_service_response (struct ServiceSession * request,
   tmp_exp = gcry_sexp_find_token (request->remote_pubkey, "g", 0);
   gcry_sexp_release (request->remote_pubkey);
   request->remote_pubkey = NULL;
-  if (!tmp_exp) {
+  if (!tmp_exp)
+  {
     GNUNET_break_op (0);
     gcry_mpi_release (remote_n);
     goto except;
   }
   remote_g = gcry_sexp_nth_mpi (tmp_exp, 1, GCRYMPI_FMT_USG);
-  if (!remote_g) {
+  if (!remote_g)
+  {
     GNUNET_break (0);
     gcry_mpi_release (remote_n);
     gcry_sexp_release (tmp_exp);
@@ -1327,7 +1336,8 @@ compute_service_response (struct ServiceSession * request,
   // vectors, which get rid of all the lookups in p/q.
   // however, ap/aq are not absolutely necessary but are just abstraction
   // Calculate Kp = E(S + a_pi) (+) E(S - r_pi - b_pi)
-  for (i = 0; i < count; i++) {
+  for (i = 0; i < count; i++)
+  {
     // E(S - r_pi - b_pi)
     gcry_mpi_sub (r[i], my_offset, rand_pi[i]);
     gcry_mpi_sub (r[i], r[i], b_pi[i]);
@@ -1341,7 +1351,8 @@ compute_service_response (struct ServiceSession * request,
   GNUNET_free (rand_pi);
 
   // Calculate Kq = E(S + a_qi) (+) E(S - r_qi)
-  for (i = 0; i < count; i++) {
+  for (i = 0; i < count; i++)
+  {
     // E(S - r_qi)
     gcry_mpi_sub (r_prime[i], my_offset, rand_pi_prime[i]);
     encrypt_element (r_prime[i], r_prime[i], remote_g, remote_n, remote_nsquare);
@@ -1383,7 +1394,8 @@ compute_service_response (struct ServiceSession * request,
     ret = GNUNET_OK;
 
 except:
-  for (i = 0; i < count; i++) {
+  for (i = 0; i < count; i++)
+  {
     gcry_mpi_release (b[i]);
     gcry_mpi_release (request->a[i]);
   }
@@ -1391,7 +1403,9 @@ except:
   GNUNET_free (b);
   GNUNET_free (request->a);
   request->a = NULL;
-
+  GNUNET_free_non_null (p);
+  GNUNET_free_non_null (q);
+  GNUNET_free (rand);
   return ret;
 }
 
@@ -1437,7 +1451,8 @@ prepare_service_request_multipart (void *cls)
   a = gcry_mpi_new (KEYBITS * 2);
   current = (unsigned char *) &msg[1];
   // encrypt our vector and generate string representations
-  for (i = session->last_processed, j = 0; i < session->total; i++) {
+  for (i = session->last_processed, j = 0; i < session->total; i++)
+  {
     // is this a used element?
     if (session->mask[i / 8] & 1 << (i % 8)) {
       if (todo_count <= j)
@@ -1683,7 +1698,7 @@ handle_client_request (void *cls,
   if ((ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) +element_count * sizeof (int32_t) + mask_length))
       || (0 == element_count)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _ ("Invalid message received from client, session information incorrect!\n"));
+                _("Invalid message received from client, session information incorrect!\n"));
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
@@ -1694,7 +1709,7 @@ handle_client_request (void *cls,
                                      element_count,
                                      NULL, NULL)) {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _ ("Duplicate session information received, cannot create new session with key `%s'\n"),
+                _("Duplicate session information received, cannot create new session with key `%s'\n"),
                 GNUNET_h2s (&msg->key));
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
@@ -1712,9 +1727,10 @@ handle_client_request (void *cls,
   session->vector = GNUNET_malloc (sizeof (int32_t) * element_count);
   vector = (int32_t *) & msg[1];
 
-  if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type) {
+  if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type)
+  {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                _ ("Got client-request-session with key %s, preparing channel to remote service.\n"),
+                _("Got client-request-session with key %s, preparing channel to remote service.\n"),
                 GNUNET_h2s (&session->key));
 
     session->role = ALICE;
@@ -1724,7 +1740,8 @@ handle_client_request (void *cls,
 
     // copy over the elements
     session->used = 0;
-    for (i = 0; i < element_count; i++) {
+    for (i = 0; i < element_count; i++)
+    {
       session->vector[i] = ntohl (vector[i]);
       if (session->vector[i] == 0)
         session->mask[i / 8] &= ~(1 << (i % 8));
@@ -1732,7 +1749,8 @@ handle_client_request (void *cls,
         session->used++;
     }
 
-    if (0 == session->used) {
+    if (0 == session->used)
+    {
       GNUNET_break_op (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1740,7 +1758,8 @@ handle_client_request (void *cls,
       return;
     }
     //session with ourself makes no sense!
-    if (!memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity))) {
+    if (!memcmp (&msg->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
+    {
       GNUNET_break (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1773,7 +1792,8 @@ handle_client_request (void *cls,
                                       session);
 
   }
-  else {
+  else
+  {
     struct ServiceSession * requesting_session;
     enum SessionState needed_state = SERVICE_REQUEST_RECEIVED;
 
@@ -1793,16 +1813,22 @@ handle_client_request (void *cls,
                                                 &session->key,
                                                 session->total,
                                                 &needed_state, NULL);
-    if (NULL != requesting_session) {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s and a matching service-request-session set, processing.\n"), GNUNET_h2s (&session->key));
+    if (NULL != requesting_session)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  _("Got client-responder-session with key %s and a matching service-request-session set, processing.\n"),
+                  GNUNET_h2s (&session->key));
       if (GNUNET_OK != compute_service_response (requesting_session, session))
         session->client_notification_task =
               GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
                                         session);
 
     }
-    else {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s but NO matching service-request-session set, queuing element for later use.\n"), GNUNET_h2s (&session->key));
+    else
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  _("Got client-responder-session with key %s but NO matching service-request-session set, queuing element for later use.\n"),
+                  GNUNET_h2s (&session->key));
       // no matching session exists yet, store the response
       // for later processing by handle_service_request()
     }
@@ -1826,11 +1852,13 @@ static void *
 channel_incoming_handler (void *cls,
                          struct GNUNET_MESH_Channel *channel,
                          const struct GNUNET_PeerIdentity *initiator,
-                         uint32_t port, enum MeshOption options)
+                         uint32_t port, enum GNUNET_MESH_ChannelOption options)
 {
   struct ServiceSession * c = GNUNET_new (struct ServiceSession);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("New incoming channel from peer %s.\n"), GNUNET_i2s (initiator));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              _("New incoming channel from peer %s.\n"),
+              GNUNET_i2s (initiator));
 
   c->peer = *initiator;
   c->channel = channel;
@@ -1927,7 +1955,8 @@ compute_scalar_product (struct ServiceSession * session)
   // due to the introduced static offset S, we now also have to remove this
   // from the E(a_pi)(+)E(-b_pi-r_pi) and E(a_qi)(+)E(-r_qi) twice each,
   // the result is E((S + a_pi) + (S -b_pi-r_pi)) and E(S + a_qi + S - r_qi)
-  for (i = 0; i < count; i++) {
+  for (i = 0; i < count; i++)
+  {
     decrypt_element (session->r[i], session->r[i], my_mu, my_lambda, my_n, my_nsquare);
     gcry_mpi_sub (session->r[i], session->r[i], my_offset);
     gcry_mpi_sub (session->r[i], session->r[i], my_offset);
@@ -2044,20 +2073,27 @@ handle_service_request_multipart (void *cls,
     }
     session->transferred += contained_elements;
 
-    if (session->transferred == used_elements) {
+    if (session->transferred == used_elements)
+    {
       // single part finished
       session->state = SERVICE_REQUEST_RECEIVED;
-      if (session->response) {
-        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s and a matching element set, processing.\n"), GNUNET_h2s (&session->key));
+      if (session->response)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                    _ ("Got session with key %s and a matching element set, processing.\n"),
+                    GNUNET_h2s (&session->key));
         if (GNUNET_OK != compute_service_response (session, session->response)) {
           //something went wrong, remove it again...
           goto except;
         }
       }
       else
-        GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                    _("Got session with key %s without a matching element set, queueing.\n"),
+                    GNUNET_h2s (&session->key));
     }
-    else {
+    else
+    {
       // multipart message
     }
   }
@@ -2131,9 +2167,12 @@ handle_service_request (void *cls,
           +mask_length + pk_length + contained_elements * PAILLIER_ELEMENT_LENGTH;
 
   //sanity check: is the message as long as the message_count fields suggests?
-  if ((ntohs (msg->header.size) != msg_length) || (element_count < used_elements) || (used_elements < contained_elements)
-      || (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0)))
-      ) {
+  if ( (ntohs (msg->header.size) != msg_length) ||
+       (element_count < used_elements) ||
+       (used_elements < contained_elements) ||
+       (0 == used_elements) ||
+       (mask_length != (element_count / 8 + ((element_count % 8) ? 1 : 0))) )
+  {
     GNUNET_free (session);
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
@@ -2142,8 +2181,11 @@ handle_service_request (void *cls,
                              &msg->key,
                              element_count,
                              NULL,
-                             NULL)) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"), (const char *) &(msg->key));
+                             NULL))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _ ("Got message with duplicate session key (`%s'), ignoring service request.\n"),
+                (const char *) &(msg->key));
     GNUNET_free (session);
     return GNUNET_SYSERR;
   }