]> git.ozlabs.org Git - ccan/commitdiff
shachain: add shachain_next_index()
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 15 Aug 2017 04:18:19 +0000 (13:48 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 15 Aug 2017 04:18:19 +0000 (13:48 +0930)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/crypto/shachain/shachain.c
ccan/crypto/shachain/shachain.h
ccan/crypto/shachain/test/run.c

index 2c9cb3d5464c42855d7e9d3dc0c18455ed6907ea..d4508a05a0f7addf2e33442ff1f13d3564bab9e7 100644 (file)
@@ -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);
 
index 90f2380ed39b89d8277db3542e669fcc22f7176e..8eabbab457a72b49c2c47736ce6e7d19f0b8fe82 100644 (file)
@@ -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:
index 36dfaaeb7adaebb0ed03e438b2a652b126282488..c7b4035a21ffb3f6d31289834538242eaec7a7de 100644 (file)
@@ -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));