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
//! Padding support for sponge hash
//!

pub(crate) fn pad<T>(
    messages: &[T],
    width: usize,
    pad_value: T,
    eom_value: T,
) -> Vec<T>
where
    T: Clone,
{
    let length = messages.len() + 1;
    let arity = width - 1;
    let offset = ((length % arity) != 0) as usize;
    let size = (length / arity + offset) * width;

    let zero = pad_value;
    let one = eom_value;
    let mut words = vec![zero; size];
    let mut messages = messages.iter();

    for chunk in words.chunks_mut(width) {
        for elem in chunk.iter_mut().skip(1) {
            if let Some(message) = messages.next() {
                *elem = message.clone();
            } else {
                *elem = one;
                return words;
            }
        }
    }
    words
}

#[cfg(test)]
mod tests {
    use super::*;
    use dusk_plonk::prelude::*;

    #[test]
    fn test_scalar_padding_width_3() {
        let padder = BlsScalar::zero();
        let eom = BlsScalar::one();
        let two = BlsScalar::from(2u64);
        let three = BlsScalar::from(3u64);
        let four = BlsScalar::from(4u64);

        assert_eq!(&pad(&[two], 3, padder, eom), &[padder, two, eom]);
        assert_eq!(
            &pad(&[two, three], 3, padder, eom),
            &[padder, two, three, padder, eom, padder]
        );
        assert_eq!(
            &pad(&[two, three, four], 3, padder, eom),
            &[padder, two, three, padder, four, eom]
        );
    }

    #[test]
    fn test_scalar_padding_width_4() {
        let padder = BlsScalar::zero();
        let eom = BlsScalar::one();
        let two = BlsScalar::from(2u64);
        let three = BlsScalar::from(3u64);
        let four = BlsScalar::from(4u64);

        assert_eq!(&pad(&[two], 4, padder, eom), &[padder, two, eom, padder]);
        assert_eq!(
            &pad(&[two, three], 4, padder, eom),
            &[padder, two, three, eom]
        );
        assert_eq!(
            &pad(&[two, three, four], 4, padder, eom),
            &[padder, two, three, four, padder, eom, padder, padder]
        );
    }

    #[test]
    fn test_variable_padding() {
        let mut composer = StandardComposer::new();
        let padder = composer.add_input(BlsScalar::zero());
        let eom = composer.add_input(BlsScalar::one());
        let two = composer.add_input(BlsScalar::from(2u64));
        let three = composer.add_input(BlsScalar::from(3u64));
        let four = composer.add_input(BlsScalar::from(4u64));

        assert_eq!(&pad(&[two], 3, padder, eom), &[padder, two, eom]);
        assert_eq!(
            &pad(&[two, three], 3, padder, eom),
            &[padder, two, three, padder, eom, padder]
        );
        assert_eq!(
            &pad(&[two, three, four], 3, padder, eom),
            &[padder, two, three, padder, four, eom]
        );
    }
}