@@ -5,7 +5,7 @@ use miden_lib::transaction::memory::{
55} ;
66use miden_objects:: account:: AccountId ;
77use miden_objects:: note:: { NoteId , NoteInputs } ;
8- use miden_objects:: { Word , ZERO } ;
8+ use miden_objects:: { Hasher , Word } ;
99use miden_processor:: { EventError , ExecutionError , Felt , ProcessState } ;
1010
1111use crate :: errors:: TransactionKernelError ;
@@ -149,55 +149,45 @@ impl<'a> TransactionKernelProcess for ProcessState<'a> {
149149 Ok ( ( inputs, script_root, serial_num) )
150150 }
151151
152- /// Extracts and validates note inputs from the advice provider using trial unhashing.
153- ///
154- /// This function tries to determine the correct number of inputs by:
155- /// 1. Finding the last non-zero element as a starting point
156- /// 2. Building NoteInputs and checking if the hash matches inputs_commitment
157- /// 3. If not, incrementing num_inputs and trying again (up to 6 more times)
158- /// 4. If num_inputs grows to the size of inputs_data and there's still no match, returning an
159- /// error
152+ /// Extracts and validates note inputs from the advice provider.
160153 fn read_note_inputs_from_adv_map (
161154 & self ,
162155 inputs_commitment : & Word ,
163156 ) -> Result < NoteInputs , TransactionKernelError > {
164157 let inputs_data = self . advice_provider ( ) . get_mapped_values ( inputs_commitment) ;
165158
166- let inputs = match inputs_data {
167- None => NoteInputs :: default ( ) ,
159+ match inputs_data {
160+ None => Ok ( NoteInputs :: default ( ) ) ,
168161 Some ( inputs) => {
169- // Start with the last non-zero element as a hint
170- let initial_num_inputs =
171- inputs. iter ( ) . rposition ( |& x| x != ZERO ) . map ( |pos| pos + 1 ) . unwrap_or ( 0 ) ;
172-
173- // Try different input counts using trial unhashing
174- let mut num_inputs = initial_num_inputs;
175-
176- loop {
177- let candidate_inputs = NoteInputs :: new ( inputs[ 0 ..num_inputs] . to_vec ( ) )
178- . map_err ( TransactionKernelError :: MalformedNoteInputs ) ?;
179-
180- if candidate_inputs. commitment ( ) == * inputs_commitment {
181- return Ok ( candidate_inputs) ;
182- }
183-
184- num_inputs += 1 ;
185- if num_inputs > inputs. len ( ) {
186- break ;
187- }
162+ let inputs_commitment_hash = Hasher :: hash_elements ( inputs_commitment. as_elements ( ) ) ;
163+ let num_inputs = self
164+ . advice_provider ( )
165+ . get_mapped_values ( & inputs_commitment_hash)
166+ . ok_or_else ( || {
167+ TransactionKernelError :: other (
168+ "expected num_inputs to be present in advice provider" ,
169+ )
170+ } ) ?;
171+ if num_inputs. len ( ) != 1 {
172+ return Err ( TransactionKernelError :: other (
173+ "expected num_inputs advice entry to contain exactly one element" ,
174+ ) ) ;
175+ }
176+ let num_inputs = num_inputs[ 0 ] . as_int ( ) as usize ;
177+
178+ let note_inputs = NoteInputs :: new ( inputs[ 0 ..num_inputs] . to_vec ( ) )
179+ . map_err ( TransactionKernelError :: MalformedNoteInputs ) ?;
180+
181+ if & note_inputs. commitment ( ) == inputs_commitment {
182+ Ok ( note_inputs)
183+ } else {
184+ Err ( TransactionKernelError :: InvalidNoteInputs {
185+ expected : * inputs_commitment,
186+ actual : note_inputs. commitment ( ) ,
187+ } )
188188 }
189-
190- // If we've exhausted all attempts, return an error
191- return Err ( TransactionKernelError :: InvalidNoteInputs {
192- expected : * inputs_commitment,
193- actual : NoteInputs :: new ( inputs[ 0 ..num_inputs. min ( inputs. len ( ) ) ] . to_vec ( ) )
194- . map ( |i| i. commitment ( ) )
195- . unwrap_or_default ( ) ,
196- } ) ;
197189 } ,
198- } ;
199-
200- Ok ( inputs)
190+ }
201191 }
202192
203193 fn has_advice_map_entry ( & self , key : Word ) -> bool {
0 commit comments