@@ -175,6 +175,12 @@ bool __attribute__((noinline)) preprocess_inputs(
175175 return false;
176176 }
177177
178+ // sanity check before accumulating, to avoid overflowing the total
179+ if (input .prevout_amount > BITCOIN_TOTAL_SUPPLY ) {
180+ PRINTF ("Input amount exceeds Bitcoin total supply!\n" );
181+ SEND_SW (dc , SW_INCORRECT_DATA );
182+ return false;
183+ }
178184 st -> inputs_total_amount += input .prevout_amount ;
179185 }
180186
@@ -209,6 +215,12 @@ bool __attribute__((noinline)) preprocess_inputs(
209215 }
210216 } else {
211217 // we extract the scriptPubKey and prevout amount from the witness utxo
218+ // sanity check before accumulating, to avoid overflowing the total
219+ if (wit_utxo_prevout_amount > BITCOIN_TOTAL_SUPPLY ) {
220+ PRINTF ("Input amount exceeds Bitcoin total supply!\n" );
221+ SEND_SW (dc , SW_INCORRECT_DATA );
222+ return false;
223+ }
212224 st -> inputs_total_amount += wit_utxo_prevout_amount ;
213225
214226 input .prevout_amount = wit_utxo_prevout_amount ;
@@ -217,13 +229,6 @@ bool __attribute__((noinline)) preprocess_inputs(
217229 }
218230 }
219231
220- if (input .prevout_amount > BITCOIN_TOTAL_SUPPLY ) {
221- // sanity check to avoid overflows in amounts
222- PRINTF ("Input amount exceed Bitcoin total supply!\n" );
223- SEND_SW (dc , SW_INCORRECT_DATA );
224- return false;
225- }
226-
227232 // check if the input is internal; if not, continue
228233
229234 int is_internal = is_in_out_internal (dc , st , sign_psbt_cache , & input .in_out , true);
0 commit comments