util: extract big-integer decimal formatting
What changed, and why it matters
This commit is a straightforward code cleanup: it pulls out existing big-number-to-text conversion into named helper functions so they can be tested directly. The actual behavior is unchanged because the new helpers still call the same underlying library method (num-bigint's to_string()). There is no security fix or vulnerability here.
No security action needed. Treat as normal refactoring; optionally add unit tests for the newly exposed helpers.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors decimal formatting in the Rust utility module. It introduces biguint_to_string() and bigint_to_string() wrappers and a decimal_string() trait method, routing EIP-712 typed-message signing and amount formatting through them. Both wrappers delegate to num_bigint::BigUint/BigInt::to_string(), so the produced strings are identical to before. No input validation, parsing, or output behavior changes.
Changed components
src/rust/util/src/decimal.rssrc/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rsInspect captured patch +23 / −7
### src/rust/bitbox02-rust/src/hww/api/ethereum/sign_typed_msg.rs
@@ -321,7 +321,7 @@ fn encode_value(
}
(
leftpad32(&value, false)?,
- BigUint::from_bytes_be(&value).to_string(),
+ util::decimal::biguint_to_string(&BigUint::from_bytes_be(&value)),
)
}
DataType::Int => {
@@ -330,7 +330,7 @@ fn encode_value(
}
(
leftpad32(&value, true)?,
- BigInt::from_signed_bytes_be(&value).to_string(),
+ util::decimal::bigint_to_string(&BigInt::from_signed_bytes_be(&value)),
)
}
DataType::Bool => {
### src/rust/util/src/decimal.rs
@@ -3,20 +3,36 @@
extern crate alloc;
use alloc::string::{String, ToString};
-use num_bigint::BigUint;
+use num_bigint::{BigInt, BigUint};
-pub trait Format: ToString {}
+pub trait Format: ToString {
+ fn decimal_string(&self) -> String {
+ self.to_string()
+ }
+}
impl Format for u64 {}
-impl Format for &BigUint {}
+impl Format for &BigUint {
+ fn decimal_string(&self) -> String {
+ biguint_to_string(self)
+ }
+}
+
+pub fn biguint_to_string(value: &BigUint) -> String {
+ value.to_string()
+}
+
+pub fn bigint_to_string(value: &BigInt) -> String {
+ value.to_string()
+}
/// Formats integer `value` as `value / 10^decimals`, with up to `decimals` decimal places,
/// trimming traling zeroes.
/// E.g. "123450" with decimals=3: "123.45".
/// Value must consists only of '0'-'9' digits.
pub fn format<F: Format>(value: F, decimals: usize) -> String {
- let mut v: String = format!("{:0>width$}", value.to_string(), width = decimals + 1);
+ let mut v: String = format!("{:0>width$}", value.decimal_string(), width = decimals + 1);
v.insert(v.len() - decimals, '.');
v.trim_end_matches('0').trim_end_matches('.').into()
}
@@ -26,7 +42,7 @@ pub fn format<F: Format>(value: F, decimals: usize) -> String {
/// E.g. "123450" with decimals=3: "123.450".
/// Value must consists only of '0'-'9' digits.
pub fn format_no_trim<F: Format>(value: F, decimals: usize) -> String {
- let mut v: String = format!("{:0>width$}", value.to_string(), width = decimals + 1);
+ let mut v: String = format!("{:0>width$}", value.decimal_string(), width = decimals + 1);
if decimals > 0 {
v.insert(v.len() - decimals, '.');
}Why this scored 19/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.