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
use super::types::BigNum;
use core::fmt;

#[derive(Debug, Clone, Copy)]
pub enum ValueError {
    UnequalSizeVectors(usize, usize),
    IncorrectSize(usize),
    NonPowerOf2(usize),
    OutOfRange(usize),
    NegativeValue(BigNum),
}

#[derive(Debug, Clone, Copy)]
pub enum SerzDeserzError {
    FieldElementBytesIncorrectSize(usize, usize),
    G1BytesIncorrectSize(usize, usize),
    G2BytesIncorrectSize(usize, usize),
    GTBytesIncorrectSize(usize, usize),
    RequiredHexChar,
    CannotParseFP,
}

impl fmt::Display for SerzDeserzError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SerzDeserzError::FieldElementBytesIncorrectSize(a, b) => {
                write!(f, "Incorrect bytes size for field element. Given {} but expected {}", a, b)
            }
            SerzDeserzError::G1BytesIncorrectSize(a, b) => {
                write!(f, "Incorrect bytes size for G1 group element. Given {} but expected {}", a, b)
            }
            SerzDeserzError::G2BytesIncorrectSize(a, b) => {
                write!(f, "Incorrect bytes size for G2 group element. Given {} but expected {}", a, b)
            }
            SerzDeserzError::GTBytesIncorrectSize(a, b) => {
                write!(f, "Incorrect bytes size for G2 group element. Given {} but expected {}", a, b)
            }
            SerzDeserzError::RequiredHexChar => write!(f, "Required hex character"),
            SerzDeserzError::CannotParseFP => write!(f, "Error while parsing FP"),
        }
    }
}

#[macro_export]
macro_rules! check_vector_size_for_equality {
    ( $a:expr, $b:expr ) => {{
        if $a.len() != $b.len() {
            Err(ValueError::UnequalSizeVectors($a.len(), $b.len()))
        } else {
            Ok(())
        }
    }};
}

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

    #[test]
    fn test_vector_size_equality() {
        let a1 = vec![1, 4, 6, 8];
        let a2 = vec![4, 5, 2, 1];
        assert!(check_vector_size_for_equality!(a1, a2).is_ok());

        let a3 = vec![1, 4, 6];
        assert!(check_vector_size_for_equality!(a3, a2).is_err());
    }
}