verify-local-zeros.py

back to table · edit · history · where entries came from · files · download

17848 bytes, as of the version from 2026-11-01 21:35 (current). Recorded here, not run.

"""Certify only the stored rectangles of zeros of zeta'(s), using SageMath.

    sage -pip install numberdb
    sage -python verify-local-zeros.py snapshot.json

The input is a NumberDB table export. Each decimal component means its exact
decimal centre plus or minus one unit in its own last written place. All
acceptance comparisons use exact rationals, never floating-point residuals.
This program prints JSON, writes nothing, accesses no API, and neither locates
additional roots nor proves a global rank or completeness of the input list.

For the exact rectangle R with centre m, let f=zeta', and fix a nonzero exact
dyadic complex Y approximating 1/f'(m). Arb encloses f(m) and f'(R). With
D=1-Y*f'(R), q=sup|Re(D)|+sup|Im(D)| bounds the real infinity-norm derivative
of g(z)=z-Y*f(z). Require q<1 and strict componentwise inclusion in R of
K=m-Y*f(m)+D*(R-m). On the convex rectangle, integration along straight lines
gives g(R) subset K. Banach's fixed-point theorem then proves exactly one
zero of f in R. The q bound also excludes f'=0 there, so the zero is simple.
The only pole of zeta' is excluded from the entire Arb evaluation rectangle.

The Arb rectangles can be outward enlargements of the exact decimal input;
their containment is checked. Inclusion is always tested against the original
exact decimal rectangle, not against this enlargement. JSON decimal bounds
are rounded outward using exact integer floor/ceiling; Y is recorded exactly.
"""

import argparse
import hashlib
import json
import os
import platform
import re
import sys
import time
from datetime import datetime, timezone
from decimal import Decimal
from fractions import Fraction
from pathlib import Path

import numberdb.sage as numberdb
from sage.rings.complex_arb import ComplexBallField
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField
from sage.version import version as sage_version


DECIMAL_PATTERN = r"[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?"
COMPLEX_PATTERN = re.compile(
    r"^\s*(" + DECIMAL_PATTERN + r")\s*\+\s*i\s*\*\s*("
    + DECIMAL_PATTERN + r")\s*$")


class CertificateRejected(Exception):
    def __init__(self, reason, details=None):
        super().__init__(reason)
        self.reason = reason
        self.details = details or {}


def require(condition, reason, details=None):
    if not condition:
        raise CertificateRejected(reason, details)


def finite(value, label):
    require(value.real().is_finite() and value.imag().is_finite(),
            "nonfinite " + label)
    return value


def real_bounds(value):
    require(value.is_finite(), "nonfinite real endpoint")
    return value.lower().exact_rational(), value.upper().exact_rational()


def component_bounds(value):
    finite(value, "complex enclosure")
    return (real_bounds(value.real()), real_bounds(value.imag()))


def contains_zero(value):
    return all(lower <= 0 <= upper for lower, upper in component_bounds(value))


def decimal_bound(value, upwards, places=60):
    scale = QQ(10)**places
    scaled = value * scale
    integer = int(scaled.ceil() if upwards else scaled.floor())
    sign = "-" if integer < 0 else ""
    digits = str(abs(integer)).rjust(places + 1, "0")
    return sign + digits[:-places] + "." + digits[-places:]


def written_bounds(pair, places=60):
    return [decimal_bound(pair[0], False, places),
            decimal_bound(pair[1], True, places)]


def decimal_component(text):
    require("." in text or "e" in text.lower(),
            "an exact integer has no implied last-place uncertainty")
    decimal = Decimal(text)
    require(decimal.is_finite(), "nonfinite stored decimal")
    return (QQ(Fraction(decimal)), QQ(10)**decimal.as_tuple().exponent,
            len(decimal.as_tuple().digits))


def parse_rectangle(text):
    match = COMPLEX_PATTERN.fullmatch(text)
    require(match is not None, "unsupported stored complex format")
    real_centre, real_radius, real_digits = decimal_component(match.group(1))
    imag_centre, imag_radius, imag_digits = decimal_component(match.group(2))
    return ((real_centre, imag_centre), (real_radius, imag_radius),
            (real_digits, imag_digits))


def arb_rectangle(field, centre, radii):
    real_field = RealBallField(field.precision())
    components = [real_field(centre_value).add_error(real_field(radius).upper())
                  for centre_value, radius in zip(centre, radii)]
    rectangle = finite(field(*components), "evaluation rectangle")
    for pair, centre_value, radius in zip(component_bounds(rectangle), centre, radii):
        require(radius > 0, "nonpositive rectangle radius")
        require(pair[0] <= centre_value - radius <= centre_value + radius <= pair[1],
                "Arb rectangle does not enclose the exact input rectangle")
    return rectangle


def local_certificate(centre, radii, field, function, derivative, fixed_inverse=None):
    rectangle = arb_rectangle(field, centre, radii)
    rectangle_bounds = component_bounds(rectangle)
    pole_in_rectangle = (rectangle_bounds[0][0] <= 1 <= rectangle_bounds[0][1]
                         and rectangle_bounds[1][0] <= 0 <= rectangle_bounds[1][1])
    require(not pole_in_rectangle, "evaluation rectangle contains the pole at 1")
    midpoint = finite(field(*centre), "exact-centre enclosure")
    function_midpoint = finite(function(midpoint), "f(m)")
    derivative_midpoint = finite(derivative(midpoint), "f'(m)")
    if fixed_inverse is None:
        require(not contains_zero(derivative_midpoint), "f'(m) may vanish")
        inverse_ball = finite(1 / derivative_midpoint, "inverse derivative")
        inverse_coordinates = tuple(component.mid().exact_rational()
                                    for component in (inverse_ball.real(), inverse_ball.imag()))
    else:
        inverse_coordinates = tuple(QQ(value) for value in fixed_inverse)
    require(any(value != 0 for value in inverse_coordinates), "fixed Y is zero")
    inverse = finite(field(*inverse_coordinates), "fixed Y")
    require(component_bounds(inverse) == tuple((value, value)
                                              for value in inverse_coordinates),
            "Y is not represented as an exact fixed complex constant")
    derivative_rectangle = finite(derivative(rectangle), "f'(R)")
    derivative_factor = finite(1 - inverse * derivative_rectangle, "1-Y*f'(R)")
    contraction = sum(max(abs(lower), abs(upper))
                      for lower, upper in component_bounds(derivative_factor))
    require(contraction < 1, "contraction bound is not strictly below one",
            {"contraction_upper": decimal_bound(contraction, True)})
    displacement = arb_rectangle(field, (QQ(0), QQ(0)), radii)
    image = finite(midpoint - inverse * function_midpoint
                   + derivative_factor * displacement, "Krawczyk image")
    image_bounds = component_bounds(image)
    offsets = [((lower - centre_value) / radius, (upper - centre_value) / radius)
               for (lower, upper), centre_value, radius
               in zip(image_bounds, centre, radii)]
    offset_text = {axis: written_bounds(pair, places=60)
                   for axis, pair in zip(("real", "imaginary"), offsets)}
    require(all(-1 < lower <= upper < 1 for lower, upper in offsets),
            "Krawczyk image is not strictly inside the stored rectangle",
            {"image_offsets_in_stored_ulps": offset_text})
    minimum_margin = min(min(1 + lower, 1 - upper) for lower, upper in offsets)
    contraction_text = decimal_bound(contraction, True)
    require(QQ(Fraction(Decimal(contraction_text))) < 1,
            "rounded evidence contraction bound is too coarse")
    for pair in offset_text.values():
        require(-1 < Fraction(Decimal(pair[0])) <= Fraction(Decimal(pair[1])) < 1,
                "rounded evidence inclusion bounds are too coarse")
    return {
        "certified": True,
        "conclusion": "exactly one simple zero in the stored decimal rectangle",
        "fixed_Y_exact_rationals": {
            axis: str(value) for axis, value
            in zip(("real", "imaginary"), inverse_coordinates)},
        "f_at_exact_centre_bounds": {
            axis: written_bounds(pair) for axis, pair
            in zip(("real", "imaginary"), component_bounds(function_midpoint))},
        "f_prime_rectangle_bounds": {
            axis: written_bounds(pair) for axis, pair
            in zip(("real", "imaginary"), component_bounds(derivative_rectangle))},
        "contraction_upper": contraction_text,
        "image_offsets_in_stored_ulps": offset_text,
        "minimum_interior_margin_in_stored_ulps_lower": decimal_bound(
            minimum_margin, False),
        "evaluation_rectangle_contains_exact_input": True,
        "evaluation_rectangle_excludes_pole": True,
        "all_evaluations_finite": True,
    }


def zeta_derivative(value):
    return value.zetaderiv(1)


def zeta_second_derivative(value):
    return value.zetaderiv(2)


def rejected_control(name, operation, reason):
    try:
        operation()
    except CertificateRejected as problem:
        require(reason in problem.reason, "unexpected control rejection", {
            "control": name, "expected": reason, "actual": problem.reason})
        return {"name": name, "passed": True, "rejection": problem.reason,
                "details": problem.details}
    raise CertificateRejected("invalid control was accepted", {"control": name})


def controls(field, first_rectangle):
    centre = (QQ(2), QQ(3))
    radii = (QQ(1) / 10**20, QQ(1) / 10**19)
    linear = local_certificate(centre, radii, field,
                               lambda value: value - field(*centre),
                               lambda value: field(1))
    require(Decimal(linear["contraction_upper"]) == 0, "linear control is not exact")
    nonzero = finite(zeta_derivative(field(2)), "known nonzero zeta'(2)")
    require(real_bounds(nonzero.real())[1] < 0, "zeta'(2) was not negative")
    output = [
        {"name": "f(z)=z-(2+3i), known unique root", "passed": True,
         "contraction_upper": linear["contraction_upper"]},
        {"name": "zeta'(2)<0 by its absolutely convergent Dirichlet series",
         "passed": True, "real_bounds": written_bounds(real_bounds(nonzero.real()))},
    ]
    original_centre, original_radii, unused_digits = first_rectangle
    wrong_centre = (original_centre[0] + QQ(1) / 10, original_centre[1])
    wrong_box_value = finite(zeta_derivative(arb_rectangle(field, wrong_centre,
                                                          original_radii)),
                             "wrong-box direct evaluation")
    require(not contains_zero(wrong_box_value), "wrong-box exclusion is inconclusive")
    output.append(rejected_control(
        "first box shifted by +1/10 in the real direction; f(box) excludes zero",
        lambda: local_certificate(wrong_centre, original_radii, field,
                                  zeta_derivative, zeta_second_derivative),
        "not strictly inside"))
    output.append(rejected_control(
        "nonfinite derivative", lambda: local_certificate(
            centre, radii, field, lambda value: value - field(*centre),
            lambda value: field("nan")), "nonfinite"))
    output.append(rejected_control(
        "finite but noncontracting map", lambda: local_certificate(
            centre, radii, field, lambda value: field(0), lambda value: field(0),
            fixed_inverse=(1, 0)), "contraction bound"))
    output.append(rejected_control(
        "zero fixed preconditioner", lambda: local_certificate(
            centre, radii, field, lambda value: value - field(*centre),
            lambda value: field(1), fixed_inverse=(0, 0)), "fixed Y is zero"))
    return output


def resource_limits(required):
    def read_first(*paths):
        for location in paths:
            path = Path(location)
            if path.exists():
                return path.read_text().strip()
        return None

    memory = read_first("/sys/fs/cgroup/memory.max",
                        "/sys/fs/cgroup/memory/memory.limit_in_bytes")
    cpu = read_first("/sys/fs/cgroup/cpu.max")
    if cpu:
        quota, period = cpu.split()
    else:
        quota = read_first("/sys/fs/cgroup/cpu/cpu.cfs_quota_us")
        period = read_first("/sys/fs/cgroup/cpu/cpu.cfs_period_us")
    network = sorted(os.listdir("/sys/class/net")) if Path("/sys/class/net").exists() else []
    if required:
        require(memory not in (None, "max") and 0 < int(memory) <= 1_500_000_000,
                "memory limit exceeds 1.5 GB")
        require(quota not in (None, "max") and period is not None
                and 0 < int(quota) <= int(period), "CPU limit exceeds one CPU")
        require(network == ["lo"], "container network is not disabled")
    return {"memory_limit_bytes": memory, "cpu_quota": quota, "cpu_period": period,
            "network_interfaces": network}


def main(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("table_json", type=Path)
    parser.add_argument("--bits", type=int, default=512)
    parser.add_argument("--only", action="append", help="check only these existing entry IDs")
    parser.add_argument("--expected-sha256")
    parser.add_argument("--require-container-limits", action="store_true")
    options = parser.parse_args(argv)
    require(options.bits >= 128, "working precision must be at least 128 bits")
    started = time.monotonic()
    environment_limits = resource_limits(options.require_container_limits)
    source = options.table_json.read_bytes()
    source_hash = hashlib.sha256(source).hexdigest()
    if options.expected_sha256:
        require(source_hash == options.expected_sha256, "snapshot hash differs")
    document = json.loads(source)
    numbers = document["Numbers"]
    require(isinstance(numbers, dict) and numbers, "no stored entries")
    requested = set(options.only) if options.only else set(numbers)
    require(requested <= set(numbers), "requested ID is not in the snapshot")
    entries = [(identity, record["number"] if isinstance(record, dict) else record)
               for identity, record in numbers.items() if identity in requested]
    field = ComplexBallField(options.bits)
    control_results = controls(field, parse_rectangle(entries[0][1]))
    results = []
    exact_rectangles = []
    for identity, text in entries:
        result = {"entry_id": identity, "stored_number": text}
        try:
            centre, radii, digits = parse_rectangle(text)
            require(centre[1] - radii[1] > 0, "stored rectangle is not in upper half-plane")
            certificate = local_certificate(centre, radii, field,
                                            zeta_derivative, zeta_second_derivative)
            result.update(certificate)
            result["component_significant_digits"] = dict(zip(("real", "imaginary"), digits))
            result["stored_component_ulps_exact"] = dict(zip(
                ("real", "imaginary"), map(str, radii)))
            exact_rectangles.append((centre, radii))
        except CertificateRejected as problem:
            result.update({"certified": False, "failure": problem.reason,
                           "details": problem.details})
        results.append(result)
    certified = [result for result in results if result["certified"]]
    increasing_disjoint_ordinates = all(
        earlier[0][1] + earlier[1][1] < later[0][1] - later[1][1]
        for earlier, later in zip(exact_rectangles, exact_rectangles[1:]))
    require(hashlib.sha256(options.table_json.read_bytes()).hexdigest() == source_hash,
            "snapshot changed during verification")
    output = {
        "status": "passed" if len(certified) == len(entries) else "failed",
        "finished_at_utc": datetime.now(timezone.utc).isoformat(),
        "elapsed_seconds": round(time.monotonic() - started, 3),
        "snapshot_sha256": source_hash,
        "verifier_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
        "environment": {"python": platform.python_version(), "sage": sage_version,
                        "numberdb": numberdb.__version__, "working_bits": options.bits,
                        "docker_image_id": os.environ.get("T387_DOCKER_IMAGE_ID"),
                        **environment_limits},
        "summary": {
            "stored_entries": len(numbers), "checked": len(entries), "certified": len(certified),
            "failed": len(entries) - len(certified),
            "all_stored_entries_checked": requested == set(numbers),
            "max_contraction_upper": max(
                (result["contraction_upper"] for result in certified), key=Decimal, default=None),
            "minimum_interior_margin_in_stored_ulps_lower": min(
                (result["minimum_interior_margin_in_stored_ulps_lower"]
                 for result in certified), key=Decimal, default=None),
            "certified_rectangles_have_increasing_disjoint_ordinates": increasing_disjoint_ordinates,
        },
        "controls": control_results,
        "certificates": results,
        "scope": "Local existence and uniqueness only. Entry n labels the supplied located list; no global zero count, completeness or global rank is certified.",
        "provenance": "Fresh checks of the supplied snapshot, independent of any recovered historical generator or claimed historical computation.",
        "published_numbers_changed": False,
        "api_writes": 0,
    }
    print(json.dumps(output, indent=2))
    return 0 if output["status"] == "passed" else 1


if __name__ == "__main__":
    try:
        sys.exit(main())
    except CertificateRejected as problem:
        print(json.dumps({"status": "failed", "failure": problem.reason,
                          "details": problem.details}, indent=2))
        sys.exit(1)