crypto/shachain: enforce that indexes have to be added incrementally.
[ccan] / ccan / crypto / shachain / shachain.h
1 /* MIT (BSD) license - see LICENSE file for details */
2 #ifndef CCAN_CRYPTO_SHACHAIN_H
3 #define CCAN_CRYPTO_SHACHAIN_H
4 #include "config.h"
5 #include <ccan/crypto/sha256/sha256.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8
9 /* Useful for testing. */
10 #ifndef shachain_index_t
11 #define shachain_index_t uint64_t
12 #endif
13
14 void shachain_from_seed(const struct sha256 *seed, shachain_index_t index,
15                         struct sha256 *hash);
16
17 struct shachain {
18         shachain_index_t max_index;
19         unsigned int num_valid;
20         struct {
21                 shachain_index_t index;
22                 struct sha256 hash;
23         } known[sizeof(shachain_index_t) * 8];
24 };
25
26 void shachain_init(struct shachain *shachain);
27
28 bool shachain_add_hash(struct shachain *shachain,
29                        shachain_index_t index, const struct sha256 *hash);
30
31 bool shachain_get_hash(const struct shachain *shachain,
32                        shachain_index_t index, struct sha256 *hash);
33 #endif /* CCAN_CRYPTO_SHACHAIN_H */