interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / m3 / receipt_ledger.s

176 lines · 5175 bytes Raw Blame History
1/*
2 * m3/receipt_ledger.s - Cryptographic Receipt Chain & Bounded Evidence Ledger
3 * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine)
4 *
5 * Append-only bounded receipt ledger. 32 receipts maximum.
6 * Each receipt binds the previous receipt's SHA-256 digest.
7 * Full ledger halts admission (fail-closed, never silently overwrite evidence).
8 * Storage: PHYSICS_RECEIPT_LEDGER @ 0x4020A000 (8 KiB)
9 */
10
11 .global init_receipt_ledger
12 .global get_receipt_count
13 .global is_receipt_ledger_full
14 .global commit_receipt
15
16 .extern sha256_be
17
18 .section .text, "ax"
19 .balign 4
20
21 .equ RECEIPT_LEDGER_BASE, 0x4020A000
22 .equ RECEIPT_LEDGER_CAPACITY, 32
23 .equ RECEIPT_HEADER_SIZE, 128
24 .equ RECEIPT_RECORD_SIZE, 192
25 .equ RECEIPT_ENTRIES_BASE, 0x4020A080
26 .equ RECEIPT_MAGIC, 0x524350544C444752 /* 'RCPTLDGR' */
27
28/*
29 * init_receipt_ledger():
30 * Zeros the 8 KiB ledger area, then writes the ledger header:
31 * +0x00: magic (0x524350544C444752)
32 * +0x08: capacity (u32 = 32), count (u32 = 0)
33 * +0x10: machine_generation (u64 = 1)
34 * +0x18: reserved (8 zero bytes)
35 * +0x20: latest_receipt_digest (32 zero bytes)
36 */
37init_receipt_ledger:
38 stp x29, x30, [sp, #-16]!
39 mov x29, sp
40
41 /* Zero 8192 bytes */
42 ldr x0, =RECEIPT_LEDGER_BASE
43 ldr x1, =8192
44.Lzero_rcpt_loop:
45 cbz x1, .Lzero_rcpt_done
46 stp xzr, xzr, [x0], #16
47 sub x1, x1, #16
48 b .Lzero_rcpt_loop
49
50.Lzero_rcpt_done:
51 ldr x0, =RECEIPT_LEDGER_BASE
52 ldr x1, =RECEIPT_MAGIC
53 str x1, [x0, #0] /* +0x00: magic */
54
55 mov w1, #RECEIPT_LEDGER_CAPACITY
56 str w1, [x0, #8] /* +0x08: capacity = 32 */
57 str wzr, [x0, #12] /* +0x0C: count = 0 */
58
59 mov x1, #1
60 str x1, [x0, #16] /* +0x10: machine_generation = 1 */
61
62 ldp x29, x30, [sp], #16
63 ret
64
65/*
66 * get_receipt_count() -> w0
67 */
68get_receipt_count:
69 ldr x1, =RECEIPT_LEDGER_BASE
70 ldr w0, [x1, #12]
71 ret
72
73/*
74 * is_receipt_ledger_full() -> w0 (1 if full, 0 if space available)
75 */
76is_receipt_ledger_full:
77 ldr x1, =RECEIPT_LEDGER_BASE
78 ldr w0, [x1, #12] /* count */
79 cmp w0, #RECEIPT_LEDGER_CAPACITY
80 cset w0, ge
81 ret
82
83/*
84 * commit_receipt(receipt_data_ptr)
85 * x0: Pointer to a populated ReceiptData block on stack/memory (first 96 bytes: 0x00..0x5F)
86 * Returns:
87 * x0: Pointer to committed receipt in PHYSICS_RECEIPT_LEDGER, or 0 if ledger full.
88 */
89commit_receipt:
90 stp x29, x30, [sp, #-48]!
91 mov x29, sp
92 stp x19, x20, [sp, #16]
93 stp x21, x22, [sp, #32]
94
95 mov x19, x0 /* x19 = src receipt data */
96
97 /* 1. Check ledger full */
98 ldr x20, =RECEIPT_LEDGER_BASE
99 ldr w21, [x20, #12] /* w21 = current count */
100 cmp w21, #RECEIPT_LEDGER_CAPACITY
101 b.ge .Lledger_exhausted
102
103 /* 2. Compute destination entry address: RECEIPT_ENTRIES_BASE + count * 192 */
104 ldr x22, =RECEIPT_ENTRIES_BASE
105 mov x1, #RECEIPT_RECORD_SIZE
106 madd x22, x21, x1, x22 /* x22 = dest receipt ptr */
107
108 /* 3. Copy first 96 bytes (0x00..0x5F) from src to dst */
109 /* 12 x 8-byte words */
110 ldp x2, x3, [x19, #0]
111 stp x2, x3, [x22, #0]
112 ldp x4, x5, [x19, #16]
113 stp x4, x5, [x22, #16]
114 ldp x6, x7, [x19, #32]
115 stp x6, x7, [x22, #32]
116 ldp x8, x9, [x19, #48]
117 stp x8, x9, [x22, #48]
118 ldp x10, x11, [x19, #64]
119 stp x10, x11, [x22, #64]
120 ldp x12, x13, [x19, #80]
121 stp x12, x13, [x22, #80]
122
123 /* Ensure version = 1 and length = 192 */
124 mov w1, #1
125 strh w1, [x22, #0] /* version = 1 */
126 mov w1, #RECEIPT_RECORD_SIZE
127 strh w1, [x22, #2] /* length = 192 */
128
129 /* Bind current machine_generation at +0x50 */
130 ldr x14, [x20, #16] /* machine_generation */
131 str x14, [x22, #0x50]
132
133 /* 4. Copy previous_receipt_digest (32 bytes) from header.latest_receipt_digest (+0x20) to dst (+0x60) */
134 ldp x2, x3, [x20, #0x20]
135 ldp x4, x5, [x20, #0x30]
136 stp x2, x3, [x22, #0x60]
137 stp x4, x5, [x22, #0x70]
138
139 /* 5. Compute SHA-256 seal over bytes 0x00..0x7F (128 bytes) of destination receipt */
140 /* Store result into dst + 0x80 (receipt_digest) */
141 mov x0, x22 /* data ptr */
142 mov x1, #128 /* len = 128 bytes */
143 add x2, x22, #0x80 /* out = dst + 0x80 */
144 bl sha256_be
145 cbnz w0, .Lledger_exhausted
146
147 /* Zero trailing padding +0xA0..+0xBF (32 bytes) */
148 stp xzr, xzr, [x22, #0xA0]
149 stp xzr, xzr, [x22, #0xB0]
150
151 /* 6. Update header.latest_receipt_digest with new digest */
152 ldp x2, x3, [x22, #0x80]
153 ldp x4, x5, [x22, #0x90]
154 stp x2, x3, [x20, #0x20]
155 stp x4, x5, [x20, #0x30]
156
157 /* 7. Increment count and machine_generation */
158 add w21, w21, #1
159 str w21, [x20, #12] /* count */
160
161 ldr x14, [x20, #16]
162 add x14, x14, #1
163 str x14, [x20, #16] /* machine_generation */
164
165 mov x0, x22 /* Return committed receipt pointer */
166 ldp x19, x20, [sp, #16]
167 ldp x21, x22, [sp, #32]
168 ldp x29, x30, [sp], #48
169 ret
170
171.Lledger_exhausted:
172 mov x0, #0
173 ldp x19, x20, [sp, #16]
174 ldp x21, x22, [sp, #32]
175 ldp x29, x30, [sp], #48
176 ret