Skip to main content

uor_addr/codemodule/
pipeline.rs

1//! `codemodule::address` — the code-module AST realization's public
2//! entry point.
3//!
4//! 1. [`SExprCanon::validate`] checks the CCMAS S-expression grammar at
5//!    the host boundary over the borrowed input — no buffer, no caps.
6//! 2. [`AddressModel::forward`] runs the shared ψ-tower: the borrowed
7//!    [`SExprCanon`] flows in as an ADR-060 `Stream` carrier that emits
8//!    Rivest canonical bytes on demand (CCMAS canonical form), and ψ₉
9//!    folds them through `H = Sha256Hasher` to mint the κ-label.
10//! 3. [`AddressOutcome::from_grounded`] extracts the owned κ-label +
11//!    replayable TC-05 witness.
12//!
13//! The entry point is `no_alloc`: CCMAS canonical bytes stream from the
14//! borrowed input without materialization.
15
16pub use crate::outcome::{AddressOutcome, AddressWitness, VerifyError};
17
18/// Failure modes from [`address`].
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum AddressFailure {
21    /// The input bytes were not a well-formed CCMAS S-expression.
22    InvalidAst,
23    /// Defensive: foundation's catamorphism or a resolver returned a
24    /// shape violation. Unreachable for well-formed inputs.
25    PipelineFailure,
26}
27
28use crate::codemodule::model::{
29    AddressModel, AddressModelBlake3, AddressModelKeccak256, AddressModelSha3_256,
30    AddressModelSha512,
31};
32use crate::codemodule::value::CodeModuleCarrier;
33use crate::sexp::SExprCanon;
34use prism::pipeline::PrismModel;
35
36/// **uor-addr's codemodule entry point** (σ-axis `Sha256Hasher`) — one
37/// ψ-pipeline content-address inference, yielding a `sha256:<64hex>`
38/// κ-label.
39///
40/// # Errors
41///
42/// - [`AddressFailure::InvalidAst`] — the input is not well-formed.
43/// - [`AddressFailure::PipelineFailure`] — defensive; unreachable.
44pub fn address(input_bytes: &[u8]) -> Result<AddressOutcome<71>, AddressFailure> {
45    SExprCanon::validate(input_bytes).map_err(|_| AddressFailure::InvalidAst)?;
46    let canon = SExprCanon::new(input_bytes);
47    let grounded = AddressModel::forward(CodeModuleCarrier::new(&canon))
48        .map_err(|_| AddressFailure::PipelineFailure)?;
49    AddressOutcome::<71>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
50}
51
52/// The codemodule entry point under σ-axis `Blake3Hasher` — yields a
53/// `blake3:<64hex>` κ-label. See [`address`] for the error contract.
54///
55/// # Errors
56///
57/// As [`address`].
58pub fn address_blake3(input_bytes: &[u8]) -> Result<AddressOutcome<71>, AddressFailure> {
59    SExprCanon::validate(input_bytes).map_err(|_| AddressFailure::InvalidAst)?;
60    let canon = SExprCanon::new(input_bytes);
61    let grounded = AddressModelBlake3::forward(CodeModuleCarrier::new(&canon))
62        .map_err(|_| AddressFailure::PipelineFailure)?;
63    AddressOutcome::<71>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
64}
65
66/// The codemodule entry point under σ-axis `Sha3_256Hasher` — yields a
67/// `sha3-256:<64hex>` κ-label. See [`address`] for the error contract.
68///
69/// # Errors
70///
71/// As [`address`].
72pub fn address_sha3_256(input_bytes: &[u8]) -> Result<AddressOutcome<73>, AddressFailure> {
73    SExprCanon::validate(input_bytes).map_err(|_| AddressFailure::InvalidAst)?;
74    let canon = SExprCanon::new(input_bytes);
75    let grounded = AddressModelSha3_256::forward(CodeModuleCarrier::new(&canon))
76        .map_err(|_| AddressFailure::PipelineFailure)?;
77    AddressOutcome::<73>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
78}
79
80/// The codemodule entry point under σ-axis `Keccak256Hasher` — yields a
81/// `keccak256:<64hex>` κ-label. See [`address`] for the error contract.
82///
83/// # Errors
84///
85/// As [`address`].
86pub fn address_keccak256(input_bytes: &[u8]) -> Result<AddressOutcome<74>, AddressFailure> {
87    SExprCanon::validate(input_bytes).map_err(|_| AddressFailure::InvalidAst)?;
88    let canon = SExprCanon::new(input_bytes);
89    let grounded = AddressModelKeccak256::forward(CodeModuleCarrier::new(&canon))
90        .map_err(|_| AddressFailure::PipelineFailure)?;
91    AddressOutcome::<74>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
92}
93
94/// The codemodule entry point under σ-axis `Sha512Hasher` — yields a
95/// `sha512:<128hex>` κ-label (135 bytes, 64-byte fingerprint). See
96/// [`address`] for the error contract.
97///
98/// # Errors
99///
100/// As [`address`].
101pub fn address_sha512(input_bytes: &[u8]) -> Result<AddressOutcome<135, 64>, AddressFailure> {
102    SExprCanon::validate(input_bytes).map_err(|_| AddressFailure::InvalidAst)?;
103    let canon = SExprCanon::new(input_bytes);
104    let grounded = AddressModelSha512::forward(CodeModuleCarrier::new(&canon))
105        .map_err(|_| AddressFailure::PipelineFailure)?;
106    AddressOutcome::<135, 64>::from_grounded(&grounded).map_err(|_| AddressFailure::PipelineFailure)
107}