otp: avoid pow() when displaying asset amounts
What changed, and why it matters
This commit is a harmless code-size optimization. It replaces the mathematical pow() function (used to calculate 10 raised to a small power) with a simple lookup table of powers of 10, allowing the compiler to remove the floating-point pow() library code and save about 3.9 KB of firmware space. The displayed asset amounts are computed the same way as before.
No security action needed. Treat as a normal firmware size/optimization improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
In main/ui/sign_tx.c, the code previously called pow(10, asset_info.precision) to obtain a uint32_t scale factor for formatting asset/fee amounts. The commit removes the
Changed components
main/ui/sign_tx.cInspect captured patch +10 / −3
diff --git a/main/ui/sign_tx.c b/main/ui/sign_tx.c
index 2a534bc..8e02dd6 100644
--- a/main/ui/sign_tx.c
+++ b/main/ui/sign_tx.c
@@ -1,7 +1,6 @@
#ifndef AMALGAMATED_BUILD
#include <assets_snapshot.h>
#include <inttypes.h>
-#include <math.h>
#include <wally_elements.h>
#include <wally_transaction.h>
@@ -30,6 +29,14 @@ static const char VERIFIED_WALLET_OUTPUT_MSG[] = "Verified wallet output";
static const char TICKER_BTC[] = "BTC";
+static const uint32_t POW_10[9] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
+
+static uint32_t get_asset_scale_factor(const asset_info_t* asset_info)
+{
+ JADE_ASSERT(asset_info && asset_info->precision < sizeof(POW_10) / sizeof(POW_10[0]));
+ return POW_10[asset_info->precision];
+}
+
// Don't display pre-validated (eg. change) outputs (if provided) unless they have an associated warning message.
// Should work for elements and standard btc, but liquid hides scriptless outputs (fees)
static bool display_output(
@@ -115,7 +122,7 @@ static bool get_asset_display_info(const network_t network_id, const asset_info_
}
// Amount scaled and displayed at relevant precision
- const uint32_t scale_factor = pow(10, asset_info.precision);
+ const uint32_t scale_factor = get_asset_scale_factor(&asset_info);
ret = snprintf(amount, amount_len, "%.*f", asset_info.precision, 1.0 * value / scale_factor);
JADE_ASSERT(ret > 0 && ret < amount_len);
@@ -773,7 +780,7 @@ bool show_elements_final_confirmation_activity(
// Fee amount scaled and displayed at relevant precision
char feeamount[32];
- const uint32_t scale_factor = pow(10, asset_info.precision);
+ const uint32_t scale_factor = get_asset_scale_factor(&asset_info);
ret = snprintf(feeamount, sizeof(feeamount), "%.*f", asset_info.precision, 1.0 * fee / scale_factor);
JADE_ASSERT(ret > 0 && ret < sizeof(feeamount));
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.