-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ml
100 lines (84 loc) · 3.28 KB
/
test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
open Core
(* Variable to store the message *)
let message = "Message to encrypt"
(* Call the blake3 with the message and get the hash value *)
let hash = In_channel.input_line_exn (In_channel.create "./blake3")
(* Create the XML file and write the data to it in the correct format *)
let file = Out_channel.create "output.xml"
Out_channel.output_string file ("<message>" ^ message ^ "</message>\n")
Out_channel.output_string file ("<hash>" ^ hash ^ "</hash>\n")
Out_channel.close file
let paint_test_input buf =
for i = 0 to Array.length buf - 1 do
buf.(i) <- i mod 251
done
type cases = {
_comment: string;
key: string;
context_string: string;
cases: case list;
}
and case = {
input_len: int;
hash: string;
keyed_hash: string;
derive_key: string;
}
let generate_json () =
let cases = ref [] in
for i = 0 to Array.length TEST_CASES - 1 do
let input = Array.make (TEST_CASES.(i)) 0 in
paint_test_input input;
let hash_out = Array.make OUTPUT_LEN 0 in
let hasher = Blake3.Hasher.new () in
Blake3.Hasher.update hasher input;
Blake3.Hasher.finalize_xof hasher;
Blake3.Hasher.fill hasher hash_out;
let keyed_hash_out = Array.make OUTPUT_LEN 0 in
let hasher = Blake3.Hasher.new_keyed TEST_KEY in
Blake3.Hasher.update hasher input;
Blake3.Hasher.finalize_xof hasher;
Blake3.Hasher.fill hasher keyed_hash_out;
let derive_key_out = Array.make OUTPUT_LEN 0 in
let hasher = Blake3.Hasher.new_derive_key TEST_CONTEXT in
Blake3.Hasher.update hasher input;
Blake3.Hasher.finalize_xof hasher;
Blake3.Hasher.fill hasher derive_key_out;
cases := {
input_len = TEST_CASES.(i);
hash = Hex.encode hash_out;
keyed_hash = Hex.encode keyed_hash_out;
derive_key = Hex.encode derive_key_out;
} :: !cases
done;
let json = Json.to_string {
_comment = COMMENT.trim().replace("\n", " ");
key = Bytes.to_string TEST_KEY;
context_string = Bytes.to_string TEST_CONTEXT;
cases = !cases;
} in
json ^ "\n"
let read_test_vectors_file () =
let test_vectors_file_path = "./test_vectors.json" in
match Sys.file_exists test_vectors_file_path with
| Yes -> Sys.file_content test_vectors_file_path | No | `Unknown ->
failwith "failed to read test_vectors.json"
let parse_test_cases () =
let json = read_test_vectors_file () in
match Json.from_string json with
| Ok x -> x | Error e -> failwith "failed to parse test_vectors.json"
let run_test_vectors () =
let cases = parse_test_cases () in
let key = cases.key |> Bytes.of_string in
List.iter (fun case ->
let input = Bytes.create case.input_len in
paint_test_input input;
let expected_hash = Hex.decode case.hash in
let expected_keyed_hash = Hex.decode case.keyed_hash in
let expected_derive_key = Hex.decode case.derive_key in
test_reference_impl_all_at_once key input expected_hash expected_keyed_hash expected_derive_key;
test_reference_impl_one_at_a_time key input expected_hash expected_keyed_hash expected_derive_key;
test_incremental_all_at_once key input expected_hash expected_keyed_hash expected_derive_key;
test_incremental_one_at_a_time key input expected_hash expected_keyed_hash expected_derive_key;
test_recursive key input expected_hash expected_keyed_hash expected_derive_key
) cases.cases