# ------------------------------------------------------------
# PSUT IoT Engineering Studio | CR50
# Experiment: authenticated key agreement trust decision
# Purpose: expose algorithm states and verify the experiment invariant.
# ------------------------------------------------------------
# CR50 | Authenticated key agreement: trust decision
# Execute the named operation; expose states and assert the laboratory invariant.
import json



DEFAULT={'authenticated': True, 'substituted': False}
ALTERNATE={'substituted': True}

def run(overrides=None):
    # Merge only explicitly supplied inputs with the reference baseline.
    params={**DEFAULT, **(overrides or {})}
    authenticated=params['authenticated']
    substituted=params['substituted']
    if type(authenticated) is not bool or type(substituted) is not bool: raise ValueError('Boolean inputs required')
    steps=[{'state':'receive public value','substituted':substituted},{'state':'verify identity binding','required':authenticated,'valid':not substituted}];accept=not authenticated or not substituted
    steps.append({'state':'establish session' if accept else 'abort'});result={'accepted':accept,'peer_identity_verified':authenticated and not substituted}
    # Independently check the stated mathematical or protocol invariant.
    assert not (authenticated and substituted and result["accepted"]), "verification failed"
    return {"steps":steps,"result":result}

if __name__ == "__main__":
    import argparse
    parser=argparse.ArgumentParser();parser.add_argument("--inputs",default="{}");parser.add_argument("--test",action="store_true");args=parser.parse_args()
    if args.test:
        for case in [{},ALTERNATE]:run(case)
        print("PASS")
    else:print(json.dumps(run(json.loads(args.inputs)),indent=2))
