miden_testing/mock_chain/
auth.rs

1// AUTH
2// ================================================================================================
3use alloc::vec::Vec;
4
5use miden_lib::{
6    account::auth::{RpoFalcon512, RpoFalcon512ProcedureAcl},
7    transaction::TransactionKernel,
8};
9use miden_objects::{
10    Digest,
11    account::{AccountComponent, AuthSecretKey},
12    crypto::dsa::rpo_falcon512::SecretKey,
13    testing::account_component::{
14        ConditionalAuthComponent, IncrNonceAuthComponent, NoopAuthComponent,
15    },
16};
17use miden_tx::auth::BasicAuthenticator;
18use rand::SeedableRng;
19use rand_chacha::ChaCha20Rng;
20
21/// Specifies which authentication mechanism is desired for accounts
22#[derive(Debug, Clone)]
23pub enum Auth {
24    /// Creates a [SecretKey] for the account and creates a [BasicAuthenticator] used to
25    /// authenticate the account with [RpoFalcon512].
26    BasicAuth,
27
28    /// Creates a [SecretKey] for the account, and creates a [BasicAuthenticator] used to
29    /// authenticate the account with [RpoFalcon512ProcedureAcl]. Authentication will only be
30    /// triggered if any of the procedures specified in the list are called during execution.
31    ProcedureAcl { auth_trigger_procedures: Vec<Digest> },
32
33    /// Creates a mock authentication mechanism for the account that only increments the nonce.
34    IncrNonce,
35
36    /// Creates a mock authentication mechanism for the account that does nothing.
37    Noop,
38
39    /// TODO update once #1501 is ready.
40    Conditional,
41}
42
43impl Auth {
44    /// Converts `self` into its corresponding authentication [`AccountComponent`] and an optional
45    /// [`BasicAuthenticator`]. The component is always returned, but the authenticator is only
46    /// `Some` when [`Auth::BasicAuth`] is passed."
47    pub fn build_component(&self) -> (AccountComponent, Option<BasicAuthenticator<ChaCha20Rng>>) {
48        match self {
49            Auth::BasicAuth => {
50                let mut rng = ChaCha20Rng::from_seed(Default::default());
51                let sec_key = SecretKey::with_rng(&mut rng);
52                let pub_key = sec_key.public_key();
53
54                let component = RpoFalcon512::new(pub_key).into();
55                let authenticator = BasicAuthenticator::<ChaCha20Rng>::new_with_rng(
56                    &[(pub_key.into(), AuthSecretKey::RpoFalcon512(sec_key))],
57                    rng,
58                );
59
60                (component, Some(authenticator))
61            },
62            Auth::ProcedureAcl { auth_trigger_procedures } => {
63                let mut rng = ChaCha20Rng::from_seed(Default::default());
64                let sec_key = SecretKey::with_rng(&mut rng);
65                let pub_key = sec_key.public_key();
66
67                let component =
68                    RpoFalcon512ProcedureAcl::new(pub_key, auth_trigger_procedures.clone())
69                        .expect("component creation failed")
70                        .into();
71                let authenticator = BasicAuthenticator::<ChaCha20Rng>::new_with_rng(
72                    &[(pub_key.into(), AuthSecretKey::RpoFalcon512(sec_key))],
73                    rng,
74                );
75
76                (component, Some(authenticator))
77            },
78            Auth::IncrNonce => {
79                let assembler = TransactionKernel::assembler();
80                let component = IncrNonceAuthComponent::new(assembler).unwrap();
81                (component.into(), None)
82            },
83
84            Auth::Noop => {
85                let assembler = TransactionKernel::assembler();
86                let component = NoopAuthComponent::new(assembler).unwrap();
87                (component.into(), None)
88            },
89            Auth::Conditional => {
90                let assembler = TransactionKernel::assembler();
91                let component = ConditionalAuthComponent::new(assembler).unwrap();
92                (component.into(), None)
93            },
94        }
95    }
96}
97
98impl From<Auth> for AccountComponent {
99    fn from(auth: Auth) -> Self {
100        let (component, _) = auth.build_component();
101        component
102    }
103}