Skip to content

Commit e93950a

Browse files
committed
cshake: tweak customization
1 parent f18ed8c commit e93950a

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

cshake/src/lib.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<Rate: BlockSizes> CShake<Rate> {
6666
}
6767

6868
#[inline(always)]
69-
pub(crate) fn left_encode(val: u64, b: &mut [u8; 9]) -> &[u8] {
69+
fn left_encode(val: u64, b: &mut [u8; 9]) -> &[u8] {
7070
b[1..].copy_from_slice(&val.to_be_bytes());
7171
let i = b[1..8].iter().take_while(|&&a| a == 0).count();
7272
b[i] = (8 - i) as u8;
@@ -77,19 +77,22 @@ impl<Rate: BlockSizes> CShake<Rate> {
7777
let mut buffer: EagerBuffer<Rate> = Default::default();
7878
let state = &mut state.state;
7979
let mut b = [0u8; 9];
80+
8081
buffer.digest_blocks(left_encode(Rate::U64, &mut b), |blocks| {
8182
update_blocks(f1600, state, blocks)
8283
});
83-
buffer.digest_blocks(
84-
left_encode(8 * (function_name.len() as u64), &mut b),
85-
|blocks| update_blocks(f1600, state, blocks),
86-
);
87-
buffer.digest_blocks(function_name, |blocks| update_blocks(f1600, state, blocks));
88-
buffer.digest_blocks(
89-
left_encode(8 * (customization.len() as u64), &mut b),
90-
|blocks| update_blocks(f1600, state, blocks),
91-
);
92-
buffer.digest_blocks(customization, |blocks| update_blocks(f1600, state, blocks));
84+
85+
let mut encode_str = |str: &[u8]| {
86+
let str_bits_len = 8 * u64::try_from(str.len())
87+
.expect("in practice strings can not be longer than u64::MAX");
88+
let encoded_len = left_encode(str_bits_len, &mut b);
89+
buffer.digest_blocks(encoded_len, |blocks| update_blocks(f1600, state, blocks));
90+
buffer.digest_blocks(str, |blocks| update_blocks(f1600, state, blocks));
91+
};
92+
93+
encode_str(function_name);
94+
encode_str(customization);
95+
9396
update_blocks(f1600, state, &[buffer.pad_with_zeros()])
9497
});
9598

0 commit comments

Comments
 (0)