#include <stdbool.h>
#include <stdint.h>
-/* Useful for testing. */
-#ifndef shachain_index_t
-#define shachain_index_t uint64_t
+#ifndef SHACHAIN_BITS
+#define SHACHAIN_BITS (sizeof(uint64_t) * 8)
#endif
/**
* 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);
/**
* added.
*/
struct shachain {
- shachain_index_t min_index;
+ uint64_t min_index;
unsigned int num_valid;
struct {
- shachain_index_t index;
+ uint64_t index;
struct sha256 hash;
- } known[sizeof(shachain_index_t) * 8 + 1];
+ } known[SHACHAIN_BITS + 1];
};
/**
*/
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.
+ */
+uint64_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:
* }
*/
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.
*
* Example:
* #include <ccan/structeq/structeq.h>
+ * // Defines sha256_eq
+ * STRUCTEQ_DEF(sha256, 0, u);
*
* static void next_hash(const struct sha256 *hash)
* {
* else {
* struct sha256 check;
* assert(shachain_get_hash(&chain, index+1, &check));
- * assert(structeq(&check, hash));
+ * assert(sha256_eq(&check, hash));
* }
* }
*/
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 */