aboutsummaryrefslogtreecommitdiff
path: root/src/scalarproduct
diff options
context:
space:
mode:
authorChristian Fuchs <christian.fuchs@cfuchs.net>2013-10-07 17:36:30 +0000
committerChristian Fuchs <christian.fuchs@cfuchs.net>2013-10-07 17:36:30 +0000
commit2866a3a85ea457f29f4746ccae57af369746c5fa (patch)
treecd109ea8647a8de8506dd7991b6beb638aa0d8b3 /src/scalarproduct
parente85c90bbd1f2ac05da8d77ecc850d8657b6eeda7 (diff)
downloadgnunet-2866a3a85ea457f29f4746ccae57af369746c5fa.tar.gz
gnunet-2866a3a85ea457f29f4746ccae57af369746c5fa.zip
more work towards multipart sending, this time the service response.
added two stubs for the multipart chunk receiving message handlers
Diffstat (limited to 'src/scalarproduct')
-rw-r--r--src/scalarproduct/gnunet-service-scalarproduct.c197
1 files changed, 160 insertions, 37 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct.c b/src/scalarproduct/gnunet-service-scalarproduct.c
index 91f4d45c6..bdec7ebd8 100644
--- a/src/scalarproduct/gnunet-service-scalarproduct.c
+++ b/src/scalarproduct/gnunet-service-scalarproduct.c
@@ -168,6 +168,11 @@ struct ServiceSession
168 * Bob's permutation q of R 168 * Bob's permutation q of R
169 */ 169 */
170 gcry_mpi_t * r_prime; 170 gcry_mpi_t * r_prime;
171
172 /**
173 * Bobs matching response session from the client
174 */
175 struct ServiceSession * response;
171 176
172 /** 177 /**
173 * The computed scalar 178 * The computed scalar
@@ -816,6 +821,89 @@ prepare_client_end_notification (void * cls,
816 821
817} 822}
818 823
824static void
825prepare_service_response_multipart (void *cls,
826 const struct GNUNET_SCHEDULER_TaskContext *tc)
827{
828 struct ServiceSession * session = cls;
829 unsigned char * current;
830 unsigned char * element_exported;
831 struct GNUNET_SCALARPRODUCT_multipart_message * msg;
832 unsigned int i;
833 uint32_t msg_length;
834 uint32_t todo_count;
835 size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that
836
837 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_multipart_message);
838 todo_count = session->used_element_count - session->transferred_element_count;
839
840 if (todo_count > MULTIPART_ELEMENT_CAPACITY/2)
841 // send the currently possible maximum chunk, we always transfer both permutations
842 todo_count = MULTIPART_ELEMENT_CAPACITY/2;
843
844 msg_length += todo_count * PAILLIER_ELEMENT_LENGTH * 2;
845 msg = GNUNET_malloc (msg_length);
846 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB_MULTIPART);
847 msg->header.size = htons (msg_length);
848 msg->multipart_element_count = htonl (todo_count);
849
850 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
851 current = (unsigned char *) &msg[1];
852 // convert k[][]
853 for (i = session->transferred_element_count; i < session->transferred_element_count + todo_count; i++)
854 {
855 //k[i][p]
856 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
857 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
858 element_exported, PAILLIER_ELEMENT_LENGTH,
859 &element_length,
860 session->r[i]));
861 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
862 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
863 current += PAILLIER_ELEMENT_LENGTH;
864 //k[i][q]
865 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
866 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
867 element_exported, PAILLIER_ELEMENT_LENGTH,
868 &element_length,
869 session->r_prime[i]));
870 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
871 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
872 current += PAILLIER_ELEMENT_LENGTH;
873 }
874 GNUNET_free (element_exported);
875 for (i = session->transferred_element_count; i < session->transferred_element_count; i++)
876 {
877 gcry_mpi_release (session->r_prime[i]);
878 gcry_mpi_release (session->r[i]);
879 }
880 session->transferred_element_count+=todo_count;
881 session->msg = (struct GNUNET_MessageHeader *) msg;
882 session->service_transmit_handle =
883 GNUNET_MESH_notify_transmit_ready (session->tunnel,
884 GNUNET_YES,
885 GNUNET_TIME_UNIT_FOREVER_REL,
886 msg_length,
887 &do_send_message,
888 session);
889 //disconnect our client
890 if (NULL == session->service_transmit_handle)
891 {
892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
893 session->state = FINALIZED;
894
895 session->response->client_notification_task =
896 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
897 session->response);
898 return;
899 }
900 if (session->transferred_element_count != session->used_element_count)
901 // multipart
902 session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
903 else
904 //singlepart
905 session->state = FINALIZED;
906}
819 907
820/** 908/**
821 * Bob executes: 909 * Bob executes:
@@ -826,21 +914,16 @@ prepare_client_end_notification (void * cls,
826 * S: $S := E_A(sum (r_i + b_i)^2)$ 914 * S: $S := E_A(sum (r_i + b_i)^2)$
827 * S': $S' := E_A(sum r_i^2)$ 915 * S': $S' := E_A(sum r_i^2)$
828 * 916 *
829 * @param r (1)[]: $E_A(a_{pi(i)}) times E_A(- r_{pi(i)} - b_{pi(i)}) &= E_A(a_{pi(i)} - r_{pi(i)} - b_{pi(i)})$
830 * @param r_prime (2)[]: $E_A(a_{pi'(i)}) times E_A(- r_{pi'(i)}) &= E_A(a_{pi'(i)} - r_{pi'(i)})$
831 * @param s S: $S := E_A(sum (r_i + b_i)^2)$ 917 * @param s S: $S := E_A(sum (r_i + b_i)^2)$
832 * @param s_prime S': $S' := E_A(sum r_i^2)$ 918 * @param s_prime S': $S' := E_A(sum r_i^2)$
833 * @param request the associated requesting session with alice 919 * @param session the associated requesting session with alice
834 * @param response the associated responder session with bob's client 920 * @return GNUNET_NO if we could not send our message
835 * @return GNUNET_SYSERR if the function was called with NULL parameters or if there was an error
836 * GNUNET_NO if we could not send our message
837 * GNUNET_OK if the operation succeeded 921 * GNUNET_OK if the operation succeeded
838 */ 922 */
839static int 923static int
840prepare_service_response (gcry_mpi_t s, 924prepare_service_response (gcry_mpi_t s,
841 gcry_mpi_t s_prime, 925 gcry_mpi_t s_prime,
842 struct ServiceSession * request, 926 struct ServiceSession * session)
843 struct ServiceSession * response)
844{ 927{
845 struct GNUNET_SCALARPRODUCT_service_response * msg; 928 struct GNUNET_SCALARPRODUCT_service_response * msg;
846 uint32_t msg_length = 0; 929 uint32_t msg_length = 0;
@@ -852,22 +935,22 @@ prepare_service_response (gcry_mpi_t s,
852 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_response) 935 msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
853 + 2 * PAILLIER_ELEMENT_LENGTH; // s, stick 936 + 2 * PAILLIER_ELEMENT_LENGTH; // s, stick
854 937
855 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + 2 * request->used_element_count * PAILLIER_ELEMENT_LENGTH){ //kp, kq 938 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > msg_length + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH){ //kp, kq
856 msg_length += + 2 * request->used_element_count * PAILLIER_ELEMENT_LENGTH; 939 msg_length += + 2 * session->used_element_count * PAILLIER_ELEMENT_LENGTH;
857 request->transferred_element_count = request->used_element_count; 940 session->transferred_element_count = session->used_element_count;
858 } 941 }
859 else { 942 else {
860 request->transferred_element_count = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - msg_length) / (PAILLIER_ELEMENT_LENGTH * 2); 943 session->transferred_element_count = (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - msg_length) / (PAILLIER_ELEMENT_LENGTH * 2);
861 } 944 }
862 945
863 msg = GNUNET_malloc (msg_length); 946 msg = GNUNET_malloc (msg_length);
864 947
865 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE); 948 msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE);
866 msg->header.size = htons (msg_length); 949 msg->header.size = htons (msg_length);
867 msg->total_element_count = htonl (request->element_count); 950 msg->total_element_count = htonl (session->element_count);
868 msg->contained_element_count = htonl (request->used_element_count); 951 msg->contained_element_count = htonl (session->used_element_count);
869 msg->contained_element_count = htonl (request->transferred_element_count); 952 msg->contained_element_count = htonl (session->transferred_element_count);
870 memcpy (&msg->key, &request->key, sizeof (struct GNUNET_HashCode)); 953 memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
871 current = (unsigned char *) &msg[1]; 954 current = (unsigned char *) &msg[1];
872 955
873 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH); 956 element_exported = GNUNET_malloc (PAILLIER_ELEMENT_LENGTH);
@@ -895,17 +978,14 @@ prepare_service_response (gcry_mpi_t s,
895 current += PAILLIER_ELEMENT_LENGTH; 978 current += PAILLIER_ELEMENT_LENGTH;
896 979
897 // convert k[][] 980 // convert k[][]
898 for (i = 0; i < request->used_element_count; i++) 981 for (i = 0; i < session->transferred_element_count; i++)
899 { 982 {
900 if (request->transferred_element_count <= i)
901 break; //reached end of this message, can't include more
902
903 //k[i][p] 983 //k[i][p]
904 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH); 984 memset (element_exported, 0, PAILLIER_ELEMENT_LENGTH);
905 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, 985 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
906 element_exported, PAILLIER_ELEMENT_LENGTH, 986 element_exported, PAILLIER_ELEMENT_LENGTH,
907 &element_length, 987 &element_length,
908 request->r[i])); 988 session->r[i]));
909 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH); 989 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
910 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH); 990 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
911 current += PAILLIER_ELEMENT_LENGTH; 991 current += PAILLIER_ELEMENT_LENGTH;
@@ -914,46 +994,46 @@ prepare_service_response (gcry_mpi_t s,
914 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, 994 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG,
915 element_exported, PAILLIER_ELEMENT_LENGTH, 995 element_exported, PAILLIER_ELEMENT_LENGTH,
916 &element_length, 996 &element_length,
917 request->r_prime[i])); 997 session->r_prime[i]));
918 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH); 998 adjust (element_exported, element_length, PAILLIER_ELEMENT_LENGTH);
919 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH); 999 memcpy (current, element_exported, PAILLIER_ELEMENT_LENGTH);
920 current += PAILLIER_ELEMENT_LENGTH; 1000 current += PAILLIER_ELEMENT_LENGTH;
921 } 1001 }
922 1002
923 GNUNET_free (element_exported); 1003 GNUNET_free (element_exported);
924 for (i = 0; i < request->transferred_element_count; i++) 1004 for (i = 0; i < session->transferred_element_count; i++)
925 { 1005 {
926 gcry_mpi_release (request->r_prime[i]); 1006 gcry_mpi_release (session->r_prime[i]);
927 gcry_mpi_release (request->r[i]); 1007 gcry_mpi_release (session->r[i]);
928 } 1008 }
929 gcry_mpi_release (s); 1009 gcry_mpi_release (s);
930 gcry_mpi_release (s_prime); 1010 gcry_mpi_release (s_prime);
931 1011
932 request->msg = (struct GNUNET_MessageHeader *) msg; 1012 session->msg = (struct GNUNET_MessageHeader *) msg;
933 request->service_transmit_handle = 1013 session->service_transmit_handle =
934 GNUNET_MESH_notify_transmit_ready (request->tunnel, 1014 GNUNET_MESH_notify_transmit_ready (session->tunnel,
935 GNUNET_YES, 1015 GNUNET_YES,
936 GNUNET_TIME_UNIT_FOREVER_REL, 1016 GNUNET_TIME_UNIT_FOREVER_REL,
937 msg_length, 1017 msg_length,
938 &do_send_message, 1018 &do_send_message,
939 request); 1019 session);
940 //disconnect our client 1020 //disconnect our client
941 if (NULL == request->service_transmit_handle) 1021 if (NULL == session->service_transmit_handle)
942 { 1022 {
943 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n")); 1023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
944 request->state = FINALIZED; 1024 session->state = FINALIZED;
945 1025
946 response->client_notification_task = 1026 session->response->client_notification_task =
947 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification, 1027 GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
948 response); 1028 session->response);
949 return GNUNET_NO; 1029 return GNUNET_NO;
950 } 1030 }
951 if (request->transferred_element_count != request->used_element_count) 1031 if (session->transferred_element_count != session->used_element_count)
952 // multipart 1032 // multipart
953 request->state = WAITING_FOR_MULTIPART_TRANSMISSION; 1033 session->state = WAITING_FOR_MULTIPART_TRANSMISSION;
954 else 1034 else
955 //singlepart 1035 //singlepart
956 request->state = FINALIZED; 1036 session->state = FINALIZED;
957 1037
958 return GNUNET_OK; 1038 return GNUNET_OK;
959} 1039}
@@ -1124,6 +1204,7 @@ compute_service_response (struct ServiceSession * request,
1124 1204
1125 request->r = r; 1205 request->r = r;
1126 request->r_prime = r_prime; 1206 request->r_prime = r_prime;
1207 request->response = response;
1127 1208
1128 // Calculate S' = E(SUM( r_i^2 )) 1209 // Calculate S' = E(SUM( r_i^2 ))
1129 s_prime = compute_square_sum (rand, count); 1210 s_prime = compute_square_sum (rand, count);
@@ -1146,7 +1227,7 @@ compute_service_response (struct ServiceSession * request,
1146 gcry_mpi_release (rand[i]); 1227 gcry_mpi_release (rand[i]);
1147 1228
1148 // copy the r[], r_prime[], S and Stick into a new message, prepare_service_response frees these 1229 // copy the r[], r_prime[], S and Stick into a new message, prepare_service_response frees these
1149 if (GNUNET_YES != prepare_service_response (s, s_prime, request, response)) 1230 if (GNUNET_YES != prepare_service_response (s, s_prime, request))
1150 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Failed to communicate with `%s', scalar product calculation aborted.\n"), 1231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Failed to communicate with `%s', scalar product calculation aborted.\n"),
1151 GNUNET_i2s (&request->peer)); 1232 GNUNET_i2s (&request->peer));
1152 else 1233 else
@@ -1266,6 +1347,7 @@ prepare_service_request_multipart (void *cls,
1266 //final part 1347 //final part
1267 session->state = WAITING_FOR_SERVICE_RESPONSE; 1348 session->state = WAITING_FOR_SERVICE_RESPONSE;
1268} 1349}
1350
1269/** 1351/**
1270 * Executed by Alice, fills in a service-request message and sends it to the given peer 1352 * Executed by Alice, fills in a service-request message and sends it to the given peer
1271 * 1353 *
@@ -1875,6 +1957,26 @@ prepare_client_response (void *cls,
1875 GNUNET_h2s (&session->key)); 1957 GNUNET_h2s (&session->key));
1876} 1958}
1877 1959
1960/**
1961 * Handle a multipart-chunk of a request from another service to calculate a scalarproduct with us.
1962 *
1963 * @param cls closure (set from #GNUNET_MESH_connect)
1964 * @param tunnel connection to the other end
1965 * @param tunnel_ctx place to store local state associated with the tunnel
1966 * @param sender who sent the message
1967 * @param message the actual message
1968 * @param atsi performance data for the connection
1969 * @return #GNUNET_OK to keep the connection open,
1970 * #GNUNET_SYSERR to close it (signal serious error)
1971 */
1972static int
1973handle_service_request_multipart (void *cls,
1974 struct GNUNET_MESH_Tunnel * tunnel,
1975 void **tunnel_ctx,
1976 const struct GNUNET_MessageHeader * message)
1977{
1978 return GNUNET_SYSERR;
1979}
1878 1980
1879/** 1981/**
1880 * Handle a request from another service to calculate a scalarproduct with us. 1982 * Handle a request from another service to calculate a scalarproduct with us.
@@ -2061,6 +2163,27 @@ except:
2061 2163
2062 2164
2063/** 2165/**
2166 * Handle a multipart chunk of a response we got from another service we wanted to calculate a scalarproduct with.
2167 *
2168 * @param cls closure (set from #GNUNET_MESH_connect)
2169 * @param tunnel connection to the other end
2170 * @param tunnel_ctx place to store local state associated with the tunnel
2171 * @param sender who sent the message
2172 * @param message the actual message
2173 * @param atsi performance data for the connection
2174 * @return #GNUNET_OK to keep the connection open,
2175 * #GNUNET_SYSERR to close it (signal serious error)
2176 */
2177static int
2178handle_service_response_multipart (void *cls,
2179 struct GNUNET_MESH_Tunnel * tunnel,
2180 void **tunnel_ctx,
2181 const struct GNUNET_MessageHeader * message)
2182{
2183 return GNUNET_SYSERR;
2184}
2185
2186/**
2064 * Handle a response we got from another service we wanted to calculate a scalarproduct with. 2187 * Handle a response we got from another service we wanted to calculate a scalarproduct with.
2065 * 2188 *
2066 * @param cls closure (set from #GNUNET_MESH_connect) 2189 * @param cls closure (set from #GNUNET_MESH_connect)