// Example 28: Phase 5 No-JIT Hardware Intrinsics, Bulk SIMD Bytes & Cross-Target Verification
// Demonstrates the 6 Phase 5 hardware/bulk builtins across all 3 execution tracks
// (`f64/sqrt`, `bytes/eq`, `bytes/index-of`, `bytes/xor`, `bytes/xor!`, `bytes/aes-enc-block`),
// NIST FIPS-197 AES-128 & AES-256 Known-Answer Tests, AES-256-GCM AEAD round-trip,
// and IEEE 802.3 Sarwate table-driven `bits/crc32`.
//
// Track A (ORO VM) : veko run examples/28_simd_optimization_and_arm64_cross_compile.vk
// Track B (Wasm3) : veko run --wasm --fuel=0 examples/28_simd_optimization_and_arm64_cross_compile.vk
// Track C (Native) : veko run --native examples/28_simd_optimization_and_arm64_cross_compile.vk
// Cross AArch64 : veko compile --native --target=linux-arm64 examples/28_simd_optimization_and_arm64_cross_compile.vk -o dist/28_arm64
// Cross macOS arm64: veko compile --native --target=macos-arm64 examples/28_simd_optimization_and_arm64_cross_compile.vk -o dist/28_macos
use [
m = math
bits
hex = codec/hex
aes = crypto/aes
]
must = fn [cond bool, msg str] [
if not cond [
println (str/join ['FAIL: ' msg] '')
os/exit 1
]
.ok
]
// 1. Hardware IEEE-754 square root (`f64/sqrt` -> SQRTSD / FSQRT / f64.sqrt)
s81 = f64/sqrt 81.0
s2 = f64/sqrt 2.0
s0 = f64/sqrt 0.0
m2 = m/sqrt 2.0
must (s81 == 9.0 and s0 == 0.0) 'f64/sqrt exact values'
must ((abs (s2 - 1.4142135623730951)) < 0.000000000001 and (abs (m2 - s2)) < 0.000000000001) 'f64/sqrt vs math/sqrt parity'
println 'sqrt=ok|f64_sqrt_81=9.0|math_sqrt_parity=true'
// 2. Bulk SIMD / 8-byte word-wide buffer builtins (`bytes/eq`, `bytes/index-of`, `bytes/xor`, `bytes/xor!`)
// Exercise 96-byte buffers so x86_64 AVX2/SSE2 (32B/16B lanes) and ARM64 8B word loops both run.
mut buf_a = bytes/with-cap 96
mut buf_b = bytes/with-cap 96
mut key_buf = bytes/with-cap 96
mut i = 0
while i < 96 [
_ = bytes/push mut buf_a ((i * 7 + 13) & 0xff)
_ = bytes/push mut buf_b ((i * 7 + 13) & 0xff)
_ = bytes/push mut key_buf ((i * 19 + 5) & 0xff)
i = i + 1
]
must (bytes/eq buf_a buf_b) 'bytes/eq equal 96B buffers'
_ = bytes/set mut buf_b 95 ((buf_b[95] ^ 1) & 0xff)
must (not (bytes/eq buf_a buf_b)) 'bytes/eq detects trailing-byte diff'
// Search with `bytes/index-of` across >64B haystack
hay = bytes/from-str 'veko-simd-acceleration-pipeline-zero-jit-deterministic-memory-sentinel-99-tail'
pos_mid = bytes/index-of hay (bytes/from-str 'sentinel-99')
pos_head = bytes/index-of hay (bytes/from-str 'veko-simd')
pos_miss = bytes/index-of hay (bytes/from-str 'not-present')
pos_empty = bytes/index-of hay (bytes/with-cap 0)
must (pos_head == 0 and pos_mid == 62 and pos_miss == -1 and pos_empty == 0) 'bytes/index-of positions'
// Functional `bytes/xor` and in-place ORO `bytes/xor!` round-trip
xored = bytes/xor buf_a key_buf
restored = bytes/xor xored key_buf
must ((not (bytes/eq xored buf_a)) and (bytes/eq restored buf_a)) 'bytes/xor round-trip'
mut in_place = bytes/with-cap 96
_ = bytes/push-all mut in_place buf_a
_ = bytes/xor! mut in_place key_buf
must (bytes/eq in_place xored) 'bytes/xor! parity with bytes/xor'
_ = bytes/xor! mut in_place key_buf
must (bytes/eq in_place buf_a) 'bytes/xor! inverse'
println (str/join ['bytes_bulk=ok|len=96|index_of=' (mold pos_mid) '|xor_inplace=true'] '')
// 3. Hardware AES-NI / Constant-Time Algebraic `bytes/aes-enc-block` (NIST FIPS-197 Known-Answer Tests)
// FIPS-197 Appendix B (AES-128 via `crypto/aes/encrypt-block` -> `bytes/aes-enc-block`)
k128 = hex/decode '2b7e151628aed2a6abf7158809cf4f3c'
pt128 = hex/decode '3243f6a8885a308d313198a2e0370734'
ct128 = aes/encrypt-block k128 pt128
h128 = hex/encode ct128
must (h128 == '3925841d02dc09fbdc118597196a0b32') (str/join ['FIPS-197 AES-128 mismatch: ' h128] '')
// FIPS-197 Appendix C.3 (AES-256)
k256 = hex/decode '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
pt256 = hex/decode '00112233445566778899aabbccddeeff'
ct256 = aes/encrypt-block k256 pt256
h256 = hex/encode ct256
must (h256 == '8ea2b7ca516745bfeafc49904b496089') (str/join ['FIPS-197 AES-256 mismatch: ' h256] '')
// Direct `bytes/aes-enc-block` bounds check (`rk` must be 176/208/240 bytes) + zero-key direct call
bad_rk = bytes/aes-enc-block/or pt128 k128 [ bytes/fill 1 0xff ]
must ((len bad_rk) == 1 and bad_rk[0] == 0xff) 'bytes/aes-enc-block rejects unexpanded key'
z_blk = bytes/fill 16 0
z_rk = bytes/fill 176 0
z_ct = bytes/aes-enc-block z_blk z_rk
must ((len z_ct) == 16) 'bytes/aes-enc-block direct 176B round-key call'
p128 = h128[0..16]
p256 = h256[0..16]
println (str/join ['aes_nist_kat=ok|aes128=' p128 '...|aes256=' p256 '...'] '')
// 4. Accelerated AES-256-GCM AEAD round-trip + IEEE 802.3 Sarwate CRC32
nonce = hex/decode '00112233445566778899aabb'
aad = bytes/from-str 'veko-phase5-arm64'
msg = bytes/from-str 'Deterministic No-JIT ORO execution across x86_64, AArch64, and Wasm 3.0!'
sealed = aes/gcm-encrypt k256 nonce msg aad
opened = aes/gcm-decrypt k256 nonce sealed aad
must (bytes/eq opened msg) 'AES-256-GCM round-trip'
crc = bits/crc32 (bytes/from-str '123456789')
must (crc == 0xcbf43926) 'IEEE 802.3 CRC32 vector'
println (str/join ['aes256_gcm=ok|sealed_len=' (mold (len sealed)) '|crc32_ieee=0xcbf43926'] '')