From: Rusty Russell Date: Tue, 15 Aug 2017 04:18:19 +0000 (+0930) Subject: shachain: add shachain_next_index() X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=8ad6ab9ce9b78c6b28b727b11d571d11856479c6;hp=2a03b7b304b185680ff0255731c671b71a7fb1b2 shachain: add shachain_next_index() Signed-off-by: Rusty Russell --- diff --git a/ccan/crypto/shachain/shachain.c b/ccan/crypto/shachain/shachain.c index 2c9cb3d5..d4508a05 100644 --- a/ccan/crypto/shachain/shachain.c +++ b/ccan/crypto/shachain/shachain.c @@ -66,10 +66,16 @@ void shachain_from_seed(const struct sha256 *seed, shachain_index_t index, derive(0, index, seed, hash); } +shachain_index_t shachain_next_index(const struct shachain *chain) +{ + return chain->min_index - 1; +} + void shachain_init(struct shachain *chain) { chain->num_valid = 0; - chain->min_index = 0; + /* This is 0 in the case where SHACHAIN_BITS is 64. */ + chain->min_index = (shachain_index_t)((UINT64_MAX >> (64 - SHACHAIN_BITS)) + 1); } bool shachain_add_hash(struct shachain *chain, @@ -78,9 +84,7 @@ bool shachain_add_hash(struct shachain *chain, unsigned int i, pos; /* You have to insert them in order! */ - assert(index == chain->min_index - 1 || - (index == (shachain_index_t)(UINT64_MAX >> (64 - SHACHAIN_BITS)) - && chain->num_valid == 0)); + assert(index == shachain_next_index(chain)); pos = count_trailing_zeroes(index); diff --git a/ccan/crypto/shachain/shachain.h b/ccan/crypto/shachain/shachain.h index 90f2380e..8eabbab4 100644 --- a/ccan/crypto/shachain/shachain.h +++ b/ccan/crypto/shachain/shachain.h @@ -71,20 +71,28 @@ struct shachain { */ void shachain_init(struct shachain *chain); +/** + * shachain_next_index - what's the next index I can add to the shachain? + * @chain: the chain + * + * This returns 0xFFFFFFFFFFFFFFFF (for a freshly + * initialized chain), or one less than the previously successfully + * added value. + */ +shachain_index_t shachain_next_index(const struct shachain *chain); + /** * shachain_add_hash - record the hash for the next index. * @chain: the chain to add to * @index: the index of the hash * @hash: the hash value. * - * You can only add index 0xFFFFFFFFFFFFFFFF (for a freshly - * initialized chain), or one less than the previously successfully - * added value. + * You can only add shachain_next_index(@chain). * * This can fail (return false without altering @chain) if the hash * for this index isn't consistent with previous hashes (ie. wasn't * generated from the same seed), though it can't always detect that. - * If the hash is inconsistent yet undetected, the next addition will + * If the hash is inconsistent yet undetected, a future addition will * fail. * * Example: diff --git a/ccan/crypto/shachain/test/run.c b/ccan/crypto/shachain/test/run.c index 36dfaaeb..c7b4035a 100644 --- a/ccan/crypto/shachain/test/run.c +++ b/ccan/crypto/shachain/test/run.c @@ -13,7 +13,7 @@ int main(void) uint64_t i, j; /* This is how many tests you plan to run */ - plan_tests(NUM_TESTS * 3 + NUM_TESTS * (NUM_TESTS + 1) - 1); + plan_tests(NUM_TESTS * 4 + NUM_TESTS * (NUM_TESTS + 1) - 1); memset(&seed, 0, sizeof(seed)); /* Generate a whole heap. */ @@ -34,6 +34,7 @@ int main(void) i--) { struct sha256 hash; int expidx = 0xFFFFFFFFFFFFFFFFULL - i; + ok1(shachain_next_index(&chain) == i); ok1(shachain_add_hash(&chain, i, &expect[expidx])); for (j = i; j != 0; j++) { ok1(shachain_get_hash(&chain, j, &hash));