2023-12-14 15:39:16 +00:00
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
|
|
|
/// Decomposed amount table.
|
|
|
|
///
|
|
|
|
static DECOMPOSED_AMOUNTS: OnceLock<[u64; 172]> = OnceLock::new();
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
pub fn decomposed_amounts() -> &'static [u64; 172] {
|
|
|
|
DECOMPOSED_AMOUNTS.get_or_init(|| {
|
|
|
|
[
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
|
|
10, 20, 30, 40, 50, 60, 70, 80, 90,
|
|
|
|
100, 200, 300, 400, 500, 600, 700, 800, 900,
|
|
|
|
1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000,
|
|
|
|
10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000,
|
|
|
|
100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000,
|
|
|
|
1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000,
|
|
|
|
10000000, 20000000, 30000000, 40000000, 50000000, 60000000, 70000000, 80000000, 90000000,
|
|
|
|
100000000, 200000000, 300000000, 400000000, 500000000, 600000000, 700000000, 800000000, 900000000,
|
|
|
|
1000000000, 2000000000, 3000000000, 4000000000, 5000000000, 6000000000, 7000000000, 8000000000, 9000000000,
|
|
|
|
10000000000, 20000000000, 30000000000, 40000000000, 50000000000, 60000000000, 70000000000, 80000000000, 90000000000,
|
|
|
|
100000000000, 200000000000, 300000000000, 400000000000, 500000000000, 600000000000, 700000000000, 800000000000, 900000000000,
|
|
|
|
1000000000000, 2000000000000, 3000000000000, 4000000000000, 5000000000000, 6000000000000, 7000000000000, 8000000000000, 9000000000000,
|
|
|
|
10000000000000, 20000000000000, 30000000000000, 40000000000000, 50000000000000, 60000000000000, 70000000000000, 80000000000000, 90000000000000,
|
|
|
|
100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 900000000000000,
|
|
|
|
1000000000000000, 2000000000000000, 3000000000000000, 4000000000000000, 5000000000000000, 6000000000000000, 7000000000000000, 8000000000000000, 9000000000000000,
|
|
|
|
10000000000000000, 20000000000000000, 30000000000000000, 40000000000000000, 50000000000000000, 60000000000000000, 70000000000000000, 80000000000000000, 90000000000000000,
|
|
|
|
100000000000000000, 200000000000000000, 300000000000000000, 400000000000000000, 500000000000000000, 600000000000000000, 700000000000000000, 800000000000000000, 900000000000000000,
|
|
|
|
1000000000000000000, 2000000000000000000, 3000000000000000000, 4000000000000000000, 5000000000000000000, 6000000000000000000, 7000000000000000000, 8000000000000000000, 9000000000000000000,
|
2024-01-05 22:36:47 +00:00
|
|
|
10000000000000000000
|
|
|
|
]
|
2023-12-14 15:39:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that an output amount is decomposed.
|
|
|
|
///
|
|
|
|
/// This is also used during miner tx verification.
|
|
|
|
///
|
2024-01-07 01:15:33 +00:00
|
|
|
/// ref: https://monero-book.cuprate.org/consensus_rules/transactions/ring_signatures.html#output-amount
|
|
|
|
/// ref: https://monero-book.cuprate.org/consensus_rules/blocks/miner_tx.html#output-amounts
|
2023-12-14 15:39:16 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn is_decomposed_amount(amount: &u64) -> bool {
|
|
|
|
decomposed_amounts().binary_search(amount).is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn decomposed_amounts_return_decomposed() {
|
|
|
|
for amount in decomposed_amounts() {
|
|
|
|
assert!(is_decomposed_amount(amount))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-01-05 22:36:47 +00:00
|
|
|
fn non_decomposed_amounts_return_not_decomposed() {
|
2023-12-14 15:39:16 +00:00
|
|
|
assert!(!is_decomposed_amount(&21));
|
|
|
|
assert!(!is_decomposed_amount(&345431));
|
|
|
|
assert!(!is_decomposed_amount(&20000001));
|
|
|
|
}
|
|
|
|
}
|