pub const FORMAT_VERSION: u32 = 1;
pub const TARGET_WASM_GC: &str = "wasm-gc";
pub const TARGET_WASIP2: &str = "wasip2";
pub const PROFILE_ID: &str = "AverUserProfile/v1";
pub const RUNTIME_ABI_WASM_GC: &str = "aver-wasm-gc/0";
pub const RUNTIME_ABI_WASIP2: &str = "aver-wasip2/0";
pub const WASIP2_COMPONENT_ENVELOPE_FIELD: &str = "wasip2ComponentEnvelope";
pub const WASIP2_COMPONENT_ENVELOPE_KIND_FIELD: &str = "kind";
pub const WASIP2_COMPONENT_ENVELOPE_PREFIX_LEN_FIELD: &str = "prefix_len";
pub const WASIP2_COMPONENT_ENVELOPE_CORE_LEN_FIELD: &str = "embedded_core_module_len";
pub const WASIP2_COMPONENT_ENVELOPE_SUFFIX_LEN_FIELD: &str = "suffix_len";
pub const WASIP2_COMPONENT_ENVELOPE_KIND: &str = "prefix-core-suffix/v1";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Wasip2ComponentEnvelopeDeclaration {
pub prefix_len: u64,
pub embedded_core_module_len: u64,
pub suffix_len: u64,
}
impl Wasip2ComponentEnvelopeDeclaration {
pub const fn from_lengths(
prefix_len: u64,
embedded_core_module_len: u64,
suffix_len: u64,
) -> Self {
Self {
prefix_len,
embedded_core_module_len,
suffix_len,
}
}
pub const fn kind(&self) -> &'static str {
WASIP2_COMPONENT_ENVELOPE_KIND
}
pub fn component_len(&self) -> Option<u64> {
self.prefix_len
.checked_add(self.embedded_core_module_len)?
.checked_add(self.suffix_len)
}
pub fn embedded_core_module_range(&self) -> Option<std::ops::Range<u64>> {
let start = self.prefix_len;
let end = start.checked_add(self.embedded_core_module_len)?;
Some(start..end)
}
pub fn split_component<'a>(
&self,
component: &'a [u8],
) -> Option<(&'a [u8], &'a [u8], &'a [u8])> {
if self.embedded_core_module_len == 0 {
return None;
}
let prefix_len = usize::try_from(self.prefix_len).ok()?;
let core_len = usize::try_from(self.embedded_core_module_len).ok()?;
let suffix_len = usize::try_from(self.suffix_len).ok()?;
let core_end = prefix_len.checked_add(core_len)?;
let declared_len = core_end.checked_add(suffix_len)?;
if declared_len != component.len() {
return None;
}
Some((
&component[..prefix_len],
&component[prefix_len..core_end],
&component[core_end..],
))
}
}
pub const CERT_SCHEMA_VERSION: u32 = 9;
pub const PLAN_CLASS: &str = "source-plan-v1";
pub const MAX_CANDIDATE_LEN: usize = 200;
pub const ARTIFACT_CERTIFICATE_ROOT: &str = "AverCert.Artifact.certificate";
pub const FN_CLAIM_DISCHARGE_THEOREM: &str = "AcceptanceSoundness.fn_claim_discharges";
pub const CURRENT_WALL_ID: &str =
"sha256:7d577458dae255e1d07d57f7c70e51aabc376f7a5221ae9f661bf2d254eafb75";
pub const WASM_GC_CAPABILITIES: &[(&str, &str)] = &[
("aver", "console_print"),
("aver", "console_error"),
("aver", "console_warn"),
("aver", "time_unix_ms"),
("aver", "process_stop_requested"),
("aver", "provider_contract_violation"),
("aver", "request_method"),
("aver", "request_url"),
("aver", "request_query"),
("aver", "request_body"),
("aver", "request_headers_load"),
("aver", "response_text"),
("aver", "response_set_header"),
("aver", "http_send"),
("aver", "http_add_request_header"),
("aver", "http_clear_request_headers"),
("aver", "env_get"),
("aver", "env_set"),
("aver", "console_read_line"),
("aver", "args_len"),
("aver", "args_get"),
("aver", "random_float"),
("aver", "random_int"),
("aver", "time_sleep"),
("aver", "time_now"),
("aver", "float_sin"),
("aver", "float_cos"),
("aver", "float_atan2"),
("aver", "float_pow"),
("aver", "terminal_enable_raw_mode"),
("aver", "terminal_disable_raw_mode"),
("aver", "terminal_clear"),
("aver", "terminal_move_to"),
("aver", "terminal_print"),
("aver", "terminal_set_color"),
("aver", "terminal_reset_color"),
("aver", "terminal_read_key"),
("aver", "terminal_size"),
("aver", "terminal_hide_cursor"),
("aver", "terminal_show_cursor"),
("aver", "terminal_flush"),
("aver", "disk_read_text"),
("aver", "disk_write_text"),
("aver", "disk_append_text"),
("aver", "disk_read_bytes"),
("aver", "disk_read_bytes_at"),
("aver", "disk_write_bytes"),
("aver", "disk_append_bytes"),
("aver", "disk_size"),
("aver", "disk_exists"),
("aver", "disk_delete"),
("aver", "disk_delete_dir"),
("aver", "disk_list_dir"),
("aver", "disk_make_dir"),
("aver", "disk_sync"),
("aver", "tcp_connect"),
("aver", "tcp_begin_connect"),
("aver", "tcp_dialled"),
("aver", "tcp_listen"),
("aver", "tcp_accept"),
("aver", "tcp_peer_address"),
("aver", "tcp_write_line"),
("aver", "tcp_write_bytes"),
("aver", "tcp_write_now"),
("aver", "tcp_read_line"),
("aver", "tcp_read_bytes"),
("aver", "tcp_read_some"),
("aver", "tcp_read_now"),
("aver", "tcp_poll"),
("aver", "tcp_close"),
("aver", "tcp_close_dial"),
("aver", "tcp_close_listener"),
("aver", "tcp_send"),
("aver", "tcp_send_bytes"),
("aver", "tcp_ping"),
("aver", "http_get"),
("aver", "http_head"),
("aver", "http_delete"),
("aver", "http_post"),
("aver", "http_put"),
("aver", "http_patch"),
("aver", "record_enter_group"),
("aver", "record_set_branch"),
("aver", "record_exit_group"),
("aver", "wait_poll"),
("aver", "work_cancel"),
("aver", "work_begin"),
("aver", "work_take"),
("aver", "run_fail"),
("aver", "run_failure"),
("aver", "run_wait_starts"),
("aver", "run_wait_ends"),
("aver", "run_last_turn"),
("aver:work/v1", "submit"),
("aver:work/v1", "take"),
("aver:work/v1", "task"),
("aver:work/v1", "complete"),
];
pub const WASIP2_CAPABILITIES: &[(&str, &str)] = &[
("wasi:cli/stdout@0.2.4", "get-stdout"),
("wasi:cli/stderr@0.2.4", "get-stderr"),
(
"wasi:io/streams@0.2.4",
"[method]output-stream.blocking-write-and-flush",
),
("wasi:clocks/wall-clock@0.2.4", "now"),
("wasi:random/random@0.2.4", "get-random-u64"),
("wasi:cli/environment@0.2.4", "get-arguments"),
("wasi:cli/environment@0.2.4", "get-environment"),
("wasi:cli/stdin@0.2.4", "get-stdin"),
(
"wasi:io/streams@0.2.4",
"[method]input-stream.blocking-read",
),
("wasi:io/streams@0.2.4", "[method]input-stream.subscribe"),
("wasi:io/streams@0.2.4", "[method]input-stream.read"),
("wasi:io/streams@0.2.4", "[method]output-stream.check-write"),
("wasi:io/streams@0.2.4", "[method]output-stream.write"),
("wasi:io/streams@0.2.4", "[method]output-stream.flush"),
("wasi:io/streams@0.2.4", "[method]output-stream.subscribe"),
("wasi:clocks/monotonic-clock@0.2.4", "subscribe-duration"),
("wasi:io/poll@0.2.4", "poll"),
("wasi:io/poll@0.2.4", "[resource-drop]pollable"),
("wasi:filesystem/preopens@0.2.4", "get-directories"),
("wasi:filesystem/types@0.2.4", "[method]descriptor.stat-at"),
("wasi:filesystem/types@0.2.4", "[method]descriptor.open-at"),
(
"wasi:filesystem/types@0.2.4",
"[method]descriptor.read-via-stream",
),
("wasi:filesystem/types@0.2.4", "[resource-drop]descriptor"),
("wasi:io/streams@0.2.4", "[resource-drop]input-stream"),
(
"wasi:filesystem/types@0.2.4",
"[method]descriptor.write-via-stream",
),
("wasi:io/streams@0.2.4", "[resource-drop]output-stream"),
(
"wasi:filesystem/types@0.2.4",
"[method]descriptor.unlink-file-at",
),
(
"wasi:filesystem/types@0.2.4",
"[method]descriptor.remove-directory-at",
),
(
"wasi:filesystem/types@0.2.4",
"[method]descriptor.create-directory-at",
),
("wasi:filesystem/types@0.2.4", "[method]descriptor.sync"),
(
"wasi:filesystem/types@0.2.4",
"[method]descriptor.append-via-stream",
),
(
"wasi:filesystem/types@0.2.4",
"[method]descriptor.read-directory",
),
(
"wasi:filesystem/types@0.2.4",
"[method]directory-entry-stream.read-directory-entry",
),
(
"wasi:filesystem/types@0.2.4",
"[resource-drop]directory-entry-stream",
),
("wasi:http/types@0.2.4", "[constructor]fields"),
("wasi:http/types@0.2.4", "[constructor]outgoing-request"),
(
"wasi:http/types@0.2.4",
"[method]outgoing-request.set-scheme",
),
(
"wasi:http/types@0.2.4",
"[method]outgoing-request.set-authority",
),
(
"wasi:http/types@0.2.4",
"[method]outgoing-request.set-path-with-query",
),
("wasi:http/outgoing-handler@0.2.4", "handle"),
(
"wasi:http/types@0.2.4",
"[method]future-incoming-response.subscribe",
),
(
"wasi:http/types@0.2.4",
"[method]future-incoming-response.get",
),
("wasi:http/types@0.2.4", "[method]incoming-response.status"),
("wasi:http/types@0.2.4", "[method]incoming-response.consume"),
("wasi:http/types@0.2.4", "[method]incoming-body.stream"),
("wasi:http/types@0.2.4", "[static]incoming-body.finish"),
("wasi:http/types@0.2.4", "[resource-drop]outgoing-request"),
(
"wasi:http/types@0.2.4",
"[resource-drop]future-incoming-response",
),
("wasi:http/types@0.2.4", "[resource-drop]incoming-response"),
("wasi:http/types@0.2.4", "[resource-drop]future-trailers"),
("wasi:http/types@0.2.4", "[resource-drop]incoming-body"),
("wasi:http/types@0.2.4", "[method]incoming-response.headers"),
("wasi:http/types@0.2.4", "[method]fields.entries"),
("wasi:http/types@0.2.4", "[resource-drop]fields"),
(
"wasi:http/types@0.2.4",
"[method]outgoing-request.set-method",
),
("wasi:http/types@0.2.4", "[method]outgoing-request.body"),
("wasi:http/types@0.2.4", "[method]outgoing-body.write"),
("wasi:http/types@0.2.4", "[static]outgoing-body.finish"),
("wasi:http/types@0.2.4", "[method]fields.append"),
("wasi:http/types@0.2.4", "[resource-drop]outgoing-body"),
("wasi:http/types@0.2.4", "[method]incoming-request.method"),
(
"wasi:http/types@0.2.4",
"[method]incoming-request.path-with-query",
),
("wasi:http/types@0.2.4", "[method]incoming-request.headers"),
("wasi:http/types@0.2.4", "[method]incoming-request.consume"),
("wasi:http/types@0.2.4", "[resource-drop]incoming-request"),
("wasi:http/types@0.2.4", "[constructor]outgoing-response"),
(
"wasi:http/types@0.2.4",
"[method]outgoing-response.set-status-code",
),
("wasi:http/types@0.2.4", "[method]outgoing-response.body"),
("wasi:http/types@0.2.4", "[static]response-outparam.set"),
("wasi:sockets/instance-network@0.2.4", "instance-network"),
("wasi:sockets/ip-name-lookup@0.2.4", "resolve-addresses"),
(
"wasi:sockets/ip-name-lookup@0.2.4",
"[method]resolve-address-stream.resolve-next-address",
),
(
"wasi:sockets/ip-name-lookup@0.2.4",
"[method]resolve-address-stream.subscribe",
),
(
"wasi:sockets/ip-name-lookup@0.2.4",
"[resource-drop]resolve-address-stream",
),
("wasi:sockets/tcp-create-socket@0.2.4", "create-tcp-socket"),
("wasi:sockets/tcp@0.2.4", "[method]tcp-socket.start-connect"),
(
"wasi:sockets/tcp@0.2.4",
"[method]tcp-socket.finish-connect",
),
("wasi:sockets/tcp@0.2.4", "[method]tcp-socket.subscribe"),
("wasi:sockets/tcp@0.2.4", "[method]tcp-socket.shutdown"),
("wasi:sockets/tcp@0.2.4", "[resource-drop]tcp-socket"),
];
pub fn is_capability_import_for_target(target: &str, module: &str, field: &str) -> bool {
let standard = match target {
TARGET_WASM_GC => WASM_GC_CAPABILITIES.contains(&(module, field)),
TARGET_WASIP2 => WASIP2_CAPABILITIES.contains(&(module, field)),
_ => return false,
};
standard || is_custom_capability_import(module, field)
}
pub fn is_wasm_gc_capability_import(module: &str, field: &str) -> bool {
is_capability_import_for_target(TARGET_WASM_GC, module, field)
}
pub fn is_wasip2_capability_import(module: &str, field: &str) -> bool {
is_capability_import_for_target(TARGET_WASIP2, module, field)
}
fn is_custom_capability_import(module: &str, field: &str) -> bool {
const MODULE_PREFIX: &str = "aver:user/cap-n";
const OP_PREFIX: &str = "op-n";
let Some(module_tail) = module.strip_prefix(MODULE_PREFIX) else {
return false;
};
let Some((module_hex, contract_hash)) = module_tail.split_once("-c") else {
return false;
};
let Some(operation_hex) = field.strip_prefix(OP_PREFIX) else {
return false;
};
is_nonempty_even_lower_hex(module_hex)
&& contract_hash.len() == 64
&& is_lower_hex(contract_hash)
&& is_nonempty_even_lower_hex(operation_hex)
}
fn is_nonempty_even_lower_hex(value: &str) -> bool {
!value.is_empty() && value.len().is_multiple_of(2) && is_lower_hex(value)
}
fn is_lower_hex(value: &str) -> bool {
value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wasip2_envelope_surface_names_component_binding() {
assert_eq!(TARGET_WASM_GC, "wasm-gc");
assert_eq!(TARGET_WASIP2, "wasip2");
assert_eq!(RUNTIME_ABI_WASM_GC, "aver-wasm-gc/0");
assert_eq!(RUNTIME_ABI_WASIP2, "aver-wasip2/0");
assert_eq!(WASIP2_COMPONENT_ENVELOPE_FIELD, "wasip2ComponentEnvelope");
assert_eq!(WASIP2_COMPONENT_ENVELOPE_KIND_FIELD, "kind");
assert_eq!(WASIP2_COMPONENT_ENVELOPE_PREFIX_LEN_FIELD, "prefix_len");
assert_eq!(
WASIP2_COMPONENT_ENVELOPE_CORE_LEN_FIELD,
"embedded_core_module_len"
);
assert_eq!(WASIP2_COMPONENT_ENVELOPE_SUFFIX_LEN_FIELD, "suffix_len");
assert_eq!(WASIP2_COMPONENT_ENVELOPE_KIND, "prefix-core-suffix/v1");
assert_eq!(CERT_SCHEMA_VERSION, 9);
}
#[test]
fn wasip2_component_envelope_declaration_splits_by_declared_lengths_only() {
let declaration = Wasip2ComponentEnvelopeDeclaration::from_lengths(2, 3, 1);
assert_eq!(declaration.kind(), WASIP2_COMPONENT_ENVELOPE_KIND);
assert_eq!(declaration.component_len(), Some(6));
assert_eq!(declaration.embedded_core_module_range(), Some(2..5));
let (prefix, core, suffix) = declaration
.split_component(b"abcdef")
.expect("declared lengths match the component");
assert_eq!(prefix, b"ab");
assert_eq!(core, b"cde");
assert_eq!(suffix, b"f");
assert!(
Wasip2ComponentEnvelopeDeclaration::from_lengths(2, 0, 1)
.split_component(b"abc")
.is_none(),
"an empty embedded core declaration is not meaningful"
);
assert!(
Wasip2ComponentEnvelopeDeclaration::from_lengths(2, 2, 0)
.split_component(b"abc")
.is_none(),
"the declaration must account for the whole delivered component"
);
assert_eq!(
Wasip2ComponentEnvelopeDeclaration::from_lengths(u64::MAX, 1, 0).component_len(),
None,
"length overflow must fail closed"
);
}
#[test]
fn capability_pairs_are_unique() {
assert_eq!(WASIP2_CAPABILITIES.len(), 80);
for registry in [WASM_GC_CAPABILITIES, WASIP2_CAPABILITIES] {
let unique = registry
.iter()
.copied()
.collect::<std::collections::BTreeSet<_>>();
assert_eq!(unique.len(), registry.len());
}
}
#[test]
fn standard_capability_imports_are_target_specific_and_version_exact() {
let wasi_pair = ("wasi:cli/stdout@0.2.4", "get-stdout");
assert!(is_wasip2_capability_import(wasi_pair.0, wasi_pair.1));
assert!(!is_wasm_gc_capability_import(wasi_pair.0, wasi_pair.1));
assert!(!is_wasip2_capability_import(
"wasi:cli/stdout@0.2.5",
wasi_pair.1
));
assert!(!is_wasip2_capability_import(
"wasi:cli/unknown@0.2.4",
wasi_pair.1
));
assert!(!is_wasip2_capability_import(
wasi_pair.0,
"unknown-operation"
));
let wasm_gc_pair = ("aver", "console_print");
assert!(is_wasm_gc_capability_import(wasm_gc_pair.0, wasm_gc_pair.1));
assert!(!is_wasip2_capability_import(wasm_gc_pair.0, wasm_gc_pair.1));
}
#[test]
fn custom_capability_import_syntax_is_exact() {
let hash = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
assert!(is_wasm_gc_capability_import(
&format!("aver:user/cap-n436c6f636b-c{hash}"),
"op-n6e6f77"
));
assert!(is_wasip2_capability_import(
&format!("aver:user/cap-n436c6f636b-c{hash}"),
"op-n6e6f77"
));
for (module, field) in [
(format!("aver:user/cap-n-c{hash}"), "op-n6e6f77"),
(format!("aver:user/cap-n436c6f636b-c{hash}0"), "op-n6e6f77"),
(format!("aver:user/cap-n436C6f636b-c{hash}"), "op-n6e6f77"),
(format!("aver:user/cap-n436c6f636b-c{hash}"), "op-n"),
(format!("aver:user/cap-n436c6f636b-c{hash}"), "op-nxyz"),
] {
assert!(!is_wasm_gc_capability_import(&module, field));
}
}
#[test]
fn format_spec_states_the_current_wall_identity_and_source_count() {
let spec = include_str!("../../docs/certificate-format.md");
assert!(
spec.contains(CURRENT_WALL_ID),
"certificate-format.md does not state the current wall id {CURRENT_WALL_ID}; \
update the wall identity printed in the spec"
);
let count_phrase = format!(
"{} embedded `.lean` wall sources",
crate::wall::SOURCES.len()
);
assert!(
spec.contains(&count_phrase),
"certificate-format.md does not state \"{count_phrase}\"; \
update the wall source count printed in the spec"
);
}
#[test]
fn format_spec_states_the_current_schema_version() {
let spec = include_str!("../../docs/certificate-format.md");
for phrase in [
format!("`{CERT_SCHEMA_VERSION}` (`CERT_SCHEMA_VERSION`)"),
format!("schema_version = {CERT_SCHEMA_VERSION}"),
] {
assert!(
spec.contains(&phrase),
"certificate-format.md does not state \"{phrase}\"; \
update the schema version printed in the spec"
);
}
}
#[test]
fn format_spec_documents_wasip2_envelope_surface() {
let spec = include_str!("../../docs/certificate-format.md");
for phrase in [
TARGET_WASIP2,
RUNTIME_ABI_WASIP2,
WASIP2_COMPONENT_ENVELOPE_FIELD,
WASIP2_COMPONENT_ENVELOPE_KIND_FIELD,
WASIP2_COMPONENT_ENVELOPE_PREFIX_LEN_FIELD,
WASIP2_COMPONENT_ENVELOPE_CORE_LEN_FIELD,
WASIP2_COMPONENT_ENVELOPE_SUFFIX_LEN_FIELD,
WASIP2_COMPONENT_ENVELOPE_KIND,
] {
assert!(
spec.contains(phrase),
"certificate-format.md does not document wasip2 envelope surface `{phrase}`"
);
}
}
#[test]
fn the_two_schema_version_constants_agree() {
let engine_src = include_str!("engine/mod.rs");
let expected = "pub const CERT_SCHEMA_VERSION: u32 = crate::format::CERT_SCHEMA_VERSION;";
assert!(
engine_src.contains(expected),
"engine/mod.rs does not delegate to `{expected}`; the producer would emit a \
schema version the transport parser rejects"
);
}
#[test]
fn every_wall_contract_string_has_a_verbatim_producer_twin() {
let axes = include_str!("../assets/wall/current/ClaimAxes.lean");
let engine_src = include_str!("engine/mod.rs");
let mut seen = 0usize;
let mut lines = axes.lines();
while let Some(line) = lines.next() {
let trimmed = line.trim();
if !(trimmed.starts_with("def ") && trimmed.ends_with("Contract : String :=")) {
continue;
}
let literal = lines
.next()
.expect("a contract definition is followed by its literal")
.trim();
assert!(
literal.starts_with('"') && literal.ends_with('"'),
"expected a one-line string literal under `{trimmed}`, got: {literal}"
);
assert!(
engine_src.contains(literal),
"wall contract `{trimmed}` has no verbatim producer twin in \
engine/mod.rs: {literal}"
);
seen += 1;
}
assert!(
seen >= 12,
"only {seen} wall contract definitions were found; the scan shape has \
drifted and this test has stopped checking anything"
);
}
}