Hi everyone,
Quick note: English is my second language, so I am using an AI to help translate this into proper technical English, as Spanish is my native tongue.
I wanted to share a complete architecture designed in Ada/SPARK for high-integrity cryptography. It is a secure, bias-free entropy conversion engine that maps raw random bytes (harvested via Windows ProcessPrng / BcryptGenRandom) into human-readable sequences.
Note on Architecture: The production package declares 14 specialized procedures for different charset variations. Structurally, all 14 procedures are divided into and implement one of the 3 core algorithmic patterns. The
Chain_Safe_Tokenprocedure in the.adssection below is provided as an explicit example of how these 14 entry points are specified.
The entire core codebase achieves 100% Proved status under gnatprove. It guarantees zero runtime exceptions, zero buffer overflows, zero division-by-zero, and proven loop termination. This formal math allows the implementation to safely strip runtime checks in production via -gnatp, unlocking performance ranges between 1100 MB/s and 1400 MB/s depending on the targeted character sets.
4. Summary of SPARK Analysis Results
Here is the full verification report generated by gnatprove. The engine achieved a flawless 100% Proved status across 925 total verification paths with absolutely zero unproved properties:
---------------------------------------------------------------------------------------------------------------------------------
SPARK Analysis results Total Flow CodePeer Provers Justified Unproved
---------------------------------------------------------------------------------------------------------------------------------
Data Dependencies 32 32 . . . .
Flow Dependencies 21 21 . . . .
Initialization 20 20 . . . .
Non-Aliasing . . . . . .
Run-time Checks 620 . . 620 (CVC4 89%, Trivial 7%, Z3 4%, colibri 0%) . .
Assertions 105 . . 105 (CVC4 97%, Trivial 2%, Z3 1%) . .
Functional Contracts 103 . . 103 (CVC4 89%, Trivial 2%, Z3 10%) . .
LSP Verification . . . . . .
Termination 24 . . 24 (CVC4) . .
Concurrency . . . . . .
---------------------------------------------------------------------------------------------------------------------------------
Total 925 73 (8%) . 852 (92%) . .
Max steps used for successful proof: 33855
Questions for Discussion
I would love to gather your feedback on this implementation, especially on two fronts:
- Type-Driven Design vs Explicit Contracts: Do you consider this strict subtype clamping approach cleaner than writing expansive runtime preconditions for dynamic charsets in SPARK?
- Timing Resiliency: Since the execution profile variations depend exclusively on the host hardware/kernel entropy rejection rates, do you agree this eliminates standard state-dependent timing vulnerabilities without needing full constant-time logic?
Thanks for reading, and I’m happy to dive deeper into other procedures if you’d like to look at the rest of the engine!
Empirical Performance Testing Strategy
To benchmark and audit the distribution, a verification suite (Gen_test) executes dynamic stress pipelines over billions of iterations. Here is an optimized look at how the high-throughput test harness populates the baseline character array under perfect power-of-2 distributions:
if Is_Perfect_RNG then
Start := Ada.Real_Time.Clock;
Outer_Loop :
for I in 1 .. Round loop
pragma Optimize (time);
Success := NTSTATUS'Last;
ProcessPrng_Public(Buffer => Rnd_Buffer,
Status => Success);
if Success /= 1 then
Success := NTSTATUS'Last;
BcryptGenRandom_Public(Buffer => Rnd_Buffer,
Status => Success);
if Success /= 0 then
return;
end if;
end if;
pragma Loop_Optimize (unroll);
for Idx in Rnd_Buffer'First .. Rnd_Buffer'Last loop
B(Rnd_Buffer(Idx)) := B(Rnd_Buffer(Idx)) + 1;
end loop;
end loop Outer_Loop;
---------------------------
Finish := Ada.Real_Time.Clock;
Architectural Specification (.ads Example)
Here is the structural design, enforcing cryptographic bounds and strict information-flow contracts right into the type system:
package Math_Functions with SPARK_Mode => On is
subtype mxb is Positive range 256 .. 256;
max_byte : constant mxb := 256;
subtype Byte_Length is Positive range 1 .. 256;
subtype Sesgo_Free is Positive range 128 .. 255;
subtype Noused is Positive range 1 .. 128;
subtype PerRecjt is Float range 0.39 .. 50.00;
function Byte_Division (Div : in Byte_Length) return Byte_Length
with
Global => Null,
Post => Byte_Division'Result = Byte_Length(max_byte / Div);
function Unbiased_Secure (Len : Byte_Length) return Sesgo_Free
with
Global => Null,
Post => Unbiased_Secure'Result = Sesgo_Free(Len * Byte_Division(Div => Len) - 1);
function Reject (Vod : Sesgo_Free) return Noused
with
Global => Null,
Post => Reject'Result = Noused(max_byte - Vod);
function Percent (Trh : in Noused) return PerRecjt
with
Global => Null,
Post => Percent'Result = PerRecjt(Float'Min(50.00, Float'Max(0.39, (Float(Trh) / Float(max_byte)) * 100.0)));
end Math_Functions;
subtype Safe_Token is String
with Dynamic_Predicate =>
(for all Simple in Safe_Token'Range =>
Safe_Token(Simple) in '0' .. '9' | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-');
-- Example of one of the 14 entry points defined in the package
procedure Chain_Safe_Token (Chain : in out Safe_Token;
Success : out NTSTATUS;
Works : in out Boolean;
Entropy : in out sub_entropy)
with
Global => (Input => Ada.Real_Time.Clock_Time),
Depends => (Chain => Chain,
Success => Chain,
Works =>+ Chain,
Entropy =>+ Chain,
null => Ada.Real_Time.Clock_Time),
Pre => (Chain'Length >= 1) and then (Chain'First = 1)
and then (Chain'Last = Chain'Length) and then (Chain'Length <= 10_000)
and then (for all Char in Chain'Range => Chain(Char) = '0')
and then Works = False
and then (Entropy = 0.0),
Post => (if Works then
(for all Char in Chain'Range => Chain(Char) in '0' .. '9' | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-')
else
(for all Char in Chain'Range => Chain(Char) = Chain'Old(Char)) and then Entropy = Entropy'Old);
The private logic relies on a static dynamic-sizing map (`Dynamic_Length`) ensuring that the underlying random arrays always carry a massive buffer margin, mathematically guaranteeing that the loops will never run out of entropy tokens.
------------------------------
-- Chain_Alphanumeric_Mixed --
------------------------------
procedure Chain_Alphanumeric_Mixed (Chain : in out Alphanumeric_Mixed;
Success : out NTSTATUS;
Works : in out Boolean;
Entropy : in out sub_entropy)
is
Chain_Set : Constant Alphanumeric_Mixed(1 .. 69) := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#%_*-";
New_Length : Constant Positive := Dynamic_Length(Len => Chain'Length);
Rnd_Buffer : BUFFER_RNG(0 .. BUFFER_RNG_LENGTH(Chain'Length + New_Length) - 1) := (others => 0);
Rnd_Len : size_t range Rnd_Buffer'First .. Rnd_Buffer'Length := Rnd_Buffer'First;
Chain_Len : Natural range 0 .. Chain'Length := 0;
Sesgo : Constant Sesgo_Free := Unbiased_Secure(Len => Chain_Set'Length);
subtype Unbiased is Interfaces.Unsigned_8 range 0 .. Interfaces.Unsigned_8(Sesgo);
begin
Success := NTSTATUS'Last;
ProcessPrng_Public(Buffer => Rnd_Buffer, Status => Success);
if Success /= 1 then
Success := NTSTATUS'Last;
BcryptGenRandom_Public(Buffer => Rnd_Buffer, Status => Success);
if Success /= 0 then
return;
else
Works := (Success = 0);
end if;
else
Works := (Success = 1);
end if;
while Chain_Len < Chain'Length and then Rnd_Len < Rnd_Buffer'Length loop
pragma Loop_Variant (Increases => Rnd_Len);
pragma Loop_Invariant (Chain_Len in 0 .. Chain'Length);
pragma Loop_Invariant (Rnd_Len in Rnd_Buffer'First .. Rnd_Buffer'Length);
if Rnd_Buffer (Rnd_Len) in Unbiased then
Chain_Len := Chain_Len + 1;
Chain (Chain_Len) := Chain_Set ((Natural (Rnd_Buffer(Rnd_Len)) mod Chain_Set'Length) + 1);
end if;
Rnd_Len := Rnd_Len + 1;
end loop;
Rnd_Buffer := (others => 0);
pragma Unreferenced(Rnd_Buffer);
Calculate_Shannon_Entropy(Item => Chain, Ento => Entropy);
end Chain_Alphanumeric_Mixed;
------------------------
-- Chain_Only_Numbers --
------------------------
procedure Chain_Only_Numbers (Chain : in out Only_Numbers;
Success : out NTSTATUS;
Works : in out Boolean;
Entropy : in out sub_entropy)
is
New_Length : Constant Positive := Dynamic_Length(Len => Chain'Length);
Rnd_Buffer : BUFFER_RNG(0 .. BUFFER_RNG_LENGTH(Chain'Length + New_Length) - 1) := (others => 0);
Rnd_Len : size_t range Rnd_Buffer'First .. Rnd_Buffer'Length := Rnd_Buffer'First;
Chain_Len : Natural range 0 .. Chain'Length := 0;
subtype Unbiased is Interfaces.Unsigned_8 range 0 .. 249;
begin
Success := NTSTATUS'Last;
ProcessPrng_Public(Buffer => Rnd_Buffer, Status => Success);
if Success /= 1 then
Success := NTSTATUS'Last;
BcryptGenRandom_Public(Buffer => Rnd_Buffer, Status => Success);
if Success /= 0 then
return;
else
Works := (Success = 0);
end if;
else
Works := (Success = 1);
end if;
while Chain_Len < Chain'Length and then Rnd_Len < Rnd_Buffer'Length loop
pragma Loop_Variant (Increases => Rnd_Len);
pragma Loop_Invariant (Chain_Len in 0 .. Chain'Length);
pragma Loop_Invariant (Rnd_Len in Rnd_Buffer'First .. Rnd_Buffer'Length);
if Rnd_Buffer(Rnd_Len) in Unbiased then
Chain_Len := Chain_Len + 1;
pragma Assert (Character'Val (Character'Pos('0') + Natural(Rnd_Buffer(Rnd_Len)) mod 10) in '0' .. '9');
Chain(Chain_Len) := Character'Val (Character'Pos('0') + (Natural (Rnd_Buffer(Rnd_Len)) mod 10));
end if;
Rnd_Len := Rnd_Len + 1;
end loop;
Rnd_Buffer := (others => 0);
pragma Unreferenced(Rnd_Buffer);
Calculate_Shannon_Entropy(Item => Chain, Ento => Entropy);
end Chain_Only_Numbers;
-------------------------
-- Chain_Hex_Uppercase --
-------------------------
procedure Chain_Hex_Uppercase (Chain : in out Hex_Uppercase;
Success : out NTSTATUS;
Works : in out Boolean;
Entropy : in out sub_entropy)
is
Chain_Set : Constant Hex_Uppercase(1 .. 16) := "0123456789ABCDEF";
New_Length : Constant Positive := Dynamic_Length(Len => Chain'Length);
Rnd_Buffer : BUFFER_RNG(0 .. BUFFER_RNG_LENGTH(Chain'Length + New_Length) - 1) := (others => 0);
Rnd_Len : size_t range Rnd_Buffer'First .. Rnd_Buffer'Length := Rnd_Buffer'First;
Chain_Len : Natural range 0 .. Chain'Length := 0;
begin
Success := NTSTATUS'Last;
ProcessPrng_Public(Buffer => Rnd_Buffer, Status => Success);
if Success /= 1 then
Success := NTSTATUS'Last;
BcryptGenRandom_Public(Buffer => Rnd_Buffer, Status => Success);
if Success /= 0 then
return;
else
Works := (Success = 0);
end if;
else
Works := (Success = 1);
end if;
while Chain_Len < Chain'Length and then Rnd_Len < Rnd_Buffer'Length loop
pragma Loop_Variant (Increases => Chain_Len);
pragma Loop_Variant (Increases => Rnd_Len);
pragma Loop_Invariant (Chain_Len in 0 .. Chain'Length);
pragma Loop_Invariant (Rnd_Len in Rnd_Buffer'First .. Rnd_Buffer'Length);
Chain_Len := Chain_Len + 1;
Chain (Chain_Len) := Chain_Set ((Natural (Rnd_Buffer(Rnd_Len)) mod Chain_Set'Length) + 1);
Rnd_Len := Rnd_Len + 1;
end loop;
Rnd_Buffer := (others => 0);
pragma Unreferenced(Rnd_Buffer);
Calculate_Shannon_Entropy(Item => Chain, Ento => Entropy);
end Chain_Hex_Uppercase;
Empirical Audit Logs (97+ Billion Characters Stress Test)
To prove that the mathematical constraints translate perfectly into a completely uniform distribution, here is an extraction of the empirical test logs from a massive 97,657,298,301 characters audit run on the Pure Numeric configuration:
==========================================================================================================================================================================================
== Date : 2026-08-03 | Day : Monday | Hour : 12:39 PM ==
==========================================================================================================================================================================================
Charset => 0123456789
Charset Length => 10
Global Classification => [ PURE NUMERIC (0-9) ]
Entropy Loss: [ 0.00 % ] => Charset is Byte-Pure. No duplicates.
--- Character Frequency [ Total Chars 97657298301 ] ---
Character => '0' Appear => 9765902809 times
Character => '1' Appear => 9765770362 times
Character => '2' Appear => 9765699784 times
Character => '3' Appear => 9765664853 times
Character => '4' Appear => 9765854612 times
Character => '5' Appear => 9765775939 times
Character => '6' Appear => 9765716940 times
Character => '7' Appear => 9765636036 times
Character => '8' Appear => 9765613991 times
Character => '9' Appear => 9765662975 times
-------------------------------------------------------------------------------------------
Max Repeats => Character '0' with 9765902809 times.
Min Repeats => Character '8' with 9765613991 times.
Divergence Variance Delta => 288818 units.
Theoretical Ideal Average => 9765730304.00
Real Standard Deviation => 90084.07
Percet Deviation => 0.0009 %
Real Time Duration => 152.038718900
Process MB/s => 612.56 MB/s
Free Unbiased Range => 0 .. 249
Unused Bytes => 6
Reject Percent => 2.73 %
Free Unbiased Formula => Charset'Length * (256 / Charset'Length) - 1
Entropy Source => ProcessPrng (Principal) - BcryptGenRandom (Secondary)
Entropy Default => ProcessPrng
Entropy Fallback => BcryptGenRandom
-------------------------------------------------------------------------------------------
Distribution Stability => 100.00% (Optimal Objective > 99.50%)
Audit Verdict => [ PERFECT ] -> Uniformity matches military-grade physical noise.
-------------------------------------------------------------------------------------------