code wiki / (root) / nx_xor_arena.nx

nx_xor_arena.nx source

↩ module page · 63 lines · 2636 B

1// nx_xor_arena.nx -- demo of the arena-based architecture-neutral 2// library pattern that resolves F5 (cross-architecture library + 3// orchestrator collision) per docs/NISHI_LANG_FRICTION_CATALOG.md. 4// 5// Why this exists: F5 demonstrated that crypto libraries 6// importing `nx_syscalls.nx` for sys_mmap become RV64-pinned, 7// preventing composition with x86_64 socket smokes. The 8// CANONICAL RESOLUTION is the arena pattern shipped in 9// `nx_arena_types.nx` (zero-syscall, caller-owned buffers per 10// cardinal user-owns-every-bit). This module is a tiny working 11// proof: a library that does real work (XOR two buffers, 12// allocate the output via arena) WITHOUT importing any syscall 13// file. The orchestrator provides the arena's backing buffer 14// using whichever architecture's syscall layer matches the 15// target. 16// 17// What this proves: 18// - Library is architecture-neutral (no syscall import). 19// - Library composes cleanly with x86_64 socket code in one 20// compilation unit (see bench/nx_arena_x86_64_compose_smoke.nx). 21// - Library equally composes with RV64 + qemu (caller swaps 22// the syscall layer for the arena backing buffer; library 23// code is unchanged). 24// 25// The crypto-stack refactor to follow this pattern (nx_x25519 + 26// nx_chacha20 + nx_poly1305 + nx_sha256 etc. taking *NxArena 27// instead of calling sys_mmap directly) is a 5-10 turn arc. 28// This module documents the target pattern + provides a proof- 29// of-correctness for the cross-architecture compose so the 30// refactor has a concrete checkpoint to validate against. 31// 32// nx_capability_claims: 33// needs: [arena_alloc, pointer_arithmetic] 34// provides: [xor_buf_arena] 35// safety: [no_syscall, no_floating_point, target_agnostic, 36// bit_equal_reproducible] 37// verdict: [no_silent_failure (arena oom returns null)] 38// license: ORIGINAL 39// kind: racing_crew_specialist 40// 41// license_tier: ORIGINAL 42// lineage_id: nishi_arena_pattern_demo_q10 43 44// nx_safety_envelope: 45// intended_use: AUTO_APPLIED -- primitive-specific tuning queued 46// sil_target: SIL1 47// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail] 48// verdict: NOT_YET_EVALUATED 49 50import "nx_arena_types.nx" 51 52// XOR two buffers; output is allocated from the caller-supplied 53// arena. Returns the output pointer or NULL on arena OOM. 54func xor_buf_arena(a: *NxArena, x: *u8, y: *u8, n: i64) -> *u8 { 55 let out: *u8 = nx_arena_alloc(a, n, 1) 56 if out == (0 as *u8) { return out } 57 var i: i64 = 0 58 while i < n { 59 out[i] = x[i] ^ y[i] 60 i = i + 1 61 } 62 return out 63}