aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r--src/util/crypto_hash.c54
1 files changed, 10 insertions, 44 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;