1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#![no_std]

use core::cmp;
use gimli_permutation::{ SIZE, gimli, state_with as with };


const RATE: usize = 16;

#[derive(Clone)]
pub struct GimliHash {
    state: [u32; SIZE],
    pos: usize
}

impl Default for GimliHash {
    fn default() -> Self {
        GimliHash { state: [0; SIZE], pos: 0 }
    }
}

impl GimliHash {
    #[inline]
    pub fn update(&mut self, buf: &[u8]) {
        self.absorb(buf);
    }

    #[inline]
    pub fn finalize(self, buf: &mut [u8]) {
        self.xof().squeeze(buf);
    }

    #[inline]
    pub fn xof(mut self) -> XofReader {
        self.pad();
        XofReader { state: self.state, pos: 0 }
    }

    pub fn fill_block(&mut self) {
        self.pos = 0;
        gimli(&mut self.state);
    }

    fn absorb(&mut self, buf: &[u8]) {
        let take = cmp::min(RATE - self.pos, buf.len());
        let (prefix, buf) = buf.split_at(take);

        if !prefix.is_empty() {
            let pos = self.pos;
            with(&mut self.state, |state| {
                for (dest, src) in state.iter_mut()
                    .skip(pos)
                    .zip(prefix)
                {
                    *dest ^= *src;
                }
            });

            self.pos += prefix.len();

            if self.pos == RATE {
                gimli(&mut self.state);
                self.pos = 0;
            }
        }

        let mut iter = buf.chunks_exact(RATE);
        for chunk in &mut iter {
            with(&mut self.state, |state| {
                for (dest, src) in state.iter_mut().zip(chunk) {
                    *dest ^= *src;
                }
            });
            gimli(&mut self.state);
        }

        let chunk = iter.remainder();
        if !chunk.is_empty() {
            with(&mut self.state, |state| {
                for (dest, src) in state.iter_mut().zip(chunk) {
                    *dest ^= *src;
                }
            });
            self.pos += chunk.len();
        }
    }

    fn pad(&mut self) {
        let pos = self.pos;
        with(&mut self.state, |state| {
            state[pos] ^= 0x1f;
            state[RATE - 1] ^= 0x80;
        });
        gimli(&mut self.state);
    }
}


pub struct XofReader {
    state: [u32; SIZE],
    pos: usize
}

impl XofReader {
    pub fn squeeze(&mut self, buf: &mut [u8]) {
        let take = cmp::min(RATE - self.pos, buf.len());
        let (prefix, buf) = buf.split_at_mut(take);

        if !prefix.is_empty() {
            let pos = self.pos;
            with(&mut self.state, |state| {
                prefix.copy_from_slice(&state[pos..][..prefix.len()]);
            });

            self.pos += prefix.len();

            if self.pos == RATE {
                gimli(&mut self.state);
                self.pos = 0;
            }
        }

        let mut iter = buf.chunks_exact_mut(RATE);
        for chunk in &mut iter {
            with(&mut self.state, |state| chunk.copy_from_slice(&state[..RATE]));
            gimli(&mut self.state);
        }

        let chunk = iter.into_remainder();
        if !chunk.is_empty() {
            with(&mut self.state, |state| {
                chunk.copy_from_slice(&state[..chunk.len()]);
            });
            self.pos += take;
        }
    }
}