furtif_core/traits/
referee.rs

1// This program is free software: you can redistribute it and/or modify
2// it under the terms of the Lesser GNU General Public License as published
3// by the Free Software Foundation, either version 3 of the License, or
4// (at your option) any later version.
5
6// This program is distributed in the hope that it will be useful,
7// but WITHOUT ANY WARRANTY; without even the implied warranty of
8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9// Lesser GNU General Public License for more details.
10
11// You should have received a copy of the Lesser GNU General Public License
12// along with this program.  If not, see <https://www.gnu.org/licenses/>.
13
14// Copyright 2024 Frederic Dambreville, Jean Dezert Developers.
15
16
17use std::hash::Hash;
18
19use crate::{ structs::{Assignment, SafeArray}, traits::Lattice, };
20
21/// Trait defining Referee functions
22pub trait Referee {
23    /// Test if fusion is allowed
24    /// * does not concern lattice coherence, which is tested by `from_conditions`
25    /// * typically concerns the number of entries or the algebraic properties of lattice
26    /// * `lattice: &L` : reference lattice
27    /// * `bbas: &[&Assignment<L::Item>]` : sequence of bbas to be fused
28    /// * Output: a boolean
29    fn is_allowed<L>(&self, lattice: &L, bbas: &[&Assignment<L::Item>]) -> bool where L: Lattice, L::Item: Eq + Ord + Hash,;
30
31    /// unsafe conditional referee decision
32    /// * `lattice: &L` : reference lattice
33    /// * `bbas: &[&Assignment<L::Item>]` : sequence of bbas to be fused 
34    /// * `conditions: SafeArray<L::Item>` : conditionning safe elements array
35    /// * Output: fused assigment or error
36    unsafe fn unsafe_from_conditions<L>(&self, lattice: &L, bbas: &[&Assignment<L::Item>], conditions: SafeArray<L::Item>)
37            -> Result<Assignment<L::Item>,String> where L: Lattice, L::Item: Eq + Ord + Hash,;
38
39    /// Conditional referee decision
40    /// * `lattice: &L` : reference lattice
41    /// * `bbas: &[&Assignment<L::Item>]` : sequence of bbas to be fused 
42    /// * `conditions: SafeArray<L::Item>` : conditionning safe elements array 
43    /// * Output: fused assigment or error 
44    fn from_conditions<L>(&self, lattice: &L, bbas: &[&Assignment<L::Item>], conditions: SafeArray<L::Item>)
45            -> Result<Assignment<L::Item>,String> where L: Lattice, L::Item: Eq + Ord + Hash, {
46        if bbas.len() != conditions.len() { return Err("Mismatching entries lengths".to_string()); }
47        let lattice_hash = lattice.lattice_hash();
48        if lattice_hash != conditions.lattice_hash { return Err("Conditions not within lattice".to_string()); }
49        for (u,bba) in bbas.iter().enumerate() {
50            if lattice_hash != bba.lattice_hash { return Err(format!("Bba with index {u} is not defined over lattice")); }
51        }
52        if self.is_allowed(lattice, bbas) {
53            unsafe { self.unsafe_from_conditions(lattice, bbas, conditions) }
54        } else { Err("Entries not allowed".to_string()) } 
55    }
56}