11-- SPDX-License-Identifier: PMPL-1.0-or-later
22-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <jonathan.jewell@open.ac.uk>
3- --
4- ||| ABI Type Definitions Template
5- |||
6- ||| This module defines the Application Binary Interface (ABI) for this library.
7- ||| All type definitions include formal proofs of correctness.
8- |||
9- ||| Replace I_HUMAN with your project name.
3+
4+ ||| I, HUMAN (Project Veracity) - ABI Type Definitions
105|||
11- ||| @see https://idris2.readthedocs.io for Idris2 documentation
6+ ||| This module defines the Application Binary Interface for the identity
7+ ||| verification kernel. It ensures that sensitive human-verification data
8+ ||| is handled with strict type safety across the Idris/Zig boundary.
129
1310module I_HUMAN.ABI.Types
1411
@@ -19,215 +16,50 @@ import Data.Vect
1916%default total
2017
2118-- ------------------------------------------------------------------------------
22- -- Platform Detection
19+ -- Platform Context
2320-- ------------------------------------------------------------------------------
2421
25- ||| Supported platforms for this ABI
22+ ||| Verified targets for identity verification logic.
2623public export
2724data Platform = Linux | Windows | MacOS | BSD | WASM
2825
29- ||| Compile-time platform detection
30- ||| This will be set during compilation based on target
26+ ||| Resolves the execution environment at compile time.
3127public export
3228thisPlatform : Platform
3329thisPlatform =
3430 % runElab do
35- -- Platform detection logic
36- pure Linux -- Default, override with compiler flags
31+ pure Linux
3732
3833-- ------------------------------------------------------------------------------
39- -- Core Types
34+ -- Verification Result Codes
4035-- ------------------------------------------------------------------------------
4136
42- ||| Result codes for FFI operations
43- ||| Use C-compatible integers for cross-language compatibility
37+ ||| Formal outcome of an identity verification operation.
4438public export
4539data Result : Type where
46- ||| Operation succeeded
40+ ||| Verification Successful
4741 Ok : Result
48- ||| Generic error
42+ ||| Verification Failed: Identity could not be confirmed
4943 Error : Result
50- ||| Invalid parameter provided
44+ ||| Parameter Error: Malformed input data
5145 InvalidParam : Result
52- ||| Out of memory
46+ ||| System Error: Out of memory during cryptographic operation
5347 OutOfMemory : Result
54- ||| Null pointer encountered
48+ ||| Safety Error: Internal null pointer encountered
5549 NullPointer : Result
5650
57- ||| Convert Result to C integer
58- public export
59- resultToInt : Result -> Bits32
60- resultToInt Ok = 0
61- resultToInt Error = 1
62- resultToInt InvalidParam = 2
63- resultToInt OutOfMemory = 3
64- resultToInt NullPointer = 4
65-
66- ||| Results are decidably equal
67- public export
68- DecEq Result where
69- decEq Ok Ok = Yes Refl
70- decEq Error Error = Yes Refl
71- decEq InvalidParam InvalidParam = Yes Refl
72- decEq OutOfMemory OutOfMemory = Yes Refl
73- decEq NullPointer NullPointer = Yes Refl
74- decEq _ _ = No absurd
75-
7651-- ------------------------------------------------------------------------------
77- -- Opaque Handles
52+ -- Opaque Identity Handles
7853-- ------------------------------------------------------------------------------
7954
80- ||| Opaque handle type for FFI
81- ||| Prevents direct construction, enforces creation through safe API
55+ ||| An opaque reference to a verified identity session.
56+ ||| INVARIANT: The internal pointer is guaranteed to be non-null.
8257public export
8358data Handle : Type where
8459 MkHandle : (ptr : Bits64) -> {auto 0 nonNull : So (ptr /= 0)} -> Handle
8560
86- ||| Safely create a handle from a pointer value
87- ||| Returns Nothing if pointer is null
61+ ||| Safe constructor for identity handles.
8862public export
8963createHandle : Bits64 -> Maybe Handle
9064createHandle 0 = Nothing
9165createHandle ptr = Just (MkHandle ptr)
92-
93- ||| Extract pointer value from handle
94- public export
95- handlePtr : Handle -> Bits64
96- handlePtr (MkHandle ptr) = ptr
97-
98- -- ------------------------------------------------------------------------------
99- -- Platform-Specific Types
100- -- ------------------------------------------------------------------------------
101-
102- ||| C int size varies by platform
103- public export
104- CInt : Platform -> Type
105- CInt Linux = Bits32
106- CInt Windows = Bits32
107- CInt MacOS = Bits32
108- CInt BSD = Bits32
109- CInt WASM = Bits32
110-
111- ||| C size_t varies by platform
112- public export
113- CSize : Platform -> Type
114- CSize Linux = Bits64
115- CSize Windows = Bits64
116- CSize MacOS = Bits64
117- CSize BSD = Bits64
118- CSize WASM = Bits32
119-
120- ||| C pointer size varies by platform
121- public export
122- ptrSize : Platform -> Nat
123- ptrSize Linux = 64
124- ptrSize Windows = 64
125- ptrSize MacOS = 64
126- ptrSize BSD = 64
127- ptrSize WASM = 32
128-
129- ||| Pointer type for platform
130- public export
131- CPtr : Platform -> Type -> Type
132- CPtr p _ = Bits (ptrSize p)
133-
134- -- ------------------------------------------------------------------------------
135- -- Memory Layout Proofs
136- -- ------------------------------------------------------------------------------
137-
138- ||| Proof that a type has a specific size
139- public export
140- data HasSize : Type -> Nat -> Type where
141- SizeProof : {0 t : Type } -> {n : Nat } -> HasSize t n
142-
143- ||| Proof that a type has a specific alignment
144- public export
145- data HasAlignment : Type -> Nat -> Type where
146- AlignProof : {0 t : Type } -> {n : Nat } -> HasAlignment t n
147-
148- ||| Size of C types (platform-specific)
149- public export
150- cSizeOf : (p : Platform) -> (t : Type ) -> Nat
151- cSizeOf p (CInt _ ) = 4
152- cSizeOf p (CSize _ ) = if ptrSize p == 64 then 8 else 4
153- cSizeOf p Bits32 = 4
154- cSizeOf p Bits64 = 8
155- cSizeOf p Double = 8
156- cSizeOf p _ = ptrSize p `div` 8
157-
158- ||| Alignment of C types (platform-specific)
159- public export
160- cAlignOf : (p : Platform) -> (t : Type ) -> Nat
161- cAlignOf p (CInt _ ) = 4
162- cAlignOf p (CSize _ ) = if ptrSize p == 64 then 8 else 4
163- cAlignOf p Bits32 = 4
164- cAlignOf p Bits64 = 8
165- cAlignOf p Double = 8
166- cAlignOf p _ = ptrSize p `div` 8
167-
168- -- ------------------------------------------------------------------------------
169- -- Example Struct with Layout Proof
170- -- ------------------------------------------------------------------------------
171-
172- ||| Example C-compatible struct
173- ||| Replace this with your actual data types
174- public export
175- record ExampleStruct where
176- constructor MkExampleStruct
177- field1 : Bits32
178- field2 : Bits64
179- field3 : Double
180-
181- ||| Prove the struct has correct size
182- public export
183- exampleStructSize : (p : Platform) -> HasSize ExampleStruct 16
184- exampleStructSize p =
185- -- 4 bytes (Bits32) + 4 padding + 8 bytes (Bits64) + 8 bytes (Double) = 24
186- -- But with alignment, it's actually platform-specific
187- SizeProof
188-
189- ||| Prove the struct has correct alignment
190- public export
191- exampleStructAlign : (p : Platform) -> HasAlignment ExampleStruct 8
192- exampleStructAlign p = AlignProof
193-
194- -- ------------------------------------------------------------------------------
195- -- FFI Declarations
196- -- ------------------------------------------------------------------------------
197-
198- ||| Declare external C functions
199- ||| These will be implemented in Zig FFI
200- namespace Foreign
201-
202- ||| External function example
203- export
204- % foreign " C:example_function, libexample"
205- prim__exampleFunction : Bits64 -> PrimIO Bits32
206-
207- ||| Safe wrapper around FFI function
208- export
209- exampleFunction : Handle -> IO (Either Result Bits32)
210- exampleFunction h = do
211- result <- primIO (prim__exampleFunction (handlePtr h))
212- pure (Right result)
213-
214- -- ------------------------------------------------------------------------------
215- -- Verification
216- -- ------------------------------------------------------------------------------
217-
218- ||| Compile-time verification of ABI properties
219- namespace Verify
220-
221- ||| Verify struct sizes are correct
222- export
223- verifySizes : IO ()
224- verifySizes = do
225- -- Add compile-time checks here
226- putStrLn " ABI sizes verified"
227-
228- ||| Verify struct alignments are correct
229- export
230- verifyAlignments : IO ()
231- verifyAlignments = do
232- -- Add compile-time checks here
233- putStrLn " ABI alignments verified"
0 commit comments