// Example 24: Signed `'VKP1'` Package Bundles (Merkle Tree + Hybrid Ed25519 & Post-Quantum ML-DSA-65)
// Demonstrates packing a multi-module Veko package into a single-file `'VKP1'` archive,
// dual-signing with Ed25519 + FIPS 204 ML-DSA-65, verifying trust anchors, extracting modules,
// and rejecting both tampered bytes and untrusted keys.
//
// Track A (ORO VM) : veko run examples/24_signed_vkp_package_bundles.vk
// Track B (Wasm 3) : veko run --wasm --check-slabs --fuel=0 examples/24_signed_vkp_package_bundles.vk
// Track C (Native) : veko compile --native examples/24_signed_vkp_package_bundles.vk -o dist/24.exe ; ./dist/24.exe
use [
hex = codec/hex
x25 = crypto/x25519
dsa = crypto/mldsa
vkp = pkg/vkp
]
// 1. Generate classical Ed25519 + post-quantum ML-DSA-65 signing keys
ed_seed = bytes/from-str 'vkp1-example-ed25519-seed-012345'
ed_pk = x25/ed25519-pubkey ed_seed
pq_kp = dsa/keygen .mldsa65
manifest = {
name = 'veko-stats'
version = '1.0.0'
description = 'Pure-Veko descriptive statistics package'
}
files = {
'mod.vk' = 'use [st = math/stats]\npub mean = fn [xs block] i64 [ st/avg xs ]\n'
'math/stats.vk' = 'pub avg = fn [xs block] i64 [ (xs[0] + xs[1]) / 2 ]\n'
}
// 2. Pack and hybrid dual-sign (Ed25519 + ML-DSA-65)
bundle_bytes = vkp/pack manifest files ed_seed pq_kp
ed_trust = vkp/format-trust-key .ed25519 ed_pk
pq_trust = vkp/format-trust-key .mldsa65 pq_kp.pk
// 3. Verify against trusted Ed25519 and ML-DSA-65 keys
verified_ed = vkp/verify bundle_bytes [ed_trust]
verified_pq = vkp/verify bundle_bytes [pq_trust]
if (len verified_ed.entries) != 2 or (len verified_pq.signatures) != 2 [
err 'example24: vkp verify failed'
]
mod_src = vkp/extract-module verified_pq 'math/stats.vk'
if not (str/contains? mod_src 'pub avg') [
err 'example24: extract-module failed'
]
// 4. Tamper detection: flip 1 byte in payload -> rejected by SHA-256 / Merkle / signature check
mut tampered = bytes/with-cap (len bundle_bytes)
_ = bytes/push-all mut tampered bundle_bytes
flip_pos = (len tampered) / 2
_ = bytes/set mut tampered flip_pos ((tampered[flip_pos]) ^ 0x01)
tamper_ok = vkp/verify/or tampered [ed_trust] [ .rejected ]
if tamper_ok != .rejected [
err 'example24: tampered bundle was not rejected'
]
// 5. Trust-anchor enforcement: valid bundle signed by unknown key -> rejected
other_seed = bytes/from-str 'untrusted-other-ed25519-seed-999'
other_trust = vkp/format-trust-key .ed25519 (x25/ed25519-pubkey other_seed)
untrusted_ok = vkp/verify/or bundle_bytes [other_trust] [ .rejected ]
if untrusted_ok != .rejected [
err 'example24: untrusted key was not rejected'
]
println (str/join ['vkp1=ok|bytes=' (mold (len bundle_bytes)) '|entries=' (mold (len verified_pq.entries)) '|sigs=Ed25519+ML-DSA-65|merkle=' (hex/encode verified_pq.merkle_root)[0..16]] '')
println 'security=tamper-rejected|untrusted-key-rejected'