- for (i = 0; i < chain->num_valid; i++) {
- /* If we could derive this value, we don't need it,
- * not any others (since they're in order). */
- if (can_derive(index, chain->known[i].index))
- break;
+bool shachain_add_hash(struct shachain *chain,
+ uint64_t index, const struct sha256 *hash)
+{
+ unsigned int i, pos;
+
+ /* You have to insert them in order! */
+ assert(index == shachain_next_index(chain));
+
+ pos = count_trailing_zeroes(index);
+
+ /* All derivable answers must be valid. */
+ /* FIXME: Is it sufficient to check just the next answer? */
+ for (i = 0; i < pos; i++) {
+ struct sha256 expect;
+
+ /* Make sure the others derive as expected! */
+ derive(index, chain->known[i].index, hash, &expect);
+ if (memcmp(&expect, &chain->known[i].hash, sizeof(expect)))
+ return false;