Skip to content

Commit 23d5528

Browse files
committed
Begin outlining u128 experiment.
1 parent b8d29ea commit 23d5528

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

crates/pgen/src/bip39_algorithm.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,45 @@ fn get_word_from_11_bits(value: u16) -> &'static str {
5656
WL_BIP39[value as usize]
5757
}
5858

59+
/// Extract 11 bit chunks from entropy bytes. Alternate implementation.
60+
///
61+
/// Returns a `Vec<u16>` of 11 bit chunks, along with an `usize` specifying
62+
/// the number of bits that are left over for checksum in the last `u16` element of the `Vec`.
63+
fn chunk_to_11_bit_groups_alt_via_u128(ent: &[u8]) -> (Vec<u16>, usize) {
64+
// This function pads the last `u16` of output with zeros, leaving space for checksum.
65+
// The checksum bits can then be added to the result elsewhere. Adding checksum is not
66+
// a responsibility of this function.
67+
let (chunk_size, checksum_num_bits): (usize, usize) = match ent.len() {
68+
16 => (16, 4), // one full u128
69+
20 => (4, 5), // five u128 with 32 bits used each
70+
24 => (8, 6), // two u128 with 64 bits used each
71+
28 => (4, 7), // seven u128 with 32 bits used each
72+
32 => (16, 8), // two full u128
73+
// Caller is responsible for ensuring that array length matches one of the BIP39
74+
// valid number of entropy bytes, available above. Since the chunk function is crate internal,
75+
// we can assume that this is taken into account, and we can simply panic if it's not.
76+
// No point in returning an error as the situation would be unrecoverable anyway.
77+
_ => unreachable!(),
78+
};
79+
80+
eprintln!("u128 has size {}", size_of::<u128>());
81+
let groups_128 = ent
82+
.chunks(chunk_size)
83+
.map(|c| match ent.len() {
84+
16 | 32 => u128::from_be_bytes(c.try_into().unwrap()),
85+
24 => (u64::from_be_bytes(c.try_into().unwrap()) as u128) << 64,
86+
_ => (u32::from_be_bytes(c.try_into().unwrap()) as u128) << 96,
87+
})
88+
.collect::<Vec<_>>();
89+
90+
for group_128 in groups_128 {
91+
eprintln!("Group {group_128:#0128b}");
92+
}
93+
94+
// TODO: Continue implementation of this function.
95+
todo!();
96+
}
97+
5998
/// Extract 11 bit chunks from entropy bytes.
6099
///
61100
/// Returns a `Vec<u16>` of 11 bit chunks, along with an `usize` specifying
@@ -208,7 +247,6 @@ mod test {
208247
let _ = get_word_from_11_bits(value);
209248
}
210249

211-
#[test_case(&[0xff, 0xff], &[0b11111111111, 0b11111000000], 6; "simple non-BIP39 input")]
212250
// 128 bits of input should have 12 chunks of output, with 4 bits left in last byte for checksum, according to BIP39.
213251
#[test_case(&[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], &[0,0,0,0,0,0,0,0,0,0,0,0], 4; "with 128 bits of input of all zeros")]
214252
#[test_case(&[0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff], &[0x7ff,0x7ff,0x7ff,0x7ff,0x7ff,0x7ff,0x7ff,0x7ff,0x7ff,0x7ff,0x7ff,0b11111110000], 4; "with 128 bits of input of all ones")]

0 commit comments

Comments
 (0)