pub fn hsalsa20(
    key: &GenericArray<u8, U32>,
    input: &GenericArray<u8, U16>,
) -> GenericArray<u8, U32> {
    let mut state = [0u32; 16];

    state[0] = CONSTANTS[0];
    state[5] = CONSTANTS[1];
    state[10] = CONSTANTS[2];
    state[15] = CONSTANTS[3];

    for (i, chunk) in key.chunks(4).take(4).enumerate() {
        state[1 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
    }

    for (i, chunk) in key.chunks(4).skip(4).enumerate() {
        state[11 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
    }

    for (i, chunk) in input.chunks(4).enumerate() {
        state[6 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
    }

    // 20 rounds consisting of 10 column rounds and 10 diagonal rounds
    for _ in 0..10 {
        // column rounds
        quarter_round(0, 4, 8, 12, &mut state);
        quarter_round(5, 9, 13, 1, &mut state);
        quarter_round(10, 14, 2, 6, &mut state);
        quarter_round(15, 3, 7, 11, &mut state);

        // diagonal rounds
        quarter_round(0, 1, 2, 3, &mut state);
        quarter_round(5, 6, 7, 4, &mut state);
        quarter_round(10, 11, 8, 9, &mut state);
        quarter_round(15, 12, 13, 14, &mut state);
    }

    let mut output = GenericArray::default();
    let key_idx: [usize; 8] = [0, 5, 10, 15, 6, 7, 8, 9];

    for (i, chunk) in output.chunks_mut(4).enumerate() {
        chunk.copy_from_slice(&state[key_idx[i]].to_le_bytes());
    }

    output
}
/// The HSalsa20 function defined in the paper "Extending the Salsa20 nonce"
///
/// <https://cr.yp.to/snuffle/xsalsa-20110204.pdf>
///
/// HSalsa20 takes 512-bits of input:
///
/// - Constants (`u32` x 4)
/// - Key (`u32` x 8)
/// - Nonce (`u32` x 4)
///
/// It produces 256-bits of output suitable for use as a Salsa20 key
The HSalsa20 function defined in the paper "Extending the Salsa20 nonce"

HSalsa20 takes 512-bits of input.



It produces 256-bits of output suitable for use as a Salsa20 key
The HSalsa20 function defined in the paper " Extending the Salsa20 nonce " HSalsa20 takes 512 - bits of input . It produces 256 - bits of output suitable for use as a Salsa20 key
pub fn hsalsa20(
    key: &GenericArray<u8, U32>,
    input: &GenericArray<u8, U16>,
) -> GenericArray<u8, U32> {
    let mut state = [0u32; 16];
    state[0] = CONSTANTS[0];
    state[5] = CONSTANTS[1];
    state[10] = CONSTANTS[2];
    state[15] = CONSTANTS[3];
    for (i, chunk) in key.chunks(4).take(4).enumerate() {
        state[1 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
    }
    for (i, chunk) in key.chunks(4).skip(4).enumerate() {
        state[11 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
    }
    for (i, chunk) in input.chunks(4).enumerate() {
        state[6 + i] = u32::from_le_bytes(chunk.try_into().unwrap());
    }
    // 20 rounds consisting of 10 column rounds and 10 diagonal rounds
    for _ in 0..10 {
        // column rounds
        quarter_round(0, 4, 8, 12, &mut state);
        quarter_round(5, 9, 13, 1, &mut state);
        quarter_round(10, 14, 2, 6, &mut state);
        quarter_round(15, 3, 7, 11, &mut state);
        // diagonal rounds
        quarter_round(0, 1, 2, 3, &mut state);
        quarter_round(5, 6, 7, 4, &mut state);
        quarter_round(10, 11, 8, 9, &mut state);
        quarter_round(15, 12, 13, 14, &mut state);
    }
    let mut output = GenericArray::default();
    let key_idx: [usize; 8] = [0, 5, 10, 15, 6, 7, 8, 9];
    for (i, chunk) in output.chunks_mut(4).enumerate() {
        chunk.copy_from_slice(&state[key_idx[i]].to_le_bytes());
    }
    output
}
pub fn hsalsa20 ( key : & GenericArray < u8 , U32 > , input : & GenericArray < u8 , U16 > , ) -> GenericArray < u8 , U32 > { let mut state = [ 0u32 ; 16 ] ; state [ 0 ] = CONSTANTS [ 0 ] ; state [ 5 ] = CONSTANTS [ 1 ] ; state [ 10 ] = CONSTANTS [ 2 ] ; state [ 15 ] = CONSTANTS [ 3 ] ; for ( i , chunk ) in key . chunks ( 4 ) . take ( 4 ) . enumerate ( ) { state [ 1 + i ] = u32 :: from_le_bytes ( chunk . try_into ( ) . unwrap ( ) ) ; } for ( i , chunk ) in key . chunks ( 4 ) . skip ( 4 ) . enumerate ( ) { state [ 11 + i ] = u32 :: from_le_bytes ( chunk . try_into ( ) . unwrap ( ) ) ; } for ( i , chunk ) in input . chunks ( 4 ) . enumerate ( ) { state [ 6 + i ] = u32 :: from_le_bytes ( chunk . try_into ( ) . unwrap ( ) ) ; } // 20 rounds consisting of 10 column rounds and 10 diagonal rounds for _ in 0 .. 10 { // column rounds quarter_round ( 0 , 4 , 8 , 12 , & mut state ) ; quarter_round ( 5 , 9 , 13 , 1 , & mut state ) ; quarter_round ( 10 , 14 , 2 , 6 , & mut state ) ; quarter_round ( 15 , 3 , 7 , 11 , & mut state ) ; // diagonal rounds quarter_round ( 0 , 1 , 2 , 3 , & mut state ) ; quarter_round ( 5 , 6 , 7 , 4 , & mut state ) ; quarter_round ( 10 , 11 , 8 , 9 , & mut state ) ; quarter_round ( 15 , 12 , 13 , 14 , & mut state ) ; } let mut output = GenericArray :: default ( ) ; let key_idx : [ usize ; 8 ] = [ 0 , 5 , 10 , 15 , 6 , 7 , 8 , 9 ] ; for ( i , chunk ) in output . chunks_mut ( 4 ) . enumerate ( ) { chunk . copy_from_slice ( & state [ key_idx [ i ] ] . to_le_bytes ( ) ) ; } output }
The HSalsa20 function defined in the paper "Extending the Salsa20 nonce"
<https://cr.yp.to/snuffle/xsalsa-20110204.pdf>
The HSalsa20 function defined in the paper " Extending the Salsa20 nonce " <https : // cr . yp . to / snuffle / xsalsa - 20110204 . pdf >
key
&GenericArray<u8, U32>
input
&GenericArray<u8, U16>