Skip to main content

prism_crypto/
shapes.rs

1//! Parametric `ConstrainedTypeShape` carriers per [Wiki ADR-031][09-adr-031]:
2//! `Digest<N>`, `PublicKey<N>`, `Signature<N>`.
3//!
4//! Each shape is a phantom `N`-byte carrier. Per ADR-017's closure rule
5//! identity flows through `(SITE_COUNT, CONSTRAINTS)`, so distinct
6//! shape types with the same byte count content-address identically —
7//! the Rust name is for the developer, the content-address is for the
8//! ecosystem.
9//!
10//! [09-adr-031]: https://github.com/UOR-Foundation/UOR-Framework/wiki/09-Architecture-Decisions
11
12use uor_foundation::enforcement::{GroundedShape, ShapeViolation};
13use uor_foundation::pipeline::{ConstrainedTypeShape, ConstraintRef, IntoBindingValue};
14
15macro_rules! parametric_byte_shape {
16    ($(#[$attr:meta])* $name:ident) => {
17        $(#[$attr])*
18        #[derive(Debug, Clone, Copy)]
19        pub struct $name<const BYTES: usize>;
20
21        impl<const BYTES: usize> Default for $name<BYTES> {
22            fn default() -> Self {
23                Self
24            }
25        }
26
27        impl<const BYTES: usize> ConstrainedTypeShape for $name<BYTES> {
28            const IRI: &'static str = "https://uor.foundation/type/ConstrainedType";
29            const SITE_COUNT: usize = BYTES;
30            const CONSTRAINTS: &'static [ConstraintRef] = &[];
31            #[allow(clippy::cast_possible_truncation)]
32            const CYCLE_SIZE: u64 = 256u64.saturating_pow(BYTES as u32);
33        }
34
35        impl<const BYTES: usize> uor_foundation::pipeline::__sdk_seal::Sealed
36            for $name<BYTES>
37        {
38        }
39        impl<const BYTES: usize> GroundedShape for $name<BYTES> {}
40        impl<const BYTES: usize> IntoBindingValue for $name<BYTES> {
41            const MAX_BYTES: usize = BYTES;
42
43            fn into_binding_bytes(&self, _out: &mut [u8]) -> Result<usize, ShapeViolation> {
44                Ok(0)
45            }
46        }
47    };
48}
49
50parametric_byte_shape! {
51    /// Hash-digest shape: `N` bytes carrying a hash output.
52    ///
53    /// Per ADR-031's `Digest<32>` / `Digest<64>` shape commitment.
54    /// Common widths: `Digest<32>` (SHA-256, SHA3-256, Keccak-256,
55    /// BLAKE3), `Digest<64>` (SHA-512), `Digest<48>` (SHA-384).
56    Digest
57}
58
59parametric_byte_shape! {
60    /// Public-key shape: `N` bytes carrying an elliptic-curve public
61    /// key (compressed or raw, application-defined). Per ADR-031's
62    /// `PublicKey<32>` shape commitment.
63    PublicKey
64}
65
66parametric_byte_shape! {
67    /// Signature shape: `N` bytes carrying a signature. Per ADR-031's
68    /// `Signature<64>` shape commitment.
69    ///
70    /// Common widths: `Signature<64>` (Ed25519, secp256k1 raw r||s),
71    /// `Signature<96>` (BLS12-381 G1 compressed).
72    Signature
73}