aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-01-02 23:55:18 +0100
committerChristian Grothoff <christian@grothoff.org>2022-01-02 23:55:18 +0100
commitf30d3de6fb8bbd01072df00815b5f18a1808312e (patch)
tree1932f5174084f5df09cf1c95f599d15266022736 /src/util
parent7b6ad5eedc4444ed8739932a388189bfd4e35e44 (diff)
downloadgnunet-f30d3de6fb8bbd01072df00815b5f18a1808312e.tar.gz
gnunet-f30d3de6fb8bbd01072df00815b5f18a1808312e.zip
-DHT: clean up peer selection logic
Diffstat (limited to 'src/util')
-rw-r--r--src/util/crypto_hash.c54
-rw-r--r--src/util/test_crypto_hash.c70
2 files changed, 10 insertions, 114 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index b51ecd242..dcd46e5f9 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -146,41 +146,6 @@ GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
146} 146}
147 147
148 148
149uint64_t
150GNUNET_CRYPTO_hash_bucket_distance (const struct GNUNET_HashCode *xor,
151 unsigned int bucket)
152{
153 const uint64_t *u = (const uint64_t *) xor;
154 unsigned int idx;
155 unsigned int bits;
156 uint64_t rval;
157
158 if (bucket == 8 * sizeof(*xor))
159 return 0;
160 bucket++;
161 idx = bucket / 64;
162 bits = bucket % 64;
163 if (idx >= sizeof (*xor) / sizeof (*u))
164 return 0;
165 if (0 == bits)
166 {
167 /* keeps no bits */
168 rval = 0;
169 }
170 else
171 {
172 /* keeps lowest (64-bits) bits */
173 rval = GNUNET_ntohll (u[idx]) << bits;
174 }
175 if (idx + 1 < sizeof (*xor) / sizeof (*u))
176 {
177 /* discards lowest (bits) bits */
178 rval |= GNUNET_ntohll (u[idx + 1]) >> (64 - bits);
179 }
180 return rval;
181}
182
183
184void 149void
185GNUNET_CRYPTO_hash_to_aes_key ( 150GNUNET_CRYPTO_hash_to_aes_key (
186 const struct GNUNET_HashCode *hc, 151 const struct GNUNET_HashCode *hc,
@@ -277,18 +242,19 @@ GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
277 const struct GNUNET_HashCode *h2, 242 const struct GNUNET_HashCode *h2,
278 const struct GNUNET_HashCode *target) 243 const struct GNUNET_HashCode *target)
279{ 244{
280 unsigned int d1; 245 const unsigned long long *l1 = (const unsigned long long *) h1;
281 unsigned int d2; 246 const unsigned long long *l2 = (const unsigned long long *) h2;
247 const unsigned long long *t = (const unsigned long long *) target;
282 248
283 for (ssize_t i = sizeof(struct GNUNET_HashCode) / sizeof(unsigned int) - 1; 249 GNUNET_static_assert (0 == sizeof (*h1) % sizeof (*l1));
284 i >= 0; 250 for (size_t i = 0; i < sizeof(*h1) / sizeof(*l1); i++)
285 i--)
286 { 251 {
287 d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i]; 252 unsigned long long x1 = l1[i] ^ t[i];
288 d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i]; 253 unsigned long long x2 = l2[i] ^ t[i];
289 if (d1 > d2) 254
255 if (x1 > x2)
290 return 1; 256 return 1;
291 else if (d1 < d2) 257 if (x1 < x2)
292 return -1; 258 return -1;
293 } 259 }
294 return 0; 260 return 0;
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c
index 1fddcfba8..8241676da 100644
--- a/src/util/test_crypto_hash.c
+++ b/src/util/test_crypto_hash.c
@@ -134,76 +134,6 @@ test_arithmetic (void)
134 GNUNET_CRYPTO_hash_count_leading_zeros (&h1)); 134 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
135 GNUNET_assert (512 - 42 - 1 == 135 GNUNET_assert (512 - 42 - 1 ==
136 GNUNET_CRYPTO_hash_count_tailing_zeros (&h1)); 136 GNUNET_CRYPTO_hash_count_tailing_zeros (&h1));
137 memset (&h1,
138 0,
139 sizeof (h1));
140 memset (&h2,
141 0,
142 sizeof (h2));
143 h1.bits[3] = htonl (0x00800011); /* 3*32 + 8 Bits identical */
144 h2.bits[3] = htonl (0x00000101); /* residual delta: 0x000220.. (+1 bit)*/
145 /* Note: XOR: 0x00800110 */
146 h1.bits[4] = htonl (0x14144141);
147 h2.bits[4] = htonl (0x28288282); /* residual delta: 0x3C3CC3.. */
148 /* Note: XOR: 0x3C3CC3C3 */
149 /* Note: XOR<<1: 0x78798786 */
150 GNUNET_assert (104 ==
151 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
152 GNUNET_CRYPTO_hash_xor (&h1,
153 &h2,
154 &s);
155 GNUNET_assert (104 ==
156 GNUNET_CRYPTO_hash_count_leading_zeros (&s));
157 GNUNET_assert (0x0002207879878600LLU ==
158 GNUNET_CRYPTO_hash_bucket_distance (&s,
159 104));
160
161 memset (&h1,
162 0,
163 sizeof (h1));
164 memset (&h2,
165 0,
166 sizeof (h2));
167 h1.bits[4] = htonl (0x00000001); /* 5*32 - 1 Bits identical */
168 h2.bits[4] = htonl (0x00000000);
169 /* Note: XOR: 0x00000001 */
170 h1.bits[5] = htonl (0x14144141);
171 h2.bits[5] = htonl (0x28288282);
172 /* Note: XOR: 0x3C3CC3C3 */
173 GNUNET_assert (159 ==
174 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
175 GNUNET_CRYPTO_hash_xor (&h1,
176 &h2,
177 &s);
178 GNUNET_assert (159 ==
179 GNUNET_CRYPTO_hash_count_leading_zeros (&s));
180 GNUNET_assert (0x3C3CC3C300000000 ==
181 GNUNET_CRYPTO_hash_bucket_distance (&s,
182 159));
183
184 memset (&h1,
185 0,
186 sizeof (h1));
187 memset (&h2,
188 0,
189 sizeof (h2));
190 h1.bits[14] = htonl (0x00000001); /* 15*32 - 1 Bits identical */
191 h2.bits[14] = htonl (0x00000000);
192 /* Note: XOR: 0x00000001 */
193 h1.bits[15] = htonl (0x14144141);
194 h2.bits[15] = htonl (0x28288282);
195 /* Note: XOR: 0x3C3CC3C3 */
196 GNUNET_assert (479 ==
197 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
198 GNUNET_CRYPTO_hash_xor (&h1,
199 &h2,
200 &s);
201 GNUNET_assert (479 ==
202 GNUNET_CRYPTO_hash_count_leading_zeros (&s));
203 GNUNET_assert (0x3C3CC3C300000000 ==
204 GNUNET_CRYPTO_hash_bucket_distance (&s,
205 479));
206
207 return 0; 137 return 0;
208} 138}
209 139