A formally verified mathematical library written in SPARK/Ada, providing common mathematical functions for both integer and fixed-point types with compile-time proof of correctness.
| Function | Description | Complexity |
|---|---|---|
Sqrt |
Integer square root (floor of √N) | O(log N) |
Power |
Exponentiation with overflow protection | O(Exp) |
GCD |
Greatest common divisor (Euclidean algorithm) | O(log min(A,B)) |
Max |
Maximum of two values | O(1) |
Min |
Minimum of two values | O(1) |
| Function | Description | Complexity |
|---|---|---|
Sqrt |
Square root approximation (within 'Small) |
O(log(N/Small)) |
Max |
Maximum of two values | O(1) |
Min |
Minimum of two values | O(1) |
The goal is to provide SPARK-verified equivalents of Ada.Numerics.Generic_Elementary_Functions for fixed-point and integer types.
| Category | Functions | Status |
|---|---|---|
| Basic | Sqrt |
✅ Implemented |
| Exponential | Log, Log(Base), Exp, ** |
⬚ Planned |
| Trigonometric | Sin, Cos, Tan, Cot |
⬚ Planned |
| Inverse Trig | Arcsin, Arccos, Arctan, Arccot |
⬚ Planned |
| Hyperbolic | Sinh, Cosh, Tanh, Coth |
⬚ Planned |
| Inverse Hyperbolic | Arcsinh, Arccosh, Arctanh, Arccoth |
⬚ Planned |
Unlike floating-point implementations, fixed-point math requires:
- CORDIC algorithms for trigonometric functions (no FPU dependency)
- Polynomial approximations with bounded error analysis
- Range reduction techniques proven correct in SPARK
- Cycle parameter support for degree/radian flexibility
Contributions for any of these functions are welcome!
Add the dependency to your project:
alr with spark_mathOr add it manually to your alire.toml:
[[depends-on]]
spark_math = "^0.1.0"Clone the repository and add it to your GPR project path:
git clone https://github.com/heziode/spark_math.gitIn your .gpr file:
with "spark_math.gpr";with Spark_Math.Integer;
procedure Example is
-- Instantiate for your integer type
package My_Math is new Spark_Math.Integer (Integer);
Result : Integer;
begin
-- Square root (floor)
Result := My_Math.Sqrt (17); -- Returns 4
-- Exponentiation
Result := My_Math.Power (2, 10); -- Returns 1024
-- Greatest common divisor
Result := My_Math.GCD (48, 18); -- Returns 6
-- Max and Min
Result := My_Math.Max (5, 3); -- Returns 5
Result := My_Math.Min (5, 3); -- Returns 3
end Example;with Spark_Math.Fixed;
procedure Example is
-- Define your fixed-point type
type Voltage is delta 0.001 range 0.0 .. 100.0;
-- Instantiate for your type
package Voltage_Math is new Spark_Math.Fixed (Voltage);
Result : Voltage;
begin
-- Square root (approximation within 'Small)
Result := Voltage_Math.Sqrt (4.0); -- Returns ≈ 2.0
Result := Voltage_Math.Sqrt (2.0); -- Returns ≈ 1.414
-- Max and Min
Result := Voltage_Math.Max (3.5, 2.1); -- Returns 3.5
Result := Voltage_Math.Min (3.5, 2.1); -- Returns 2.1
end Example;This library is designed for formal verification with SPARK. All functions include:
- Preconditions (
Pre): Specify valid input constraints - Postconditions (
Post): Guarantee output properties - Loop invariants: Enable proof of loop correctness
- No runtime exceptions: All potential issues are caught at proof time
cd tests
alr exec -- gnatprove -P spark_math_tests.gpr --mode=gold proof.adbSPARK defines five proof levels:
| Level | Description |
|---|---|
| Stone | Valid SPARK code |
| Bronze | Initialization and correct data flow |
| Silver | Absence of run-time errors (AoRTE) |
| Gold | Proof of key integrity properties |
| Platinum | Full functional proof of requirements |
| Package | Level |
|---|---|
Spark_Math.Numerics_Core |
🥇 Gold |
Spark_Math.Integer |
🥇 Gold |
Spark_Math.Fixed |
🥇 Gold |
spark_math/
├── alire.toml # Alire package manifest
├── spark_math.gpr # Main GPR project file
├── src/
│ ├── spark_math.ads # Root package
│ ├── spark_math-numerics_core.ads/.adb # Generic core algorithms
│ ├── spark_math-integer.ads/.adb # Integer math package
│ └── spark_math-fixed.ads # Fixed-point math package
├── tests/
│ ├── alire.toml # Test project manifest
│ ├── spark_math_tests.gpr # Test GPR project file
│ └── src/
│ ├── test_runner.adb # AUnit test runner
│ ├── test_integer_math.ads/.adb # Integer math tests
│ └── test_fixed_math.ads/.adb # Fixed-point math tests
└── README.md
cd tests
alr runUnlike traditional runtime-checked libraries, Spark_Math provides:
- Compile-time guarantees: Proofs eliminate entire classes of bugs
- No runtime overhead: Checks are done at proof time, not runtime
- Overflow protection:
Powerfunction has preconditions preventing overflow - Division safety: All divisions are protected against divide-by-zero
- Formal documentation: Contracts serve as executable specifications
Contributions are welcome!
Please, follow the project's Ada style guide defined in STYLE_GUIDE.md.
This project is licensed under the Apache-2.0 WITH LLVM-exception license. See LICENSE for details.