sfinx

sphincs+ inspired signing utility
git clone git://git.finwo.net/app/sfinx
Log | Files | Refs | README | LICENSE

04-subtree.c (3975B)


      1 #include <string.h>
      2 
      3 #include "_tap.c"
      4 #include "src/sfinx.c"
      5 
      6 int main(void) {
      7   static const uint8_t seed[]     = "sfinx subtree seed";
      8   uint8_t              prefix[3]  = {0x11, 0x22, 0x33};
      9   uint8_t              prefix2[3] = {0x11, 0x22, 0x34};
     10   uint8_t              root[SFINX_HASH_LEN_MAX];
     11   uint8_t              root2[SFINX_HASH_LEN_MAX];
     12   uint8_t              auth[8 * SFINX_HASH_LEN_MAX];
     13   uint8_t              leaf[SFINX_HASH_LEN_MAX];
     14   uint8_t              leaf2[SFINX_HASH_LEN_MAX];
     15   uint8_t              built[SFINX_HASH_LEN_MAX];
     16   uint8_t              key[32];
     17   uint8_t              nodes[256 * SFINX_HASH_LEN_MAX];
     18   uint8_t              idx      = 0x5a;
     19   size_t               len      = sfinx_hash_len(SFINX_HASH_256);
     20   size_t               seed_len = strlen((const char *)seed);
     21   size_t               count;
     22 
     23   tap_begin("sfinx subtree");
     24 
     25   tap(_sfinx_subtree_build(NULL, SFINX_HASH_256, seed, seed_len, prefix, 3, idx, root, auth), "subtree builds",
     26       "build failed");
     27 
     28   memcpy(key, seed, seed_len);
     29   memcpy(key + seed_len, prefix, 3);
     30   key[seed_len + 3] = idx;
     31   _sfinx_wots_pubkey(SFINX_HASH_256, key, seed_len + 4, seed_len, leaf);
     32   _sfinx_subtree_verify(SFINX_HASH_256, prefix, 3, leaf, idx, auth, built);
     33   tap(memcmp(built, root, len) == 0, "auth path rebuilds the subtree root", "root differs");
     34 
     35   auth[5] ^= 0x01;
     36   _sfinx_subtree_verify(SFINX_HASH_256, prefix, 3, leaf, idx, auth, built);
     37   tap(memcmp(built, root, len) != 0, "tampered auth fails", "root matched");
     38   auth[5] ^= 0x01;
     39 
     40   memcpy(leaf2, leaf, len);
     41   leaf2[0] ^= 0x01;
     42   _sfinx_subtree_verify(SFINX_HASH_256, prefix, 3, leaf2, idx, auth, built);
     43   tap(memcmp(built, root, len) != 0, "tampered leaf fails", "root matched");
     44 
     45   tap(_sfinx_subtree_build(NULL, SFINX_HASH_256, seed, seed_len, prefix2, 3, idx, root2, NULL), "second subtree builds",
     46       "build failed");
     47   tap(memcmp(root, root2, len) != 0, "prefix changes the subtree root", "roots match");
     48 
     49   for (size_t i = 0; i < 256; i++) {
     50     memcpy(key, seed, seed_len);
     51     memcpy(key + seed_len, prefix, 3);
     52     key[seed_len + 3] = (uint8_t)i;
     53     _sfinx_wots_pubkey(SFINX_HASH_256, key, seed_len + 4, seed_len, nodes + i * len);
     54   }
     55   count = 256;
     56   for (size_t bitlen = 8; bitlen-- > 0;) {
     57     count /= 2;
     58     for (size_t j = 0; j < count; j++) {
     59       _sfinx_merkle_parent(SFINX_HASH_256, (uint8_t)bitlen,
     60                            _sfinx_node_mask((uint8_t)bitlen, (uint8_t)(j << (8 - bitlen))), prefix, 3,
     61                            nodes + (2 * j) * len, nodes + (2 * j + 1) * len, nodes + j * len);
     62     }
     63   }
     64   tap(memcmp(nodes, root, len) == 0, "manual fold matches the builder", "roots differ");
     65 
     66   // The batched builder must match a per-key fold at every hash size
     67   static const sfinx_hash sizes[] = {SFINX_HASH_224, SFINX_HASH_384, SFINX_HASH_512};
     68   for (size_t s = 0; s < sizeof(sizes) / sizeof(sizes[0]); s++) {
     69     sfinx_hash h   = sizes[s];
     70     size_t     l   = sfinx_hash_len(h);
     71     size_t     cnt = 256;
     72     uint8_t    broot[SFINX_HASH_LEN_MAX];
     73     uint8_t    mkey[32];
     74     uint8_t    mnodes[256 * SFINX_HASH_LEN_MAX];
     75 
     76     tap(_sfinx_subtree_build(NULL, h, seed, seed_len, prefix, 3, idx, broot, NULL) != 0, "subtree builds at every size",
     77         "build failed");
     78     for (size_t i = 0; i < 256; i++) {
     79       memcpy(mkey, seed, seed_len);
     80       memcpy(mkey + seed_len, prefix, 3);
     81       mkey[seed_len + 3] = (uint8_t)i;
     82       _sfinx_wots_pubkey(h, mkey, seed_len + 4, seed_len, mnodes + i * l);
     83     }
     84     for (size_t bitlen = 8; bitlen-- > 0;) {
     85       cnt /= 2;
     86       for (size_t j = 0; j < cnt; j++) {
     87         _sfinx_merkle_parent(h, (uint8_t)bitlen, _sfinx_node_mask((uint8_t)bitlen, (uint8_t)(j << (8 - bitlen))),
     88                              prefix, 3, mnodes + (2 * j) * l, mnodes + (2 * j + 1) * l, mnodes + j * l);
     89       }
     90     }
     91     tap(memcmp(broot, mnodes, l) == 0, "batched builder matches the per-key fold at every size", "roots differ");
     92   }
     93 
     94   return tap_plan();
     95 }