fountain_engine 2.0.1

Core algorithms for fountain code encoding and decoding
Documentation
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright (c) 2025 Shenghao Yang. All rights reserved.
// Licensed under AGPL-3.0 or commercial license. See LICENSE for details.

//! System-based decoder that works on `MasterSystem` and `InactiveSystem`.
//!
//! Uses the sparse master-system representation (doc sec:representation_of_the_system)
//! and the inactive system F B_i = X_u' (doc sec:solv_inactive).

use super::inactivation::InactiveSystem;
use super::sparse_equation::SparseSystem;
use crate::data_manager::DataManager;
use crate::traits::{CodeScheme, HDPC};
use crate::types::{CodeParams, DecodeStatus, DegreeSetFn, SolverType, SubstitutionMethod};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SolvingPhase {
    BP,
    GE,
    BS,
}

pub struct SystemSolver {
    params: CodeParams,
    hdpc: Option<Box<dyn HDPC>>,
    sparse_system: SparseSystem,
    gen_degree_set: DegreeSetFn,
    inactive_system: InactiveSystem,
    pub phase: SolvingPhase,
    pub status: DecodeStatus,
    bs_method: SubstitutionMethod,
    /// True after GE `add_row_from_master`; HDPC path rebuilds only when set.
    inactive_dirty: bool,
}

impl SystemSolver {
    pub fn new<T: CodeScheme>(
        custom: &T,
        manager: &mut DataManager,
        solver_type: SolverType,
    ) -> Self {
        let params = custom.get_params();
        let gen_degree_set = custom.create_degree_set_fn();
        let (hdpc, ldpc_opt) = custom.create_precode();
        let decoding_config = custom.decoding_config();
        let max_inactive_num = decoding_config.max_inactive_num;
        let inac_strategy = decoding_config.inac_strategy;
        let bs_method = decoding_config.subs_method;

        // degree_set generator is not stored in `MasterSystem` for now; we only keep it here.
        let mut master = SparseSystem::new(&params, max_inactive_num, inac_strategy);

        if let Some(ldpc) = ldpc_opt {
            master.add_ldpc_constraints(manager, ldpc.as_ref());
        }

        if let Some(hdpc_box) = &hdpc {
            // See `precode_encode`: `GF2_FIELD_POLY` for binary HDPC, else GF(256).
            manager.config_finite_field(hdpc_box.gf_poly());
        }

        let inactive_system = InactiveSystem::new(&params);

        let num_k = params.k;
        let num_a = params.a;

        let mut solver = Self {
            params,
            hdpc,
            sparse_system: master,
            gen_degree_set,
            inactive_system,
            phase: SolvingPhase::BP,
            status: DecodeStatus::NotDecoded,
            bs_method,
            inactive_dirty: false,
        };

        match solver_type {
            SolverType::OrdEnc => {
                unreachable!()
            }
            SolverType::OrdDec => {
                //add padding vectors as decoded variables
                for var_id in num_a - manager.num_padding()..num_a {
                    let new_data_id = manager.coded_data_id(var_id);
                    manager.ensure_zero_one(new_data_id);
                    solver
                        .sparse_system
                        .add_lt_coded_vector(manager, new_data_id, &[var_id]);
                }
                let _ = solver.sparse_system.run_bp_inactivation(manager);
            }
            SolverType::SysEnc => {
                for coded_id in 0..manager.num_source() {
                    let new_data_id = manager.coded_data_id(coded_id);
                    manager.copy_to(coded_id, new_data_id);
                    solver.add_coded_vector(manager, coded_id, new_data_id);
                }
                for coded_id in manager.num_source()..num_k {
                    let new_data_id = manager.coded_data_id(coded_id);
                    manager.ensure_zero_one(new_data_id);
                    solver.add_coded_vector(manager, coded_id, new_data_id);
                }
            }
            SolverType::SysDec => {
                //add padding vectors as all-zero received coded vectors
                for coded_id in manager.num_source()..num_k {
                    let new_data_id = manager.coded_data_id(coded_id);
                    manager.ensure_zero_one(new_data_id);
                    solver.add_coded_vector(manager, coded_id, new_data_id);
                }
            }
        }

        solver
    }

    /// Solve the inactive system.
    fn solve_inactive(&mut self, manager: &mut DataManager) -> DecodeStatus {
        //let remove_redundant = !matches!(self.phase, SolvingPhase::GE);
        let remove_redundant = false;
        let mut r = self
            .inactive_system
            .lu_decomposition(manager, remove_redundant);
        let num_inactive = self.sparse_system.num_inactive();
        if r < num_inactive && self.hdpc.is_some() && num_inactive - r <= self.params.h {
            if self.inactive_dirty {
                self.rebuild_inactive_from_unused();
                r = self
                    .inactive_system
                    .lu_decomposition(manager, remove_redundant);
            }
            // G4.2: skip HDPC append when packed GF(2) rows already have full rank.
            if r < num_inactive {
                if manager.gf256().is_none() {
                    let tilde_g = self.sparse_system.tilde_g_packed();
                    self.inactive_system.append_hdpc_constraints_packed(
                        self.hdpc.as_deref().unwrap(),
                        &tilde_g,
                        manager,
                    );
                } else {
                    let tilde_g_rows = self.sparse_system.tilde_g_rows();
                    self.inactive_system.append_hdpc_constraints(
                        self.hdpc.as_deref().unwrap(),
                        &tilde_g_rows,
                        manager,
                    );
                }
                r = self
                    .inactive_system
                    .lu_decomposition(manager, remove_redundant);
            }
            self.hdpc = None;
        }
        if r < num_inactive {
            return DecodeStatus::NotDecoded;
        }
        if !self.inactive_system.has_full_rank_factorization() {
            self.rebuild_inactive_from_unused();
            r = self
                .inactive_system
                .lu_decomposition(manager, remove_redundant);
            if r < num_inactive {
                return DecodeStatus::NotDecoded;
            }
        }
        let data_ids_sol_inactive = self.inactive_system.lu_solve(manager);

        // enter BS phase
        self.phase = SolvingPhase::BS;

        match self.bs_method {
            SubstitutionMethod::Direct => {
                self.direct_back_substitution(manager, data_ids_sol_inactive);
            }
            SubstitutionMethod::Original => {
                self.original_back_substitution(manager, data_ids_sol_inactive);
            }
        }

        DecodeStatus::Decoded
    }

    fn direct_back_substitution(
        &mut self,
        manager: &mut DataManager,
        data_ids_sol_inactive: Vec<usize>,
    ) {
        let num_inactive = self.sparse_system.num_inactive();
        let coeff_matrix = self.sparse_system.inactive_coeff_matrix();
        let mut inactive_data_ids = Vec::with_capacity(8);
        for (var_id, equ_id) in self.sparse_system.iter_decoded_var() {
            let target = manager.data_id_of_variable_vector(var_id);
            let mut iter = coeff_matrix.iter_set_bits(equ_id, num_inactive);
            match iter.next() {
                None => {}
                Some(seq0) => {
                    let id0 = data_ids_sol_inactive[seq0];
                    match iter.next() {
                        None => manager.add_one_to_vector(id0, target),
                        Some(seq1) => {
                            let id1 = data_ids_sol_inactive[seq1];
                            match iter.next() {
                                None => manager.add_two_to_vector(id0, id1, target),
                                Some(seq2) => {
                                    let id2 = data_ids_sol_inactive[seq2];
                                    match iter.next() {
                                        None => manager.add_three_to_vector(id0, id1, id2, target),
                                        Some(seq3) => {
                                            inactive_data_ids.clear();
                                            inactive_data_ids.push(id0);
                                            inactive_data_ids.push(id1);
                                            inactive_data_ids.push(id2);
                                            inactive_data_ids.push(data_ids_sol_inactive[seq3]);
                                            for seq in iter {
                                                inactive_data_ids.push(data_ids_sol_inactive[seq]);
                                            }
                                            manager.add_to_vector(&inactive_data_ids, target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        for (var_id, pos) in self.sparse_system.iter_inactive_var() {
            //dbg!("inactive variable", var_id, pos);
            //dbg!(manager.get_data_vector(data_ids_sol_inactive[pos]));
            manager.move_to(
                data_ids_sol_inactive[pos],
                manager.data_id_of_variable_vector(var_id),
            );
        }
    }

    fn original_back_substitution(
        &mut self,
        manager: &mut DataManager,
        data_ids_sol_inactive: Vec<usize>,
    ) {
        self.sparse_system.reverse_bp(manager);
        self.direct_back_substitution(manager, data_ids_sol_inactive);
        self.sparse_system.forward_bp(manager);
        self.sparse_system.clear_bs_peers();
    }

    /// Rebuild the inactive system from all unused sparse equations (fresh matrix, `r = 0`).
    fn rebuild_inactive_from_unused(&mut self) {
        self.inactive_system.init(self.sparse_system.num_inactive());
        let mut add_row = |master: &crate::core::binary_matrix::BinaryMatrix, equ_id, data_id| {
            self.inactive_system
                .add_row_from_master(master, equ_id, data_id);
        };
        self.sparse_system
            .inactive_system_from_unused_equations_packed(&mut add_row);
        self.inactive_dirty = false;
    }

    /// Called when BP phase on `MasterSystem` is complete and there are inactive variables.
    fn phase_change(&mut self, _manager: &DataManager) {
        self.rebuild_inactive_from_unused();
        self.sparse_system.build_bs_peers();
        self.phase = SolvingPhase::GE;
    }

    /// Add one coded vector and advance the BP/GE phases.
    pub fn add_coded_vector(&mut self, manager: &mut DataManager, coded_id: usize, data_id: usize) {
        let degree_set = (self.gen_degree_set)(coded_id);

        let equ_id = self
            .sparse_system
            .add_lt_coded_vector(manager, data_id, &degree_set);

        self.status = match self.phase {
            SolvingPhase::BP => {
                let num_decoded = self.sparse_system.run_bp_inactivation(manager);
                if num_decoded < self.params.num_total() {
                    if self.sparse_system.is_bp_complete() {
                        self.phase_change(manager);
                        self.solve_inactive(manager)
                    } else {
                        DecodeStatus::NotDecoded
                    }
                } else {
                    DecodeStatus::Decoded
                }
            }
            SolvingPhase::GE => {
                self.inactive_system.add_row_from_master(
                    self.sparse_system.inactive_coeff_matrix(),
                    equ_id,
                    data_id,
                );
                self.inactive_dirty = true;
                self.solve_inactive(manager)
            }
            SolvingPhase::BS => DecodeStatus::Decoded,
        };
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data_manager::DataManager;
    use crate::traits::{CodeScheme, PrecodePair};
    use crate::types::{CodeParams, CodeType, DecodingConfig, DegreeSetFn, Operation, SolverType};

    #[derive(Clone)]
    struct DummySystematic {
        k: usize,
    }

    impl CodeScheme for DummySystematic {
        fn get_params(&self) -> CodeParams {
            CodeParams::new(self.k, self.k, 0, 0)
        }

        fn code_type(&self) -> CodeType {
            CodeType::Systematic
        }

        fn create_degree_set_fn(&self) -> DegreeSetFn {
            Box::new(|_| vec![0])
        }

        fn create_precode(&self) -> PrecodePair {
            (None, None)
        }

        fn decoding_config(&self) -> DecodingConfig {
            DecodingConfig::default()
        }
    }

    fn ensure_zero_ids(mgr: &DataManager) -> Vec<usize> {
        let mut ids = Vec::new();
        for op in mgr.get_operations() {
            match op {
                Operation::EnsureZero { list_id } => ids.extend(list_id.iter().copied()),
                Operation::EnsureZeroOne { id } => ids.push(*id),
                _ => {}
            }
        }
        ids
    }

    fn coded_vector_data_ids(mgr: &DataManager, coded_ids: &[usize]) -> Vec<usize> {
        mgr.get_operations()
            .iter()
            .filter_map(|op| match op {
                Operation::InfoCodedVector { coded_id, data_id }
                    if coded_ids.contains(coded_id) =>
                {
                    Some(*data_id)
                }
                _ => None,
            })
            .collect()
    }

    #[test]
    fn sysenc_zero_fills_padding_coded_slots() {
        let scheme = DummySystematic { k: 12 };
        let mut mgr = DataManager::new();
        mgr.config_from(scheme.get_params(), SolverType::SysEnc);
        mgr.set_num_source(10);
        let _ = SystemSolver::new(&scheme, &mut mgr, SolverType::SysEnc);
        let zeroed = ensure_zero_ids(&mgr);
        for data_id in coded_vector_data_ids(&mgr, &[10, 11]) {
            assert!(
                zeroed.contains(&data_id),
                "padding coded data id {data_id} should be zeroed"
            );
        }
        assert_eq!(
            mgr.get_operations()
                .iter()
                .filter(|op| matches!(op, Operation::CopyTo { .. }))
                .count(),
            10
        );
    }

    #[test]
    fn sysenc_without_padding_copies_all_message_vectors() {
        let scheme = DummySystematic { k: 12 };
        let mut mgr = DataManager::new();
        mgr.config_from(scheme.get_params(), SolverType::SysEnc);
        let _ = SystemSolver::new(&scheme, &mut mgr, SolverType::SysEnc);
        assert_eq!(
            mgr.get_operations()
                .iter()
                .filter(|op| matches!(op, Operation::CopyTo { .. }))
                .count(),
            12
        );
        let zeroed = ensure_zero_ids(&mgr);
        for data_id in coded_vector_data_ids(&mgr, &[10, 11]) {
            assert!(
                !zeroed.contains(&data_id),
                "payload coded data id {data_id} should come from copy_to, not ensure_zero"
            );
        }
    }
}