What changed, and why it matters
This commit is a code-size optimization for the BitBox02 hardware wallet firmware. It replaces generic big-integer formatting and multiplication with smaller, hand-written scalar loops specifically sized for Ethereum's needs. There is no security fix or vulnerability being patched; the goal is to reduce firmware size by about 6,420 bytes while keeping the same arithmetic results.
No security action required. Treat as a normal firmware optimization commit. Reviewers may want to verify the new scalar loops preserve correctness for all Ethereum gas-limit and fee values, and that the CI symbol regexes correctly catch regressions.
Security signals we found
No security-relevant signal in commit message or diff
Change is framed as size optimization, not vulnerability remediation
CI check added to enforce future code-size constraints, not to block unsafe APIs
Evidence from the diff
The change removes reliance on num-bigint’s general decimal formatting (to_string/to_str_radix) and general multiplication/division routines in Ethereum fee parsing. It introduces multiply_by_be_bytes() in sign.rs that computes gas_price * gas_limit by iterating gas-limit bytes from MSB to LSB with shifts and scalar BigUint * u32 products. It also replaces BigUint/BigInt display formatting in util/src/decimal.rs with repeated division/remainder by 10u32. A CI symbol check is added to prevent these larger routines from being silently reintroduced. The commit explicitly states that precision, validation, decimal placement, trimming, and EIP-712 sign extension are unchanged.
Changed components
src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rssrc/rust/util/src/decimal.rs.ci/check-unwanted-symbolsInspect captured patch +44 / −6
### .ci/check-unwanted-symbols
@@ -61,6 +61,20 @@ check_firmware() {
"(__aeabi_f(add|sub|mul|div)|__(add|sub|mul|div|neg)sf3|compiler_builtins::float::(add|sub|mul|div)::.*f32)" \
"Software f32 arithmetic helpers add significant binary bloat." \
"Use integer arithmetic in firmware code instead."
+
+ check_symbols \
+ "$elf" \
+ "num-bigint decimal formatting" \
+ "num_bigint::(biguint::BigUint|bigint::BigInt).*(to_str_radix|as core::fmt::Display)" \
+ "Generic BigUint/BigInt decimal formatting pulls in large-number arithmetic." \
+ "Use the util::decimal helpers instead."
+
+ check_symbols \
+ "$elf" \
+ "num-bigint general multiplication and division" \
+ "num_bigint::biguint::(multiplication::(mac3|mul3)|division::div_rem_core)" \
+ "General BigUint multiplication and division add significant binary bloat." \
+ "Use scalar arithmetic where possible, as in Ethereum's parse_fee()."
}
check_bootloader() {
### src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs
@@ -20,7 +20,7 @@ use alloc::vec::Vec;
use hex_lit::hex;
use pb::eth_response::Response;
-use core::ops::{Add, Mul};
+use core::ops::Add;
use num_bigint::BigUint;
// 1 ETH = 1e18 wei.
@@ -202,22 +202,31 @@ async fn verify_payment_request_recipient(
// 1) actual gas used will often be lower than gas limit (in the case of contract interactions, not simple ETH transfers)
// 2) in the case of 1559 base fee at execution time might also be lower so that `base fee + priority fee < max fee per gas`
fn parse_fee<'a>(request: &Transaction<'_>, params: &'a Params) -> Amount<'a> {
- let gas_limit = BigUint::from_bytes_be(request.gas_limit());
+ let gas_limit = request.gas_limit();
+ fn multiply_by_be_bytes(value: &BigUint, bytes: &[u8]) -> BigUint {
+ // Scalar products avoid pulling in num-bigint's large-number multiplication routines.
+ let mut result = BigUint::ZERO;
+ for &byte in bytes {
+ result <<= 8;
+ result += value * u32::from(byte);
+ }
+ result
+ }
match request {
Transaction::Legacy(legacy) => {
let gas_price = BigUint::from_bytes_be(&legacy.gas_price);
Amount {
unit: params.unit,
decimals: WEI_DECIMALS,
- value: gas_price.mul(gas_limit),
+ value: multiply_by_be_bytes(&gas_price, gas_limit),
}
}
Transaction::Eip1559(eip1559) => {
let max_fee_per_gas = BigUint::from_bytes_be(&eip1559.max_fee_per_gas);
Amount {
unit: params.unit,
decimals: WEI_DECIMALS,
- value: max_fee_per_gas.mul(gas_limit),
+ value: multiply_by_be_bytes(&max_fee_per_gas, gas_limit),
}
}
}
### src/rust/util/src/decimal.rs
@@ -20,11 +20,26 @@ impl Format for &BigUint {
}
pub fn biguint_to_string(value: &BigUint) -> String {
- value.to_string()
+ // Scalar division avoids pulling in num-bigint's large-number radix conversion routines.
+ if value == &BigUint::ZERO {
+ return "0".into();
+ }
+ let mut value = value.clone();
+ let mut digits = alloc::vec::Vec::new();
+ while value != BigUint::ZERO {
+ digits.push(b'0' + u8::try_from(&value % 10u32).unwrap());
+ value /= 10u32;
+ }
+ digits.reverse();
+ String::from_utf8(digits).unwrap()
}
pub fn bigint_to_string(value: &BigInt) -> String {
- value.to_string()
+ let mut text = biguint_to_string(value.magnitude());
+ if value.sign() == num_bigint::Sign::Minus {
+ text.insert(0, '-');
+ }
+ text
}
/// Formats integer `value` as `value / 10^decimals`, with up to `decimals` decimal places,Why this scored 12/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.