Merge remote-tracking branch 'agent/benma-agent/compact-erc20-storage'
What changed, and why it matters
This commit is a code-size optimization for how the BitBox02 hardware wallet stores ERC-20 token metadata (token symbols, decimals, and contract addresses). It replaces a simpler but larger table with a compact, bit-packed lookup table generated at build time. There is no direct evidence in the commit that this fixes a security bug; it appears to be a firmware-size reduction refactor. The only user-visible code change is switching from a static string reference to an owned String for the token unit, which is a consequence of the new packed storage format.
No security action required. Treat as a normal firmware optimization. If reviewing for release, verify that the generated token table still passes the existing tests and that the new `String` allocation in `Params::unit` does not meaningfully affect stack/heap usage on the embedded target.
Security signals we found
Refactor only: no new unsafe blocks, no removal of bounds checks, no change to cryptographic or signing logic
Token lookup still uses binary search over fixed-size address arrays and validated build-time data
Unit decoder reads packed bytes with fixed `len` and `bits_per_char` derived from the same grouped metadata; no user-controlled lengths
No mention of CVE, security fix, bug bounty, or external report in commit message or diff
Evidence from the diff
The change refactors erc20_params to compress the embedded ERC-20 token list. Instead of storing per-token records with a pointer to a unit string and a 20-byte address, it now concatenates all contract addresses into one array and packs unit strings into a shared bitstream using a 5- or 6-bit alphabet encoding. Groups of tokens are described by a Group { count, decimals, unit_len, unit_is_ambiguous, bits_per_char } struct, and get() walks these groups to binary-search the address slice and decode the unit on demand. The sign.rs caller is updated to borrow the now-heap-allocated unit String. The commit includes unit tests for the new decoder. No vulnerability, bounds-checking mistake, or unsafe change is visible in the diff.
Changed components
src/rust/erc20_params/build.rssrc/rust/erc20_params/src/lib.rssrc/rust/bitbox02-rust/src/hww/api/ethereum/sign.rsInspect captured patch +187 / −61
### src/rust/Cargo.lock
@@ -886,6 +886,7 @@ name = "erc20_params"
version = "0.1.0"
dependencies = [
"hex",
+ "hex_lit",
]
[[package]]
### src/rust/bitbox02-rust/src/hww/api/ethereum/sign.rs
@@ -360,7 +360,7 @@ async fn verify_erc20_transaction(
if let Some(payment_request) = payment_request {
let token_params = erc20_params.ok_or(Error::InvalidInput)?;
let displayed_source_amount = Amount {
- unit: token_params.unit,
+ unit: &token_params.unit,
decimals: token_params.decimals as _,
value: erc20_value.clone(),
}
@@ -385,7 +385,7 @@ async fn verify_erc20_transaction(
let (formatted_value, formatted_total) = match erc20_params {
Some(erc20_params) => {
let value = Amount {
- unit: erc20_params.unit,
+ unit: &erc20_params.unit,
decimals: erc20_params.decimals as _,
value: erc20_value,
}
### src/rust/erc20_params/Cargo.toml
@@ -9,6 +9,7 @@ license = "Apache-2.0"
[dev-dependencies]
hex = { workspace = true }
+hex_lit = { workspace = true }
[build-dependencies]
hex = { workspace = true }
### src/rust/erc20_params/build.rs
@@ -2,6 +2,7 @@
#![allow(clippy::format_collect)]
+use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet};
use std::fs::{File, OpenOptions};
use std::io::{self, BufRead, Write};
@@ -53,15 +54,42 @@ fn main() {
.insert(token.contract_address);
}
- // Group tokens by decimals
- let mut grouped_tokens: BTreeMap<(u8, u8), Vec<&Token>> = BTreeMap::new();
+ // The 32 most frequent characters fit in five bits.
+ let mut frequencies = BTreeMap::<u8, usize>::new();
+ for byte in tokens.iter().flat_map(|token| token.unit.bytes()) {
+ *frequencies.entry(byte).or_default() += 1;
+ }
+ let mut alphabet: Vec<u8> = frequencies.keys().copied().collect();
+ alphabet.sort_by_key(|byte| (Reverse(frequencies[byte]), *byte));
+ assert!(
+ alphabet.len() <= 64,
+ "ERC-20 unit alphabet exceeds 64 characters"
+ );
+ let alphabet: String = alphabet.into_iter().map(char::from).collect();
+ let encode_char = |byte| alphabet.bytes().position(|ch| ch == byte).unwrap() as u8;
+
+ // Group tokens by decimals, unit length, ambiguity and encoding width.
+ let mut grouped_tokens: BTreeMap<(u8, u8, bool, u8), Vec<&Token>> = BTreeMap::new();
for token in &tokens {
+ let bits_per_char = if token.unit.bytes().all(|byte| encode_char(byte) < 32) {
+ 5
+ } else {
+ 6
+ };
grouped_tokens
- .entry((token.decimals, token.unit.len().try_into().unwrap()))
+ .entry((
+ token.decimals,
+ token.unit.len().try_into().unwrap(),
+ contracts_by_unit[token.unit.as_str()].len() > 1,
+ bits_per_char,
+ ))
.or_default()
.push(token);
}
+ let mut grouped_tokens: Vec<_> = grouped_tokens.into_iter().collect();
+ grouped_tokens.sort_by_key(|(_, tokens)| Reverse(tokens.len()));
+
let out_filename = Path::new(&std::env::var("OUT_DIR").unwrap()).join("tokens.rs");
let mut output_file = OpenOptions::new()
.create(true)
@@ -70,30 +98,15 @@ fn main() {
.open(out_filename)
.unwrap();
- // BTreeMap iteration keeps this list sorted for binary search at runtime.
- writeln!(output_file, "const AMBIGUOUS_UNITS: &[&str] = &[").unwrap();
- for (unit, contracts) in &contracts_by_unit {
- if contracts.len() > 1 {
- writeln!(output_file, " \"{}\",", unit.escape_default()).unwrap();
- }
- }
- writeln!(output_file, "];\n").unwrap();
-
- for ((decimals, unit_len), tokens) in &mut grouped_tokens {
+ writeln!(output_file, "const CONTRACT_ADDRESSES: &[[u8; 20]] = &[").unwrap();
+ for (_, tokens) in &mut grouped_tokens {
// Sort by contract address so we can look up by contract
// address more efficiently.
tokens.sort_by_key(|token| token.contract_address);
- writeln!(
- output_file,
- "const PARAMS_D{}_U{}: &[P] = &[",
- decimals, unit_len
- )
- .unwrap();
- for token in tokens {
+ for token in tokens.iter() {
writeln!(
output_file,
- " P {{ unit: b\"{}\".as_ptr(), contract_address: *b\"{}\" }},",
- token.unit,
+ " *b\"{}\",",
token
.contract_address
.iter()
@@ -102,18 +115,48 @@ fn main() {
)
.unwrap();
}
- writeln!(output_file, "];").unwrap();
}
+ writeln!(output_file, "];").unwrap();
+ let mut packed_units = Vec::<u8>::new();
+ let mut bit_offset = 0usize;
+ // Emit one bit at a time; this runs only at build time.
+ for ((_, _, _, bits_per_char), tokens) in &grouped_tokens {
+ for byte in tokens.iter().flat_map(|token| token.unit.bytes()) {
+ let code = encode_char(byte);
+ for bit in 0..*bits_per_char {
+ if bit_offset.is_multiple_of(8) {
+ packed_units.push(0);
+ }
+ packed_units[bit_offset / 8] |= ((code >> bit) & 1) << (bit_offset % 8);
+ bit_offset += 1;
+ }
+ }
+ }
+ writeln!(
+ output_file,
+ "const UNIT_ALPHABET: &[u8] = b\"{}\";",
+ alphabet.escape_default(),
+ )
+ .unwrap();
+ writeln!(
+ output_file,
+ "const UNITS: &[u8] = b\"{}\";",
+ packed_units
+ .iter()
+ .map(|byte| format!("\\x{byte:02x}"))
+ .collect::<String>(),
+ )
+ .unwrap();
writeln!(
output_file,
- "const ALL: &[(u8, u8, &[P])] = &[{}];",
+ "const ALL: &[Group] = &[{}];",
grouped_tokens
- .keys()
- .map(|(decimal, unit_len)| format!(
- "({}, {}, PARAMS_D{}_U{})",
- decimal, unit_len, decimal, unit_len
- ))
+ .iter()
+ .map(|((decimals, unit_len, unit_is_ambiguous, bits_per_char), tokens)| {
+ let count: u16 = tokens.len().try_into().unwrap();
+ format!("Group {{ count: {count}, decimals: {decimals}, unit_len: {unit_len}, unit_is_ambiguous: {unit_is_ambiguous}, bits_per_char: {bits_per_char} }}")
+ })
.collect::<Vec<String>>()
.join(", ")
)
### src/rust/erc20_params/src/lib.rs
@@ -2,71 +2,152 @@
#![no_std]
-#[repr(Rust, packed)]
-struct P {
- // Null-terminated strings, saving a few bytes of binary space per token stored versus using
- // `&str`, which also stores the size of the string. We could further optimize for space by
- // grouping tokens by unit of len 1, 2, 3, ..., removing the need for a null terminator byte, as
- // most of them are length 3.
- pub unit: *const u8,
- pub contract_address: [u8; 20],
+extern crate alloc;
+
+use alloc::string::String;
+
+struct Group {
+ count: u16,
+ decimals: u8,
+ unit_len: u8,
+ unit_is_ambiguous: bool,
+ /// Bits per `UNIT_ALPHABET` index: 5 for units using only the 32 most frequent
+ /// characters, otherwise 6.
+ bits_per_char: u8,
}
pub struct Params {
- pub unit: &'static str,
+ pub unit: String,
pub contract_address: [u8; 20],
pub decimals: u8,
+ unit_is_ambiguous: bool,
}
impl Params {
/// Whether multiple registered contracts share this displayed unit, regardless of decimals.
pub fn unit_is_ambiguous(&self) -> bool {
- AMBIGUOUS_UNITS.binary_search(&self.unit).is_ok()
- }
-
- fn from_p(p: &P, decimals: u8, unit_len: u8) -> Self {
- let unit = unsafe { core::slice::from_raw_parts(p.unit, unit_len as usize) };
- Params {
- // SAFETY: `p.unit` points to a Rust byte-string generated from a validated ASCII unit.
- unit: unsafe { core::str::from_utf8_unchecked(unit) },
- contract_address: p.contract_address,
- decimals,
- }
+ self.unit_is_ambiguous
}
}
-// Includes `const PARAMS_18: &[P] = ...` for each existing decimals.
-// And `const ALL: &[(u8, &[P])] = ...` listing all params by decimals so we can iterate them.
+// Includes `const ALL: &[Group] = ...` grouped by decimals, unit length, ambiguity and encoding width.
// This way we don't repeat the decimal in every token, saving ~1 byte of binary space per token.
-// Also includes the sorted AMBIGUOUS_UNITS list for contract-identity confirmation.
+// Each group's ASCII units are concatenated in contract-address order, saving a pointer per token.
+// Addresses and units are concatenated across groups; counts and widths determine the lookup offsets.
+// Grouping by ambiguity avoids a separate symbol index for contract-identity confirmation.
// Generated by build.rs.
include!(concat!(env!("OUT_DIR"), "/tokens.rs"));
+fn decode_unit(
+ data: &[u8],
+ alphabet: &[u8],
+ start_bit: usize,
+ len: usize,
+ bits_per_char: usize,
+) -> String {
+ let mut unit = String::with_capacity(len);
+ let mask = (1u8 << bits_per_char) - 1;
+ // Alphabet indices are packed least-significant bits first, without padding per unit or group.
+ for index in 0..len {
+ let bit_offset = start_bit + index * bits_per_char;
+ let shift = bit_offset % 8;
+ let mut code = data[bit_offset / 8] >> shift;
+ if shift + bits_per_char > 8 {
+ code |= data[bit_offset / 8 + 1] << (8 - shift);
+ }
+ unit.push(char::from(alphabet[usize::from(code & mask)]));
+ }
+ unit
+}
+
pub fn get(chain_id: u64, contract_address: [u8; 20]) -> Option<Params> {
if chain_id != 1 {
return None;
}
- for &(decimals, unit_len, params) in ALL.iter() {
- let result = params
- .binary_search_by_key(&contract_address, |p| p.contract_address)
- .ok()
- .map(|idx| Params::from_p(¶ms[idx], decimals, unit_len));
- if result.is_some() {
- return result;
+ let mut address_offset = 0;
+ let mut unit_bit_offset = 0;
+ for group in ALL {
+ let count = usize::from(group.count);
+ let unit_len = usize::from(group.unit_len);
+ let bits_per_char = usize::from(group.bits_per_char);
+ let unit_bits = unit_len * bits_per_char;
+ let address_end = address_offset + count;
+ if let Ok(index) =
+ CONTRACT_ADDRESSES[address_offset..address_end].binary_search(&contract_address)
+ {
+ let start_bit = unit_bit_offset + index * unit_bits;
+ return Some(Params {
+ unit: decode_unit(UNITS, UNIT_ALPHABET, start_bit, unit_len, bits_per_char),
+ contract_address,
+ decimals: group.decimals,
+ unit_is_ambiguous: group.unit_is_ambiguous,
+ });
}
+ address_offset = address_end;
+ unit_bit_offset += count * unit_bits;
}
None
}
#[cfg(test)]
mod tests {
use super::*;
+ use hex_lit::hex;
extern crate std;
use std::fs::File;
use std::io::{self, BufRead};
use std::vec::Vec;
+ const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+ #[test]
+ fn test_decode_unit() {
+ let data = hex!("40a6cd74efff");
+ let expected = "AZaz09+/";
+ assert_eq!(decode_unit(&data, ALPHABET, 0, expected.len(), 6), expected);
+ for index in 0..expected.len() {
+ assert_eq!(
+ decode_unit(&data, ALPHABET, index * 6, 1, 6),
+ &expected[index..index + 1],
+ );
+ }
+ assert_eq!(decode_unit(&data, ALPHABET, 12, 4, 6), "az09");
+ }
+
+ #[test]
+ fn test_decode_unit_five_bits() {
+ let data = hex!("20072dfef0");
+ assert_eq!(decode_unit(&data, ALPHABET, 0, 8, 5), "AZBaCfDe");
+ assert_eq!(decode_unit(&data, ALPHABET, 10, 4, 5), "BaCf");
+ }
+
+ #[test]
+ fn test_decode_unit_mixed_widths() {
+ let data = hex!("e00fbee8074ff6a29bfce0");
+ let expected = "A/B+C9D8E7F6G5H4";
+ let mut start_bit: usize = 0;
+ for index in 0..expected.len() {
+ let bits_per_char = if index % 2 == 0 { 5 } else { 6 };
+ let end = (start_bit + bits_per_char).div_ceil(8);
+ assert_eq!(
+ decode_unit(&data[..end], ALPHABET, start_bit, 1, bits_per_char),
+ &expected[index..index + 1],
+ );
+ start_bit += bits_per_char;
+ }
+ }
+
+ #[test]
+ fn test_decode_unit_tail() {
+ assert_eq!(decode_unit(&[], ALPHABET, 0, 0, 5), "");
+ assert_eq!(decode_unit(&[], ALPHABET, 0, 0, 6), "");
+ assert_eq!(decode_unit(&hex!("19"), ALPHABET, 0, 1, 6), "Z");
+ assert_eq!(decode_unit(&hex!("4000"), ALPHABET, 6, 1, 6), "B");
+ assert_eq!(decode_unit(&hex!("402000"), ALPHABET, 12, 1, 6), "C");
+ assert_eq!(decode_unit(&hex!("40200c"), ALPHABET, 18, 1, 6), "D");
+ }
+
#[test]
fn test_get() {
// Not found for chainID 0.Why this scored 17/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.