]> git.ozlabs.org Git - ccan/blobdiff - ccan/crypto/shachain/test/run-can_derive.c
shachain: clarify design in terms of binary tree, reverse indexes.
[ccan] / ccan / crypto / shachain / test / run-can_derive.c
diff --git a/ccan/crypto/shachain/test/run-can_derive.c b/ccan/crypto/shachain/test/run-can_derive.c
new file mode 100644 (file)
index 0000000..5a1bf47
--- /dev/null
@@ -0,0 +1,41 @@
+#define shachain_index_t uint8_t
+
+#include <ccan/crypto/shachain/shachain.h>
+/* Include the C files directly. */
+#include <ccan/crypto/shachain/shachain.c>
+#include <ccan/tap/tap.h>
+
+#include <stdio.h>
+
+static bool bit_set(shachain_index_t index, int bit)
+{
+       return index & (1ULL << bit);
+}
+
+/* As per design.txt */
+static bool naive_can_derive(shachain_index_t from, shachain_index_t to)
+{
+       int i;
+
+       for (i = count_trailing_zeroes(from); i < 8; i++) {
+               if (bit_set(from, i) != bit_set(to, i))
+                       return false;
+       }
+       return true;
+}
+
+int main(void)
+{
+       int i, j;
+
+       /* This is how many tests you plan to run */
+       plan_tests(65536);
+
+       for (i = 0; i < 256; i++) {
+               for (j = 0; j < 256; j++) {
+                       ok1(can_derive(i, j) == naive_can_derive(i, j));
+               }
+       }
+
+       return exit_status();
+}