shachain: clarify design in terms of binary tree, reverse indexes.
[ccan] / ccan / crypto / shachain / test / run-badhash.c
index bc9785120b3cacc794b3ae0445e16de1c919b866..742a36b8363df394439d38a6f177b9de893745cd 100644 (file)
@@ -9,26 +9,27 @@ int main(void)
 {
        struct sha256 seed;
        struct shachain chain;
 {
        struct sha256 seed;
        struct shachain chain;
-       size_t i;
+       uint64_t i;
 
        plan_tests(NUM_TESTS);
 
        memset(&seed, 0xFF, sizeof(seed));
        shachain_init(&chain);
 
 
        plan_tests(NUM_TESTS);
 
        memset(&seed, 0xFF, sizeof(seed));
        shachain_init(&chain);
 
-       for (i = 0; i < NUM_TESTS; i++) {
+       for (i = 0xFFFFFFFFFFFFFFFFULL;
+            i > 0xFFFFFFFFFFFFFFFFULL - NUM_TESTS;
+            i--) {
                struct sha256 expect;
                struct sha256 expect;
-               unsigned int num_known = chain.num_valid;
 
                shachain_from_seed(&seed, i, &expect);
                /* Screw it up. */
                expect.u.u8[0]++;
 
 
                shachain_from_seed(&seed, i, &expect);
                /* Screw it up. */
                expect.u.u8[0]++;
 
-               /* Either it should fail, or it couldn't derive any others. */
+               /* Either it should fail, or it couldn't derive any others (ie. pos 0). */
                if (shachain_add_hash(&chain, i, &expect)) {
                if (shachain_add_hash(&chain, i, &expect)) {
-                       ok1(chain.num_valid == num_known + 1);
+                       ok1(chain.known[0].index == i);
                        /* Fix it up in-place */
                        /* Fix it up in-place */
-                       chain.known[num_known].hash.u.u8[0]--;
+                       chain.known[0].hash.u.u8[0]--;
                } else {
                        expect.u.u8[0]--;
                        ok1(shachain_add_hash(&chain, i, &expect));
                } else {
                        expect.u.u8[0]--;
                        ok1(shachain_add_hash(&chain, i, &expect));