Compare commits
No commits in common. "6a99a09089113fb2854f88e731865299b8519bd6" and "7467a30c40e44f11e79be90c790758ec5100c3c4" have entirely different histories.
6a99a09089
...
7467a30c40
|
@ -76,34 +76,32 @@ fn build_wordlist() -> Vec<String> {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bitslice_to_usize(bitslice: [bool; 11]) -> usize {
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||||
let mut index = 0usize;
|
fn u8_to_bitslice(byte: &u8) -> [bool; 8] {
|
||||||
for i in 0..11 {
|
[
|
||||||
index += usize::from(bitslice[10 - i]) << i;
|
byte & (1 << 0) != 0,
|
||||||
}
|
byte & (1 << 1) != 0,
|
||||||
index
|
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 entropy_to_bits(bytes: &[u8]) -> Vec<bool> {
|
fn bitslice_to_usize(bitslice: [bool; 11]) -> usize {
|
||||||
let bit_count = bytes.len() * 8;
|
usize::from(bitslice[10])
|
||||||
let hash = generate_slice_hash(bytes);
|
+ (usize::from(bitslice[9]) << 1)
|
||||||
|
+ (usize::from(bitslice[8]) << 2)
|
||||||
assert_eq!(bit_count % 32, 0, "bit count must be in 32 bit increments");
|
+ (usize::from(bitslice[7]) << 3)
|
||||||
|
+ (usize::from(bitslice[6]) << 4)
|
||||||
let mut bits = vec![false; bit_count + bit_count / 32];
|
+ (usize::from(bitslice[5]) << 5)
|
||||||
|
+ (usize::from(bitslice[4]) << 6)
|
||||||
for byte_index in 0..bit_count / 8 {
|
+ (usize::from(bitslice[3]) << 7)
|
||||||
for bit_index in 0..8 {
|
+ (usize::from(bitslice[2]) << 8)
|
||||||
bits[byte_index * 8 + bit_index] = (bytes[byte_index] & (1 << (7 - bit_index))) > 0;
|
+ (usize::from(bitslice[1]) << 9)
|
||||||
}
|
+ (usize::from(bitslice[0]) << 10)
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
@ -135,8 +133,18 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let entropy = &mut [0u8; 256 / 8];
|
let entropy = &mut [0u8; 256 / 8];
|
||||||
random_handle.read_exact(&mut entropy[..])?;
|
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
|
let words = seed_bits
|
||||||
.chunks_exact(11)
|
.chunks_exact(11)
|
||||||
.map(|chunk| wordlist[bitslice_to_usize(chunk.try_into().expect("11 bit chunks"))].clone())
|
.map(|chunk| wordlist[bitslice_to_usize(chunk.try_into().expect("11 bit chunks"))].clone())
|
||||||
|
@ -171,8 +179,14 @@ mod tests {
|
||||||
panic!("bad test: {test}");
|
panic!("bad test: {test}");
|
||||||
};
|
};
|
||||||
let hex = hex::decode(hex_.as_str().unwrap()).unwrap();
|
let hex = hex::decode(hex_.as_str().unwrap()).unwrap();
|
||||||
|
let hash = generate_slice_hash(&hex);
|
||||||
let seed_bits = entropy_to_bits(&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
|
let words = seed_bits
|
||||||
.chunks_exact(11)
|
.chunks_exact(11)
|
||||||
|
@ -180,8 +194,7 @@ mod tests {
|
||||||
wordlist[bitslice_to_usize(chunk.try_into().expect("11 bit chunks"))].clone()
|
wordlist[bitslice_to_usize(chunk.try_into().expect("11 bit chunks"))].clone()
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
panic!("{}", words.join(" "));
|
||||||
assert_eq!(words.join(" "), seed.as_str().unwrap());
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue