Converts the given `message` into a vector of digits. ## Output format - sequence of `n_digits` many digits - each digit a `u32` value in range `0..2.pow(log2_base)` - message bytes are reversed (but not their nibbles!)
(n_digits: u32, log2_base: u32, message: &[u8])
| 45 | /// - each digit a `u32` value in range `0..2.pow(log2_base)` |
| 46 | /// - message bytes are reversed (but not their nibbles!) |
| 47 | pub(crate) fn message_to_digits(n_digits: u32, log2_base: u32, message: &[u8]) -> Vec<u32> { |
| 48 | debug_assert!((4..=8).contains(&log2_base)); |
| 49 | debug_assert!( |
| 50 | message.len() as u32 * 8 <= n_digits * log2_base, |
| 51 | "Message is too long to fit into the given number of digits" |
| 52 | ); |
| 53 | |
| 54 | let mut digits = vec![0u32; n_digits as usize]; // cast safety: 32-bit machine or higher |
| 55 | let mut digit_idx: u32 = 0; |
| 56 | let mut bit_idx: u32 = 0; |
| 57 | |
| 58 | for mut byte in message.iter().copied() { |
| 59 | for _ in 0..8 { |
| 60 | if bit_idx == log2_base { |
| 61 | bit_idx = 0; |
| 62 | digit_idx += 1; |
| 63 | } |
| 64 | digits[digit_idx as usize] |= ((byte & 1) as u32) << bit_idx; // cast safety: 32-bit machine or higher |
| 65 | byte >>= 1; |
| 66 | bit_idx += 1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | digits.reverse(); |
| 71 | digits |
| 72 | } |
| 73 | |
| 74 | /// Returns a Bitcoin script that converts a message into a number. |
| 75 | /// |
no outgoing calls