You can use SHACHAIN_BITS to contrain the size.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
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;
-static bool can_derive(shachain_index_t from, shachain_index_t to)
+static bool can_derive(uint64_t from, uint64_t to)
/* 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;
int i;
assert(can_derive(from, to));
int i;
assert(can_derive(from, 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;
}
{
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)
}
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)
#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
-
-#define SHACHAIN_BITS (sizeof(shachain_index_t) * 8)
+#define SHACHAIN_BITS (sizeof(uint64_t) * 8)
* 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);
/**
* added.
*/
struct shachain {
* added.
*/
struct shachain {
- shachain_index_t min_index;
unsigned int num_valid;
struct {
unsigned int num_valid;
struct {
- shachain_index_t index;
struct sha256 hash;
} known[SHACHAIN_BITS + 1];
};
struct sha256 hash;
} known[SHACHAIN_BITS + 1];
};
* 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.
* }
*/
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.
* }
*/
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 */
-#define shachain_index_t uint8_t
#include <ccan/crypto/shachain/shachain.h>
/* Include the C files directly. */
#include <ccan/crypto/shachain/shachain.h>
/* Include the C files directly. */
-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)