Debian Codegraph @

debian/rust-vendor/blake3/c/test.py

raw
1 #! /usr/bin/env python3
2
3 from binascii import hexlify
4 import json
5 from os import path
6 import subprocess
7
8 HERE = path.dirname(__file__)
9 TEST_VECTORS_PATH = path.join(HERE, "..", "test_vectors", "test_vectors.json")
10 TEST_VECTORS = json.load(open(TEST_VECTORS_PATH))
11
12
13 def run_blake3(args, input):
14 output = subprocess.run([path.join(HERE, "blake3")] + args,
15 input=input,
16 stdout=subprocess.PIPE,
17 check=True)
18 return output.stdout.decode().strip()
19
20
21 # Fill the input with a repeating byte pattern. We use a cycle length of 251,
22 # because that's the largest prime number less than 256. This makes it unlikely
23 # to swapping any two adjacent input blocks or chunks will give the same
24 # answer.
25 def make_test_input(length):
26 i = 0
27 buf = bytearray()
28 while len(buf) < length:
29 buf.append(i)
30 i = (i + 1) % 251
31 return buf
32
33
34 def main():
35 for case in TEST_VECTORS["cases"]:
36 input_len = case["input_len"]
37 input = make_test_input(input_len)
38 hex_key = hexlify(TEST_VECTORS["key"].encode())
39 context_string = TEST_VECTORS["context_string"]
40 expected_hash_xof = case["hash"]
41 expected_hash = expected_hash_xof[:64]
42 expected_keyed_hash_xof = case["keyed_hash"]
43 expected_keyed_hash = expected_keyed_hash_xof[:64]
44 expected_derive_key_xof = case["derive_key"]
45 expected_derive_key = expected_derive_key_xof[:64]
46
47 # Test the default hash.
48 test_hash = run_blake3([], input)
49 for line in test_hash.splitlines():
50 assert expected_hash == line, \
51 "hash({}): {} != {}".format(input_len, expected_hash, line)
52
53 # Test the extended hash.
54 xof_len = len(expected_hash_xof) // 2
55 test_hash_xof = run_blake3(["--length", str(xof_len)], input)
56 for line in test_hash_xof.splitlines():
57 assert expected_hash_xof == line, \
58 "hash_xof({}): {} != {}".format(
59 input_len, expected_hash_xof, line)
60
61 # Test the default keyed hash.
62 test_keyed_hash = run_blake3(["--keyed", hex_key], input)
63 for line in test_keyed_hash.splitlines():
64 assert expected_keyed_hash == line, \
65 "keyed_hash({}): {} != {}".format(
66 input_len, expected_keyed_hash, line)
67
68 # Test the extended keyed hash.
69 xof_len = len(expected_keyed_hash_xof) // 2
70 test_keyed_hash_xof = run_blake3(
71 ["--keyed", hex_key, "--length",
72 str(xof_len)], input)
73 for line in test_keyed_hash_xof.splitlines():
74 assert expected_keyed_hash_xof == line, \
75 "keyed_hash_xof({}): {} != {}".format(
76 input_len, expected_keyed_hash_xof, line)
77
78 # Test the default derive key.
79 test_derive_key = run_blake3(["--derive-key", context_string], input)
80 for line in test_derive_key.splitlines():
81 assert expected_derive_key == line, \
82 "derive_key({}): {} != {}".format(
83 input_len, expected_derive_key, line)
84
85 # Test the extended derive key.
86 xof_len = len(expected_derive_key_xof) // 2
87 test_derive_key_xof = run_blake3(
88 ["--derive-key", context_string, "--length",
89 str(xof_len)], input)
90 for line in test_derive_key_xof.splitlines():
91 assert expected_derive_key_xof == line, \
92 "derive_key_xof({}): {} != {}".format(
93 input_len, expected_derive_key_xof, line)
94
95
96 if __name__ == "__main__":
97 main()