Created
March 2, 2025 04:42
-
-
Save rygorous/fe562754efca31ac11558b9fc78498c0 to your computer and use it in GitHub Desktop.
Base64 fixed point test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bit_set::BitSet; | |
use std::mem; | |
// Vanilla RFC 4648 | |
const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
// URL-safe RFC 4648 | |
//const ALPHABET: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | |
const COUNT: usize = 1usize << 24; // 3 bytes worth suffices for this test | |
fn lookup(index: u32) -> u32 { | |
ALPHABET.as_bytes()[(index & 63) as usize].into() | |
} | |
fn encode(value: u32) -> u32 { | |
// All big endian for consistency | |
let b0 = lookup(value >> 18); | |
let b1 = lookup(value >> 12); | |
let b2 = lookup(value >> 6); | |
(b0 << 16) | (b1 << 8) | b2 | |
} | |
fn main() { | |
let encode_map: Vec<u32> = (0..(COUNT as u32)).map(encode).collect(); | |
let mut curr = BitSet::with_capacity(COUNT); | |
let mut next = BitSet::with_capacity(COUNT); | |
// Initial bit set has all possible bytes | |
for x in 0..COUNT { | |
curr.insert(x); | |
} | |
loop { | |
let count = curr.len(); | |
println!("{0} elements in set", count); | |
if count < 100 { | |
for x in &curr { | |
println!(" {0}{1}{2}", ((x >> 16) & 255) as u8 as char, ((x >> 8) & 255) as u8 as char, (x & 255) as u8 as char); | |
} | |
} | |
next.clear(); | |
for x in &curr { | |
next.insert(encode_map[x] as usize); | |
} | |
if next == curr { | |
break; | |
} | |
mem::swap(&mut curr, &mut next); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment