]> git.ozlabs.org Git - ccan/blobdiff - ccan/crypto/shachain/shachain.h
shachain: add shachain_next_index()
[ccan] / ccan / crypto / shachain / shachain.h
index ba4ba11cd64c02d1d66a362d35bac98e39a2f0bc..8eabbab457a72b49c2c47736ce6e7d19f0b8fe82 100644 (file)
 #define shachain_index_t uint64_t
 #endif
 
+#ifndef SHACHAIN_BITS
+#define SHACHAIN_BITS (sizeof(shachain_index_t) * 8)
+#endif
+
 /**
  * shachain_from_seed - Generate an unpredictable SHA from a seed value.
  * @seed: (secret) seed value to use
@@ -56,7 +60,7 @@ struct shachain {
        struct {
                shachain_index_t index;
                struct sha256 hash;
-       } known[sizeof(shachain_index_t) * 8 + 1];
+       } known[SHACHAIN_BITS + 1];
 };
 
 /**
@@ -67,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: