Skip to main content

noethers_turnstile_core/
lib.rs

1//! # turnstile-core
2//!
3//! Pure Rust implementation of the admissibility compiler.
4//!
5//! The judgment form is:
6//! ```text
7//! Γ ⊢ z : p until ε
8//! ```
9//!
10//! The compiler is parameterized over a [`PermissionChain`] — a validated
11//! total order of named levels with role anchors (Bottom, ExpiryFloor, Refused,
12//! Unsatisfied, BlockerThreshold, Top). The default chain (the historical
13//! 12-level `OOC < EXP < REF < UNS < ETA < ESC < ROL < DIA < REV < AEX < ALR < AAA`)
14//! is available via [`PermissionChain::default_chain`].
15//!
16//! See `docs/specs/permission_chain_refactor_spec.md` for the full design.
17//!
18//! ## Quick start (default chain)
19//!
20//! ```rust
21//! use noethers_turnstile_core::{
22//!     context::{Membership, ProofContext, Scope},
23//!     compiler::compile,
24//!     expiry::Expiry,
25//!     default_levels,
26//! };
27//!
28//! let ctx = ProofContext {
29//!     claim_id: "my-claim".into(),
30//!     candidate_id: "z-001".into(),
31//!     context_id: "ctx-001".into(),
32//!     context_fingerprint: "fp-001".into(),
33//!     allowed_use: "diagnostics".into(),
34//!     disallowed_uses: vec![],
35//!     scope: Scope::default(),
36//!     gaps: vec![],
37//!     profiles: vec![],
38//!     tokens: vec![],
39//!     expiry: Expiry::never(),
40//!     authority_ceiling: None,
41//!     permission_ceiling: None,
42//!     expected_chain_hash: None,
43//!     membership: Membership::InClass,
44//! };
45//!
46//! let judgment = compile(ctx).unwrap();
47//! // No profiles registered → OOC.
48//! assert_eq!(judgment.permission, default_levels::OOC());
49//! ```
50
51pub mod audit;
52pub mod certifier;
53pub mod compiler;
54pub mod composition;
55pub mod context;
56pub mod default_levels;
57pub mod error;
58pub mod expiry;
59pub mod gap;
60pub mod permission;
61pub mod registry;
62pub mod token;
63
64// Re-export the most commonly used types at the crate root.
65pub use compiler::{compile, compile_at, compile_at_with_chain, compile_with_chain, Judgment};
66pub use composition::{
67    compose, compose_judgments, compose_n, compose_n_with_chain, compose_with_chain,
68};
69pub use context::ProofContext;
70pub use expiry::{Expiry, LiveJudgment, RuntimeContext};
71pub use permission::{
72    AuditError, ChainError, ChainHash, ChainRegistry, ChainRole, InMemoryChainRegistry,
73    NameRejectionReason, Permission, PermissionChain, MAX_LEVELS, MAX_NAME_LEN,
74};
75pub use token::NegativeControlStatus;
76
77/// Verify that a judgment's chain_hash resolves in the given registry, and
78/// that the resolved chain re-hashes to the same value.
79///
80/// This is the audit-time check that converts publication from prose to API.
81/// See §3.3 mechanism 4 of `docs/specs/permission_chain_refactor_spec.md`.
82pub fn verify_published<R: ChainRegistry>(
83    judgment: &Judgment,
84    registry: &R,
85) -> Result<(), AuditError> {
86    match registry.lookup(&judgment.chain_hash) {
87        None => Err(AuditError::NotPublished {
88            hash: judgment.chain_hash,
89        }),
90        Some(chain) => {
91            let actual = chain.chain_hash();
92            if actual == judgment.chain_hash {
93                Ok(())
94            } else {
95                Err(AuditError::HashMismatch {
96                    expected: judgment.chain_hash,
97                    actual,
98                })
99            }
100        }
101    }
102}