Skip to main content

miden_standards/account/auth/
no_auth.rs

1use miden_protocol::account::component::{AccountComponentCode, AccountComponentMetadata};
2use miden_protocol::account::{AccountComponent, AccountComponentName};
3
4use crate::account::account_component_code;
5
6account_component_code!(NO_AUTH_CODE, "auth/no_auth.masl");
7
8/// An [`AccountComponent`] implementing a no-authentication scheme.
9///
10/// This component provides **no authentication**! It only checks if the account
11/// state has actually changed during transaction execution by comparing the initial
12/// account commitment with the current commitment and increments the nonce if
13/// they differ. This avoids unnecessary nonce increments for transactions that don't
14/// modify the account state.
15///
16/// It exports the procedure `auth_no_auth`, which:
17/// - Checks if the account state has changed by comparing initial and final commitments
18/// - Only increments the nonce if the account state has actually changed
19/// - Provides no cryptographic authentication
20pub struct NoAuth;
21
22impl NoAuth {
23    /// The name of the component.
24    pub const NAME: &'static str = "miden::standards::components::auth::no_auth";
25
26    /// Returns the canonical [`AccountComponentName`] of this component.
27    pub const fn name() -> AccountComponentName {
28        AccountComponentName::from_static_str(Self::NAME)
29    }
30
31    /// Returns the [`AccountComponentCode`] of this component.
32    pub fn code() -> &'static AccountComponentCode {
33        &NO_AUTH_CODE
34    }
35
36    /// Creates a new [`NoAuth`] component.
37    pub fn new() -> Self {
38        Self
39    }
40
41    /// Returns the [`AccountComponentMetadata`] for this component.
42    pub fn component_metadata() -> AccountComponentMetadata {
43        AccountComponentMetadata::new(Self::NAME).with_description("No authentication component")
44    }
45}
46
47impl Default for NoAuth {
48    fn default() -> Self {
49        Self::new()
50    }
51}
52
53impl From<NoAuth> for AccountComponent {
54    fn from(_: NoAuth) -> Self {
55        let metadata = NoAuth::component_metadata();
56
57        AccountComponent::new(NoAuth::code().clone(), vec![], metadata)
58            .expect("NoAuth component should satisfy the requirements of a valid account component")
59    }
60}
61
62// TESTS
63// ================================================================================================
64
65#[cfg(test)]
66mod tests {
67    use miden_protocol::account::AccountBuilder;
68
69    use super::*;
70    use crate::account::wallets::BasicWallet;
71
72    #[test]
73    fn test_no_auth_component() {
74        // Create an account using the NoAuth component
75        let _account = AccountBuilder::new([0; 32])
76            .with_auth_component(NoAuth)
77            .with_component(BasicWallet)
78            .build()
79            .expect("account building failed");
80    }
81}