@@ -178,6 +178,12 @@ bool __attribute__((noinline)) preprocess_inputs(
178178 return false;
179179 }
180180
181+ // sanity check before accumulating, to avoid overflowing the total
182+ if (input .prevout_amount > BITCOIN_TOTAL_SUPPLY ) {
183+ PRINTF ("Input amount exceeds Bitcoin total supply!\n" );
184+ SEND_SW (dc , SW_INCORRECT_DATA );
185+ return false;
186+ }
181187 st -> inputs_total_amount += input .prevout_amount ;
182188 }
183189
@@ -212,6 +218,12 @@ bool __attribute__((noinline)) preprocess_inputs(
212218 }
213219 } else {
214220 // we extract the scriptPubKey and prevout amount from the witness utxo
221+ // sanity check before accumulating, to avoid overflowing the total
222+ if (wit_utxo_prevout_amount > BITCOIN_TOTAL_SUPPLY ) {
223+ PRINTF ("Input amount exceeds Bitcoin total supply!\n" );
224+ SEND_SW (dc , SW_INCORRECT_DATA );
225+ return false;
226+ }
215227 st -> inputs_total_amount += wit_utxo_prevout_amount ;
216228
217229 input .prevout_amount = wit_utxo_prevout_amount ;
@@ -220,13 +232,6 @@ bool __attribute__((noinline)) preprocess_inputs(
220232 }
221233 }
222234
223- if (input .prevout_amount > BITCOIN_TOTAL_SUPPLY ) {
224- // sanity check to avoid overflows in amounts
225- PRINTF ("Input amount exceed Bitcoin total supply!\n" );
226- SEND_SW (dc , SW_INCORRECT_DATA );
227- return false;
228- }
229-
230235 // check if the input is internal; if not, continue
231236
232237 int is_internal = is_in_out_internal (dc , st , sign_psbt_cache , & input .in_out , true);
0 commit comments