in crypto/src/hash/rescue/rp62_248/mod.rs [95:151]
fn hash(bytes: &[u8]) -> Self::Digest {
// compute the number of elements required to represent the string; we will be processing
// the string in 7-byte chunks, thus the number of elements will be equal to the number
// of such chunks (including a potential partial chunk at the end).
let num_elements = if bytes.len() % 7 == 0 {
bytes.len() / 7
} else {
bytes.len() / 7 + 1
};
// initialize state to all zeros, except for the last element of the capacity part, which
// is set to the number of elements to be hashed. this is done so that adding zero elements
// at the end of the list always results in a different hash.
let mut state = [BaseElement::ZERO; STATE_WIDTH];
state[STATE_WIDTH - 1] = BaseElement::new(num_elements as u64);
// break the string into 7-byte chunks, convert each chunk into a field element, and
// absorb the element into the rate portion of the state. we use 7-byte chunks because
// every 7-byte chunk is guaranteed to map to some field element.
let mut i = 0;
let mut buf = [0_u8; 8];
for chunk in bytes.chunks(7) {
if i < num_elements - 1 {
buf[..7].copy_from_slice(chunk);
} else {
// if we are dealing with the last chunk, it may be smaller than 7 bytes long, so
// we need to handle it slightly differently. we also append a byte with value 1
// to the end of the string; this pads the string in such a way that adding
// trailing zeros results in different hash
let chunk_len = chunk.len();
buf = [0_u8; 8];
buf[..chunk_len].copy_from_slice(chunk);
buf[chunk_len] = 1;
}
// convert the bytes into a field element and absorb it into the rate portion of the
// state; if the rate is filled up, apply the Rescue permutation and start absorbing
// again from zero index.
state[i] += BaseElement::new(u64::from_le_bytes(buf));
i += 1;
if i % RATE_WIDTH == 0 {
apply_permutation(&mut state);
i = 0;
}
}
// if we absorbed some elements but didn't apply a permutation to them (would happen when
// the number of elements is not a multiple of RATE_WIDTH), apply the Rescue permutation.
// we don't need to apply any extra padding because we injected total number of elements
// in the input list into the capacity portion of the state during initialization.
if i > 0 {
apply_permutation(&mut state);
}
// return the first 4 elements of the state as hash result
ElementDigest::new(state[..DIGEST_SIZE].try_into().unwrap())
}