miden_protocol/transaction/kernel/advice_inputs.rs
1use alloc::vec::Vec;
2
3use miden_processor::advice::AdviceMutation;
4
5use crate::account::{AccountHeader, PartialAccount};
6use crate::block::account_tree::{AccountIdKey, AccountWitness};
7use crate::crypto::SequentialCommit;
8use crate::crypto::merkle::InnerNodeInfo;
9use crate::transaction::{
10 AccountInputs,
11 InputNote,
12 PartialBlockchain,
13 TransactionInputs,
14 TransactionKernel,
15};
16use crate::vm::AdviceInputs;
17use crate::{EMPTY_WORD, Felt, Word, ZERO};
18
19// TRANSACTION ADVICE INPUTS
20// ================================================================================================
21
22/// Advice inputs wrapper for inputs that are meant to be used exclusively in the transaction
23/// kernel.
24#[derive(Debug, Clone, Default)]
25pub struct TransactionAdviceInputs(AdviceInputs);
26
27impl TransactionAdviceInputs {
28 /// Creates a [`TransactionAdviceInputs`].
29 ///
30 /// The created advice inputs will be populated with the data required for executing a
31 /// transaction with the specified transaction inputs.
32 pub fn new(tx_inputs: &TransactionInputs) -> Self {
33 let mut inputs = TransactionAdviceInputs(tx_inputs.advice_inputs().clone());
34
35 inputs.build_stack(tx_inputs);
36 inputs.add_kernel_commitment();
37 inputs.add_partial_blockchain(tx_inputs.blockchain());
38 inputs.add_input_notes(tx_inputs);
39
40 // Add the script's MAST forest's advice inputs.
41 if let Some(tx_script) = tx_inputs.tx_args().tx_script() {
42 inputs.extend_map(
43 tx_script
44 .mast()
45 .advice_map()
46 .iter()
47 .map(|(key, values)| (*key, values.to_vec())),
48 );
49 }
50
51 // Inject native account.
52 let partial_native_acc = tx_inputs.account();
53 inputs.add_account(partial_native_acc);
54
55 // If a seed was provided, extend the map appropriately.
56 if let Some(seed) = tx_inputs.account().seed() {
57 // ACCOUNT_ID |-> ACCOUNT_SEED
58 let account_id_key = AccountIdKey::from(partial_native_acc.id());
59 inputs.add_map_entry(account_id_key.as_word(), seed.to_vec());
60 }
61
62 // if the account is new, insert the storage map entries into the advice provider.
63 if partial_native_acc.is_new() {
64 for storage_map in partial_native_acc.storage().maps() {
65 let map_entries = storage_map
66 .entries()
67 .flat_map(|(key, value)| {
68 value.as_elements().iter().chain(key.as_elements().iter()).copied()
69 })
70 .collect();
71 inputs.add_map_entry(storage_map.root(), map_entries);
72 }
73 }
74
75 // Extend with extra user-supplied advice.
76 inputs.extend(tx_inputs.tx_args().advice_inputs().clone());
77
78 inputs
79 }
80
81 /// Returns a reference to the underlying advice inputs.
82 pub fn as_advice_inputs(&self) -> &AdviceInputs {
83 &self.0
84 }
85
86 /// Converts these transaction advice inputs into the underlying advice inputs.
87 pub fn into_advice_inputs(self) -> AdviceInputs {
88 self.0
89 }
90
91 /// Consumes self and returns an iterator of [`AdviceMutation`]s in arbitrary order.
92 pub fn into_advice_mutations(self) -> impl Iterator<Item = AdviceMutation> {
93 [
94 AdviceMutation::ExtendMap { other: self.0.map },
95 AdviceMutation::ExtendMerkleStore {
96 infos: self.0.store.inner_nodes().collect(),
97 },
98 AdviceMutation::ExtendStack { values: self.0.stack },
99 ]
100 .into_iter()
101 }
102
103 // PUBLIC UTILITIES
104 // --------------------------------------------------------------------------------------------
105
106 // MUTATORS
107 // --------------------------------------------------------------------------------------------
108
109 /// Extends these advice inputs with the provided advice inputs.
110 pub fn extend(&mut self, adv_inputs: AdviceInputs) {
111 self.0.extend(adv_inputs);
112 }
113
114 /// Adds the provided account inputs into the advice inputs.
115 pub fn add_foreign_accounts<'inputs>(
116 &mut self,
117 foreign_account_inputs: impl IntoIterator<Item = &'inputs AccountInputs>,
118 ) {
119 for foreign_acc in foreign_account_inputs {
120 self.add_account(foreign_acc.account());
121 self.add_account_witness(foreign_acc.witness());
122
123 // for foreign accounts, we need to insert the id to state mapping
124 // NOTE: keep this in sync with the account::load_from_advice procedure
125 let account_id_key = AccountIdKey::from(foreign_acc.id());
126 let header = AccountHeader::from(foreign_acc.account());
127
128 // ACCOUNT_ID |-> [ID_AND_NONCE, VAULT_ROOT, STORAGE_COMMITMENT, CODE_COMMITMENT]
129 self.add_map_entry(account_id_key.as_word(), header.to_elements());
130 }
131 }
132
133 /// Extend the advice stack with the transaction inputs.
134 ///
135 /// The following data is pushed to the advice stack (words shown in memory-order):
136 ///
137 /// [
138 /// PARENT_BLOCK_COMMITMENT,
139 /// PARTIAL_BLOCKCHAIN_COMMITMENT,
140 /// ACCOUNT_ROOT,
141 /// NULLIFIER_ROOT,
142 /// TX_COMMITMENT,
143 /// TX_KERNEL_COMMITMENT
144 /// VALIDATOR_KEY_COMMITMENT,
145 /// [block_num, version, timestamp, 0],
146 /// [0, verification_base_fee, fee_faucet_id_suffix, fee_faucet_id_prefix]
147 /// [0, 0, 0, 0]
148 /// NOTE_ROOT,
149 /// kernel_version
150 /// [account_nonce, 0, account_id_suffix, account_id_prefix],
151 /// ACCOUNT_VAULT_ROOT,
152 /// ACCOUNT_STORAGE_COMMITMENT,
153 /// ACCOUNT_CODE_COMMITMENT,
154 /// number_of_input_notes,
155 /// TX_SCRIPT_ROOT,
156 /// TX_SCRIPT_ARGS,
157 /// AUTH_ARGS,
158 /// ]
159 fn build_stack(&mut self, tx_inputs: &TransactionInputs) {
160 let header = tx_inputs.block_header();
161
162 // --- block header data (keep in sync with kernel's process_block_data) --
163 self.extend_stack(header.prev_block_commitment());
164 self.extend_stack(header.chain_commitment());
165 self.extend_stack(header.account_root());
166 self.extend_stack(header.nullifier_root());
167 self.extend_stack(header.tx_commitment());
168 self.extend_stack(header.tx_kernel_commitment());
169 self.extend_stack(header.validator_key().to_commitment());
170 self.extend_stack([
171 header.block_num().into(),
172 Felt::from(header.version()),
173 Felt::from(header.timestamp()),
174 ZERO,
175 ]);
176 self.extend_stack([
177 ZERO,
178 Felt::from(header.fee_parameters().verification_base_fee()),
179 header.fee_parameters().fee_faucet_id().suffix(),
180 header.fee_parameters().fee_faucet_id().prefix().as_felt(),
181 ]);
182 self.extend_stack([ZERO, ZERO, ZERO, ZERO]);
183 self.extend_stack(header.note_root());
184
185 // --- core account items (keep in sync with process_account_data) ----
186 let account = tx_inputs.account();
187 self.extend_stack([
188 account.nonce(),
189 ZERO,
190 account.id().suffix(),
191 account.id().prefix().as_felt(),
192 ]);
193 self.extend_stack(account.vault().root());
194 self.extend_stack(account.storage().commitment());
195 self.extend_stack(account.code().commitment());
196
197 // --- number of notes, script root and args --------------------------
198 self.extend_stack([Felt::from(tx_inputs.input_notes().num_notes())]);
199 let tx_args = tx_inputs.tx_args();
200 self.extend_stack(tx_args.tx_script().map_or(Word::empty(), |script| script.root()));
201 self.extend_stack(tx_args.tx_script_args());
202
203 // --- auth procedure args --------------------------------------------
204 self.extend_stack(tx_args.auth_args());
205 }
206
207 // BLOCKCHAIN INJECTIONS
208 // --------------------------------------------------------------------------------------------
209
210 /// Inserts the partial blockchain data into the provided advice inputs.
211 ///
212 /// Inserts the following items into the Merkle store:
213 /// - Inner nodes of all authentication paths contained in the partial blockchain.
214 ///
215 /// Inserts the following data to the advice map:
216 ///
217 /// > {MMR_ROOT: [[num_blocks, 0, 0, 0], PEAK_1, ..., PEAK_N]}
218 ///
219 /// Where:
220 /// - MMR_ROOT, is the sequential hash of the padded MMR peaks
221 /// - num_blocks, is the number of blocks in the MMR.
222 /// - PEAK_1 .. PEAK_N, are the MMR peaks.
223 fn add_partial_blockchain(&mut self, mmr: &PartialBlockchain) {
224 // NOTE: keep this code in sync with the `process_chain_data` kernel procedure
225 // add authentication paths from the MMR to the Merkle store
226 self.extend_merkle_store(mmr.inner_nodes());
227
228 // insert MMR peaks info into the advice map
229 let peaks = mmr.peaks();
230 let num_leaves = Felt::try_from(peaks.num_leaves() as u64)
231 .expect("number of blocks in chain should not exceed BlockNumber::MAX");
232 let mut elements = vec![num_leaves, ZERO, ZERO, ZERO];
233 elements.extend(peaks.flatten_and_pad_peaks());
234 self.add_map_entry(peaks.hash_peaks(), elements);
235 }
236
237 // KERNEL INJECTIONS
238 // --------------------------------------------------------------------------------------------
239
240 /// Inserts the kernel commitment and its procedure roots into the advice map.
241 ///
242 /// Inserts the following entries into the advice map:
243 /// - The commitment of the kernel |-> array of the kernel's procedure roots.
244 fn add_kernel_commitment(&mut self) {
245 // insert the kernel commitment with its procedure roots into the advice map
246 self.add_map_entry(TransactionKernel.to_commitment(), TransactionKernel.to_elements());
247 }
248
249 // ACCOUNT INJECTION
250 // --------------------------------------------------------------------------------------------
251
252 /// Inserts account data into the advice inputs.
253 ///
254 /// Inserts the following items into the Merkle store:
255 /// - The Merkle nodes associated with the account vault tree.
256 /// - If present, the Merkle nodes associated with the account storage maps.
257 ///
258 /// Inserts the following entries into the advice map:
259 /// - The account storage commitment |-> storage slots and types vector.
260 /// - The account code commitment |-> procedures vector.
261 /// - The leaf hash |-> (key, value), for all leaves of the partial vault.
262 /// - If present, the Merkle leaves associated with the account storage maps.
263 fn add_account(&mut self, account: &PartialAccount) {
264 // --- account code -------------------------------------------------------
265
266 // CODE_COMMITMENT -> [[ACCOUNT_PROCEDURE_DATA]]
267 let code = account.code();
268 self.add_map_entry(code.commitment(), code.to_elements());
269
270 // --- account storage ----------------------------------------------------
271
272 // STORAGE_COMMITMENT |-> [[STORAGE_SLOT_DATA]]
273 let storage_header = account.storage().header();
274 self.add_map_entry(storage_header.to_commitment(), storage_header.to_elements());
275
276 // populate Merkle store and advice map with nodes info needed to access storage map entries
277 self.extend_merkle_store(account.storage().inner_nodes());
278 self.extend_map(
279 account
280 .storage()
281 .leaves()
282 .map(|leaf| (leaf.hash(), leaf.to_elements().collect())),
283 );
284
285 // --- account vault ------------------------------------------------------
286
287 // populate Merkle store and advice map with nodes info needed to access vault assets
288 self.extend_merkle_store(account.vault().inner_nodes());
289 self.extend_map(
290 account.vault().leaves().map(|leaf| (leaf.hash(), leaf.to_elements().collect())),
291 );
292 }
293
294 /// Adds an account witness to the advice inputs.
295 ///
296 /// This involves extending the map to include the leaf's hash mapped to its elements, as well
297 /// as extending the merkle store with the nodes of the witness.
298 fn add_account_witness(&mut self, witness: &AccountWitness) {
299 // populate advice map with the account's leaf
300 let leaf = witness.leaf();
301 self.add_map_entry(leaf.hash(), leaf.to_elements().collect());
302
303 // extend the merkle store and map with account witnesses merkle path
304 self.extend_merkle_store(witness.authenticated_nodes());
305 }
306
307 // NOTE INJECTION
308 // --------------------------------------------------------------------------------------------
309
310 /// Populates the advice inputs for all input notes.
311 ///
312 /// The advice provider is populated with:
313 ///
314 /// - For each note:
315 /// - The note's private arguments.
316 /// - The note's details (serial number, script root, and its storage / assets commitment).
317 /// - The note's public metadata (sender account ID, note type, note tag, attachment
318 /// schemes).
319 /// - The note's storage (unpadded).
320 /// - The note's assets (key and value words).
321 /// - For authenticated notes (determined by the `is_authenticated` flag):
322 /// - The note's authentication path against its block's note tree.
323 /// - The block number, sub commitment, note root.
324 /// - The note's position in the note tree
325 ///
326 /// The data above is processed by `prologue::process_input_notes_data`.
327 fn add_input_notes(&mut self, tx_inputs: &TransactionInputs) {
328 if tx_inputs.input_notes().is_empty() {
329 return;
330 }
331
332 let mut note_data = Vec::new();
333 for input_note in tx_inputs.input_notes().iter() {
334 let note = input_note.note();
335 let assets = note.assets();
336 let recipient = note.recipient();
337 let note_arg = tx_inputs.tx_args().get_note_args(note.id()).unwrap_or(&EMPTY_WORD);
338
339 // recipient storage
340 self.add_map_entry(recipient.storage().commitment(), recipient.storage().to_elements());
341 // assets commitments
342 self.add_map_entry(assets.commitment(), assets.to_elements());
343
344 // ATTACHMENTS_COMMITMENT |-> [[ATTACHMENT_COMMITMENTS]]
345 self.add_map_entry(
346 note.attachments().to_commitment(),
347 note.attachments()
348 .commitments()
349 .iter()
350 .flat_map(Word::as_elements)
351 .copied()
352 .collect(),
353 );
354
355 // ATTACHMENT_COMMITMENT |-> [ATTACHMENT_ELEMENTS] for each attachment
356 for attachment in note.attachments().iter() {
357 let commitment = attachment.content().to_commitment();
358 let elements = attachment.content().to_elements();
359 self.add_map_entry(commitment, elements);
360 }
361
362 // note metadata / details
363 note_data.extend(*note_arg);
364 note_data.extend(recipient.serial_num());
365 note_data.extend(Word::from(recipient.script().root()));
366 note_data.extend(*recipient.storage().commitment());
367 note_data.extend(*assets.commitment());
368 note_data.extend(note.metadata().to_metadata_word());
369 note_data.extend(note.attachments().to_commitment());
370 note_data.push(Felt::from(recipient.storage().num_items()));
371 note_data.push(Felt::from(assets.num_assets() as u32));
372 note_data.extend(assets.to_elements());
373
374 // authentication vs unauthenticated
375 match input_note {
376 InputNote::Authenticated { note, proof } => {
377 // Push the `is_authenticated` flag
378 note_data.push(Felt::ONE);
379
380 // Merkle path
381 self.extend_merkle_store(proof.authenticated_nodes(note.id()));
382
383 let block_num = proof.location().block_num();
384 let block_header = if block_num == tx_inputs.block_header().block_num() {
385 tx_inputs.block_header()
386 } else {
387 tx_inputs
388 .blockchain()
389 .get_block(block_num)
390 .expect("block not found in partial blockchain")
391 };
392
393 note_data.push(block_num.into());
394 note_data.extend(block_header.sub_commitment());
395 note_data.extend(block_header.note_root());
396 note_data.push(Felt::from(proof.location().block_note_tree_index()));
397 },
398 InputNote::Unauthenticated { .. } => {
399 // push the `is_authenticated` flag
400 note_data.push(Felt::ZERO)
401 },
402 }
403 }
404
405 self.add_map_entry(tx_inputs.input_notes().commitment(), note_data);
406 }
407
408 // HELPER METHODS
409 // --------------------------------------------------------------------------------------------
410
411 /// Extends the map of values with the given argument, replacing previously inserted items.
412 fn extend_map(&mut self, iter: impl IntoIterator<Item = (Word, Vec<Felt>)>) {
413 self.0.map.extend(iter);
414 }
415
416 fn add_map_entry(&mut self, key: Word, values: Vec<Felt>) {
417 self.0.map.extend([(key, values)]);
418 }
419
420 /// Extends the stack with the given elements.
421 fn extend_stack(&mut self, iter: impl IntoIterator<Item = Felt>) {
422 self.0.stack.extend(iter);
423 }
424
425 /// Extends the [`MerkleStore`](crate::crypto::merkle::MerkleStore) with the given
426 /// nodes.
427 fn extend_merkle_store(&mut self, iter: impl Iterator<Item = InnerNodeInfo>) {
428 self.0.store.extend(iter);
429 }
430}
431
432// CONVERSIONS
433// ================================================================================================
434
435impl From<TransactionAdviceInputs> for AdviceInputs {
436 fn from(wrapper: TransactionAdviceInputs) -> Self {
437 wrapper.0
438 }
439}
440
441impl From<AdviceInputs> for TransactionAdviceInputs {
442 fn from(inner: AdviceInputs) -> Self {
443 Self(inner)
444 }
445}