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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::{
    collections::HashMap,
    convert::TryFrom,
    fmt::Display,
    ops::{BitAnd, BitOr},
};

use regex::Regex;

use crate::{
    error::{FormatErr, ParsingError},
    gpsw::AsBytes,
};

#[derive(Clone, PartialEq, Debug)]
pub struct MonotoneSpanProgram<I> {
    pub(crate) nb_row: usize,
    pub(crate) nb_col: usize,
    pub(crate) matrix: Vec<Vec<I>>,
    pub(crate) attr_to_row: HashMap<u32, usize>,
    pub(crate) row_to_attr: Vec<u32>,
}

// Convert an string-array with only integer values to a real array
pub fn attributes_parse(range: &str) -> Result<Vec<u32>, FormatErr> {
    // remove all whitespaces
    let mut clean_str: String = range.split_whitespace().collect();
    // check for outers bracket
    if let Some(c) = clean_str.pop() {
        if c != ']' {
            return Err(
                ParsingError::UnexpectedCharacter("last character must be `]`".to_string()).into(),
            )
        }
    } else {
        return Err(ParsingError::EmptyString.into())
    }

    if clean_str.remove(0) != '[' {
        return Err(
            ParsingError::UnexpectedCharacter("first character must be `[`".to_string()).into(),
        )
    }

    let vec = clean_str
        .split(',')
        .map(|item| {
            let mut interval = item.split('-');
            let start = if let Some(b) = interval.next() {
                b.parse::<u32>()?
            } else {
                return Err(FormatErr::from(ParsingError::RangeError))
            };
            let end = if let Some(b) = interval.next() {
                b.parse::<u32>()?
            } else {
                start
            };
            Ok((start..=end).collect::<Vec<u32>>())
        })
        .collect::<Result<Vec<Vec<u32>>, _>>()?;
    // `flat_map` does not works with `Result` thus we must `flatten` after
    // `collect` above
    let vec = vec.into_iter().flatten().collect();

    Ok(vec)
}

impl<I: AsBytes> AsBytes for MonotoneSpanProgram<I> {
    fn as_bytes(&self) -> Result<Vec<u8>, FormatErr> {
        let mut res = Vec::with_capacity(
            8 + (self.nb_row * self.nb_col * self.matrix[0][0].len_bytes()) + (self.nb_row * 4),
        );
        res.append(&mut u32::try_from(self.nb_row)?.to_be_bytes().to_vec());
        res.append(&mut u32::try_from(self.nb_col)?.to_be_bytes().to_vec());
        for u in &self.row_to_attr {
            res.append(&mut (*u).to_be_bytes().to_vec())
        }
        for r in &self.matrix {
            for c in r {
                res.append(&mut c.as_bytes()?);
            }
        }

        Ok(res)
    }

    fn from_bytes(bytes: &[u8]) -> Result<Self, FormatErr> {
        if bytes.len() < 8 {
            return Err(FormatErr::InvalidSize(
                "minimum len of 8 bytes is required for MSP".to_string(),
            ))
        }
        let mut nb_row = [0_u8; 4];
        nb_row.copy_from_slice(&bytes[0..4]);
        let nb_row = u32::from_be_bytes(nb_row) as usize;
        let mut nb_col = [0_u8; 4];
        nb_col.copy_from_slice(&bytes[4..8]);
        let nb_col = u32::from_be_bytes(nb_col) as usize;

        if bytes.len() < 8 + 4 * nb_row {
            return Err(FormatErr::InvalidSize(
                "invalid MSP size read from bytes".to_string(),
            ))
        }
        let mut row_to_attr = Vec::with_capacity(nb_row);
        let mut attr_to_row = HashMap::with_capacity(nb_row);
        for i in 0..nb_row {
            let mut u = [0_u8; 4];
            u.copy_from_slice(&bytes[8 + i * 4..12 + i * 4]);
            let u = u32::from_be_bytes(u);
            row_to_attr.push(u);
            if attr_to_row.insert(u, i).is_some() {
                return Err(FormatErr::Deserialization(
                    "error deserializing MSP, leaf already inserted".to_string(),
                ))
            }
        }

        let mut matrix = Vec::with_capacity(nb_row);
        let mut row = Vec::with_capacity(nb_col);
        row.push(I::from_bytes(&bytes[8 + (4 * nb_row)..])?);
        if bytes.len() < 8 + (4 * nb_row) + (nb_row * nb_col * row[0].len_bytes()) {
            return Err(FormatErr::InvalidSize(
                "invalid matrix size read from bytes".to_string(),
            ))
        }
        for c in 1..nb_col {
            let index = 8 + (4 * nb_row) + (c * row[0].len_bytes());
            row.push(I::from_bytes(&bytes[index..])?);
        }
        matrix.push(row);
        for r in 1..nb_row {
            let mut row = Vec::with_capacity(nb_col);
            for c in 0..nb_col {
                let index = 8 + (4 * nb_row) + (((r * nb_col) + c) * matrix[0][0].len_bytes());
                row.push(I::from_bytes(&bytes[index..])?);
            }
            matrix.push(row);
        }
        Ok(Self {
            nb_row,
            nb_col,
            matrix,
            attr_to_row,
            row_to_attr,
        })
    }

    fn len_bytes(&self) -> usize {
        8 + (self.nb_row * self.nb_col * self.matrix[0][0].len_bytes()) + (self.nb_row * 4)
    }
}

impl<I: std::fmt::Debug> Display for MonotoneSpanProgram<I> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f)?;
        for (attr, row) in self.row_to_attr.iter().zip(self.matrix.iter()) {
            writeln!(f, "attr {:>4}: {:?}", attr, *row)?;
        }
        Ok(())
    }
}

impl<I> MonotoneSpanProgram<I> {
    #[must_use]
    pub fn cols(&self) -> usize {
        self.nb_col
    }

    #[must_use]
    pub fn rows(&self) -> usize {
        self.nb_row
    }

    #[must_use]
    pub fn matrix(&self) -> &Vec<Vec<I>> {
        &self.matrix
    }

    #[must_use]
    pub fn get_row(&self, row: usize) -> &Vec<I> {
        &self.matrix[row]
    }

    #[must_use]
    pub fn get_attr_from_row(&self, i: usize) -> u32 {
        self.row_to_attr[i]
    }

    #[must_use]
    pub fn get_row_from_attr(&self, attr: u32) -> Option<usize> {
        self.attr_to_row.get(&attr).copied()
    }
}

impl<I: From<i32>> MonotoneSpanProgram<I>
where
    MonotoneSpanProgram<I>: From<MonotoneSpanProgram<i32>>,
{
    pub fn parse(s: &str) -> Result<Self, FormatErr> {
        let msp = Node::parse(s)?.to_msp()?;
        Ok(Self::from(msp))
    }
}

impl<I1: From<i32>> From<&MonotoneSpanProgram<i32>> for MonotoneSpanProgram<I1> {
    fn from(msp: &MonotoneSpanProgram<i32>) -> Self {
        Self {
            nb_row: msp.nb_row,
            nb_col: msp.nb_col,
            attr_to_row: msp.attr_to_row.clone(),
            row_to_attr: msp.row_to_attr.clone(),
            matrix: msp
                .matrix()
                .iter()
                .map(|v| v.iter().map(|i| (*i).into()).collect())
                .collect(),
        }
    }
}

#[derive(Clone)]
pub enum Node {
    And(Box<Node>, Box<Node>),
    Or(Box<Node>, Box<Node>),
    Leaf(u32),
}

// use A & B to construct And(A, B)
impl BitAnd for Node {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        Self::And(Box::new(self), Box::new(rhs))
    }
}

// use A | B to construct Or(A, B)
impl BitOr for Node {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self::Or(Box::new(self), Box::new(rhs))
    }
}

impl Display for Node {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let str = match self {
            Node::And(n1, n2) => format!("AND( {}, {} )", n1, n2),
            Node::Or(n1, n2) => format!("OR( {}, {} )", n1, n2),
            Node::Leaf(v) => format!("LEAF({})", v),
        };
        write!(f, "{}", str)
    }
}

impl Node {
    // from https://eprint.iacr.org/2010/351.pdf annex G
    // TODO: Ensure each Attribute appears only once in the Formula
    // TODO: Should we use Disjonctive Normal Form?
    pub fn to_msp(&self) -> Result<MonotoneSpanProgram<i32>, FormatErr> {
        let mut counter = 1;
        let mut queue = std::collections::VecDeque::new();
        let mut matrix = Vec::new();
        // compute the msp matrix
        queue.push_back((self, vec![1]));
        while let Some((node, vector)) = queue.pop_front() {
            match node {
                Node::And(n1, n2) => {
                    let mut vec_1 = vector.clone();
                    vec_1.resize(counter, 0_i32);
                    vec_1.push(1);
                    queue.push_back((n1, vec_1));
                    let mut vec_2 = vec![0_i32; counter];
                    vec_2.push(-1_i32);
                    queue.push_back((n2, vec_2));
                    counter += 1;
                }
                Node::Or(n1, n2) => {
                    queue.push_back((n1, vector.clone()));
                    queue.push_back((n2, vector.clone()));
                }
                leaf @ Node::Leaf(_) => {
                    matrix.push((leaf, vector.clone()));
                }
            };
        }
        // The resulting matrix span to the vector 1,0,⋯,0
        // For our scheme we need the msp to span the vector 1,⋯,1
        // Thus we have to change the basis such that 1,0,⋯,0 becomes 1,1,⋯1. That is
        // multiply the change-of-basis matrix 1 0 0 0 ⋯ 0
        // 1 1 0 0 ⋯ 0
        // 1 0 1 0 ⋯ 0
        // ⋯ ⋯ ⋯ ⋯ ⋯
        // 1 0 0 ⋯ ⋯ 1
        // by the transpose of msp_matrix
        // Finally it is equivalent to add the first column to the others
        let mut msp_matrix = Vec::with_capacity(matrix.len());
        let mut msp_map = HashMap::with_capacity(matrix.len());
        let mut msp_vec = Vec::with_capacity(matrix.len());
        for (i, row) in matrix.iter().enumerate() {
            if let Node::Leaf(attr) = row.0 {
                let mut vec = row.1.clone();
                vec.resize(counter, 0);
                for i in 1..vec.len() {
                    vec[i] += vec[0];
                }
                msp_matrix.push(vec);
                msp_vec.push(*attr);
                if msp_map.insert(*attr, i).is_some() {
                    return Err(FormatErr::Deserialization(
                        "error deserializing MSP, leaf already inserted".to_string(),
                    ))
                }
            } else {
                return Err(FormatErr::Deserialization(
                    "error deserializing MSP, only leaf allowed".to_string(),
                ))
            }
        }
        Ok(MonotoneSpanProgram {
            nb_row: msp_matrix.len(),
            nb_col: counter,
            matrix: msp_matrix,
            attr_to_row: msp_map,
            row_to_attr: msp_vec,
        })
    }

    // Very basic parser, the expression must be well formed
    // Example: convert the string "1 & (4 | (2 & 3))" to And(a, Box::new(Or(d,
    // Box::new(And(b, c))))) In this implementation, only digits are allowed
    // and parenthesis is expected to respect the operators priorities
    pub fn parse(s: &str) -> Result<Self, FormatErr> {
        // Authorize only digits and operators & and |
        let authorized = [
            ' ', '(', ')', '&', '|', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        ];
        if s.is_empty() || s.chars().any(|c| !authorized.contains(&c)) {
            return Err(ParsingError::UnexpectedCharacter(format!(
                "formula must contain only digits, parenthesis and operators & and |. Given \
                 formula: {}",
                s
            ))
            .into())
        }
        // Remove all spaces
        let new_s = str::replace(s, " ", "");

        // Try getting first integer and then following operation
        let int_reg = Regex::new(r"^\d+")?;
        if int_reg.is_match(&new_s) {
            if int_reg.captures_len() != 1 {
                return Err(ParsingError::UnexpectedCharacter(
                    "it must starts with an integer".to_string(),
                )
                .into())
            }
            let integer_str = int_reg
                .find_at(&new_s, 0)
                .ok_or_else(|| {
                    ParsingError::UnexpectedCharacter(format!(
                        "integer detected by regex but not found in: {}",
                        new_s,
                    ))
                })?
                .as_str();
            let integer = integer_str.parse::<u32>()?;

            // Remove integer from current formula
            let new_s = &new_s[integer_str.len()..];
            if new_s.is_empty() {
                return Ok(Node::Leaf(integer))
            }
            let a = Box::new(Node::Leaf(integer));
            let operator = new_s.chars().next().ok_or_else(|| {
                FormatErr::from(ParsingError::UnexpectedEnd(format!(
                    "no further character while detecting operator in: {}",
                    &new_s
                )))
            })?;

            // Remove operator from input string
            let new_s = &new_s[1..];
            match operator {
                '&' => Ok(Node::And(a, Box::new(Node::parse(new_s)?))),
                '|' => Ok(Node::Or(a, Box::new(Node::parse(new_s)?))),
                _ => Err(FormatErr::UnsupportedOperator(operator.to_string())),
            }
        } else {
            // Remove parenthesis on current part of formula and continue
            let first_char = new_s.chars().next().ok_or_else(|| {
                FormatErr::from(ParsingError::UnexpectedEnd(format!(
                    "no further character while getting first char in: {}",
                    &new_s
                )))
            })?;
            let new_s = &new_s[1..];
            if first_char != '(' {
                return Err(ParsingError::UnexpectedCharacter(
                    "opening parenthesis expected".to_string(),
                )
                .into())
            }

            // Check if formula contains a closing parenthesis
            let c = new_s.matches(')').count();
            if c == 0 {
                return Err(ParsingError::UnexpectedCharacter(
                    "closing parenthesis expected".to_string(),
                )
                .into())
            }

            // Search right closing parenthesis, avoiding false positive
            let mut count = 0;
            let mut right_closing_parenthesis = 0;
            for (index, c) in new_s.chars().enumerate() {
                match c {
                    '(' => count += 1,
                    ')' => count -= 1,
                    _ => {}
                };
                if count < 0 {
                    right_closing_parenthesis = index;
                    break
                }
            }

            let between_parenthesis = &new_s[..right_closing_parenthesis];
            let new_s = &new_s[between_parenthesis.len()..];

            // Skip closing parenthesis
            let new_s = &new_s[1..];
            if new_s.is_empty() {
                return Node::parse(between_parenthesis)
            }
            let operator = new_s.chars().next().ok_or_else(|| {
                ParsingError::UnexpectedEnd(format!(
                    "no further character while detecting operator in: {}",
                    &new_s
                ))
            })?;
            let new_s = &new_s[1..];

            match operator {
                '&' => Ok(Node::And(
                    Box::new(Node::parse(between_parenthesis)?),
                    Box::new(Node::parse(new_s)?),
                )),
                '|' => Ok(Node::Or(
                    Box::new(Node::parse(between_parenthesis)?),
                    Box::new(Node::parse(new_s)?),
                )),
                _ => Err(FormatErr::UnsupportedOperator(operator.to_string())),
            }
        }
    }
} // impl Node