use crate::{Convention, DataType, OpSignature, OpSpec};
pub const VYRE_OP_METADATA: vyre_spec::OpMetadata = vyre_spec::OpMetadata {
id: "decode.hex",
layer: vyre_spec::Layer::L2,
category: vyre_spec::MetadataCategory::A,
version: 1,
description: "decode hex",
signature: "(Bytes) -> Bytes",
strictness: "strict",
archetype_signature: "(Bytes) -> Bytes",
};
pub const GOLDEN: &[vyre_spec::GoldenSample] = &[vyre_spec::GoldenSample {
op_id: "decode.hex",
input: b"",
expected: b"",
reason: "empty hex input decodes to empty output",
}];
pub const KAT: &[vyre_spec::KatVector] = &[
vyre_spec::KatVector {
input: b"",
expected: b"",
source: "RFC 4648 §8 (base16)",
},
vyre_spec::KatVector {
input: b"00",
expected: b"\x00",
source: "RFC 4648 §8 (base16)",
},
vyre_spec::KatVector {
input: b"ff",
expected: b"\xff",
source: "RFC 4648 §8 (base16)",
},
vyre_spec::KatVector {
input: b"FF",
expected: b"\xff",
source: "RFC 4648 §8 case-insensitive",
},
vyre_spec::KatVector {
input: b"0123456789abcdef",
expected: b"\x01\x23\x45\x67\x89\xab\xcd\xef",
source: "hand-verified lowercase",
},
vyre_spec::KatVector {
input: b"0123456789ABCDEF",
expected: b"\x01\x23\x45\x67\x89\xab\xcd\xef",
source: "hand-verified uppercase",
},
vyre_spec::KatVector {
input: b"deadbeef",
expected: b"\xde\xad\xbe\xef",
source: "classic marker word",
},
vyre_spec::KatVector {
input: b"48656c6c6f",
expected: b"Hello",
source: "ASCII round-trip",
},
];
pub const ADVERSARIAL: &[vyre_spec::AdversarialInput] = &[
vyre_spec::AdversarialInput {
input: b"",
reason: "empty input exercises zero-length branch",
},
vyre_spec::AdversarialInput {
input: b"a",
reason: "odd-length input — half a byte, must reject",
},
vyre_spec::AdversarialInput {
input: b"gg",
reason: "non-hex alphabet characters must be rejected",
},
vyre_spec::AdversarialInput {
input: b"0x0",
reason: "0x prefix is not part of strict base16; must not be silently stripped",
},
vyre_spec::AdversarialInput {
input: b" ff ",
reason: "whitespace must not be silently skipped in strict decode",
},
];
#[inline]
pub fn vyre_op() -> OpSpec {
let id = "decode.hex";
OpSpec::builder(id)
.signature(OpSignature {
inputs: vec![DataType::Bytes],
output: DataType::Bytes,
})
.cpu_fn(cpu_fn)
.wgsl_fn(wgsl_fn)
.category(crate::Category::A {
composition_of: vec![id],
})
.laws(vec![crate::spec::law::AlgebraicLaw::Bounded {
lo: 0,
hi: u32::MAX,
}])
.strictness(crate::spec::types::Strictness::Strict)
.version(1)
.alt_wgsl_fns(vec![("category_a_handwritten", wgsl_fn)])
.convention(Convention::V1)
.boundary_values(vec![
crate::spec::types::BoundaryValue {
label: "empty",
inputs: vec![0],
},
crate::spec::types::BoundaryValue {
label: "single_element",
inputs: vec![1],
},
crate::spec::types::BoundaryValue {
label: "boundary",
inputs: vec![255],
},
crate::spec::types::BoundaryValue {
label: "max",
inputs: vec![u32::MAX],
},
])
.equivalence_classes(vec![
crate::spec::types::EquivalenceClass::specific("empty input", vec![0]),
crate::spec::types::EquivalenceClass::specific("typical input", vec![42]),
crate::spec::types::EquivalenceClass::specific("boundary input", vec![255]),
])
.spec_table(crate::spec::tables::hex_decode::ROWS)
.expect("Fix: checked-in conform spec must satisfy the typestate builder")
}
#[inline]
pub fn cpu_fn(input: &[u8]) -> Vec<u8> {
let mut out = Vec::new();
let mut cursor = 0;
while cursor < input.len() {
if hex_value(input[cursor]).is_none()
|| (cursor > 0 && hex_value(input[cursor - 1]).is_some())
{
cursor += 1;
continue;
}
let start = cursor;
let mut end = start;
while end < input.len() && hex_value(input[end]).is_some() {
end += 1;
}
if (end - start) >= 2 && (end - start) % 2 == 0 {
out.extend(
input[start..end]
.chunks_exact(2)
.filter_map(|pair| Some((hex_value(pair[0])? << 4) | hex_value(pair[1])?)),
);
}
cursor = end;
}
out
}
fn hex_value(value: u8) -> Option<u8> {
match value {
b'0'..=b'9' => Some(value - b'0'),
b'A'..=b'F' => Some(value - b'A' + 10),
b'a'..=b'f' => Some(value - b'a' + 10),
_ => None,
}
}
#[inline]
pub fn wgsl_fn() -> String {
r#"
fn hex_value(value: u32) -> i32 {
if (value >= 48u && value <= 57u) { return i32(value - 48u); }
if (value >= 65u && value <= 70u) { return i32(value - 55u); }
if (value >= 97u && value <= 102u) { return i32(value - 87u); }
return 0;
}
fn vyre_op(index: u32, input_len: u32) -> u32 {
let read = index * 2u;
if (read + 1u >= input_len) { return 0u; }
return (u32(hex_value(input.data[read])) << 4u) | u32(hex_value(input.data[read + 1u]));
}
"#
.to_string()
}