# The MASM code of the BasicSignature Authentication Account Component.
#
# See the `AuthBasicSignature` Rust type's documentation for more details.

use miden::standards::auth::signature
use miden::protocol::active_account

# CONSTANTS
# =================================================================================================

# The slot in this component's storage layout where the public key is stored.
const PUBLIC_KEY_SLOT = word("miden::standards::auth::singlesig::pub_key")

# The slot in this component's storage layout where the corresponding signature scheme id is stored.
const SCHEME_ID_SLOT = word("miden::standards::auth::singlesig::scheme")

#! Authenticate a transaction using the signature scheme specified by scheme_id.
#!
#! Supported schemes:
#! - 1 => ecdsa_k256_keccak
#! - 2 => falcon512_poseidon2
#!
#! It first increments the nonce of the account, independent of whether the account's state has
#! changed or not. Then it computes and signs the following message (in memory order):
#! [ACCOUNT_DELTA_COMMITMENT, INPUT_NOTES_COMMITMENT,
#!  OUTPUT_NOTES_COMMITMENT, [0, 0, ref_block_num, final_nonce]]
#!
#! Including the final_nonce is necessary for replay protection. The reference block number is
#! included to commit to the transaction creator's intended reference block of the transaction
#! which determines the fee parameters and therefore the fee amount that is deducted.
#!
#! Inputs:  [AUTH_ARGS, pad(12)]
#! Outputs: [pad(16)]
#!
#! Invocation: call
@auth_script
pub proc auth_tx(auth_args: word)
    dropw
    # => [pad(16)]

    # Fetch public key from storage.
    # ---------------------------------------------------------------------------------------------

    push.PUBLIC_KEY_SLOT[0..2] exec.active_account::get_initial_item
    # => [PUB_KEY, pad(16)]

    push.SCHEME_ID_SLOT[0..2] exec.active_account::get_initial_item
    # => [scheme_id, 0, 0, 0, PUB_KEY, pad(16)]

    movdn.7 drop drop drop
    # => [PUB_KEY, scheme_id, pad(16)]

    exec.signature::authenticate_transaction
    # => [pad(16)]
end
