Compare commits

..

No commits in common. "6a99a09089113fb2854f88e731865299b8519bd6" and "7467a30c40e44f11e79be90c790758ec5100c3c4" have entirely different histories.

1 changed files with 44 additions and 31 deletions

View File

@ -76,34 +76,32 @@ fn build_wordlist() -> Vec<String> {
.collect()
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn u8_to_bitslice(byte: &u8) -> [bool; 8] {
[
byte & (1 << 0) != 0,
byte & (1 << 1) != 0,
byte & (1 << 2) != 0,
byte & (1 << 3) != 0,
byte & (1 << 4) != 0,
byte & (1 << 5) != 0,
byte & (1 << 6) != 0,
byte & (1 << 7) != 0,
]
}
fn bitslice_to_usize(bitslice: [bool; 11]) -> usize {
let mut index = 0usize;
for i in 0..11 {
index += usize::from(bitslice[10 - i]) << i;
}
index
}
fn entropy_to_bits(bytes: &[u8]) -> Vec<bool> {
let bit_count = bytes.len() * 8;
let hash = generate_slice_hash(bytes);
assert_eq!(bit_count % 32, 0, "bit count must be in 32 bit increments");
let mut bits = vec![false; bit_count + bit_count / 32];
for byte_index in 0..bit_count / 8 {
for bit_index in 0..8 {
bits[byte_index * 8 + bit_index] = (bytes[byte_index] & (1 << (7 - bit_index))) > 0;
}
}
for check_bit in 0..bit_count / 32 {
bits[bit_count + check_bit] = (hash[check_bit / 8] & (1 << (7 - (check_bit % 8)))) > 0;
}
assert_eq!(bits.len() % 11, 0, "unstable bit count");
bits
usize::from(bitslice[10])
+ (usize::from(bitslice[9]) << 1)
+ (usize::from(bitslice[8]) << 2)
+ (usize::from(bitslice[7]) << 3)
+ (usize::from(bitslice[6]) << 4)
+ (usize::from(bitslice[5]) << 5)
+ (usize::from(bitslice[4]) << 6)
+ (usize::from(bitslice[3]) << 7)
+ (usize::from(bitslice[2]) << 8)
+ (usize::from(bitslice[1]) << 9)
+ (usize::from(bitslice[0]) << 10)
}
fn main() -> Result<(), Box<dyn Error>> {
@ -135,8 +133,18 @@ fn main() -> Result<(), Box<dyn Error>> {
let entropy = &mut [0u8; 256 / 8];
random_handle.read_exact(&mut entropy[..])?;
let seed_bits = entropy_to_bits(entropy);
let hash = generate_slice_hash(entropy);
let checksum = u8_to_bitslice(&hash[1]);
let mut seed_bits = entropy[..bit_size / 8]
.iter()
.flat_map(u8_to_bitslice)
.collect::<Vec<_>>();
seed_bits.extend(if bit_size == 256 {
&checksum[..8]
} else {
&checksum[..4]
});
let words = seed_bits
.chunks_exact(11)
.map(|chunk| wordlist[bitslice_to_usize(chunk.try_into().expect("11 bit chunks"))].clone())
@ -171,8 +179,14 @@ mod tests {
panic!("bad test: {test}");
};
let hex = hex::decode(hex_.as_str().unwrap()).unwrap();
let seed_bits = entropy_to_bits(&hex);
let hash = generate_slice_hash(&hex);
let checksum = u8_to_bitslice(&hash.iter().next().unwrap());
let mut seed_bits = hex.iter().flat_map(u8_to_bitslice).collect::<Vec<_>>();
seed_bits.extend(if hex.len() == 256 / 8 {
&checksum[..8]
} else {
&checksum[..4]
});
let words = seed_bits
.chunks_exact(11)
@ -180,8 +194,7 @@ mod tests {
wordlist[bitslice_to_usize(chunk.try_into().expect("11 bit chunks"))].clone()
})
.collect::<Vec<_>>();
assert_eq!(words.join(" "), seed.as_str().unwrap());
panic!("{}", words.join(" "));
}
Ok(())
}