- assert(index == chain->max_index + 1 ||
- (index == 0 && chain->num_valid == 0));
-
- 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)) {
- 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)) != 0)
- return false;
- break;
- }
+ 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;