TorchLean API

NN.Verification.Splines.PiecewisePolyCert

Piecewise polynomial certificates #

This module implements a Lean checker for one-dimensional piecewise-polynomial certificates.

Why this exists:

Our checker is conservative: it checks exact equalities over Rat with no floating-point tolerance.

Optionally, you can also run an executable float32 cross-check via checkJsonIEEE32ExecExact: if every rational in the certificate is exactly representable as a finite IEEE-754 binary32 (IEEE32Exec), we re-run the endpoint equalities under IEEE32Exec arithmetic. This is useful when the producer is actually operating in float32 and you want to confirm the certificate’s equalities hold under the same semantics.

What is checked in format = "piecewise_poly_v0":

This certificate format focuses on endpoint and piece consistency. It does not claim global range bounds for higher-degree polynomials; those require additional machinery such as Bernstein bounds or interval arithmetic with derivative-based splitting.

References (background):

Utilities: exact rationals in JSON #

Parse a rational in the format emitted by TorchLean’s Arb helpers:

  • integer: "5", "-3"
  • fraction: "5/2", "-7/10"

We avoid JSON numbers here because they are stored as Scientific and are not guaranteed to round-trip exactly for large integers.

Instances For

    Parse a Rat from a JSON string field with context.

    Instances For

      Certificate schema #

      One polynomial segment on an interval [lo, hi], using local coordinate t = x - lo.

      Instances For

        Parsed certificate payload for piecewise_poly_v0.

        degree is redundant (it should equal coeffs.size - 1), but it is useful for fast validation.

        Instances For

          Polynomial evaluation #

          def NN.Verification.Splines.PiecewisePolyCert.evalPolyHorner {α : Type} [Zero α] [Add α] [Mul α] (coeffs : Array α) (t : α) :
          α

          Evaluate a polynomial in local coordinate t using Horner’s rule.

          Given coefficients [a₀, a₁, …, a_d] representing:

          a₀ + a₁ t + a₂ t^2 + … + a_d t^d.

          This helper is generic so we can reuse it for Rat (exact checking) and for IEEE32Exec (executable float32 semantics).

          Instances For

            Parsing #

            Parse an array of rational strings, attaching ctx to any error message.

            Instances For

              Parse one polynomial segment from the certificate JSON object.

              Instances For

                Parse the piecewise_poly_v0 JSON payload into a structured certificate.

                Instances For

                  Checking #

                  Check a piecewise-polynomial certificate exactly over Rat.

                  This is the smallest checker core for this format: it uses exact rational arithmetic (no tolerances), so a passing check means the certificate’s equalities are literally true as stated.

                  Instances For

                    Check a piecewise_poly_v0 certificate payload.

                    Raises IO.userError on the first mismatch; prints a short success message otherwise.

                    Instances For

                      Attempt to convert a rational to an exact finite IEEE32Exec value.

                      This is used for optional “float32 semantics” checking: we only accept values that are exactly representable on the binary32 grid, so equality checks are meaningful at the executable IEEE level.

                      Implementation note:

                      • We reuse TorchLean’s existing Rat → IEEE32Exec outward-rounding helpers (roundRatQDown/roundRatQUp), and we accept only when the lower/upper rounding coincide.
                      Instances For

                        Check the same endpoint equalities as checkCertificateRat, but under IEEE32Exec arithmetic.

                        This is an additional (optional) check that answers: “if we interpret the certificate data as binary32 values and run the polynomial evaluation with executable IEEE-754 ops, do we still hit the claimed endpoints?”

                        The check is deliberately strict:

                        • every rational in the cert must be exactly representable as a finite IEEE32Exec,
                        • comparisons use IEEE-style BEq (so +0 == -0, and NaNs never compare equal).
                        Instances For

                          Check a piecewise-polynomial certificate stored as JSON, including IEEE32Exec semantics.

                          Instances For

                            Check a piecewise-polynomial certificate stored as a JSON file.

                            Instances For