device-operation: machine operation log and reputation; conformance: 260 rules, 17 verifiers, 791 vectors
This commit is contained in:
1 parent
c1721b3bf1
commit
6ba33c477f
281 files changed
+115161
-46
No files matched your search
@@ -7,6 +7,13 @@ It claims little, on purpose, and says so through --capabilities:
|
||||
envelope envelope.form, envelope.integrity (it cannot check an ML-DSA signature or read a chain: vectors that exercise
|
||||
those rules are reported by the runner as NOT COVERED, never as passed)
|
||||
dataset-record dataset.form, dataset.commitment, dataset.field, dataset.inclusion (RFC 9162 inclusion, salted field commitments)
|
||||
device-reputation reputation.form, reputation.figures (2026-10-20: the formula aere-device-reputation/1 of the device operation
|
||||
component, computed again from the facts and compared with the figures stated; written from the formula in words in that
|
||||
component's README, not from its code: the windows are found by bisection over their starts, not by division, the
|
||||
gaps by one pass over the windows, and the comparison is of canonical texts written by this file, never of Python values,
|
||||
in which True == 1. The form of the facts follows the members and bounds that README names and the patterns the
|
||||
published form gives; the operation log itself carries ML-DSA-65 signatures, which this file cannot check, so its
|
||||
vectors are NOT COVERED)
|
||||
|
||||
python3 implementare-integritate.py --capabilities
|
||||
python3 implementare-integritate.py <vector.json>
|
||||
@@ -14,13 +21,20 @@ It claims little, on purpose, and says so through --capabilities:
|
||||
|
||||
Limits of this implementation: numbers are written the way JavaScript writes them only for integers and for the common decimal
|
||||
floats (no exponent forms); object keys are sorted by code point, which equals JavaScript's order for keys in the Basic
|
||||
Multilingual Plane. It never opens a connection: --online is not claimed.
|
||||
Multilingual Plane. Files are read with Python's json module: a file nested deeper than its recursion limit is not read (an error,
|
||||
which the runner scores BROKEN, never a verdict), and the non-JSON words NaN and Infinity, which JSON.parse refuses, are read. It
|
||||
never opens a connection: --online is not claimed.
|
||||
"""
|
||||
import bisect
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
|
||||
# a whole number of 5,000 digits is read, as JavaScript reads it (to a number it then judges), instead of stopping the reader
|
||||
if hasattr(sys, 'set_int_max_str_digits'):
|
||||
sys.set_int_max_str_digits(0)
|
||||
|
||||
LEVELS = ['integrity', 'authenticity', 'anchoring', 'finality']
|
||||
H64 = re.compile(r'^0x[0-9a-fA-F]{64}$')
|
||||
H64_LOW = re.compile(r'^0x[0-9a-f]{64}$')
|
||||
@@ -190,9 +204,193 @@ def judge_record(inp):
|
||||
return verdict('dataset-record', {'integrity': failed} if failed else {}, reasons, passed)
|
||||
|
||||
|
||||
JUDGES = {'envelope': judge_envelope, 'dataset-record': judge_record}
|
||||
# ---------------------------------------------------------------- device-reputation (2026-10-20)
|
||||
# The formula, in the words of the device operation README ("The reputation formula"): windows of W = windowS * 1000 ms over [from, to);
|
||||
# a counted record covers by the device's clock the window its `at` falls in, and is witnessed in the window its `lo` falls in when its whole
|
||||
# interval [lo, hi] lies in that window; availability, per mille rounded down, the runs of windows not covered by the clock, the proofs of
|
||||
# equivocation and the retirements copied, the age from the first counted record and from the lowest witnessed upper bound, the evidence
|
||||
# of every window covered either way, and the hash of the canonical facts. Integers only.
|
||||
REP_KIND, REP_FORMULA, FACTS_KIND = 'aere-device-reputation', 'aere-device-reputation/1', 'aere-device-operation-facts'
|
||||
FACT_MEMBERS = ('v', 'kind', 'verdict', 'rules', 'unjudged', 'manufacturer', 'keys', 'records', 'uncounted', 'equivocations', 'retirements', 'chains')
|
||||
REC_MEMBERS = ('seq', 'hash', 'at', 'lo', 'hi', 'state', 'counter')
|
||||
REP_STATES = ('operating', 'idle', 'fault', 'maintenance')
|
||||
REP_VERDICTS = ('VALID', 'PARTIAL', 'INVALID')
|
||||
SAFE_INT = 9007199254740991 # 2^53 - 1: what JavaScript holds exactly
|
||||
TIME_MAX = 4503599627370496 # 2^52 ms
|
||||
WINDOW_MAX_S = 366 * 86400
|
||||
WINDOWS_MAX, RECORDS_MAX, KEYS_MAX, EQUIV_MAX, CHAINS_MAX, NAMES_MAX = 100000, 100000, 16, 4096, 64, 64
|
||||
P_NAME = re.compile(r'[a-z][a-z0-9-]{0,63}')
|
||||
P_HASH = re.compile(r'0x[0-9a-f]{64}')
|
||||
P_DEVICE = re.compile(r'aere-device:[0-9a-f]{40}')
|
||||
P_AERE_ID = re.compile(r'aere-id:[0-9a-f]{40}')
|
||||
|
||||
|
||||
def whole(x, lo=0, hi=SAFE_INT):
|
||||
"""The whole number JavaScript reads from this JSON value, or None. Python's json reads '7' as int and '7.0' or '7e0' as float;
|
||||
JavaScript reads all three as the number 7. A boolean is never a number (in Python True == 1)."""
|
||||
if isinstance(x, bool):
|
||||
return None
|
||||
if isinstance(x, float):
|
||||
if not x.is_integer():
|
||||
return None
|
||||
x = int(x)
|
||||
if not isinstance(x, int):
|
||||
return None
|
||||
return x if lo <= x <= hi else None
|
||||
|
||||
|
||||
def text_of(x, pattern):
|
||||
return isinstance(x, str) and pattern.fullmatch(x) is not None
|
||||
|
||||
|
||||
def ascending(xs):
|
||||
return all(xs[k] < xs[k + 1] for k in range(len(xs) - 1))
|
||||
|
||||
|
||||
def members(o, names):
|
||||
return isinstance(o, dict) and set(o) == set(names)
|
||||
|
||||
|
||||
def facts_problem(f):
|
||||
"""None when the facts are of the form the formula reads; otherwise what is not."""
|
||||
if not members(f, FACT_MEMBERS):
|
||||
return 'facts: not exactly ' + ', '.join(FACT_MEMBERS)
|
||||
if whole(f['v']) != 1 or f['kind'] != FACTS_KIND:
|
||||
return 'facts: not ' + FACTS_KIND + ' version 1'
|
||||
if not (isinstance(f['verdict'], str) and f['verdict'] in REP_VERDICTS):
|
||||
return 'facts: verdict'
|
||||
for k in ('rules', 'unjudged'):
|
||||
x = f[k]
|
||||
if not (isinstance(x, list) and len(x) <= NAMES_MAX and all(text_of(n, P_NAME) for n in x) and ascending(x)):
|
||||
return 'facts: ' + k + ' is a sorted list of distinct names'
|
||||
if not (f['manufacturer'] is None or text_of(f['manufacturer'], P_AERE_ID)):
|
||||
return 'facts: manufacturer'
|
||||
if not (isinstance(f['keys'], list) and len(f['keys']) <= KEYS_MAX and all(text_of(k, P_DEVICE) for k in f['keys'])):
|
||||
return 'facts: keys'
|
||||
rs = f['records']
|
||||
if not (isinstance(rs, list) and len(rs) <= RECORDS_MAX):
|
||||
return 'facts: records'
|
||||
before = None
|
||||
for r in rs:
|
||||
if not members(r, REC_MEMBERS):
|
||||
return 'facts: a record is not exactly ' + ', '.join(REC_MEMBERS)
|
||||
seq, at, counter = whole(r['seq']), whole(r['at'], 0, TIME_MAX), whole(r['counter'])
|
||||
bounds_ok = all(r[b] is None or whole(r[b], 0, TIME_MAX) is not None for b in ('lo', 'hi'))
|
||||
if seq is None or at is None or counter is None or not bounds_ok or not text_of(r['hash'], P_HASH) or not (isinstance(r['state'], str) and r['state'] in REP_STATES):
|
||||
return 'facts: a record has a member of another form'
|
||||
if before is not None and not (before[0] < seq and before[1] <= at and before[2] <= counter):
|
||||
return 'facts: a record does not follow the one before it'
|
||||
before = (seq, at, counter)
|
||||
if whole(f['uncounted']) is None:
|
||||
return 'facts: uncounted'
|
||||
es = f['equivocations']
|
||||
if not (isinstance(es, list) and len(es) <= EQUIV_MAX):
|
||||
return 'facts: equivocations'
|
||||
keys_e = []
|
||||
for e in es:
|
||||
if not (members(e, ('seq', 'a', 'b')) and whole(e['seq']) is not None and text_of(e['a'], P_HASH) and text_of(e['b'], P_HASH) and e['a'] != e['b']):
|
||||
return 'facts: an equivocation is not { seq, a, b } with two different record hashes'
|
||||
keys_e.append((whole(e['seq']), e['a'], e['b']))
|
||||
if not ascending(keys_e):
|
||||
return 'facts: equivocations are sorted by seq, a, b and distinct'
|
||||
ts = f['retirements']
|
||||
if not (isinstance(ts, list) and len(ts) <= KEYS_MAX and all(members(t, ('deviceId', 'at')) and text_of(t['deviceId'], P_DEVICE) and (t['at'] is None or whole(t['at'], 0, TIME_MAX) is not None) for t in ts)):
|
||||
return 'facts: retirements'
|
||||
c = f['chains']
|
||||
if not members(c, ('anchors', 'beacons')):
|
||||
return 'facts: chains'
|
||||
for k in ('anchors', 'beacons'):
|
||||
ids = c[k]
|
||||
if not (isinstance(ids, list) and len(ids) <= CHAINS_MAX and all(whole(i, 1) is not None for i in ids) and ascending([whole(i, 1) for i in ids])):
|
||||
return 'facts: chains.' + k
|
||||
return None
|
||||
|
||||
|
||||
def params_problem(p):
|
||||
if not members(p, ('from', 'to', 'windowS')):
|
||||
return 'params: not exactly from, to, windowS'
|
||||
a, b, w = whole(p['from']), whole(p['to']), whole(p['windowS'], 1, WINDOW_MAX_S)
|
||||
if a is None or b is None or not (a < b <= TIME_MAX):
|
||||
return 'params: from and to'
|
||||
if w is None:
|
||||
return 'params: windowS'
|
||||
if len(range(a, b, w * 1000)) > WINDOWS_MAX:
|
||||
return 'params: windows'
|
||||
return None
|
||||
|
||||
|
||||
def reputation_of(f, p):
|
||||
a, b, ws = whole(p['from']), whole(p['to']), whole(p['windowS'])
|
||||
span = ws * 1000
|
||||
starts = list(range(a, b, span)) # window i begins at starts[i]
|
||||
n = len(starts)
|
||||
ends = starts[1:] + [b] # and ends at the next start, the last at `to`
|
||||
|
||||
def window(t): # the window t falls in, or None outside [from, to)
|
||||
if not a <= t < b:
|
||||
return None
|
||||
return bisect.bisect_right(starts, t) - 1
|
||||
|
||||
recs = [{'seq': whole(r['seq']), 'at': whole(r['at']), 'lo': None if r['lo'] is None else whole(r['lo']), 'hi': None if r['hi'] is None else whole(r['hi']), 'state': r['state']} for r in f['records']]
|
||||
by_clock, by_witness = [None] * n, [None] * n
|
||||
in_range, states = 0, dict((s, 0) for s in REP_STATES)
|
||||
for r in recs:
|
||||
i = window(r['at'])
|
||||
if i is not None:
|
||||
in_range += 1
|
||||
states[r['state']] += 1
|
||||
by_clock[i] = r['seq'] if by_clock[i] is None else min(by_clock[i], r['seq'])
|
||||
if r['lo'] is not None and r['hi'] is not None and r['lo'] <= r['hi']:
|
||||
k = window(r['lo'])
|
||||
if k is not None and r['hi'] < ends[k]:
|
||||
by_witness[k] = r['seq'] if by_witness[k] is None else min(by_witness[k], r['seq'])
|
||||
runs, start = [], None
|
||||
for i in range(n + 1):
|
||||
empty = i < n and by_clock[i] is None
|
||||
if empty and start is None:
|
||||
start = i
|
||||
elif not empty and start is not None:
|
||||
runs.append([start, i - start])
|
||||
start = None
|
||||
covered_c = sum(1 for x in by_clock if x is not None)
|
||||
covered_w = sum(1 for x in by_witness if x is not None)
|
||||
first = min(recs, key=lambda r: r['seq']) if recs else None
|
||||
his = [r['hi'] for r in recs if r['hi'] is not None]
|
||||
low_hi = min(his) if his else None
|
||||
return {
|
||||
'v': 1, 'kind': REP_KIND, 'formula': REP_FORMULA,
|
||||
'factsHash': sha256_hex(canonical(f).encode('utf-8')),
|
||||
'params': {'from': a, 'to': b, 'windowS': ws},
|
||||
'verdict': f['verdict'], 'rules': f['rules'], 'unjudged': f['unjudged'], 'manufacturer': f['manufacturer'], 'keys': f['keys'], 'chains': f['chains'],
|
||||
'records': {'inRange': in_range, 'byState': states, 'uncounted': f['uncounted']},
|
||||
'availability': {'windows': n, 'claimed': covered_c, 'witnessed': covered_w, 'claimedPerMille': (1000 * covered_c) // n, 'witnessedPerMille': (1000 * covered_w) // n},
|
||||
'gaps': {'count': len(runs), 'longestWindows': max([r[1] for r in runs] or [0]), 'list': runs},
|
||||
'equivocations': {'count': len(f['equivocations']), 'list': [[e['seq'], e['a'], e['b']] for e in f['equivocations']]},
|
||||
'retirements': {'count': len(f['retirements']), 'list': [[t['deviceId'], t['at']] for t in f['retirements']]},
|
||||
'age': {'claimedS': (b - first['at']) // 1000 if first is not None and first['at'] < b else None,
|
||||
'witnessedS': (b - low_hi) // 1000 if low_hi is not None and low_hi < b else None},
|
||||
'evidence': {'windows': [[i, by_clock[i], by_witness[i]] for i in range(n) if by_clock[i] is not None or by_witness[i] is not None]},
|
||||
}
|
||||
|
||||
|
||||
def judge_reputation(inp):
|
||||
if not members(inp, ('facts', 'params', 'claimed')):
|
||||
return verdict('device-reputation', {'integrity': ['reputation.form']}, ['the input is not exactly { facts, params, claimed }'], {})
|
||||
why = facts_problem(inp['facts']) or params_problem(inp['params'])
|
||||
if why:
|
||||
return verdict('device-reputation', {'integrity': ['reputation.form']}, [why], {})
|
||||
try:
|
||||
same = canonical(inp['claimed']) == canonical(reputation_of(inp['facts'], inp['params']))
|
||||
except (ValueError, RecursionError):
|
||||
same = False # a claim this file cannot write as JavaScript writes it (an exponent form, Infinity) is not the formula's text
|
||||
if not same:
|
||||
return verdict('device-reputation', {'integrity': ['reputation.figures']}, ['the stated reputation is not what the formula gives for these facts and parameters'], {})
|
||||
return verdict('device-reputation', {}, [], {'integrity': 'the stated reputation is what the formula ' + REP_FORMULA + ' gives'})
|
||||
|
||||
|
||||
JUDGES = {'envelope': judge_envelope, 'dataset-record': judge_record, 'device-reputation': judge_reputation}
|
||||
CAPABILITIES = {'v': 1, 'kind': 'aere-conformance-capabilities', 'name': 'independent integrity checker (Python, standard library)', 'artifacts': sorted(JUDGES),
|
||||
'rules': ['dataset.commitment', 'dataset.field', 'dataset.form', 'dataset.inclusion', 'envelope.form', 'envelope.integrity'], 'online': False, 'batch': True}
|
||||
'rules': ['dataset.commitment', 'dataset.field', 'dataset.form', 'dataset.inclusion', 'envelope.form', 'envelope.integrity', 'reputation.figures', 'reputation.form'], 'online': False, 'batch': True}
|
||||
|
||||
|
||||
def one(path):
|
||||
|
||||
Reference in new issue
Block a user