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
//! Useful utility functions.

/// Pack a bit slice into bytes.
pub fn pack_bits(bits: &[bool]) -> Vec<u8> {
    let nbytes = (bits.len() as f64 / 8.0).ceil() as usize;
    let mut bytes = vec![0; nbytes];
    for i in 0..nbytes {
        for j in 0..8 {
            if 8 * i + j >= bits.len() {
                break;
            }
            bytes[i] |= (bits[8 * i + j] as u8) << j;
        }
    }
    bytes
}

/// Unpack a bit vector from a slice of bytes.
pub fn unpack_bits(bytes: &[u8], size: usize) -> Vec<bool> {
    let mut bits = Vec::with_capacity(size);
    for (i, byte) in bytes.iter().enumerate() {
        for j in 0..8 {
            if 8 * i + j >= size {
                break;
            }
            bits.push(((byte >> j) & 1) != 0);
        }
    }
    bits
}

/// XOR two byte arrays, outputting the result.
pub fn xor(a: &[u8], b: &[u8]) -> Vec<u8> {
    a.iter().zip(b.iter()).map(|(a, b)| a ^ b).collect()
}

/// XOR two byte arrays up to `n` bytes, outputting the result.
pub fn xor_n(a: &[u8], b: &[u8], n: usize) -> Vec<u8> {
    a[0..n]
        .iter()
        .zip(b[0..n].iter())
        .map(|(a, b)| a ^ b)
        .collect()
}

/// XOR two byte arrays in place.
pub fn xor_inplace(a: &mut [u8], b: &[u8]) {
    for (a, b) in a.iter_mut().zip(b.iter()) {
        *a ^= *b;
    }
}

/// XOR two byte arrays up to `n` bytes in place.
pub fn xor_inplace_n(a: &mut [u8], b: &[u8], n: usize) {
    for (a, b) in a[0..n].iter_mut().zip(b.iter()) {
        *a ^= *b;
    }
}

/// AND two byte arrays, outputting the result.
pub fn and(a: &[u8], b: &[u8]) -> Vec<u8> {
    a.iter().zip(b.iter()).map(|(a, b)| a & b).collect()
}

/// AND two byte arrays in place.
pub fn and_inplace(a: &mut [u8], b: &[u8]) {
    for (a, b) in a.iter_mut().zip(b.iter()) {
        *a &= *b;
    }
}

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

    #[test]
    fn test_xor() {
        let v = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        let v_ = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        let v__ = xor(&v, &v_);
        let v___ = xor(&v__, &v_);
        assert_eq!(v___, v);
    }

    #[test]
    fn test_xor_inplace() {
        let mut v = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        let goal = v.clone();
        let v_ = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        xor_inplace(&mut v, &v_);
        xor_inplace(&mut v, &v_);
        assert_eq!(v, goal);
    }

    #[test]
    fn test_and() {
        let v = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        let v_ = (0..128).map(|_| 0xFF).collect::<Vec<u8>>();
        let v__ = and(&v, &v_);
        assert_eq!(v__, v);
    }
}

#[cfg(all(feature = "nightly", test))]
mod benchmarks {
    extern crate test;
    use super::*;
    use test::Bencher;

    #[bench]
    fn bench_xor_inplace(b: &mut Bencher) {
        let mut x = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        let y = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        b.iter(|| xor_inplace(&mut x, &y));
    }

    #[bench]
    fn bench_and_inplace(b: &mut Bencher) {
        let mut x = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        let y = (0..128).map(|_| rand::random::<u8>()).collect::<Vec<u8>>();
        b.iter(|| and_inplace(&mut x, &y));
    }
}