"""Z3 certificates accept correct kernel results and reject tampered ones.""" from dataclasses import replace import pytest from geometry.certify import CertificationError, certify_query from geometry.kernels import Circle, Point, SurdPoint from geometry.language import parse_problem from geometry.logic import check_smt from geometry.solver import NoResult, solve_numeric PROBLEM = """Point A = (0, 0). Point B = (4, 0). Point C = (1, 3). Point D = (2, 0). Point E = (6, 0). Point F = (10, 0). Point Q = (1, 1). Line l through A and C. Line m: 3x + y = 12. Line p: 3x + y = 5. Line k through A and B. Line n through D and E. Circle c with center A and radius squared 9. Rotation r by (3/5, 4/5) about A. Translation t by (2, -1). Transformation u is r then t. Find the intersection of line l and line m. Find the intersection of line m and line p. Find the intersection of line k and line n. Find the circumcircle of A, B and C. Find the circumcircle of A, B and D. Find the tangents from B to circle c. Find the tangents from Q to circle c. Find the tangents from F to circle c. Find the orthocenter and centroid of triangle A, B, C. Find the orthocenter and centroid of triangle A, B, D. Find the power of B with respect to circle c. Find the power of Q with respect to circle c. Find the secant products from B to circle c along (-1, 0) and (-2, 1). Find the secant products from B to circle c along (-1, 0) and (0, 1). Find the image of C under u.""" @pytest.fixture(scope="module") def sol(): return solve_numeric(parse_problem(PROBLEM)) def status(cert): return check_smt(cert.declarations, cert.assertions, 20000).status def test_every_correct_result_is_certified_unsat(sol): assert len(sol.queries) == 15 for qid, node in sol.queries.items(): cert = certify_query(qid, node, sol) assert cert.expected == "unsat" assert status(cert) == "unsat", (qid, node.head, cert.claim) PREMISED = {"q0", "q3", "q5", "q7", "q8", "q10", "q11", "q12", "q14"} NON_EXISTENCE = {"q1", "q2", "q4", "q6", "q9", "q13"} def test_certificate_premise_parts_are_all_satisfiable(sol): """An unsat obligation only certifies a claim when its premises can happen. The six queries without a premise part state that something does not exist (parallel lines, a circle through collinear points, tangents from an interior point, a secant that misses the circle), so their whole body is the negation of existence and there is no premise to witness. """ premised, bare = set(), set() for qid, node in sol.queries.items(): cert = certify_query(qid, node, sol) if not cert.hypotheses: bare.add(qid) continue premised.add(qid) assert check_smt(cert.declarations, cert.hypotheses, 20000).status == "sat", (qid, cert.kind) assert premised == PREMISED and bare == NON_EXISTENCE def test_power_certificate_quantifies_the_direction_of_the_secant(sol): """The power claim is about every secant through the point, not one line.""" cert = certify_query("q11", sol.queries["q11"], sol) assert cert.declarations == ["(declare-const t1 Real)", "(declare-const t2 Real)", "(declare-const dx Real)", "(declare-const dy Real)"] assert "every secant" in cert.claim assert check_smt(cert.declarations, cert.hypotheses, 20000).status == "sat" TANGENT = ("Point O = (0, 0). Point P = (3, 0). Circle c with center O and radius squared 9. " "Find the secant products from P to circle c along (0, 1) and (1, 1).") DEGENERATE = ("Point O = (0, 0). Point P = (3, 0). Circle c with center O and radius squared 9. " "Find the secant products from P to circle c along (0, 0) and (1, 1).") def test_secant_certificate_counts_a_tangent_direction(): """A tangent direction has one double intersection, not no intersection.""" sol = solve_numeric(parse_problem(TANGENT)) node = sol.queries["q0"] cert = certify_query("q0", node, sol) assert status(cert) == "unsat" # the premise part is realizable even though one direction is tangent: an # old encoding demanded two distinct intersections and was vacuously unsat assert check_smt(cert.declarations, cert.hypotheses, 20000).status == "sat" assert "tangent" in cert.claim assert status(cert_for(sol, "q0", lambda r: replace(r, first=replace(r.first, product=r.first.product + 1)))) == "sat" assert status(cert_for(sol, "q0", lambda r: replace(r, equal=False))) == "sat" def test_zero_direction_is_not_certified_as_a_secant(): sol = solve_numeric(parse_problem(DEGENERATE)) with pytest.raises(CertificationError): certify_query("q0", sol.queries["q0"], sol) def cert_for(sol, qid, tamper): kind, payload = sol.results[qid] sol2 = type(sol)(points=sol.points, lines=sol.lines, circles=sol.circles, transforms=sol.transforms, results=dict(sol.results), queries=sol.queries) sol2.results[qid] = (kind, tamper(payload)) return certify_query(qid, sol.queries[qid], sol2) def premise_status(cert) -> str: return check_smt(cert.declarations, cert.hypotheses, 20000).status def test_result_kinds_of_the_fixture(sol): kinds = {q: sol.results[q][1].__class__.__name__ for q in sol.results} assert sol.results["q1"][1].kind == "parallel" and sol.results["q2"][1].kind == "coincident" assert kinds["q4"] == "NoResult" and kinds["q9"] == "NoResult" and kinds["q13"] == "NoResult" assert sol.results["q5"][1].kind == "two" and sol.results["q6"][1].kind == "none" def test_tampered_intersection_is_rejected(sol): assert status(cert_for(sol, "q0", lambda r: replace(r, point=Point(r.point.x + 1, r.point.y)))) == "sat" # claiming that crossing lines are parallel assert status(cert_for(sol, "q0", lambda r: replace(r, kind="parallel", point=None))) == "sat" # claiming that parallel lines meet assert status(cert_for(sol, "q1", lambda r: replace(r, kind="point", point=Point(0, 0)))) == "sat" # claiming that coincident lines are merely parallel assert status(cert_for(sol, "q2", lambda r: replace(r, kind="parallel"))) == "sat" def test_tampered_circumcircle_is_rejected(sol): res = sol.results["q3"][1] bad_center = replace(res, circle=Circle(Point(res.center.x + 1, res.center.y), res.circle.r2)) bad_radius = replace(res, circle=Circle(res.center, res.circle.r2 + 1)) assert status(cert_for(sol, "q3", lambda r: bad_center)) == "sat" assert status(cert_for(sol, "q3", lambda r: bad_radius)) == "sat" # collinear points cannot be given a circle assert status(cert_for(sol, "q4", lambda r: res)) == "sat" def test_tampered_tangents_are_rejected(sol): res = sol.results["q5"][1] moved = SurdPoint(res.contact_points[0].x + 1, res.contact_points[0].y) assert status(cert_for(sol, "q5", lambda r: replace(r, contact_points=(moved, r.contact_points[1])))) == "sat" assert status(cert_for(sol, "q5", lambda r: replace(r, kind="none", contact_points=()))) == "sat" # an interior point cannot have the tangents of an exterior one assert status(cert_for(sol, "q6", lambda r: res)) == "sat" def test_tampered_centers_are_rejected(sol): assert status(cert_for(sol, "q8", lambda r: replace(r, orthocenter=Point(r.orthocenter.x, r.orthocenter.y + 1)))) == "sat" assert status(cert_for(sol, "q8", lambda r: replace(r, centroid=Point(r.centroid.x + 1, r.centroid.y)))) == "sat" assert status(cert_for(sol, "q8", lambda r: replace(r, circumcenter=Point(0, 0)))) == "sat" def test_tampered_power_secants_and_image_are_rejected(sol): assert status(cert_for(sol, "q10", lambda r: replace(r, power=r.power + 1))) == "sat" assert status(cert_for(sol, "q10", lambda r: replace(r, position="inside"))) == "sat" assert status(cert_for(sol, "q12", lambda r: replace(r, equal=False))) == "sat" assert status(cert_for(sol, "q12", lambda r: replace(r, first=replace(r.first, product=r.first.product + 1)))) == "sat" assert status(cert_for(sol, "q14", lambda r: replace(r, image=Point(r.image.x + 1, r.image.y)))) == "sat" def test_negative_controls_for_the_queries_q7_q9_and_q11(sol): """The three queries that the first suite of negative controls left out.""" res7 = sol.results["q7"][1] moved = SurdPoint(res7.contact_points[0].x + 1, res7.contact_points[0].y) assert status(cert_for(sol, "q7", lambda r: replace(r, contact_points=(moved, r.contact_points[1])))) == "sat" # a payload whose kind contradicts its own contact points gets no certificate at all with pytest.raises(CertificationError): cert_for(sol, "q7", lambda r: replace(r, kind="one", contact_points=r.contact_points[:1])) assert status(cert_for(sol, "q7", lambda r: replace(r, kind="none", contact_points=()))) == "sat" # collinear A, B, D have no concurrency points at all, so the centers of the # genuine triangle A, B, C cannot be reused here: the body is refuted, and the # premise part of that certificate has no solution either, which the arbitrator # reports as a vacuous premise instead of a proof c9 = cert_for(sol, "q9", lambda r: sol.results["q8"][1]) assert status(c9) == "sat" and premise_status(c9) == "unsat" assert status(cert_for(sol, "q11", lambda r: replace(r, power=r.power + 7))) == "sat" assert status(cert_for(sol, "q11", lambda r: replace(r, position="outside"))) == "sat" def test_missing_secant_certificate_names_the_line_that_misses(sol): cert = certify_query("q13", sol.queries["q13"], sol) assert isinstance(sol.results["q13"][1], NoResult) assert cert.hypotheses == [] and cert.declarations == ["(declare-const t1 Real)"] assert len(cert.assertions) == 1 and cert.assertions[0].startswith("(assert (= ") assert "(0, 1)" in cert.claim and status(cert) == "unsat" def test_missing_secant_and_unsupported_cases(sol): assert isinstance(sol.results["q13"][1], NoResult) assert status(certify_query("q13", sol.queries["q13"], sol)) == "unsat" bad = type(sol)(points=sol.points, lines=sol.lines, circles=sol.circles, transforms=sol.transforms, results={"q0": ("circumcircle", NoResult("x"))}, queries={"q0": parse_problem("Find the circumcircle of A, A and B.").args[0].args[0]}) with pytest.raises(CertificationError): certify_query("q0", bad.queries["q0"], bad)