]> git.ozlabs.org Git - ccan/commitdiff
shachain: remove unnecessary shachain_index_t
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 15 Aug 2017 04:24:07 +0000 (13:54 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 15 Aug 2017 04:24:07 +0000 (13:54 +0930)
You can use SHACHAIN_BITS to contrain the size.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/crypto/shachain/shachain.c
ccan/crypto/shachain/shachain.h
ccan/crypto/shachain/test/run-8bit.c
ccan/crypto/shachain/test/run-can_derive.c

index d4508a05a0f7addf2e33442ff1f13d3564bab9e7..9cb54a37bdd9da6c18b1567e400ca132010af9a2 100644 (file)
@@ -10,7 +10,7 @@ static void change_bit(unsigned char *arr, size_t index)
        arr[index / CHAR_BIT] ^= (1 << (index % CHAR_BIT));
 }
 
        arr[index / CHAR_BIT] ^= (1 << (index % CHAR_BIT));
 }
 
-static unsigned int count_trailing_zeroes(shachain_index_t index)
+static unsigned int count_trailing_zeroes(uint64_t index)
 {
 #if HAVE_BUILTIN_CTZLL
        return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS;
 {
 #if HAVE_BUILTIN_CTZLL
        return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS;
@@ -25,24 +25,24 @@ static unsigned int count_trailing_zeroes(shachain_index_t index)
 #endif
 }
 
 #endif
 }
 
-static bool can_derive(shachain_index_t from, shachain_index_t to)
+static bool can_derive(uint64_t from, uint64_t to)
 {
 {
-       shachain_index_t mask;
+       uint64_t mask;
 
        /* Corner case: can always derive from seed. */
        if (from == 0)
                return true;
 
        /* Leading bits must be the same */
 
        /* Corner case: can always derive from seed. */
        if (from == 0)
                return true;
 
        /* Leading bits must be the same */
-       mask = ~((1ULL << count_trailing_zeroes(from))-1);
+       mask = ~(((uint64_t)1 << count_trailing_zeroes(from))-1);
        return ((from ^ to) & mask) == 0;
 }
 
        return ((from ^ to) & mask) == 0;
 }
 
-static void derive(shachain_index_t from, shachain_index_t to,
+static void derive(uint64_t from, uint64_t to,
                   const struct sha256 *from_hash,
                   struct sha256 *hash)
 {
                   const struct sha256 *from_hash,
                   struct sha256 *hash)
 {
-       shachain_index_t branches;
+       uint64_t branches;
        int i;
 
        assert(can_derive(from, to));
        int i;
 
        assert(can_derive(from, to));
@@ -60,13 +60,13 @@ static void derive(shachain_index_t from, shachain_index_t to,
        }
 }
 
        }
 }
 
-void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
+void shachain_from_seed(const struct sha256 *seed, uint64_t index,
                        struct sha256 *hash)
 {
        derive(0, index, seed, hash);
 }
 
                        struct sha256 *hash)
 {
        derive(0, index, seed, hash);
 }
 
-shachain_index_t shachain_next_index(const struct shachain *chain)
+uint64_t shachain_next_index(const struct shachain *chain)
 {
        return chain->min_index - 1;
 }
 {
        return chain->min_index - 1;
 }
@@ -75,11 +75,11 @@ void shachain_init(struct shachain *chain)
 {
        chain->num_valid = 0;
        /* This is 0 in the case where SHACHAIN_BITS is 64. */
 {
        chain->num_valid = 0;
        /* This is 0 in the case where SHACHAIN_BITS is 64. */
-       chain->min_index = (shachain_index_t)((UINT64_MAX >> (64 - SHACHAIN_BITS)) + 1);
+       chain->min_index = (UINT64_MAX >> (64 - SHACHAIN_BITS)) + 1;
 }
 
 bool shachain_add_hash(struct shachain *chain,
 }
 
 bool shachain_add_hash(struct shachain *chain,
-                      shachain_index_t index, const struct sha256 *hash)
+                      uint64_t index, const struct sha256 *hash)
 {
        unsigned int i, pos;
 
 {
        unsigned int i, pos;
 
@@ -108,7 +108,7 @@ bool shachain_add_hash(struct shachain *chain,
 }
 
 bool shachain_get_hash(const struct shachain *chain,
 }
 
 bool shachain_get_hash(const struct shachain *chain,
-                      shachain_index_t index, struct sha256 *hash)
+                      uint64_t index, struct sha256 *hash)
 {
        unsigned int i;
 
 {
        unsigned int i;
 
index 8eabbab457a72b49c2c47736ce6e7d19f0b8fe82..d95b97363f7d8e0bb243b6ebba9f35c8db3b5364 100644 (file)
@@ -6,13 +6,8 @@
 #include <stdbool.h>
 #include <stdint.h>
 
 #include <stdbool.h>
 #include <stdint.h>
 
-/* Useful for testing. */
-#ifndef shachain_index_t
-#define shachain_index_t uint64_t
-#endif
-
 #ifndef SHACHAIN_BITS
 #ifndef SHACHAIN_BITS
-#define SHACHAIN_BITS (sizeof(shachain_index_t) * 8)
+#define SHACHAIN_BITS (sizeof(uint64_t) * 8)
 #endif
 
 /**
 #endif
 
 /**
@@ -42,7 +37,7 @@
  *     shachain_from_seed(&seed, index--, hash);
  * }
  */
  *     shachain_from_seed(&seed, index--, hash);
  * }
  */
-void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
+void shachain_from_seed(const struct sha256 *seed, uint64_t index,
                        struct sha256 *hash);
 
 /**
                        struct sha256 *hash);
 
 /**
@@ -55,10 +50,10 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
  * added.
  */
 struct shachain {
  * added.
  */
 struct shachain {
-       shachain_index_t min_index;
+       uint64_t min_index;
        unsigned int num_valid;
        struct {
        unsigned int num_valid;
        struct {
-               shachain_index_t index;
+               uint64_t index;
                struct sha256 hash;
        } known[SHACHAIN_BITS + 1];
 };
                struct sha256 hash;
        } known[SHACHAIN_BITS + 1];
 };
@@ -79,7 +74,7 @@ void shachain_init(struct shachain *chain);
  * initialized chain), or one less than the previously successfully
  * added value.
  */
  * initialized chain), or one less than the previously successfully
  * added value.
  */
-shachain_index_t shachain_next_index(const struct shachain *chain);
+uint64_t shachain_next_index(const struct shachain *chain);
 
 /**
  * shachain_add_hash - record the hash for the next index.
 
 /**
  * shachain_add_hash - record the hash for the next index.
@@ -106,7 +101,7 @@ shachain_index_t shachain_next_index(const struct shachain *chain);
  * }
  */
 bool shachain_add_hash(struct shachain *chain,
  * }
  */
 bool shachain_add_hash(struct shachain *chain,
-                      shachain_index_t index, const struct sha256 *hash);
+                      uint64_t index, const struct sha256 *hash);
 
 /**
  * shachain_get_hash - get the hash for a given index.
 
 /**
  * shachain_get_hash - get the hash for a given index.
@@ -137,5 +132,5 @@ bool shachain_add_hash(struct shachain *chain,
  * }
  */
 bool shachain_get_hash(const struct shachain *chain,
  * }
  */
 bool shachain_get_hash(const struct shachain *chain,
-                      shachain_index_t index, struct sha256 *hash);
+                      uint64_t index, struct sha256 *hash);
 #endif /* CCAN_CRYPTO_SHACHAIN_H */
 #endif /* CCAN_CRYPTO_SHACHAIN_H */
index 90b7ef6ce41ced59c9ccbe6229a63da5ef9035e0..6fe874bed433c9d975b05c7c569d4c590bb4df89 100644 (file)
@@ -1,4 +1,4 @@
-#define shachain_index_t uint8_t
+#define SHACHAIN_BITS 8
 
 #include <ccan/crypto/shachain/shachain.h>
 /* Include the C files directly. */
 
 #include <ccan/crypto/shachain/shachain.h>
 /* Include the C files directly. */
index 5a1bf477930b583b2cd7745e3c65f8cc0f91b587..693a91a3cd30491cf7f405dfa98985e4b047a021 100644 (file)
@@ -7,13 +7,13 @@
 
 #include <stdio.h>
 
 
 #include <stdio.h>
 
-static bool bit_set(shachain_index_t index, int bit)
+static bool bit_set(uint64_t index, int bit)
 {
        return index & (1ULL << bit);
 }
 
 /* As per design.txt */
 {
        return index & (1ULL << bit);
 }
 
 /* As per design.txt */
-static bool naive_can_derive(shachain_index_t from, shachain_index_t to)
+static bool naive_can_derive(uint64_t from, shachain_index_t to)
 {
        int i;
 
 {
        int i;