miden_agglayer/remove_ger_note.rs
1//! REMOVE_GER note creation utilities.
2//!
3//! This module provides helpers for creating REMOVE_GER notes,
4//! which are used to remove a Global Exit Root from the bridge account and fold it into the
5//! running removed-GER keccak256 hash chain.
6
7use miden_protocol::account::AccountId;
8use miden_protocol::assembly::Library;
9use miden_protocol::crypto::rand::FeltRng;
10use miden_protocol::errors::NoteError;
11use miden_protocol::note::{Note, NoteScript, NoteScriptRoot};
12use miden_protocol::utils::serde::Deserializable;
13use miden_utils_sync::LazyLock;
14
15use crate::ExitRoot;
16use crate::ger_note::create_ger_note;
17
18// NOTE SCRIPT
19// ================================================================================================
20
21// Initialize the REMOVE_GER note script only once
22static REMOVE_GER_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| {
23 let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/remove_ger.masp"));
24 let library =
25 Library::read_from_bytes(bytes).expect("shipped REMOVE_GER script library is well-formed");
26 NoteScript::from_library(&library).expect("shipped REMOVE_GER script is well-formed")
27});
28
29// REMOVE_GER NOTE
30// ================================================================================================
31
32/// REMOVE_GER note.
33///
34/// This note is used to remove a Global Exit Root (GER) from the bridge account and fold it into
35/// the running removed-GER keccak256 hash chain. It carries the GER data and is always public.
36pub struct RemoveGerNote;
37
38impl RemoveGerNote {
39 // CONSTANTS
40 // --------------------------------------------------------------------------------------------
41
42 /// Expected number of storage items for a REMOVE_GER note.
43 pub const NUM_STORAGE_ITEMS: usize = 8;
44
45 // PUBLIC ACCESSORS
46 // --------------------------------------------------------------------------------------------
47
48 /// Returns the REMOVE_GER note script.
49 pub fn script() -> NoteScript {
50 REMOVE_GER_SCRIPT.clone()
51 }
52
53 /// Returns the REMOVE_GER note script root.
54 pub fn script_root() -> NoteScriptRoot {
55 REMOVE_GER_SCRIPT.root()
56 }
57
58 // BUILDERS
59 // --------------------------------------------------------------------------------------------
60
61 /// Creates a REMOVE_GER note with the given GER (Global Exit Root) data.
62 ///
63 /// The note storage contains 8 felts: GER[0..7]
64 ///
65 /// # Parameters
66 /// - `ger`: The Global Exit Root data to remove
67 /// - `sender_account_id`: The account ID of the note creator (must be the GER remover)
68 /// - `target_account_id`: The account ID that will consume this note (bridge account)
69 /// - `rng`: Random number generator for creating the note serial number
70 ///
71 /// # Errors
72 /// Returns an error if note creation fails.
73 pub fn create<R: FeltRng>(
74 ger: ExitRoot,
75 sender_account_id: AccountId,
76 target_account_id: AccountId,
77 rng: &mut R,
78 ) -> Result<Note, NoteError> {
79 create_ger_note(ger, sender_account_id, target_account_id, Self::script(), rng)
80 }
81}