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 (ExecFloat.Binary 8 23), we re-run the endpoint equalities under ExecFloat.Binary 8 23 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 $[\mathrm{lo},\mathrm{hi}]$, using local coordinate $t=x-\mathrm{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_0,a_1,\ldots,a_d]$ representing:

          $$ a_0+a_1t+a_2t^2+\cdots+a_dt^d. $$

          This helper is generic so we can reuse it for Rat (exact checking) and for ExecFloat.Binary 8 23 (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

                Require an array entry at a certificate-checking boundary, with a schema-oriented error.

                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 with no tolerances, so a passing check means the certificate's equalities hold exactly 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 ExecFloat.Binary 8 23 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:

                        • FloatLib rounds the rational outward in its binary32 model; packing that model preserves its bits. We accept only when the lower and upper rounding compare equal.
                        Instances For

                          Check the same endpoint equalities as checkCertificateRat, but under ExecFloat.Binary 8 23 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 ExecFloat.Binary 8 23,
                          • comparisons use IEEE-style BEq (so +0 == -0, and NaNs never compare equal).
                          Instances For

                            Check a piecewise-polynomial certificate stored as JSON, including ExecFloat.Binary 8 23 semantics.

                            Instances For

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

                              Instances For