1# $OpenBSD: wycheproof-json.pl,v 1.2 2022/07/08 14:33:56 tb Exp $
2
3# Copyright (c) 2022 Joel Sing <jsing@openbsd.org>
4# Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18use JSON::PP;
19
20$test_vector_path = "/usr/local/share/wycheproof/testvectors";
21
22open JSON, "$test_vector_path/primality_test.json" or die;
23@json = <JSON>;
24close JSON;
25
26$tv = JSON::PP::decode_json(join "\n", @json);
27$test_groups = %$tv{"testGroups"};
28
29my $wycheproof_struct = <<"EOL";
30struct wycheproof_testcase {
31	int id;
32	const char *value;
33	int acceptable;
34	int result;
35};
36
37struct wycheproof_testcase testcases[] = {
38EOL
39
40print $wycheproof_struct;
41
42foreach $test_group (@$test_groups) {
43	$test_group_type = %$test_group{"type"};
44	$test_group_tests = %$test_group{"tests"};
45
46	foreach $test_case (@$test_group_tests) {
47		%tc = %$test_case;
48
49		$tc_id = $tc{"tcId"};
50		$tc_value = $tc{"value"};
51		$tc_result = $tc{"result"};
52		$tc_flags = @{$tc{"flags"}};
53
54		my $result = $tc_result eq "valid" ? 1 : 0;
55
56		print "\t{\n";
57		print "\t\t.id = $tc_id,\n";
58		print "\t\t.value = \"$tc_value\",\n";
59		print "\t\t.result = $result,\n";
60
61		if ($tc_result eq "acceptable") {
62			print "\t\t.acceptable = 1,\n";
63		}
64
65		print "\t},\n";
66	}
67}
68
69print "};\n\n";
70
71print "#define N_TESTS (sizeof(testcases) / sizeof(testcases[0]))\n"
72