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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! Encoding and decoding of the (23, 12, 7) standard Golay code.

use binfield_matrix::{matrix_mul, matrix_mul_systematic};

/// Encode the given 12 data bits into a 23-bit codeword.
pub fn encode(data: u16) -> u32 {
    assert_eq!(data >> 12, 0);

    // Compute wG.
    matrix_mul_systematic(data, CORE_XPOSE)
}

/// Try to decode the given 23-bit word to the nearest codeword, correcting up to 3
/// errors and detecting 4 errors.
///
/// If decoding was successful, return `Some((data, err))`, where `data` is the 12
/// data bits and `err` is the number of corrected bits. Otherwise, return `None` to
/// indicate an unrecoverable error.
pub fn decode(word: u32) -> Option<(u16, usize)> {
    assert_eq!(word >> 23, 0);

    // Strip off parity bits.
    let data = (word >> 11) as u16;

    // Check for 1 to 3 errors isolated in the parity bits.
    let s: u16 = matrix_mul(word, PAR);
    let n = s.count_ones() as usize;

    if n <= 3 {
        return Some((data, n));
    }

    // Check for cases with 1 error in the data bits and 0 to 2 errors in the parity bits.
    for (i, &syn) in SYN.iter().enumerate() {
        let n = (s ^ syn).count_ones() as usize;

        if n <= 2 {
            return Some((data ^ 1 << i, n + 1));
        }
    }

    // Check for 2 or 3 errors isolated to the data bits (except data MSB).
    let s: u16 = matrix_mul(rotate_11(word), PAR);
    let n = s.count_ones() as usize;

    if n <= 3 {
        return Some((data ^ s, n));
    }

    // Check for cases with 2 or 3 errors in the data bits (one being the data MSB) and
    // possibly 1 error in the parity bits or 2 errors in the data bits (exluding data
    // MSB) and 1 error in the parity bits.
    for (i, &syn) in SYN.iter().enumerate() {
        let r = s ^ syn;
        let n = r.count_ones() as usize;

        if n <= 2 {
            // The 0th syndrome corresponds to the data MSB (which now lies in the upper
            // 12 bits of the rotated word), so it must be flipped, but the following
            // syndromes correspond to parity bits, which don't need to be flipped.
            return if i == 0 {
                Some((data ^ r ^ 1 << 11, n + 1))
            } else {
                Some((data ^ r, 3))
            };
        }
    }

    None
}

/// Circularly shift the given 23-bit word right by 11 bits.
fn rotate_11(word: u32) -> u32 {
    let parity = word & 0x7FF;
    word >> 11 | parity << 12
}

/// Transpose of generator parity submatrix with extended code's LSB parity bit removed,
/// also known as **A**<sup>T</sup>.
const CORE_XPOSE: &[u16] = &[
    0b101001001111,
    0b111101101000,
    0b011110110100,
    0b001111011010,
    0b000111101101,
    0b101010111001,
    0b111100010011,
    0b110111000110,
    0b011011100011,
    0b100100111110,
    0b010010011111,
];

/// Parity-check matrix, also known as **H** = [ **A**<sup>T</sup> | I ].
///
/// This is derived from the standard-form generator matrix in the standard way.
///
/// Note that the top 12 rows of **H**<sup>T</sup> equal **A**.
const PAR: &[u32] = &[
    0b10100100111110000000000,
    0b11110110100001000000000,
    0b01111011010000100000000,
    0b00111101101000010000000,
    0b00011110110100001000000,
    0b10101011100100000100000,
    0b11110001001100000010000,
    0b11011100011000000001000,
    0b01101110001100000000100,
    0b10010011111000000000010,
    0b01001001111100000000001,
];

/// Syndromes for each single-bit error in the upper 12 bits.
///
/// The first syndrome corresponds to an LSB (bit 12) error, and the last corresponds to
/// an MSB (bit 23) error.
const SYN: &[u16] = &[
    0b10001110101,
    0b10010011111,
    0b10101001011,
    0b11011100011,
    0b00110110011,
    0b01101100110,
    0b11011001100,
    0b00111101101,
    0b01111011010,
    0b11110110100,
    0b01100011101,
    0b11000111010,
];

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

    #[test]
    fn test_rotate11() {
        assert_eq!(rotate_11(0b111111111111_00000000000), 0b000000000001_11111111111);
        assert_eq!(rotate_11(0b000000000000_11111111111), 0b111111111110_00000000000);
        assert_eq!(rotate_11(0b100000000000_00000000000), 0b000000000001_00000000000);
    }

    #[test]
    fn test_encode() {
        assert_eq!(encode(0), 0);
        assert_eq!(encode(0b111111111111), 0b111111111111_11111111111);
        assert_eq!(encode(0b111111000000), 0b111111000000_11001101000);
        assert_eq!(encode(0b000000111111), 0b000000111111_00110010111);
        assert_eq!(encode(0b100000000001), 0b100000000001_01001001111);
    }

    #[test]
    fn test_decode() {
        let w = 0b101010101010;
        let e = encode(w);
        assert_eq!(e, 0b1010101010_1000101111001);

        assert_eq!(decode(e^0b01000000000000000000010), Some((w, 2)));
        assert_eq!(decode(e^0b00100000000000000000100), Some((w, 2)));
        assert_eq!(decode(e^0b00010000000000000001000), Some((w, 2)));
        assert_eq!(decode(e^0b00001000000000000010000), Some((w, 2)));
        assert_eq!(decode(e^0b00000100000000000100000), Some((w, 2)));
        assert_eq!(decode(e^0b00000010000000001000000), Some((w, 2)));
        assert_eq!(decode(e^0b00000001000000010000000), Some((w, 2)));
        assert_eq!(decode(e^0b00000000100000100000000), Some((w, 2)));
        assert_eq!(decode(e^0b00000000010001000000000), Some((w, 2)));
        assert_eq!(decode(e^0b00000000001010000000000), Some((w, 2)));
        assert_eq!(decode(e^0b00000000010000000000001), Some((w, 2)));
        assert_eq!(decode(e^0b00000000100000000000010), Some((w, 2)));
        assert_eq!(decode(e^0b00000001000000000000100), Some((w, 2)));
        assert_eq!(decode(e^0b00000010000000000001000), Some((w, 2)));
        assert_eq!(decode(e^0b00000100000000000010000), Some((w, 2)));
        assert_eq!(decode(e^0b00001000000000000100000), Some((w, 2)));
        assert_eq!(decode(e^0b00010000000000001000000), Some((w, 2)));
        assert_eq!(decode(e^0b00100000000000010000000), Some((w, 2)));
        assert_eq!(decode(e^0b01000000000000100000000), Some((w, 2)));
        assert_eq!(decode(e^0b10000000000001000000000), Some((w, 2)));
        assert_eq!(decode(e^0b10000000000010000000000), Some((w, 2)));
        assert_eq!(decode(e^0b11100000000000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b01110000000000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00111000000000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00011100000000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00001110000000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000111000000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000011100000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000001110000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000111000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000011100000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000001110000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000111000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000011100000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000001110000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000111000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000011100000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000001110000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000000111000), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000000011100), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000000001110), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000000000111), Some((w, 3)));
        assert_eq!(decode(e^0b00000000000000000000000), Some((w, 0)));
        assert_eq!(decode(e^0b00000000000000000000001), Some((w, 1)));
        assert_eq!(decode(e^0b00000000000000000000011), Some((w, 2)));
        assert_eq!(decode(e^0b00000000000000000000111), Some((w, 3)));
        assert_eq!(decode(e^0b00000001000000000000000), Some((w, 1)));
        assert_eq!(decode(e^0b00000011000000000000000), Some((w, 2)));
        assert_eq!(decode(e^0b00000111000000000000000), Some((w, 3)));
        assert_eq!(decode(e^0b00000100000000000000001), Some((w, 2)));
        assert_eq!(decode(e^0b00000110000000000000001), Some((w, 3)));
        assert_eq!(decode(e^0b00000100000000000000011), Some((w, 3)));

        // Exhaustively test that all codewords are decoded correctly.
        for w in 0..1<<12 {
            assert_eq!(decode(encode(w)), Some((w, 0)));
        }

        let w = encode(0b110111101110);

        // Exhaustively test that all 0 through 3-bit errors are detected.
        for ((i, j), k) in (0..23).zip(0..23).zip(0..23) {
            let e: u32 = 1 << i | 1 << j | 1 << k;
            let n = e.count_ones() as usize;

            assert_eq!(decode(w ^ e), Some((0b110111101110, n)));
        }

        // Exhaustively test that all 4-bit errors are detected.
        for (((h, i), j), k) in (0..23).zip(0..23).zip(0..23).zip(0..23) {
            let e: u32 = 1 << h | 1 << i | 1 << j | 1 << k;
            let n = e.count_ones() as usize;

            if n >= 4 {
                assert_eq!(decode(w ^ e), None);
            }
        }
    }
}