gestumblinde

Gestumblinde - reference implementation of SLH-DSA
git clone git://www.tkruger.se/gestumblinde.git
Log | Files | Refs | README

make_test_vectors.py (7621B)


      1 """
      2 Program for making test vectors, and dumping them
      3 in JSON.
      4 """
      5 
      6 from slh_dsa import SLHDSA
      7 from address import Address, AddressType
      8 from wotsp import wotsp_pkgen, wotsp_sign, wotsp_pk_from_sig
      9 from xmss import xmss_node, xmss_sign, xmss_pk_from_sig
     10 from fors import fors_skgen, fors_node, fors_sign, fors_pk_from_sig
     11 from slh import slh_keygen, slh_sign, slh_verify
     12 from ht import ht_sign, ht_verify
     13 from utils import toByte, cdiv
     14 import json
     15 from secrets import token_bytes
     16 
     17 def create_test_vectors(t):
     18     """Create a dict of test vectors for parameter choice t"""
     19     print(f"[+] Creating test vectors for parameter choice \"{t}\"...")
     20     tv = dict()
     21     ctx = SLHDSA(t)
     22 
     23     # WOTS+
     24     pfx = t+" WOTS+ "
     25     tv[pfx+"SK_SEED"] = b"gestumblindegaat" + token_bytes(ctx.n-16)
     26     tv[pfx+"PK_SEED"] = b"densomfraagarfar" + token_bytes(ctx.n-16)
     27     tv[pfx+"ADDRESS"] = [
     28       0x72, 0x67, 0x69, 0x53, 0x69, 0x6d, 0x61, 0x6c,
     29       0x74, 0x65, 0x68, 0x20, 0x6e, 0x6f, 0x6b, 0x20,
     30       0x00, 0x00, 0x00, 0x00, 0x72, 0x65, 0x00, 0x0c,
     31       0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x20]
     32     adrs = Address()
     33     adrs.data = bytearray(tv[pfx+"ADDRESS"])
     34     tv[pfx+"PUBLIC_KEY"] = wotsp_pkgen(tv[pfx+"SK_SEED"],
     35                                        tv[pfx+"PK_SEED"],
     36                                        adrs, ctx)
     37     tv[pfx+"MSG"]     = b"meddelandetaerde" + token_bytes(ctx.n-16)
     38     tv[pfx+"SIGNATURE"] = wotsp_sign(tv[pfx+"MSG"],
     39                                      tv[pfx+"SK_SEED"],
     40                                      tv[pfx+"PK_SEED"],
     41                                      adrs, ctx)
     42     # XMSS
     43     pfx = t+" XMSS "
     44     tv[pfx+"SK_SEED"] = b"gestumblindegaat" + token_bytes(ctx.n-16)
     45     tv[pfx+"PK_SEED"] = b"densomfraagarfar" + token_bytes(ctx.n-16)
     46     tv[pfx+"ADDRESS"] = [
     47       0x72, 0x67, 0x69, 0x53, 0x69, 0x6d, 0x61, 0x6c,
     48       0x74, 0x65, 0x68, 0x20, 0x6e, 0x6f, 0x6b, 0x20,
     49       0x00, 0x00, 0x00, 0x00, 0x72, 0x65, 0x00, 0x0c,
     50       0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x20]
     51     adrs = Address()
     52     adrs.data = bytearray(tv[pfx+"ADDRESS"])
     53     tv[pfx+"NODEI"]  = 0
     54     tv[pfx+"NODEZ"]  = 0
     55     tv[pfx+"NODE"] = xmss_node(tv[pfx+"SK_SEED"],
     56                                tv[pfx+"NODEI"],
     57                                tv[pfx+"NODEZ"],
     58                                tv[pfx+"PK_SEED"],
     59                                adrs, ctx)
     60     tv[pfx+"MSG"]       = b"meddelandetaerde" + token_bytes(ctx.n-16)
     61     tv[pfx+"SIGNIDX"]   = 0
     62     tv[pfx+"SIGNATURE"] = xmss_sign(tv[pfx+"MSG"],
     63                                     tv[pfx+"SK_SEED"],
     64                                     tv[pfx+"SIGNIDX"],
     65                                     tv[pfx+"PK_SEED"],
     66                                     adrs, ctx)
     67 
     68     # HT
     69     pfx = t+" HT "
     70     tv[pfx+"SK_SEED"]   = b"gestumblindegaat" + token_bytes(ctx.n-16)
     71     tv[pfx+"PK_SEED"]   = b"densomfraagarfar" + token_bytes(ctx.n-16)
     72     tv[pfx+"MSG"]       = b"meddelandetaerde" + token_bytes(ctx.n-16)
     73     tv[pfx+"IDX_TREE"]  = 2
     74     tv[pfx+"IDX_LEAF"]  = 6
     75     tv[pfx+"SIGNATURE"] = ht_sign(tv[pfx+"MSG"],
     76                                   tv[pfx+"SK_SEED"],
     77                                   tv[pfx+"PK_SEED"],
     78                                   tv[pfx+"IDX_TREE"],
     79                                   tv[pfx+"IDX_LEAF"],
     80                                   ctx)
     81     # FORS
     82     pfx = t+" FORS "
     83     tv[pfx+"SK_SEED"]   = b"gestumblindegaat" + token_bytes(ctx.n-16)
     84     tv[pfx+"PK_SEED"]   = b"densomfraagarfar" + token_bytes(ctx.n-16)
     85     tv[pfx+"ADDRESS"]   = [
     86       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     87       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     88       0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03,
     89       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
     90     tv[pfx+"IDX"] = 1
     91     adrs = Address()
     92     adrs.data = bytearray(tv[pfx+"ADDRESS"])
     93     tv[pfx+"SK"] = fors_skgen(tv[pfx+"SK_SEED"],
     94                               tv[pfx+"PK_SEED"],
     95                               adrs,
     96                               tv[pfx+"IDX"],
     97                               ctx)
     98     tv[pfx+"NODEI"] = 1
     99     tv[pfx+"NODEZ"] = 5
    100     tv[pfx+"NODE"] = fors_node(tv[pfx+"SK_SEED"],
    101                                tv[pfx+"NODEI"],
    102                                tv[pfx+"NODEZ"],
    103                                tv[pfx+"PK_SEED"],
    104                                adrs, ctx)
    105     tv[pfx+"MD"] = bytes(list(range(cdiv(ctx.k * ctx.a, 8))))
    106     tv[pfx+"SIGNATURE"] = fors_sign(tv[pfx+"MD"],
    107                                     tv[pfx+"SK_SEED"],
    108                                     tv[pfx+"PK_SEED"],
    109                                     adrs, ctx)
    110     tv[pfx+"PK_FROM_SIG"] = fors_pk_from_sig(tv[pfx+"SIGNATURE"],
    111                                              tv[pfx+"MD"],
    112                                              tv[pfx+"PK_SEED"],
    113                                              adrs, ctx)
    114 
    115     # SLH
    116     pfx = t+" SLH "
    117     sk_seed = b"gestumblindegaat" + token_bytes(ctx.n-16)
    118     sk_prf  = b"aanaerinteloestf" + token_bytes(ctx.n-16)
    119     pk_seed = b"oerennagonkommer" + token_bytes(ctx.n-16)
    120 
    121     # Generate pk_root from sk_seed, pk_seed
    122     adrs = Address(toByte(0, 32)) # adrs <- toByte(0,32)
    123     adrs.set_layer_address(ctx.d - 1)
    124     pk_root = xmss_node(sk_seed, 0, ctx.hp, pk_seed, adrs, ctx)
    125 
    126     tv[pfx+"SK"]   = (sk_seed, sk_prf, pk_seed, pk_root)
    127     tv[pfx+"PK"]   = (pk_seed, pk_root)
    128     tv[pfx+"MSG"]  =  b"helloworldorsomesuchmeaninglesst"
    129     tv[pfx+"SIGNATURE"] = slh_sign(tv[pfx+"MSG"],
    130                                    tv[pfx+"SK"],
    131                                    ctx)
    132 
    133     return tv
    134 
    135 def write_tv_to_file(filename, tv):
    136     """Write the test vectors in dict tv to JSON file"""
    137     print(f"[+] Writing test vector to file {filename}...")
    138     # convert bytes to lists of ints
    139     converted = dict()
    140     for k in tv.keys():
    141         if type(tv[k]) == bytes:
    142             converted[k] = list(tv[k])
    143         elif type(tv[k]) == tuple:
    144             converted[k] = tuple([list(x) for x in tv[k]])
    145         else:
    146             converted[k] = tv[k]
    147     json_object = json.dumps(converted, indent=2)
    148     with open(filename, "w") as outfile:
    149         outfile.write(json_object)
    150 
    151 if __name__ == "__main__":
    152 #    tv = create_test_vectors("SLH-DSA-SHAKE-128s")
    153 #    write_tv_to_file("/tmp/slh-dsa-shake-128s-test-vectors.json", tv)
    154 #    tv = create_test_vectors("SLH-DSA-SHAKE-128f")
    155 #    write_tv_to_file("/tmp/slh-dsa-shake-128f-test-vectors.json", tv)
    156 #    tv = create_test_vectors("SLH-DSA-SHAKE-192s")
    157 #    write_tv_to_file("/tmp/slh-dsa-shake-192s-test-vectors.json", tv)
    158 #    tv = create_test_vectors("SLH-DSA-SHAKE-192f")
    159 #    write_tv_to_file("/tmp/slh-dsa-shake-192f-test-vectors.json", tv)
    160 #    tv = create_test_vectors("SLH-DSA-SHAKE-256s")
    161 #    write_tv_to_file("/tmp/slh-dsa-shake-256s-test-vectors.json", tv)
    162 #    tv = create_test_vectors("SLH-DSA-SHAKE-256f")
    163 #    write_tv_to_file("/tmp/slh-dsa-shake-256f-test-vectors.json", tv)
    164     tv = create_test_vectors("SLH-DSA-SHA2-128s")
    165     write_tv_to_file("/tmp/slh-dsa-sha2-128s-test-vectors.json", tv)
    166     tv = create_test_vectors("SLH-DSA-SHA2-128f")
    167     write_tv_to_file("/tmp/slh-dsa-sha2-128f-test-vectors.json", tv)
    168     tv = create_test_vectors("SLH-DSA-SHA2-192s")
    169     write_tv_to_file("/tmp/slh-dsa-sha2-192s-test-vectors.json", tv)
    170     tv = create_test_vectors("SLH-DSA-SHA2-192f")
    171     write_tv_to_file("/tmp/slh-dsa-sha2-192f-test-vectors.json", tv)
    172     tv = create_test_vectors("SLH-DSA-SHA2-256s")
    173     write_tv_to_file("/tmp/slh-dsa-sha2-256s-test-vectors.json", tv)
    174     tv = create_test_vectors("SLH-DSA-SHA2-256f")
    175     write_tv_to_file("/tmp/slh-dsa-sha2-256f-test-vectors.json", tv)