fix bit counting mess
authorMartin Schanzenbach <mschanzenbach@posteo.de>
Tue, 26 May 2020 06:52:28 +0000 (08:52 +0200)
committerMartin Schanzenbach <mschanzenbach@posteo.de>
Tue, 26 May 2020 06:52:28 +0000 (08:52 +0200)
src/dht/gnunet-service-dht_neighbours.c
src/gnsrecord/gnunet-gnsrecord-tvg.c
src/include/gnunet_crypto_lib.h
src/nse/gnunet-service-nse.c
src/revocation/revocation_api.c
src/util/crypto_hash.c
src/util/gnunet-scrypt.c
src/util/test_crypto_hash.c

index c251dfa12418a746a24ce233abe9343853591a82..fce69d3f60d0556069087c3d6ff2b2cbd29e13ac 100644 (file)
@@ -927,8 +927,8 @@ get_distance (const struct GNUNET_HashCode *target,
        (i < sizeof(struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9);
        i++)
   {
-    if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
-        GNUNET_CRYPTO_hash_get_bit (have, i))
+    if (GNUNET_CRYPTO_hash_get_bit_rtl (target, i) !=
+        GNUNET_CRYPTO_hash_get_bit_rtl (have, i))
       lsb |= (1 << (bucket + 32 - 9 - i));      /* first bit set will be 10,
                                                  * last bit set will be 31 -- if
                                                  * i does not reach 512 first... */
index cf815d62937e1fef0b335b9a489eb09833acb0fe..862bd6f860902cf0c0cd2b6775d8eb152c6d84ac 100644 (file)
@@ -47,7 +47,7 @@ print_record(const struct GNUNET_GNSRECORD_Data *rd)
     fprintf (stdout,
            "EXPIRATION: %"PRIu64"\n", rd->expiration_time);
   fprintf (stdout,
-           "DATA_SIZE: %"PRIu64"\n", rd->data_size);
+           "DATA_SIZE: %zu\n", rd->data_size);
   fprintf (stdout,
            "TYPE: %d\n", rd->record_type);
   fprintf (stdout,
index a5a50e749dd00c3069f8e9fd27981b90b646c7dd..e880bd8870f297a1086ce6e1a78b52a5dadc8278 100644 (file)
@@ -874,12 +874,25 @@ GNUNET_CRYPTO_hash_to_aes_key (
  * Obtain a bit from a hashcode.
  *
  * @param code the `struct GNUNET_HashCode` to index bit-wise
- * @param bit index into the hashcode, [0...159]
+ * @param bit index into the hashcode, [0...159] where 0 is the leftmost bit
+ *        (bytes in code interpreted big endian)
  * @return Bit \a bit from hashcode \a code, -1 for invalid index
  */
 int
-GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code,
-                            unsigned int bit);
+GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code,
+                                unsigned int bit);
+
+
+/**
+ * Obtain a bit from a hashcode.
+ * @param code the GNUNET_CRYPTO_hash to index bit-wise
+ * @param bit index into the hashcode, [0...511] where 0 is the rightmost bit
+ *        (bytes in code interpreted little endian)
+ * @return Bit \a bit from hashcode \a code, -1 for invalid index
+ */
+int
+GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code,
+                                unsigned int bit);
 
 
 /**
index 411f533a505d32b8d311339277a88ba3c40e1b69..461d55a7f2dd1757e8b302346e6c54c5e16304e9 100644 (file)
@@ -780,7 +780,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash)
   unsigned int hash_count;
 
   hash_count = 0;
-  while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
+  while (0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count))
     hash_count++;
   return hash_count;
 }
index 2ae8e2df941381aa08c2cde8c467f9db30e50552..33c67d005554c0fd6d1df70503fe8c7ebf0ce0e2 100644 (file)
@@ -395,7 +395,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash)
 {
   unsigned int hash_count;
   hash_count = 0;
-  while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)))
+  while ((0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count)))
     hash_count++;
   return hash_count;
 }
index 4982ba404d596b992aef9233e300238c9dd0b885..62295347688cffec91218e869a627ea93f7113bd 100644 (file)
@@ -244,17 +244,34 @@ GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc,
 /**
  * Obtain a bit from a hashcode.
  * @param code the GNUNET_CRYPTO_hash to index bit-wise
- * @param bit index into the hashcode, [0...511]
+ * @param bit index into the hashcode, [0...511] where 0 is the leftmost bit
+ *        (bytes in code interpreted big endian)
  * @return Bit \a bit from hashcode \a code, -1 for invalid index
  */
 int
-GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code, unsigned int
-                            bit)
+GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code,
+                            unsigned int bit)
 {
   GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
   return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0;
 }
 
+/**
+ * Obtain a bit from a hashcode.
+ * @param code the GNUNET_CRYPTO_hash to index bit-wise
+ * @param bit index into the hashcode, [0...511] where 0 is the rightmost bit
+ *        (bytes in code interpreted little endian)
+ * @return Bit \a bit from hashcode \a code, -1 for invalid index
+ */
+int
+GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code,
+                                unsigned int bit)
+{
+  GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
+  return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
+}
+
+
 
 /**
  * Determine how many low order bits match in two
@@ -275,8 +292,8 @@ GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
   unsigned int i;
 
   for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++)
-    if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
-        GNUNET_CRYPTO_hash_get_bit (second, i))
+    if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) !=
+        GNUNET_CRYPTO_hash_get_bit_rtl (second, i))
       return i;
   return sizeof(struct GNUNET_HashCode) * 8;
 }
index 70ba48d82d7a6ecebf8ffe34d44f5455a054b08d..9bb766595fa936bf194547a55e86417c7fd21e4d 100644 (file)
@@ -79,7 +79,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash)
   unsigned int hash_count;
 
   hash_count = 0;
-  while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
+  while (0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count))
     hash_count++;
   return hash_count;
 }
index 12e1324dd283c9c28c2ca5c844838de07eb046d9..d22e1f5d32726ef915a920f7c0498a1eafb3c837 100644 (file)
@@ -91,10 +91,15 @@ testArithmetic ()
     return 1;
   if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
     return 1;
-  memset (&d, 0xF0, sizeof(d));
-  if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
+  memset (&d, 0x40, sizeof(d));
+  if (0 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 3))
     return 1;
-  if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
+  if (1 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 6))
+    return 1;
+  memset (&d, 0x02, sizeof(d));
+  if (0 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 3))
+    return 1;
+  if (1 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 6))
     return 1;
   memset (&d, 0, sizeof(d));
   GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);