commit ba430ff1fbce68a8940e448f0e0fe7266fa638b5
parent d76a8b89e6ff8ab39f65e75ab4ff0c6f21a63a5f
Author: Supriti Singh <supritisingh08@gmail.com>
Date: Fri, 7 Feb 2014 00:14:43 +0000
Computing finger identity using libgcrypt functions.
Diffstat:
3 files changed, 102 insertions(+), 44 deletions(-)
diff --git a/src/dht/gnunet-service-xdht_neighbours.c b/src/dht/gnunet-service-xdht_neighbours.c
@@ -485,7 +485,7 @@ static struct GNUNET_CORE_Handle *core_api;
/**
* The current finger index that we have found trail to.
*/
-static unsigned int current_finger_id;
+static unsigned int current_finger_index;
/**
@@ -777,56 +777,29 @@ get_random_friend()
/**
- * TODO: Check the logic of using current_finger_id again.
- * This code is not correct. I need to check the pointers and
- * correct use of memcpy and all the data type.
- * Use Chord formula finger[i]=(n+2^(i-1))mod m,
- * where i = current finger map index - max. 256 bits
- * n = own peer identity - 256 bits
- * m = number of bits in peer id - 256 bits
- * @return finger_peer_id for which we have to find the trail through network.
+ * Find finger id to which we want to setup the trail
+ * @return finger id
*/
-static
+static
struct GNUNET_PeerIdentity *
-finger_id_to_search()
+compute_finger_identity()
{
- struct GNUNET_PeerIdentity *finger_peer_id;
- uint32_t peer_id;
- uint32_t finger_id;
+ struct GNUNET_PeerIdentity *finger_identity;
- finger_peer_id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
-
- /* Copy unsigned char array into peer_id. */
- if (0 == memcpy(&peer_id,&my_identity.public_key.q_y,sizeof(uint32_t)))
- return NULL;
-
- /* We do all the arithmetic operation on peer_id to get finger_id*/
- finger_id = (uint32_t)(peer_id + pow(2,current_finger_id)) % MAX_FINGERS;
-
- /* Copy the finger_id to finger_peer_id. */
- if (0 == memcpy(&finger_peer_id->public_key.q_y,&finger_id,sizeof(uint32_t)))
- return NULL;
+ finger_identity = GNUNET_CRYPTO_compute_finger(&my_identity,current_finger_index);
-
- /* FIXME: Here I increment the index so that next time when we enter this
- function, then we begin the search from current index. Is it possible
- to set this value when we add the finger id to our finger table. No, because
- even there is a call going on to find the finger, we can start another call
- to search another peer. */
- current_finger_id = (current_finger_id+1) % MAX_FINGERS;
+ current_finger_index = (current_finger_index+1) % MAX_FINGERS;
/* Check if you already have an entry in finger_peers for this finger_id.
- If yes then again look for a new finger_id. */
- /*if(NULL != GNUNET_CONTAINER_multipeermap_get(finger_peers,finger_peer_id))
+ If yes then again look for a new finger_id.
+ FIXME: Should we return NULL here?
+ if(NULL != GNUNET_CONTAINER_multipeermap_get(finger_peers,finger_peer_id))
{
-
- finger_peer_id = finger_id_to_search();
- }
- */
- return finger_peer_id;
+ finger_peer_id = compute_finger_identity();
+ }*/
+ return finger_identity;
}
-
/**
* TODO: Implement after testing friend/finger map.
* TODO: Handle the case when we already have a trail to our predecessor in
@@ -838,7 +811,7 @@ finger_id_to_search()
* @return peer identity of immediate predecessor.
*/
static
-struct GNUNET_PeerIdentity*
+struct GNUNET_PeerIdentity *
find_immediate_predecessor()
{
/* Using your own peer identity, calculate your predecessor
@@ -880,7 +853,7 @@ send_find_finger_trail_message (void *cls,
else
{
/* Find the finger_peer_id for which we want to setup the trail */
- finger_peer_id = finger_id_to_search();
+ finger_peer_id = compute_finger_identity();
}
/* Choose a friend randomly from your friend_peers map. */
@@ -897,7 +870,7 @@ send_find_finger_trail_message (void *cls,
DHT_MINIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us +
GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
DHT_MAXIMUM_FIND_FINGER_TRAIL_INTERVAL.rel_value_us /
- (current_finger_id + 1));
+ (current_finger_index + 1));
find_finger_trail_task =
GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_finger_trail_message,
@@ -1059,6 +1032,11 @@ handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer,
* FIXME:1. Check if current_destination field is set correctly.
* 2. Is it correct to use GNUNET_CMP_PEER_IDENTITY to find out the successor
* of a finger.
+ * 3. We should check the interval of the keys for which a peer is responsible
+ * when we are looking to find the correct peer to store a key. But
+ * --> How do we maintain this interval?
+ * --> Should we check this interval when we are looking for trail to a finger
+ * as in this function?
* The code flow seems to be very large. Could do better.
* @param destination peer id's predecessor we are looking for.
* @return
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
@@ -1268,6 +1268,19 @@ GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateK
/**
+ * Computes a new PeerIdentity using the Chord formula.
+ * new_peer_identity = ((my_identity + pow(2,i)) mod (pow(2,m)
+ * where m, size of struct GNUNET_PeerIdentity in bits.
+ * i, 0 <= i <= m
+ * @param my_identity original PeerIdentity
+ * @param value of i.
+ * @return finger_identity
+ */
+struct GNUNET_PeerIdentity *
+GNUNET_CRYPTO_compute_finger(struct GNUNET_PeerIdentity *my_identity,unsigned int index);
+
+
+/**
* @ingroup crypto
* Derive a public key from a given public key and a label.
* Essentially calculates a public key 'V = H(l,P) * P'.
diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c
@@ -1448,6 +1448,73 @@ GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateK
/**
+ * Computes a new PeerIdentity using the Chord formula.
+ * new_peer_identity = ((my_identity + pow(2,i)) mod (pow(2,m)
+ * where m, size of struct GNUNET_PeerIdentity in bits.
+ * i, 0 <= i <= m
+ * @param my_identity original PeerIdentity
+ * @param value of i.
+ * @return finger_identity
+ */
+struct GNUNET_PeerIdentity *
+GNUNET_CRYPTO_compute_finger(struct GNUNET_PeerIdentity *my_identity, unsigned int index)
+{
+ gcry_mpi_t my_identity_mpi;
+ gcry_mpi_t finger_identity_mpi;
+ gcry_mpi_t add;
+ gcry_mpi_t mod;
+ gcry_error_t rc;
+ struct GNUNET_PeerIdentity *finger_identity;
+ size_t read = 0;
+ size_t write = 0;
+
+ finger_identity = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
+
+ /* Initialize my_identity_mpi. */
+ my_identity_mpi = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity));
+
+ /* Copy my_identity into my_id */
+ if(0 != (rc = gcry_mpi_scan(&my_identity_mpi, GCRYMPI_FMT_USG, my_identity->public_key.q_y,
+ sizeof(struct GNUNET_PeerIdentity), &read)))
+ {
+ LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
+ GNUNET_free(finger_identity);
+ return NULL;
+ }
+
+ /* Initialize finger_identity_mpi */
+ finger_identity_mpi = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity));
+
+ /* Initialize add */
+ add = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity));
+
+ /* Set the index bit in add.*/
+ gcry_mpi_set_bit(add,index);
+
+ /* Initialize mod */
+ mod = gcry_mpi_new(8*sizeof(struct GNUNET_PeerIdentity) + 1);
+ gcry_mpi_set_bit(mod,257);
+ gcry_mpi_sub_ui(mod,mod,(unsigned long)1);
+
+
+ /* finger_identity_mpi = (my_identity_mpi + add) % mod */
+ gcry_mpi_addm(finger_identity_mpi,my_identity_mpi,add,mod);
+
+
+ /* Copy finger_identity_mpi to finger_identity */
+ if(0 != (rc = gcry_mpi_print(GCRYMPI_FMT_USG,finger_identity->public_key.q_y,
+ 32,&write,finger_identity_mpi)))
+ {
+ LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_print", rc);
+ GNUNET_free(finger_identity);
+ return NULL;
+ }
+
+ return finger_identity;
+}
+
+
+/**
* Derive a public key from a given public key and a label.
* Essentially calculates a public key 'V = H(l,P) * P'.
*