congruentia-benchmark 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- congruentia_benchmark/__init__.py +15 -0
- congruentia_benchmark/_strict_json.py +258 -0
- congruentia_benchmark/catalogue.py +573 -0
- congruentia_benchmark/command.py +81 -0
- congruentia_benchmark/py.typed +0 -0
- congruentia_benchmark/schemas/units_and_windows_catalogue.v1.schema.json +161 -0
- congruentia_benchmark/schemas/units_and_windows_catalogue.v1.schema.json.license +9 -0
- congruentia_benchmark-0.1.0.dist-info/METADATA +34 -0
- congruentia_benchmark-0.1.0.dist-info/RECORD +12 -0
- congruentia_benchmark-0.1.0.dist-info/WHEEL +4 -0
- congruentia_benchmark-0.1.0.dist-info/entry_points.txt +3 -0
- congruentia_benchmark-0.1.0.dist-info/licenses/LICENSES/AGPL-3.0-or-later.txt +235 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
# Commercial license available
|
|
3
|
+
# © Concepts 1996–2026 Miroslav Šotek. All rights reserved.
|
|
4
|
+
# © Code 2020–2026 Miroslav Šotek. All rights reserved.
|
|
5
|
+
# ORCID: 0009-0009-3560-0851
|
|
6
|
+
# Contact: www.anulum.li | protoscience@anulum.li
|
|
7
|
+
# CONGRUENTIA-BENCHMARK — package identity
|
|
8
|
+
"""Congruentia Benchmark: benchmark cases, verification reports and their catalogues.
|
|
9
|
+
|
|
10
|
+
The first capability is :mod:`congruentia_benchmark.catalogue`.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
__all__ = ["__version__"]
|
|
14
|
+
|
|
15
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
# Commercial license available
|
|
3
|
+
# © Concepts 1996–2026 Miroslav Šotek. All rights reserved.
|
|
4
|
+
# © Code 2020–2026 Miroslav Šotek. All rights reserved.
|
|
5
|
+
# ORCID: 0009-0009-3560-0851
|
|
6
|
+
# Contact: www.anulum.li | protoscience@anulum.li
|
|
7
|
+
# CONGRUENTIA-BENCHMARK — strict JSON parsing and RFC 8785 canonical bytes
|
|
8
|
+
"""Parse JSON strictly and serialise the float-free value space as RFC 8785 bytes.
|
|
9
|
+
|
|
10
|
+
The value space is null, booleans, I-JSON integers, strings, arrays and objects. Exact
|
|
11
|
+
non-integers travel as decimal strings, so a binary float is refused instead of being
|
|
12
|
+
canonicalised.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import hashlib
|
|
18
|
+
import json
|
|
19
|
+
from typing import Literal, NoReturn
|
|
20
|
+
|
|
21
|
+
type JsonValue = None | bool | int | str | list[JsonValue] | dict[str, JsonValue]
|
|
22
|
+
|
|
23
|
+
I_JSON_MAX = 2**53 - 1
|
|
24
|
+
MAX_DEPTH = 64
|
|
25
|
+
|
|
26
|
+
type StrictJsonCode = Literal[
|
|
27
|
+
"byte_order_mark",
|
|
28
|
+
"document_too_large",
|
|
29
|
+
"duplicate_key",
|
|
30
|
+
"float_not_allowed",
|
|
31
|
+
"integer_out_of_range",
|
|
32
|
+
"invalid_unicode",
|
|
33
|
+
"nesting_too_deep",
|
|
34
|
+
"non_finite_number",
|
|
35
|
+
"not_json",
|
|
36
|
+
"not_utf8",
|
|
37
|
+
"unsupported_value",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class StrictJsonError(ValueError):
|
|
42
|
+
"""Refusal of a document or value outside the strict JSON value space.
|
|
43
|
+
|
|
44
|
+
Parameters
|
|
45
|
+
----------
|
|
46
|
+
code : str
|
|
47
|
+
Stable refusal code.
|
|
48
|
+
detail : str
|
|
49
|
+
Bounded human-readable context that never echoes document content.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(self, code: StrictJsonCode, detail: str) -> None:
|
|
53
|
+
super().__init__(f"{code}: {detail}")
|
|
54
|
+
self.code: StrictJsonCode = code
|
|
55
|
+
self.detail = detail
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _pairs(pairs: list[tuple[str, JsonValue]]) -> dict[str, JsonValue]:
|
|
59
|
+
result: dict[str, JsonValue] = {}
|
|
60
|
+
for key, value in pairs:
|
|
61
|
+
if key in result:
|
|
62
|
+
raise StrictJsonError("duplicate_key", "an object repeats a key")
|
|
63
|
+
result[key] = value
|
|
64
|
+
return result
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _refuse_float(_: str) -> NoReturn:
|
|
68
|
+
raise StrictJsonError("float_not_allowed", "exact non-integers travel as decimal strings")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _refuse_constant(_: str) -> NoReturn:
|
|
72
|
+
raise StrictJsonError("non_finite_number", "NaN and infinities are not JSON numbers")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _bounded_int(text: str) -> int:
|
|
76
|
+
digits = text.removeprefix("-")
|
|
77
|
+
if len(digits) > len(str(I_JSON_MAX)):
|
|
78
|
+
raise StrictJsonError("integer_out_of_range", "an integer exceeds the I-JSON range")
|
|
79
|
+
value = int(text)
|
|
80
|
+
if not -I_JSON_MAX <= value <= I_JSON_MAX:
|
|
81
|
+
raise StrictJsonError("integer_out_of_range", "an integer exceeds the I-JSON range")
|
|
82
|
+
return value
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _check_text(text: str) -> None:
|
|
86
|
+
try:
|
|
87
|
+
text.encode("utf-8")
|
|
88
|
+
except UnicodeEncodeError:
|
|
89
|
+
raise StrictJsonError("invalid_unicode", "a string holds a lone surrogate") from None
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _check_tree(value: JsonValue) -> None:
|
|
93
|
+
stack: list[tuple[JsonValue, int]] = [(value, 1)]
|
|
94
|
+
while stack:
|
|
95
|
+
item, depth = stack.pop()
|
|
96
|
+
if depth > MAX_DEPTH:
|
|
97
|
+
raise StrictJsonError("nesting_too_deep", f"nesting exceeds {MAX_DEPTH} levels")
|
|
98
|
+
if isinstance(item, str):
|
|
99
|
+
_check_text(item)
|
|
100
|
+
elif isinstance(item, list):
|
|
101
|
+
stack.extend((child, depth + 1) for child in item)
|
|
102
|
+
elif isinstance(item, dict):
|
|
103
|
+
for key, child in item.items():
|
|
104
|
+
_check_text(key)
|
|
105
|
+
stack.append((child, depth + 1))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def parse_strict(data: bytes, *, max_bytes: int) -> JsonValue:
|
|
109
|
+
"""Parse one bounded UTF-8 JSON document in the strict value space.
|
|
110
|
+
|
|
111
|
+
Parameters
|
|
112
|
+
----------
|
|
113
|
+
data : bytes
|
|
114
|
+
The exact received bytes.
|
|
115
|
+
max_bytes : int
|
|
116
|
+
Largest accepted document size, checked before decoding.
|
|
117
|
+
|
|
118
|
+
Returns
|
|
119
|
+
-------
|
|
120
|
+
JsonValue
|
|
121
|
+
The parsed value.
|
|
122
|
+
|
|
123
|
+
Raises
|
|
124
|
+
------
|
|
125
|
+
StrictJsonError
|
|
126
|
+
For oversize input, invalid UTF-8, a byte order mark, malformed JSON,
|
|
127
|
+
duplicate keys, floats, non-finite constants, integers outside I-JSON,
|
|
128
|
+
lone surrogates or nesting deeper than ``MAX_DEPTH``.
|
|
129
|
+
"""
|
|
130
|
+
if len(data) > max_bytes:
|
|
131
|
+
raise StrictJsonError("document_too_large", f"the document exceeds {max_bytes} bytes")
|
|
132
|
+
try:
|
|
133
|
+
text = data.decode("utf-8")
|
|
134
|
+
except UnicodeDecodeError:
|
|
135
|
+
raise StrictJsonError("not_utf8", "the document is not valid UTF-8") from None
|
|
136
|
+
if text.startswith(""):
|
|
137
|
+
raise StrictJsonError("byte_order_mark", "the document starts with a byte order mark")
|
|
138
|
+
try:
|
|
139
|
+
value: JsonValue = json.loads(
|
|
140
|
+
text,
|
|
141
|
+
object_pairs_hook=_pairs,
|
|
142
|
+
parse_float=_refuse_float,
|
|
143
|
+
parse_int=_bounded_int,
|
|
144
|
+
parse_constant=_refuse_constant,
|
|
145
|
+
)
|
|
146
|
+
except RecursionError:
|
|
147
|
+
raise StrictJsonError(
|
|
148
|
+
"nesting_too_deep", "the parser's nesting limit was reached"
|
|
149
|
+
) from None
|
|
150
|
+
except json.JSONDecodeError:
|
|
151
|
+
raise StrictJsonError("not_json", "the document is not one JSON value") from None
|
|
152
|
+
_check_tree(value)
|
|
153
|
+
return value
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _string(text: str) -> str:
|
|
157
|
+
_check_text(text)
|
|
158
|
+
out = ['"']
|
|
159
|
+
for character in text:
|
|
160
|
+
code = ord(character)
|
|
161
|
+
if character == '"':
|
|
162
|
+
out.append('\\"')
|
|
163
|
+
elif character == "\\":
|
|
164
|
+
out.append("\\\\")
|
|
165
|
+
elif code < 0x20:
|
|
166
|
+
out.append(_CONTROL.get(character, f"\\u{code:04x}"))
|
|
167
|
+
else:
|
|
168
|
+
out.append(character)
|
|
169
|
+
out.append('"')
|
|
170
|
+
return "".join(out)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
_CONTROL = {"\b": "\\b", "\f": "\\f", "\n": "\\n", "\r": "\\r", "\t": "\\t"}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _serialise(value: object, depth: int, out: list[str]) -> None:
|
|
177
|
+
if depth > MAX_DEPTH:
|
|
178
|
+
raise StrictJsonError("nesting_too_deep", f"nesting exceeds {MAX_DEPTH} levels")
|
|
179
|
+
if value is None:
|
|
180
|
+
out.append("null")
|
|
181
|
+
elif value is True:
|
|
182
|
+
out.append("true")
|
|
183
|
+
elif value is False:
|
|
184
|
+
out.append("false")
|
|
185
|
+
elif type(value) is int:
|
|
186
|
+
if not -I_JSON_MAX <= value <= I_JSON_MAX:
|
|
187
|
+
raise StrictJsonError("integer_out_of_range", "an integer exceeds the I-JSON range")
|
|
188
|
+
out.append(str(value))
|
|
189
|
+
elif type(value) is str:
|
|
190
|
+
out.append(_string(value))
|
|
191
|
+
elif type(value) is list:
|
|
192
|
+
out.append("[")
|
|
193
|
+
for index, item in enumerate(value):
|
|
194
|
+
if index:
|
|
195
|
+
out.append(",")
|
|
196
|
+
_serialise(item, depth + 1, out)
|
|
197
|
+
out.append("]")
|
|
198
|
+
elif type(value) is dict:
|
|
199
|
+
keys = list(value)
|
|
200
|
+
if not all(type(key) is str for key in keys):
|
|
201
|
+
raise StrictJsonError("unsupported_value", "object keys must be strings")
|
|
202
|
+
out.append("{")
|
|
203
|
+
for index, key in enumerate(sorted(keys, key=_utf16_order)):
|
|
204
|
+
if index:
|
|
205
|
+
out.append(",")
|
|
206
|
+
out.append(_string(key))
|
|
207
|
+
out.append(":")
|
|
208
|
+
_serialise(value[key], depth + 1, out)
|
|
209
|
+
out.append("}")
|
|
210
|
+
elif type(value) is float:
|
|
211
|
+
raise StrictJsonError("float_not_allowed", "exact non-integers travel as decimal strings")
|
|
212
|
+
else:
|
|
213
|
+
raise StrictJsonError("unsupported_value", f"{type(value).__name__} is not a JSON value")
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _utf16_order(key: str) -> bytes:
|
|
217
|
+
_check_text(key)
|
|
218
|
+
return key.encode("utf-16-be")
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def canonical_bytes(value: JsonValue) -> bytes:
|
|
222
|
+
"""Return the RFC 8785 serialisation of a value in the strict space.
|
|
223
|
+
|
|
224
|
+
Parameters
|
|
225
|
+
----------
|
|
226
|
+
value : JsonValue
|
|
227
|
+
Null, a boolean, an I-JSON integer, a string, a list or a string-keyed dict.
|
|
228
|
+
|
|
229
|
+
Returns
|
|
230
|
+
-------
|
|
231
|
+
bytes
|
|
232
|
+
UTF-8 bytes with members sorted by UTF-16 code units and no whitespace.
|
|
233
|
+
|
|
234
|
+
Raises
|
|
235
|
+
------
|
|
236
|
+
StrictJsonError
|
|
237
|
+
For floats, out-of-range integers, lone surrogates, non-string keys,
|
|
238
|
+
unsupported types or nesting deeper than ``MAX_DEPTH``.
|
|
239
|
+
"""
|
|
240
|
+
out: list[str] = []
|
|
241
|
+
_serialise(value, 1, out)
|
|
242
|
+
return "".join(out).encode("utf-8")
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def canonical_sha256(value: JsonValue) -> str:
|
|
246
|
+
"""Return the lowercase SHA-256 of a value's RFC 8785 bytes.
|
|
247
|
+
|
|
248
|
+
Parameters
|
|
249
|
+
----------
|
|
250
|
+
value : JsonValue
|
|
251
|
+
A value in the strict space.
|
|
252
|
+
|
|
253
|
+
Returns
|
|
254
|
+
-------
|
|
255
|
+
str
|
|
256
|
+
Sixty-four lowercase hexadecimal characters.
|
|
257
|
+
"""
|
|
258
|
+
return hashlib.sha256(canonical_bytes(value)).hexdigest()
|