Commit e6abb93d authored by Rusty Russell's avatar Rusty Russell

shachain: remove unnecessary shachain_index_t

You can use SHACHAIN_BITS to contrain the size.
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 8ad6ab9c
...@@ -10,7 +10,7 @@ static void change_bit(unsigned char *arr, size_t index) ...@@ -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 #if HAVE_BUILTIN_CTZLL
return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS; return index ? (unsigned int)__builtin_ctzll(index) : SHACHAIN_BITS;
...@@ -25,24 +25,24 @@ static unsigned int count_trailing_zeroes(shachain_index_t index) ...@@ -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. */ /* Corner case: can always derive from seed. */
if (from == 0) if (from == 0)
return true; return true;
/* Leading bits must be the same */ /* 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, const struct sha256 *from_hash,
struct sha256 *hash) struct sha256 *hash)
{ {
shachain_index_t branches; uint64_t branches;
int i; int i;
assert(can_derive(from, to)); assert(can_derive(from, to));
...@@ -60,13 +60,13 @@ static void derive(shachain_index_t from, shachain_index_t 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) struct sha256 *hash)
{ {
derive(0, index, seed, 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) ...@@ -75,11 +75,11 @@ void shachain_init(struct shachain *chain)
{ {
chain->num_valid = 0; chain->num_valid = 0;
/* This is 0 in the case where SHACHAIN_BITS is 64. */ /* 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, ...@@ -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;
......
...@@ -6,13 +6,8 @@ ...@@ -6,13 +6,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.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 @@ ...@@ -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, ...@@ -55,10 +50,10 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
* added. * added.
*/ */
struct shachain { struct shachain {
shachain_index_t min_index; uint64_t min_index;
unsigned int num_valid; unsigned int num_valid;
struct { struct {
shachain_index_t index; uint64_t index;
struct sha256 hash; struct sha256 hash;
} known[SHACHAIN_BITS + 1]; } known[SHACHAIN_BITS + 1];
}; };
...@@ -79,7 +74,7 @@ void shachain_init(struct shachain *chain); ...@@ -79,7 +74,7 @@ void shachain_init(struct shachain *chain);
* initialized chain), or one less than the previously successfully * initialized chain), or one less than the previously successfully
* added value. * 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); ...@@ -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, ...@@ -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 */
#define shachain_index_t uint8_t #define SHACHAIN_BITS 8
#include <ccan/crypto/shachain/shachain.h> #include <ccan/crypto/shachain/shachain.h>
/* Include the C files directly. */ /* Include the C files directly. */
......
...@@ -7,13 +7,13 @@ ...@@ -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); return index & (1ULL << bit);
} }
/* As per design.txt */ /* 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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment