code wiki / (root) / nx_crypto_event_compose_test.nx

nx_crypto_event_compose_test.nx source

↩ module page · 123 lines · 4838 B

1// nx_crypto_event_compose_test.nx -- crypto facade + event-driven flow. 2// 3// All 4 new primitives composed: hash + sign facades close the 4// inline-signing sweep; event_queue + state_machine drive a cell's 5// lifecycle in response to crypto-verified vesicle deliveries. 6 7import "nx_syscalls.nx" 8import "nx_tier.nx" 9import "nx_hash_facade.nx" 10import "nx_sign_facade.nx" 11import "nx_event_queue.nx" 12import "nx_state_machine.nx" 13 14func main() -> i64 { 15 // ===== Step 1: hash + sign a message ============================= 16 let msg: *u8 = (sys_mmap(32)) as *u8 17 var i: nx_size = 0 18 while i < 16 { 19 msg[i] = (65 + i) as u8 20 i = i + 1 21 } 22 let content_hash: nx_size = nx_hash_facade_compute_bytes(msg, 16) 23 if content_hash == 0 { return 1 } 24 25 // Sign with Ed25519 (commodity-grade trust 410) 26 let sk: *u8 = (sys_mmap(32)) as *u8 27 var j: nx_size = 0 28 while j < 32 { 29 sk[j] = (50 + j) as u8 30 j = j + 1 31 } 32 let sig: *u8 = (sys_mmap(128)) as *u8 33 let req: *NxSignRequest = nx_sign_request_new(NX_SA_ED25519, 34 msg, 16, sk, 32, sig, 128, 410) 35 let sig_len: nx_int = nx_sign_facade_sign(req) 36 if sig_len != 64 { return 2 } 37 38 // Verify roundtrip 39 let v: nx_int = nx_sign_facade_verify(NX_SA_ED25519, 40 msg, 16, sig, 64, sk, 32, 410) 41 if v != NX_SF_VERIFIED { return 3 } 42 43 // Tamper -> rejected 44 msg[0] = 99 as u8 45 let v_bad: nx_int = nx_sign_facade_verify(NX_SA_ED25519, 46 msg, 16, sig, 64, sk, 32, 410) 47 if v_bad != NX_SF_REJECTED { return 4 } 48 msg[0] = 65 as u8 49 50 // ===== Step 2: state machine for a cell's life =================== 51 // States: 0=NASCENT, 1=RUNNING, 2=SUSPENDED, 3=TERMINATED 52 let sm: *NxStateMachine = nx_state_machine_new(4, 0) 53 nx_state_machine_allow(sm, 0, 1) // NASCENT -> RUNNING 54 nx_state_machine_allow(sm, 1, 2) // RUNNING -> SUSPENDED 55 nx_state_machine_allow(sm, 2, 1) // SUSPENDED -> RUNNING 56 nx_state_machine_allow(sm, 1, 3) // RUNNING -> TERMINATED 57 nx_state_machine_allow(sm, 2, 3) // SUSPENDED -> TERMINATED 58 59 if nx_state_machine_transition(sm, 1) != NX_SM_OK { return 5 } 60 if nx_state_machine_current(sm) != 1 { return 6 } 61 62 // ===== Step 3: event queue dispatches lifecycle events ========== 63 let q: *NxEventQueue = nx_event_queue_new(8) 64 65 // Vesicle delivered event (signature verified upstream) 66 nx_event_queue_emit(q, 1000, NX_EV_VESICLE_DELIVERED, 100, content_hash, 100) 67 if nx_event_queue_count(q) != 1 { return 7 } 68 69 // Xeno observation event triggers SUSPEND 70 nx_event_queue_emit(q, 1100, NX_EV_XENO_OBSERVATION, 100, 0xbadf00d, 200) 71 nx_event_queue_emit(q, 1200, NX_EV_HOMEOSTASIS_SIGNAL, 100, 0xfeed, 150) 72 if nx_event_queue_count(q) != 3 { return 8 } 73 74 // Drain + react 75 let out_k: *i64 = (sys_mmap(8)) as *i64 76 let out_s: *i64 = (sys_mmap(8)) as *i64 77 let out_p: *i64 = (sys_mmap(8)) as *i64 78 let out_t: *i64 = (sys_mmap(8)) as *i64 79 80 // First event: VESICLE_DELIVERED -- cell stays RUNNING 81 nx_event_queue_drain_one(q, out_k, out_s, out_p, out_t) 82 if out_k[0] != NX_EV_VESICLE_DELIVERED { return 9 } 83 if nx_state_machine_current(sm) != 1 { return 10 } // still RUNNING 84 85 // Second event: XENO_OBSERVATION -- cell SUSPENDS 86 nx_event_queue_drain_one(q, out_k, out_s, out_p, out_t) 87 if out_k[0] != NX_EV_XENO_OBSERVATION { return 11 } 88 nx_state_machine_transition(sm, 2) // RUNNING -> SUSPENDED 89 if nx_state_machine_current(sm) != 2 { return 12 } 90 91 // Third event: HOMEOSTASIS_SIGNAL -- cell can resume RUNNING 92 nx_event_queue_drain_one(q, out_k, out_s, out_p, out_t) 93 if out_k[0] != NX_EV_HOMEOSTASIS_SIGNAL { return 13 } 94 nx_state_machine_transition(sm, 1) // SUSPENDED -> RUNNING 95 if nx_state_machine_current(sm) != 1 { return 14 } 96 97 // Queue is empty after draining all 3 98 if nx_event_queue_count(q) != 0 { return 15 } 99 if nx_event_queue_total_emitted(q) != 3 { return 16 } 100 101 // ===== Step 4: hash deterministic across separately-derived chains 102 let msg2: *u8 = (sys_mmap(32)) as *u8 103 var k: nx_size = 0 104 while k < 16 { 105 msg2[k] = (65 + k) as u8 // same as msg 106 k = k + 1 107 } 108 let hash2: nx_size = nx_hash_facade_compute_bytes(msg2, 16) 109 if hash2 != content_hash { return 17 } // deterministic across buffers 110 111 // ===== Step 5: state machine transitions are sealed ============= 112 // RUNNING -> NASCENT is NOT allowed; refused 113 if nx_state_machine_transition(sm, 0) != NX_SM_ERR_BAD_TRANSITION { return 18 } 114 115 // Terminate (RUNNING -> TERMINATED) is allowed 116 nx_state_machine_transition(sm, 3) 117 if nx_state_machine_current(sm) != 3 { return 19 } 118 119 // No exit from TERMINATED 120 if nx_state_machine_transition(sm, 1) != NX_SM_ERR_BAD_TRANSITION { return 20 } 121 122 return 0 123}