interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / crates / aienos-artifact / artifact_test.om

1270 lines · 26293 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-artifact/artifact_test.om: Comprehensive behavioral self-test
3note for aienos-artifact (tests.rs + receipt_tests.rs).
4note Compose with: si/directives/om2.om om/tls/sha2.om protocol/omnet/om/sha512/sha512.om protocol/omnet/om/ed25519/ed25519.om aienux/crates/aienos-artifact/artifact.om aienux/crates/aienos-artifact/receipt.om aienux/crates/aienos-artifact/artifact_test.om
5
6const FIX_RES_OFF ulong 240
7const FIX_PAY_OFF ulong 288
8const FIX_SIG_OFF ulong 296
9const FIX_TOTAL ulong 396
10
11text t_hex_digits "0123456789abcdef"
12text t_data_bytes "DATA"
13text t_gold_req "adf96aa3e792b1d4a48fe925532efe34ff2a82fe7c07ce013d6daa8bfecb28db"
14text t_gold_res "02f22c42bc7fff8143e11d282f2cfc7773fc483356a5ad9861c937cc6484a3e5"
15text t_gold_pay "f9f5b6ad6a7b931afdce57c59c806df4971c31d72ef560cd7a140d34aba19758"
16text t_gold_aid "71976b5c38ec15f2307565ed3eea51fde4a5375970cd49338d6e16166238cf4b"
17text t_gold_rcp_adm "16bcbe523a62d9f4c907729fb2fb09d630ce36f80d7169e202730173a59bdfdc"
18text t_gold_rcp_rej "f12ebace5ec0f2a8f06c415d58d936389dcc2fed64dbed4bc91efd01a734a6a3"
19text t_commit_ok "71cb1703484cbc9d56eabe43402a6f9bf62b1028"
20text t_commit_up "71CB1703484CBC9D56EABE43402A6F9BF62B1028"
21text t_commit_bad "71cb1703484cbc9d56eabe43402a6f9bf62b102g"
22text t_lbl_pi5 "SEED-0B-PI5"
23text t_gold_rec_hdr "4149454e52435000000060000002000000000000010001000100000000000000e28b2fcc80dd4e973fbc3d13dc62801b000000000000000000000000000000000100000000000000ff000000000000000800000002000000050000000a000000"
24
25fn t_hex_eq raw view uchar * hex_str view uchar * to bool
26n = len raw
27if ne mul n 2 len hex_str
28ret false
29end
30for i ulong n bound 512
31b = ulong get raw i
32hi = get t_hex_digits shr b 4
33lo = get t_hex_digits and b 15
34if ne hi get hex_str mul i 2
35ret false
36end
37if ne lo get hex_str add mul i 2 1
38ret false
39end
40end
41ret true
42end
43
44fn t_pattern seed uchar dst edit uchar * to void
45for i ulong 32
46put dst i uchar and add ulong seed i 0xff
47end
48end
49
50fn t_seed1 dst edit uchar * to void
51put dst 0 0x9d
52put dst 1 0x61
53put dst 2 0xb1
54put dst 3 0x9d
55put dst 4 0xef
56put dst 5 0xfd
57put dst 6 0x5a
58put dst 7 0x60
59put dst 8 0xba
60put dst 9 0x84
61put dst 10 0x4a
62put dst 11 0xf4
63put dst 12 0x92
64put dst 13 0xec
65put dst 14 0x2c
66put dst 15 0xc4
67put dst 16 0x44
68put dst 17 0x49
69put dst 18 0xc5
70put dst 19 0x69
71put dst 20 0x7b
72put dst 21 0x32
73put dst 22 0x69
74put dst 23 0x19
75put dst 24 0x70
76put dst 25 0x3b
77put dst 26 0xac
78put dst 27 0x03
79put dst 28 0x1c
80put dst 29 0xae
81put dst 30 0x7f
82put dst 31 0x60
83end
84
85fn t_seed2 dst edit uchar * to void
86put dst 0 0x4c
87put dst 1 0xcd
88put dst 2 0x08
89put dst 3 0x9b
90put dst 4 0x28
91put dst 5 0xff
92put dst 6 0x96
93put dst 7 0xda
94put dst 8 0x9d
95put dst 9 0xb6
96put dst 10 0xc3
97put dst 11 0x46
98put dst 12 0xec
99put dst 13 0x11
100put dst 14 0x4e
101put dst 15 0x0f
102put dst 16 0x5b
103put dst 17 0x8a
104put dst 18 0x31
105put dst 19 0x9f
106put dst 20 0x35
107put dst 21 0xab
108put dst 22 0xa6
109put dst 23 0x24
110put dst 24 0xda
111put dst 25 0x8c
112put dst 26 0xf6
113put dst 27 0xed
114put dst 28 0x4f
115put dst 29 0xb8
116put dst 30 0xa6
117put dst 31 0xfb
118end
119
120fn t_seed3 dst edit uchar * to void
121put dst 0 0xc5
122put dst 1 0xaa
123put dst 2 0x8d
124put dst 3 0xf4
125put dst 4 0x3f
126put dst 5 0x9f
127put dst 6 0x83
128put dst 7 0x7b
129put dst 8 0xed
130put dst 9 0xb7
131put dst 10 0x44
132put dst 11 0x2f
133put dst 12 0x31
134put dst 13 0xdc
135put dst 14 0xb7
136put dst 15 0xb1
137put dst 16 0x66
138put dst 17 0xd3
139put dst 18 0x85
140put dst 19 0x35
141put dst 20 0x07
142put dst 21 0x6f
143put dst 22 0x09
144put dst 23 0x4b
145put dst 24 0x85
146put dst 25 0xce
147put dst 26 0x3a
148put dst 27 0x2e
149put dst 28 0x0b
150put dst 29 0x44
151put dst 30 0x58
152put dst 31 0xf7
153end
154
155fn t_rfc8032_empty_sig dst edit uchar * to void
156put dst 0 0xe5
157put dst 1 0x56
158put dst 2 0x43
159put dst 3 0x00
160put dst 4 0xc3
161put dst 5 0x60
162put dst 6 0xac
163put dst 7 0x72
164put dst 8 0x90
165put dst 9 0x86
166put dst 10 0xe2
167put dst 11 0xcc
168put dst 12 0x80
169put dst 13 0x6e
170put dst 14 0x82
171put dst 15 0x8a
172put dst 16 0x84
173put dst 17 0x87
174put dst 18 0x7f
175put dst 19 0x1e
176put dst 20 0xb8
177put dst 21 0xe5
178put dst 22 0xd9
179put dst 23 0x74
180put dst 24 0xd8
181put dst 25 0x73
182put dst 26 0xe0
183put dst 27 0x65
184put dst 28 0x22
185put dst 29 0x49
186put dst 30 0x01
187put dst 31 0x55
188put dst 32 0x5f
189put dst 33 0xb8
190put dst 34 0x82
191put dst 35 0x15
192put dst 36 0x90
193put dst 37 0xa3
194put dst 38 0x3b
195put dst 39 0xac
196put dst 40 0xc6
197put dst 41 0x1e
198put dst 42 0x39
199put dst 43 0x70
200put dst 44 0x1c
201put dst 45 0xf9
202put dst 46 0xb4
203put dst 47 0x6b
204put dst 48 0xd2
205put dst 49 0x5b
206put dst 50 0xf5
207put dst 51 0xf0
208put dst 52 0x59
209put dst 53 0x5b
210put dst 54 0xbe
211put dst 55 0x24
212put dst 56 0x65
213put dst 57 0x51
214put dst 58 0x41
215put dst 59 0x43
216put dst 60 0x8e
217put dst 61 0x7a
218put dst 62 0x10
219put dst 63 0x0b
220end
221
222note Build canonical 396-byte Binary Artifact v0 fixture (tests.rs: fixture())
223fn t_build_fixture buf edit uchar * to void
224for i ulong FIX_TOTAL
225put buf i 0
226end
227for i ulong 7
228put buf i get art_magic_pfx i
229end
230put buf 7 0
231art_put_u16_le buf 8 0
232art_put_u16_le buf 10 128
233art_put_u16_le buf 12 1
234art_put_u16_le buf 14 1
235art_put_u32_le buf 20 FIX_TOTAL
236art_put_u32_le buf 24 128
237art_put_u16_le buf 28 2
238art_put_u16_le buf 30 32
239art_put_u16_le buf 32 0
240art_put_u32_le buf 36 0
241art_put_u32_le buf 40 ART_CAPABILITY_TABLE_OFFSET
242art_put_u16_le buf 44 1
243art_put_u16_le buf 46 48
244art_put_u32_le buf 48 FIX_RES_OFF
245art_put_u16_le buf 52 48
246art_put_u32_le buf 56 FIX_PAY_OFF
247art_put_u32_le buf 60 8
248art_put_u32_le buf 64 FIX_SIG_OFF
249art_put_u16_le buf 68 100
250art_put_u16_le buf 70 1
251note Code section: AArch64 RET, RX
252art_put_u16_le buf 128 1
253art_put_u16_le buf 130 5
254art_put_u32_le buf 136 0
255art_put_u32_le buf 140 4
256art_put_u32_le buf 144 4
257art_put_u32_le buf 148 4096
258note Data section: 4 initialized bytes, 1 page reserved, RW
259art_put_u16_le buf 160 2
260art_put_u16_le buf 162 3
261art_put_u32_le buf 168 4
262art_put_u32_le buf 172 4
263art_put_u32_le buf 176 4096
264art_put_u32_le buf 180 4096
265note Scoped READ capability for object 7, bytes [0, 4)
266art_put_u16_le buf 192 3
267art_put_u32_le buf 196 7
268art_put_u32_le buf 200 ART_RIGHT_READ
269art_put_u32_le buf 204 1
270art_put_u32_le buf 208 4
271art_put_u64_le buf 216 4
272art_put_u64_le buf 224 0
273art_put_u64_le buf 232 4
274note Resource envelope
275art_put_u32_le buf FIX_RES_OFF 1
276art_put_u32_le buf add FIX_RES_OFF 4 1
277art_put_u32_le buf add FIX_RES_OFF 8 1
278art_put_u16_le buf add FIX_RES_OFF 12 1
279art_put_u32_le buf add FIX_RES_OFF 16 2
280art_put_u32_le buf add FIX_RES_OFF 20 16
281art_put_u64_le buf add FIX_RES_OFF 24 1000
282art_put_u64_le buf add FIX_RES_OFF 32 2000
283art_put_u32_le buf add FIX_RES_OFF 40 16
284note Payload: RET + "DATA"
285put buf FIX_PAY_OFF 0xc0
286put buf add FIX_PAY_OFF 1 0x03
287put buf add FIX_PAY_OFF 2 0x5f
288put buf add FIX_PAY_OFF 3 0xd6
289for i ulong 4
290put buf add add FIX_PAY_OFF 4 i get t_data_bytes i
291end
292note Signature block placeholder
293art_put_u16_le buf FIX_SIG_OFF 1
294for i ulong 32
295put buf add add FIX_SIG_OFF 4 i 0xa5
296end
297for i ulong 64
298put buf add add FIX_SIG_OFF 36 i 0x5a
299end
300end
301
302note Populate admitted() receipt in globals (receipt_tests.rs)
303fn t_load_admitted to void
304rcp_clear
305put rcp_scalars 0 0
306put rcp_scalars 1 RCP_DEC_ADMITTED
307put rcp_scalars 2 RCP_TIER_QEMU
308put rcp_scalars 3 1
309put rcp_scalars 4 0
310put rcp_scalars 5 RCP_EXEC_EXITED
311put rcp_exit_status 0 0
312put rcp_scalars 6 RCP_VALID_RES_FLAGS
313put rcp_scalars 7 0
314put rcp_scalars 8 0
315put rcp_scalars 9 8
316put rcp_scalars 10 2
317put rcp_scalars 11 5
318put rcp_scalars 12 10
319d = new uchar 32 0
320t_pattern 0x10 d
321rcp_set_digest 0 d
322t_pattern 0x20 d
323rcp_set_digest 1 d
324t_pattern 0x30 d
325rcp_set_digest 2 d
326t_pattern 0x40 d
327rcp_set_digest 3 d
328t_pattern 0x50 d
329rcp_set_digest 4 d
330t_pattern 0x60 d
331rcp_set_digest 5 d
332t_pattern 0x70 d
333rcp_set_digest 6 d
334t_pattern 0x80 d
335rcp_set_digest 7 d
336nb = new uchar 16 0
337rcp_nonce d 1 nb
338for i ulong 16
339put rcp_nonce_buf i get nb i
340end
341end
342
343note Populate rejected() receipt in globals (receipt_tests.rs)
344fn t_load_rejected to void
345t_load_admitted
346put rcp_scalars 1 RCP_DEC_REJECTED
347put rcp_scalars 3 2
348put rcp_scalars 5 RCP_EXEC_NOT_RUN
349put rcp_exit_status 0 0
350put rcp_scalars 6 RCP_RES_RECLAIMED
351put rcp_scalars 7 3
352put rcp_scalars 8 ulong ART_ERR_BAD_SIGNATURE
353put rcp_scalars 9 0
354put rcp_scalars 10 0
355put rcp_scalars 11 0
356put rcp_scalars 12 0
357z = new uchar 32 0
358rcp_set_digest 2 z
359rcp_set_digest 5 z
360vid = new uchar 32 0
361rcp_get_digest 7 vid
362nb = new uchar 16 0
363rcp_nonce vid 2 nb
364for i ulong 16
365put rcp_nonce_buf i get nb i
366end
367end
368
369note Test group 1: Canonical artifact parsing, slices, and exact golden SHA-256 digests
370fn test_artifact_canonical to int
371buf = new uchar 396 0
372t_build_fixture buf
373if ne art_parse_and_identify buf ART_OK
374ret 1
375end
376if or ne get art_meta 0 1 ne get art_meta 1 1
377ret 2
378end
379if or ne get art_meta 2 0 ne get art_meta 4 1
380ret 3
381end
382if or ne get art_caps 1 7 ne get art_caps 2 ART_RIGHT_READ
383ret 4
384end
385if or ne get art_res 0 1 ne get art_meta 10 FIX_SIG_OFF
386ret 5
387end
388d_req = new uchar 32 0
389d_grt = new uchar 32 0
390d_res = new uchar 32 0
391art_requested_cap_digest part buf ART_CAPABILITY_TABLE_OFFSET 48 d_req
392art_granted_cap_digest part buf ART_CAPABILITY_TABLE_OFFSET 48 d_grt
393art_resource_envelope_digest part buf FIX_RES_OFF 48 d_res
394if art_bytes_eq d_req d_grt
395ret 6
396end
397if not t_hex_eq d_req t_gold_req
398ret 7
399end
400if not t_hex_eq d_res t_gold_res
401ret 8
402end
403pd = new uchar 32 0
404aid = new uchar 32 0
405for i ulong 32
406put pd i get art_payload_dig i
407put aid i get art_id i
408end
409if not t_hex_eq pd t_gold_pay
410ret 9
411end
412if not t_hex_eq aid t_gold_aid
413ret 10
414end
415note Every truncation 0..395 must fail
416for n ulong FIX_TOTAL
417if eq art_parse part buf 0 n ART_OK
418ret 11
419end
420end
421ret 0
422end
423
424note Test group 2: Header, length, section, entry, capability, resource, reserved mutations
425fn test_artifact_mutations to int
426buf = new uchar 396 0
427t_build_fixture buf
428put buf 0 xor get buf 0 1
429if ne art_parse buf ART_ERR_BAD_MAGIC
430ret 12
431end
432t_build_fixture buf
433art_put_u16_le buf 8 1
434if ne art_parse buf ART_ERR_UNSUPPORTED_VERSION
435ret 13
436end
437t_build_fixture buf
438art_put_u16_le buf 12 2
439if ne art_parse buf ART_ERR_WRONG_TARGET
440ret 14
441end
442t_build_fixture buf
443art_put_u16_le buf 14 2
444if ne art_parse buf ART_ERR_WRONG_ABI
445ret 15
446end
447t_build_fixture buf
448art_put_u32_le buf 20 sub FIX_TOTAL 1
449if ne art_parse buf ART_ERR_WRONG_LENGTH
450ret 16
451end
452t_build_fixture buf
453art_put_u32_le buf 64 0xfffffff0
454rc = art_parse buf
455if and ne rc ART_ERR_SECTION_OVERLAP ne rc ART_ERR_LENGTH_OVERFLOW
456ret 17
457end
458t_build_fixture buf
459art_put_u32_le buf 16 1
460if ne art_parse buf ART_ERR_UNSUPPORTED_FLAGS
461ret 18
462end
463t_build_fixture buf
464art_put_u32_le buf 168 0
465if ne art_parse buf ART_ERR_BAD_SECTION
466ret 19
467end
468t_build_fixture buf
469art_put_u32_le buf 36 4
470if ne art_parse buf ART_ERR_BAD_ENTRY_POINT
471ret 20
472end
473t_build_fixture buf
474art_put_u32_le buf 200 or ART_RIGHT_READ 0x40
475if ne art_parse buf ART_ERR_MALFORMED_CAPABILITY
476ret 21
477end
478t_build_fixture buf
479art_put_u64_le buf 224 0xffffffffffffffff
480if ne art_parse buf ART_ERR_LENGTH_OVERFLOW
481ret 22
482end
483t_build_fixture buf
484art_put_u32_le buf FIX_RES_OFF 65
485if ne art_parse buf ART_ERR_RESOURCE_LIMIT
486ret 23
487end
488t_build_fixture buf
489art_put_u64_le buf add FIX_RES_OFF 24 1000000001
490if ne art_parse buf ART_ERR_RESOURCE_LIMIT
491ret 24
492end
493t_build_fixture buf
494put buf 34 1
495if ne art_parse buf ART_ERR_UNSUPPORTED_VERSION
496ret 25
497end
498t_build_fixture buf
499put buf 194 1
500if ne art_parse buf ART_ERR_RESERVED_NON_ZERO
501ret 26
502end
503t_build_fixture buf
504put buf add FIX_RES_OFF 14 1
505if ne art_parse buf ART_ERR_RESERVED_NON_ZERO
506ret 27
507end
508t_build_fixture buf
509art_put_u16_le buf 44 17
510if ne art_parse buf ART_ERR_RESOURCE_LIMIT
511ret 28
512end
513ret 0
514end
515
516note Test group 3: Identity sensitivity, signature exclusion, attenuation, bounded fuzz
517fn test_artifact_identity_and_attenuation to int
518buf = new uchar 396 0
519t_build_fixture buf
520if ne art_parse_and_identify buf ART_OK
521ret 29
522end
523orig_id = new uchar 32 0
524orig_pd = new uchar 32 0
525for i ulong 32
526put orig_id i get art_id i
527put orig_pd i get art_payload_dig i
528end
529note Payload mutation changes artifact_id and payload_digest
530put buf FIX_PAY_OFF xor get buf FIX_PAY_OFF 1
531if ne art_parse_and_identify buf ART_OK
532ret 30
533end
534cur_id = new uchar 32 0
535cur_pd = new uchar 32 0
536for i ulong 32
537put cur_id i get art_id i
538put cur_pd i get art_payload_dig i
539end
540if or art_bytes_eq orig_id cur_id art_bytes_eq orig_pd cur_pd
541ret 31
542end
543note Capability mutation changes artifact_id
544t_build_fixture buf
545art_put_u32_le buf 200 or ART_RIGHT_READ ART_RIGHT_WRITE
546if ne art_parse_and_identify buf ART_OK
547ret 32
548end
549for i ulong 32
550put cur_id i get art_id i
551end
552if art_bytes_eq orig_id cur_id
553ret 33
554end
555note Resource mutation changes artifact_id
556t_build_fixture buf
557art_put_u64_le buf add FIX_RES_OFF 24 999
558if ne art_parse_and_identify buf ART_OK
559ret 34
560end
561for i ulong 32
562put cur_id i get art_id i
563end
564if art_bytes_eq orig_id cur_id
565ret 35
566end
567note Signature and signer fingerprint mutations do NOT change artifact_id
568t_build_fixture buf
569put buf add FIX_SIG_OFF 36 xor get buf add FIX_SIG_OFF 36 1
570if ne art_parse_and_identify buf ART_OK
571ret 36
572end
573for i ulong 32
574put cur_id i get art_id i
575end
576if not art_bytes_eq orig_id cur_id
577ret 37
578end
579t_build_fixture buf
580put buf add FIX_SIG_OFF 4 xor get buf add FIX_SIG_OFF 4 1
581if ne art_parse_and_identify buf ART_OK
582ret 38
583end
584for i ulong 32
585put cur_id i get art_id i
586end
587if not art_bytes_eq orig_id cur_id
588ret 39
589end
590ret 0
591end
592
593note Test group 3b: Capability attenuation, encode/decode round-trip, and bounded fuzzing
594fn test_artifact_attenuation_and_fuzz to int
595c_r = new ulong 8 0
596put c_r 0 3
597put c_r 1 7
598put c_r 2 ART_RIGHT_READ
599put c_r 3 1
600put c_r 4 4
601put c_r 5 4
602put c_r 6 0
603put c_r 7 4
604c_rw = new ulong 8 0
605for i ulong 8
606put c_rw i get c_r i
607end
608put c_rw 2 or ART_RIGHT_READ ART_RIGHT_WRITE
609c_dec = new ulong 8 0
610for i ulong 8
611put c_dec i get c_r i
612end
613if not art_cap_is_attenuation_of c_r c_dec
614ret 40
615end
616if art_cap_is_attenuation_of c_rw c_r
617ret 41
618end
619if not art_cap_is_attenuation_of c_r c_rw
620ret 42
621end
622cb = new uchar 48 0
623art_cap_encode c_r cb
624if ne art_cap_decode cb c_dec ART_OK
625ret 43
626end
627for i ulong 8
628if ne get c_dec i get c_r i
629ret 44
630end
631end
632fuzz = new uchar 512 0
633st = uint 0x9e3779b9
634for flen ulong 513
635for j ulong flen bound 512
636st = add mul st 1664525 1013904223
637put fuzz j uchar shr st 24
638end
639ign = art_parse part fuzz 0 flen
640end
641ret 0
642end
643
644note Test group 4a: RFC 8032 Ed25519 verification + signing test vector
645fn test_artifact_ed25519_vec to int
646pk = new uchar 32 0
647art_seed0b_test_pubkey pk
648s1 = new uchar 32 0
649t_seed1 s1
650derived = new uchar 32 0
651art_ed25519_pubkey s1 derived
652if not art_bytes_eq pk derived
653ret 45
654end
655empty_buf = new uchar 1 0
656vec_sig = new uchar 64 0
657t_rfc8032_empty_sig vec_sig
658if not art_ed25519_verify pk part empty_buf 0 0 vec_sig
659ret 46
660end
661put vec_sig 0 xor get vec_sig 0 1
662if art_ed25519_verify pk part empty_buf 0 0 vec_sig
663ret 47
664end
665t_rfc8032_empty_sig vec_sig
666gen_sig = new uchar 64 0
667art_ed25519_sign s1 part empty_buf 0 0 gen_sig
668if not art_bytes_eq gen_sig vec_sig
669ret 48
670end
671ret 0
672end
673
674note Test group 4b: Seed0b artifact authentication
675fn test_artifact_anchor to int
676pk = new uchar 32 0
677art_seed0b_test_pubkey pk
678s1 = new uchar 32 0
679t_seed1 s1
680buf = new uchar 396 0
681t_build_fixture buf
682if ne art_parse_and_identify buf ART_OK
683ret 49
684end
685aid = new uchar 32 0
686for i ulong 32
687put aid i get art_id i
688end
689msg = new uchar 61 0
690art_signature_message aid msg
691sig = new uchar 64 0
692art_ed25519_sign s1 msg sig
693fp = new uchar 32 0
694art_signer_fingerprint pk fp
695for i ulong 32
696put buf add add FIX_SIG_OFF 4 i get fp i
697end
698for i ulong 64
699put buf add add FIX_SIG_OFF 36 i get sig i
700end
701art_anchors_clear
702if ne art_verify buf ART_ERR_UNTRUSTED_SIGNER
703ret 50
704end
705if ne art_anchors_add_seed0b_test ART_OK
706ret 51
707end
708if ne art_verify buf ART_OK
709ret 52
710end
711if ne get art_meta 11 ART_TRUST_TIER_SEED0B_QUAL
712ret 53
713end
714put buf add FIX_SIG_OFF 36 xor get buf add FIX_SIG_OFF 36 1
715if ne art_verify buf ART_ERR_BAD_SIGNATURE
716ret 54
717end
718put buf add FIX_SIG_OFF 36 xor get buf add FIX_SIG_OFF 36 1
719put buf add FIX_SIG_OFF 4 xor get buf add FIX_SIG_OFF 4 1
720if ne art_verify buf ART_ERR_UNTRUSTED_SIGNER
721ret 55
722end
723put buf add FIX_SIG_OFF 4 xor get buf add FIX_SIG_OFF 4 1
724put buf FIX_PAY_OFF xor get buf FIX_PAY_OFF 1
725if ne art_verify buf ART_ERR_BAD_SIGNATURE
726ret 56
727end
728found_pk = new uchar 32 0
729if ne art_anchors_find fp found_pk ART_TRUST_TIER_SEED0B_QUAL
730ret 57
731end
732zfp = new uchar 32 0
733if ne art_anchors_find zfp found_pk ART_TRUST_TIER_NONE
734ret 58
735end
736ret 0
737end
738
739note Test group 5a: Receipt layout and golden digests/bytes
740fn test_receipt_layout_and_goldens to int
741t_load_admitted
742b = new uchar 512 0
743if ne rcp_encode b ART_OK
744ret 60
745end
746if not t_hex_eq part b 0 96 t_gold_rec_hdr
747ret 61
748end
749if not art_all_zero b 352 160
750ret 62
751end
752dig = new uchar 32 0
753rcp_digest b dig
754if not t_hex_eq dig t_gold_rcp_adm
755ret 63
756end
757put rcp_exit_status 0 -2
758put rcp_scalars 6 0
759b2 = new uchar 512 0
760if ne rcp_encode b2 ART_OK
761ret 64
762end
763if or ne get b2 68 0xfe ne get b2 69 0xff
764ret 65
765end
766if or ne get b2 70 0xff ne get b2 71 0xff
767ret 66
768end
769if ne rcp_decode b2 ART_OK
770ret 67
771end
772if ne get rcp_exit_status 0 -2
773ret 68
774end
775t_load_rejected
776r = new uchar 512 0
777if ne rcp_encode r ART_OK
778ret 69
779end
780rcp_digest r dig
781if not t_hex_eq dig t_gold_rcp_rej
782ret 70
783end
784if ne rcp_decode r ART_OK
785ret 71
786end
787ret 0
788end
789
790note Test group 5b: Receipt nonce, verifier_identity_bytes, Pi5 tier label
791fn test_receipt_nonce_and_id to int
792id1 = new uchar 32 0
793id2 = new uchar 32 0
794t_pattern 0x80 id1
795t_pattern 0x81 id2
796n1 = new uchar 16 0
797n2 = new uchar 16 0
798n3 = new uchar 16 0
799rcp_nonce id1 1 n1
800rcp_nonce id1 2 n2
801rcp_nonce id2 1 n3
802if or art_bytes_eq n1 n2 art_bytes_eq n1 n3
803ret 72
804end
805vib = new uchar 45 0
806if ne rcp_verifier_identity_bytes t_commit_ok 1 1 1 vib 45
807ret 73
808end
809if or ne get vib 40 1 ne get vib 41 0
810ret 74
811end
812if or ne get vib 42 1 ne get vib 44 1
813ret 75
814end
815if ne rcp_verifier_identity_bytes t_commit_up 1 1 1 vib -1
816ret 76
817end
818if ne rcp_verifier_identity_bytes part t_commit_ok 0 39 1 1 1 vib -1
819ret 77
820end
821if ne rcp_verifier_identity_bytes t_commit_bad 1 1 1 vib -1
822ret 78
823end
824t_load_admitted
825put rcp_scalars 2 RCP_TIER_PI5
826b = new uchar 512 0
827if ne rcp_encode b ART_OK
828ret 79
829end
830if ne rcp_decode b ART_OK
831ret 80
832end
833if ne get rcp_scalars 2 RCP_TIER_PI5
834ret 81
835end
836lbl = new uchar 32 0
837llen = rcp_tier_label_copy RCP_TIER_PI5 lbl
838if not art_bytes_eq part lbl 0 llen t_lbl_pi5
839ret 82
840end
841ret 0
842end
843
844note Test group 6: Receipt strict decoding (lengths, magic, flags, reserved, enums, presence, consistency)
845fn test_receipt_strict_decode to int
846t_load_admitted
847b = new uchar 512 0
848ign = rcp_encode b
849if ne rcp_decode part b 0 511 ART_ERR_TRUNCATED
850ret 83
851end
852if ne rcp_decode part b 0 0 ART_ERR_TRUNCATED
853ret 84
854end
855longb = new uchar 513 0
856for i ulong 512
857put longb i get b i
858end
859if ne rcp_decode longb ART_ERR_WRONG_LENGTH
860ret 85
861end
862m = new uchar 512 0
863for i ulong 512
864put m i get b i
865end
866put m 0 88
867if ne rcp_decode m ART_ERR_BAD_MAGIC
868ret 86
869end
870put m 0 get b 0
871put m 8 1
872if ne rcp_decode m ART_ERR_UNSUPPORTED_VERSION
873ret 87
874end
875put m 8 0
876put m 10 95
877if ne rcp_decode m ART_ERR_UNSUPPORTED_VERSION
878ret 88
879end
880put m 10 96
881put m 13 0
882if ne rcp_decode m ART_ERR_UNSUPPORTED_VERSION
883ret 89
884end
885put m 13 get b 13
886note Unknown flag bits 3..31
887for bit ulong 29
888sh = add bit 3
889art_put_u32_le m 16 shl ulong 1 sh
890if ne rcp_decode m ART_ERR_UNSUPPORTED_FLAGS
891ret 90
892end
893end
894art_put_u32_le m 16 0
895note Unknown result bits 8..31
896for bit ulong 24
897sh = add bit 8
898art_put_u32_le m 72 or 0xff shl ulong 1 sh
899if ne rcp_decode m ART_ERR_UNSUPPORTED_FLAGS
900ret 91
901end
902end
903art_put_u32_le m 72 0xff
904note Non-zero reserved bytes 56..64 and 500..512
905for i ulong 8
906put m add 56 i 1
907if ne rcp_decode m ART_ERR_RESERVED_NON_ZERO
908ret 92
909end
910put m add 56 i 0
911end
912for i ulong 12
913put m add 500 i 1
914if ne rcp_decode m ART_ERR_RESERVED_NON_ZERO
915ret 93
916end
917put m add 500 i 0
918end
919note Bad decision / tier / status enums
920art_put_u16_le m 20 0
921if ne rcp_decode m ART_ERR_MALFORMED_RECEIPT
922ret 94
923end
924art_put_u16_le m 20 5
925if ne rcp_decode m ART_ERR_MALFORMED_RECEIPT
926ret 95
927end
928art_put_u16_le m 20 1
929art_put_u16_le m 22 4
930if ne rcp_decode m ART_ERR_MALFORMED_RECEIPT
931ret 96
932end
933art_put_u16_le m 22 1
934art_put_u32_le m 64 7
935if ne rcp_decode m ART_ERR_MALFORMED_RECEIPT
936ret 97
937end
938ret 0
939end
940
941note Test group 7: Receipt reason codes, presence flags, decision consistency, canary rules
942fn test_receipt_semantics to int
943b = new uchar 512 0
944note Valid reasons 1..19 and 0x101..0x10a accepted; 0, 20, 0x100, 0x10b rejected
945t_load_rejected
946for r ulong 19
947put rcp_scalars 8 add r 1
948ign = rcp_encode b
949if ne rcp_decode b ART_OK
950ret 98
951end
952end
953for r ulong 10
954put rcp_scalars 8 add 0x101 r
955ign = rcp_encode b
956if ne rcp_decode b ART_OK
957ret 99
958end
959end
960put rcp_scalars 8 20
961ign = rcp_encode b
962if ne rcp_decode b ART_ERR_MALFORMED_RECEIPT
963ret 100
964end
965put rcp_scalars 8 0x10b
966ign = rcp_encode b
967if ne rcp_decode b ART_ERR_MALFORMED_RECEIPT
968ret 101
969end
970put rcp_scalars 8 18
971put rcp_scalars 7 10
972ign = rcp_encode b
973if ne rcp_decode b ART_ERR_MALFORMED_RECEIPT
974ret 102
975end
976note Optional fields require their presence flags
977t_load_admitted
978d = new uchar 32 0
979t_pattern 1 d
980rcp_set_digest 8 d
981ign = rcp_encode b
982if ne rcp_decode b ART_ERR_MALFORMED_RECEIPT
983ret 103
984end
985t_load_admitted
986put rcp_scalars 13 1
987ign = rcp_encode b
988if ne rcp_decode b ART_ERR_MALFORMED_RECEIPT
989ret 104
990end
991t_load_admitted
992put rcp_scalars 14 1
993ign = rcp_encode b
994if ne rcp_decode b ART_ERR_MALFORMED_RECEIPT
995ret 105
996end
997t_load_admitted
998put rcp_scalars 4 1
999ign = rcp_encode b
1000if ne rcp_decode b ART_ERR_MALFORMED_RECEIPT
1001ret 106
1002end
1003note All optional fields present with VALID_FLAGS accepted
1004t_load_admitted
1005put rcp_scalars 0 RCP_VALID_FLAGS
1006rcp_set_digest 8 d
1007put rcp_scalars 13 3
1008put rcp_scalars 14 4
1009put rcp_scalars 4 5
1010ign = rcp_encode b
1011if ne rcp_decode b ART_OK
1012ret 107
1013end
1014note Decision consistency rules
1015t_load_admitted
1016put rcp_scalars 7 3
1017put rcp_scalars 8 18
1018if ne rcp_validate ART_ERR_MALFORMED_RECEIPT
1019ret 108
1020end
1021t_load_rejected
1022put rcp_scalars 9 1
1023if ne rcp_validate ART_ERR_MALFORMED_RECEIPT
1024ret 109
1025end
1026t_load_rejected
1027put rcp_scalars 6 or RCP_RES_RECLAIMED RCP_RES_READ_OK
1028if ne rcp_validate ART_ERR_MALFORMED_RECEIPT
1029ret 110
1030end
1031t_load_admitted
1032put rcp_exit_status 0 3
1033if ne rcp_validate ART_ERR_MALFORMED_RECEIPT
1034ret 111
1035end
1036note Fault without RESULT_CANARY_PASSED is valid
1037t_load_admitted
1038put rcp_scalars 5 RCP_EXEC_FAULT
1039put rcp_scalars 6 or RCP_RES_RECLAIMED RCP_RES_WX_SEALED
1040if ne rcp_validate ART_OK
1041ret 112
1042end
1043note CanaryFailed decision and status must agree
1044t_load_admitted
1045put rcp_scalars 1 RCP_DEC_CANARY_FAILED
1046put rcp_scalars 5 RCP_EXEC_EXITED
1047if ne rcp_validate ART_ERR_MALFORMED_RECEIPT
1048ret 113
1049end
1050t_load_admitted
1051put rcp_scalars 1 RCP_DEC_ADMITTED
1052put rcp_scalars 5 RCP_EXEC_CANARY_FAILED
1053if ne rcp_validate ART_ERR_MALFORMED_RECEIPT
1054ret 114
1055end
1056t_load_admitted
1057put rcp_scalars 1 RCP_DEC_CANARY_FAILED
1058put rcp_scalars 5 RCP_EXEC_CANARY_FAILED
1059put rcp_scalars 6 and RCP_VALID_RES_FLAGS not RCP_RES_CANARY_PASSED
1060if ne rcp_validate ART_OK
1061ret 115
1062end
1063ret 0
1064end
1065
1066note Test group 8a: Receipt Ed25519 signing, verification, anchor separation
1067fn test_receipt_signatures to int
1068s2 = new uchar 32 0
1069s3 = new uchar 32 0
1070t_seed2 s2
1071t_seed3 s3
1072pk2 = new uchar 32 0
1073rcp_seed0b_test_pubkey pk2
1074der2 = new uchar 32 0
1075art_ed25519_pubkey s2 der2
1076if not art_bytes_eq pk2 der2
1077ret 116
1078end
1079apk = new uchar 32 0
1080art_seed0b_test_pubkey apk
1081if art_bytes_eq pk2 apk
1082ret 117
1083end
1084t_load_admitted
1085u = new uchar 512 0
1086ign = rcp_encode u
1087rcp_anchors_clear
1088ign = rcp_anchors_add_seed0b_test
1089if ne rcp_verify u ART_ERR_BAD_SIGNATURE_FORMAT
1090ret 118
1091end
1092s = new uchar 512 0
1093for i ulong 512
1094put s i get u i
1095end
1096if ne rcp_sign s s2 ART_OK
1097ret 119
1098end
1099dig_u = new uchar 32 0
1100dig_s = new uchar 32 0
1101rcp_digest u dig_u
1102rcp_digest s dig_s
1103if not art_bytes_eq dig_u dig_s
1104ret 120
1105end
1106if ne rcp_verify s ART_OK
1107ret 121
1108end
1109put s 402 1
1110if ne rcp_decode s ART_ERR_RESERVED_NON_ZERO
1111ret 122
1112end
1113put s 402 0
1114put s 400 2
1115if ne rcp_decode s ART_ERR_BAD_SIGNATURE_FORMAT
1116ret 123
1117end
1118put s 400 1
1119if ne rcp_sign s s2 ART_ERR_BAD_SIGNATURE_FORMAT
1120ret 124
1121end
1122rcp_anchors_clear
1123if ne rcp_verify s ART_ERR_UNTRUSTED_SIGNER
1124ret 125
1125end
1126ign = rcp_anchors_add_seed0b_test
1127put s 436 xor get s 436 0xff
1128if ne rcp_verify s ART_ERR_BAD_SIGNATURE
1129ret 126
1130end
1131put s 436 xor get s 436 0xff
1132for i ulong 512
1133put s i get u i
1134end
1135if ne rcp_sign s s3 ART_OK
1136ret 127
1137end
1138if ne rcp_verify s ART_ERR_UNTRUSTED_SIGNER
1139ret 128
1140end
1141t_load_rejected
1142put rcp_scalars 11 1
1143ign = rcp_encode s
1144if ne rcp_sign s s2 ART_ERR_MALFORMED_RECEIPT
1145ret 129
1146end
1147ret 0
1148end
1149
1150note Test group 8b: Receipt tamper detection and bounded fuzzing
1151fn test_receipt_tamper_and_fuzz to int
1152s2 = new uchar 32 0
1153t_seed2 s2
1154t_load_admitted
1155u = new uchar 512 0
1156ign = rcp_encode u
1157dig_u = new uchar 32 0
1158rcp_digest u dig_u
1159dig_m = new uchar 32 0
1160for bi ulong 400
1161put u bi xor get u bi 1
1162rcp_digest u dig_m
1163if art_bytes_eq dig_m dig_u
1164ret 130
1165end
1166put u bi xor get u bi 1
1167end
1168s = new uchar 512 0
1169for i ulong 512
1170put s i get u i
1171end
1172rcp_anchors_clear
1173ign = rcp_anchors_add_seed0b_test
1174ign = rcp_sign s s2
1175for bi ulong 112
1176pos = add 400 bi
1177put s pos xor get s pos 1
1178if eq rcp_verify s ART_OK
1179ret 131
1180end
1181put s pos xor get s pos 1
1182end
1183for k ulong 25
1184pos = mul k 16
1185put s pos xor get s pos 1
1186if eq rcp_verify s ART_OK
1187ret 132
1188end
1189put s pos xor get s pos 1
1190end
1191ret 0
1192end
1193
1194note Test group 8c: Arbitrary mutated 512-byte receipt records never trap
1195fn test_receipt_fuzz to int
1196t_load_admitted
1197u = new uchar 512 0
1198ign = rcp_encode u
1199st = ulong 0x9e3779b97f4a7c15
1200m = new uchar 512 0
1201for iter ulong 512
1202for i ulong 512
1203put m i get u i
1204end
1205for k ulong 8
1206st = xor st shl st 13
1207st = xor st shr st 7
1208st = xor st shl st 17
1209put m rem st 512 uchar and shr st 32 0xff
1210end
1211ign = rcp_decode m
1212end
1213ret 0
1214end
1215
1216fn main to int
1217rc = test_artifact_canonical
1218if ne rc 0
1219ret rc
1220end
1221rc = test_artifact_mutations
1222if ne rc 0
1223ret rc
1224end
1225rc = test_artifact_identity_and_attenuation
1226if ne rc 0
1227ret rc
1228end
1229rc = test_artifact_attenuation_and_fuzz
1230if ne rc 0
1231ret rc
1232end
1233rc = test_artifact_ed25519_vec
1234if ne rc 0
1235ret rc
1236end
1237rc = test_artifact_anchor
1238if ne rc 0
1239ret rc
1240end
1241rc = test_receipt_layout_and_goldens
1242if ne rc 0
1243ret rc
1244end
1245rc = test_receipt_nonce_and_id
1246if ne rc 0
1247ret rc
1248end
1249rc = test_receipt_strict_decode
1250if ne rc 0
1251ret rc
1252end
1253rc = test_receipt_semantics
1254if ne rc 0
1255ret rc
1256end
1257rc = test_receipt_signatures
1258if ne rc 0
1259ret rc
1260end
1261rc = test_receipt_tamper_and_fuzz
1262if ne rc 0
1263ret rc
1264end
1265rc = test_receipt_fuzz
1266if ne rc 0
1267ret rc
1268end
1269ret 0
1270end