Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/idris2-abi-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ on:
- 'src/idris/**'
- 'tests/abi/**'
- 'scripts/abi-fixtures.sh'
- 'src/rust/coprocessor/types.rs'
- 'scripts/ci/check-coprocessor-tags.sh'
- '.github/workflows/idris2-abi-ci.yml'
# No paths-filter on pull_request: this job is a REQUIRED status check, so it
# must report on every PR (a path-filtered required check deadlocks PRs that
Expand Down Expand Up @@ -148,6 +150,10 @@ jobs:
fi
echo "no partial or covering escape under src/abi/"

- name: Coprocessor kind tags agree with the Rust enum
# Runs on every event: a Rust-only renumbering must not skip it.
run: bash scripts/ci/check-coprocessor-tags.sh

- name: Count modules and report
if: steps.detect.outputs.relevant == 'true'
run: |
Expand Down
47 changes: 47 additions & 0 deletions scripts/ci/check-coprocessor-tags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
#
# The FFI tag of each coprocessor kind is written twice: as an explicit
# discriminant of the Rust `CoprocessorKind` (src/rust/coprocessor/types.rs)
# and as a clause of `coprocessorKindToU8` in src/abi/Coprocessor/Foreign.idr.
# Neither compiler sees the other file, so this compares the two lists as
# "Name=tag" pairs. It fails if either list is empty, if a Rust variant has no
# explicit discriminant, or if the pairs differ.
set -euo pipefail
root=${1:-.}
rs="$root/src/rust/coprocessor/types.rs"
idr="$root/src/abi/Coprocessor/Foreign.idr"

body=$(awk '/^pub enum CoprocessorKind \{/{f=1;next} f&&/^\}/{exit} f' "$rs")
# Every variant line must be exactly `Name = <decimal>` (comma optional). Anything else (no
# discriminant, a hex or underscored literal, an expression) is refused rather
# than half-parsed: a regex that stopped at the first digits would read
# `Math = 0x10` as Math=0 and pass a real mismatch.
variants=$({ printf '%s\n' "$body" | grep -E '^[[:space:]]+[A-Z]' || true; })
odd=$({ printf '%s\n' "$variants" | grep -vE '^[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+,?[[:space:]]*$' || true; })
if [[ -n "$odd" ]]; then
echo "::error file=$rs::CoprocessorKind variants must read 'Name = <decimal>' (comma optional); these do not:"
printf '%s\n' "$odd"
exit 1
fi
rust=$(printf '%s\n' "$variants" | sed -E 's/,?[[:space:]]*$//' | tr -d ' ' | sort)
clauses=$({ grep -E '^coprocessorKindToU8[[:space:]]+[A-Z]' "$idr" || true; })
odd=$({ printf '%s\n' "$clauses" | grep -vE '^coprocessorKindToU8[[:space:]]+[A-Z][A-Za-z0-9]*[[:space:]]*=[[:space:]]*[0-9]+[[:space:]]*$' || true; })
if [[ -n "$clauses" && -n "$odd" ]]; then
echo "::error file=$idr::coprocessorKindToU8 clauses must read 'coprocessorKindToU8 Name = <decimal>'; these do not:"
printf '%s\n' "$odd"
exit 1
fi
idris=$(printf '%s\n' "$clauses" | sed -E 's/^coprocessorKindToU8[[:space:]]+//' | tr -d ' ' | sed '/^$/d' | sort)

if [[ -z "$rust" ]] || [[ -z "$idris" ]]; then
echo "::error::could not read the tags (rust: ${rust:+found}${rust:-none}, idris: ${idris:+found}${idris:-none})"
exit 1
fi
if [[ "$rust" != "$idris" ]]; then
echo "::error file=$idr::coprocessorKindToU8 disagrees with the Rust CoprocessorKind discriminants"
diff <(printf '%s\n' "$rust") <(printf '%s\n' "$idris") | sed 's/^</rust /;s/^>/idris/' || true
exit 1
fi
echo "coprocessor tags agree: $(printf '%s\n' "$rust" | wc -l) kinds"
9 changes: 5 additions & 4 deletions src/abi/Coprocessor/Foreign.idr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
-- src/rust/coprocessor/mod.rs (Coprocessor trait surface)
-- src/ada/spark/coprocessor_safety.ads (SPARK trust-tier invariant)
--
-- Phase 0 declares the Math kind only. Phase 6 backends (Physics, DSP,
-- FPGA, Tensor, Vector, Crypto, Graphics, Audio, IO) extend this module
-- alongside their Rust impls — never as stubs.
-- It declares all sixteen kinds the Rust enum declares, with the same u8 tags
-- (scripts/ci/check-coprocessor-tags.sh checks the two agree). A new kind is
-- added here alongside its Rust impl, with the next tag — never as a stub.
--
-- NO believe-me anywhere. All safety is enforced by types.

Expand Down Expand Up @@ -93,7 +93,8 @@ encodeDecodeId PureFormal = Refl
||| ABI surface is unconditional. Phase 2b adds the subprocess CAS
||| backends PariGp / Maxima / Singular / Gap / Macaulay2. The order and the
||| tags follow the declaration order of `CoprocessorKind` in
||| `src/rust/coprocessor/types.rs`, which carries no explicit discriminants.
||| `src/rust/coprocessor/types.rs`, whose explicit `#[repr(u8)]` discriminants
||| equal these tags (scripts/ci/check-coprocessor-tags.sh).
public export
data CoprocessorKind : Type where
Math : CoprocessorKind -- Phase 0 (num-bigint + num-integer)
Expand Down
38 changes: 22 additions & 16 deletions src/rust/coprocessor/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,88 @@ use serde::{Deserialize, Serialize};
/// Only variants with a fully-functional backend are present. Phase 6A
/// adds Vector / Tensor / Crypto / Physics; Phase 6B will add DSP / FPGA /
/// Graphics / Audio / IO together with their implementations.
///
/// Discriminants are explicit and `#[repr(u8)]`: they are the FFI tags that
/// `src/abi/Coprocessor/Foreign.idr` (`coprocessorKindToU8`) mirrors, and
/// `scripts/ci/check-coprocessor-tags.sh` fails CI if the two disagree.
/// Append new kinds with the next number; never renumber.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u8)]
pub enum CoprocessorKind {
/// Number theory, exact arithmetic, polynomial algebra over Q.
/// Native Rust impl backed by `num-bigint` / `num-integer` / `num-rational`.
Math,
Math = 0,

/// Dense vector arithmetic (auto-vectorised f64 loops). Backs
/// SIMD-batched ops for the GNN inference path and aggregate reasoning.
Vector,
Vector = 1,

/// Dense tensor / matrix linear algebra via `nalgebra`. Backs node-
/// embedding aggregation, Pareto-frontier dominance computations, and
/// the matrix kernels that cross-prover certificate exchange depends on.
Tensor,
Tensor = 2,

/// Cryptographic primitives — hashing, signature verification.
/// Pairs with the future EasyCrypt / Tamarin / ProVerif backends as
/// the *computational* (vs symbolic) crypto surface.
Crypto,
Crypto = 3,

/// Numerical integration of ODE / Hamiltonian systems. Pairs with the
/// future KeYmaera X backend as a sanity-check oracle for hybrid-system
/// trajectories proven correct symbolically.
Physics,
Physics = 4,

/// FLINT integer and polynomial algebra — the only CAS backend that links
/// the library in-process rather than shelling out. Requires
/// `--features flint` and a system FLINT (LGPL-3) installation.
/// Enabled by `CoprocessorFactory::native`; absent without the feature.
FlintMath,
FlintMath = 5,

/// Digital signal processing — FFT/IFFT, window functions. Backed by
/// `rustfft`. Pairs with the Tamarin / ProVerif backends for proofs
/// about communication channels and signal-bearing protocols.
Dsp,
Dsp = 6,

/// File and stream I/O. Async via `tokio::fs`. Backs proof-artifact
/// handling — read a Coq .v file, hash it, line-count it.
Io,
Io = 7,

/// 2-D graphics — emits SVG strings. No GPU dependency. Backs the
/// proof-graph visualisation that the GNN-guided search consumes,
/// and renders Pareto-frontier diagnostics.
Graphics,
Graphics = 8,

/// Audio synthesis — generates PCM / WAV. Backs the completion-chime
/// machinery and any audio-signal proof obligations (rare but real
/// for cyber-physical KeYmaera X targets that involve audible signals).
Audio,
Audio = 9,

/// FPGA / hardware verification — subprocess to yosys. Trust tier
/// `ExternalSubprocess` (Tier 1). Backs proofs about hardware modules
/// expressed in Verilog / SystemVerilog.
Fpga,
Fpga = 10,

// ── Phase 2b: subprocess CAS backends (ExternalSubprocess / Tier 1) ──
/// PARI/GP number theory — subprocess to `gp`. Advanced factoring (ECM /
/// GNFS), multiplicative order, next-prime queries. PARI is the gold
/// standard for algorithmic number theory.
PariGp,
PariGp = 11,

/// Maxima symbolic algebra — subprocess to `maxima`. Simplification,
/// polynomial factoring, symbolic differentiation. Free CAS, GPL.
Maxima,
Maxima = 12,

/// Singular polynomial algebra — subprocess to `Singular`. Gröbner bases,
/// ideal membership, Krull dimension over arbitrary fields. GPL.
Singular,
Singular = 13,

/// GAP group theory — subprocess to `gap`. Permutation group order,
/// abelianness test, symmetric group order. GPL.
Gap,
Gap = 14,

/// Macaulay2 algebraic geometry — subprocess to `M2`. Gröbner bases,
/// Krull dimension, degree of quotient rings over QQ. GPL.
Macaulay2,
Macaulay2 = 15,
}

/// A request dispatched to a coprocessor.
Expand Down
Loading