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
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use std::ops::Mul;

use crate::{Element, Expression, Field};

/// A matrix of prime field elements.
pub struct ElementMatrix<F: Field> {
    rows: Vec<Vec<Element<F>>>,
}

impl<F: Field> ElementMatrix<F> {
    pub fn new(rows: Vec<Vec<Element<F>>>) -> Self {
        let num_cols = rows[0].len();
        for row in rows.iter() {
            assert_eq!(row.len(), num_cols, "Rows must have uniform length");
        }
        ElementMatrix { rows }
    }
}

impl<F: Field> Clone for ElementMatrix<F> {
    fn clone(&self) -> Self {
        ElementMatrix { rows: self.rows.clone() }
    }
}

impl<F: Field> Mul<&[Element<F>]> for &ElementMatrix<F> {
    type Output = Vec<Element<F>>;

    fn mul(self, rhs: &[Element<F>]) -> Self::Output {
        self.rows.iter().zip(rhs.iter())
            .map(|(row, val)| row.iter().fold(
                Element::zero(), |sum, row_i| sum + val * row_i))
            .collect()
    }
}

impl<F: Field> Mul<&[Expression<F>]> for &ElementMatrix<F> {
    type Output = Vec<Expression<F>>;

    fn mul(self, rhs: &[Expression<F>]) -> Self::Output {
        self.rows.iter().zip(rhs.iter())
            .map(|(row, val)| row.iter().fold(
                Expression::zero(), |sum, row_i| sum + val * row_i))
            .collect()
    }
}

impl<F: Field> Mul<&[Element<F>]> for ElementMatrix<F> {
    type Output = Vec<Element<F>>;

    fn mul(self, rhs: &[Element<F>]) -> Self::Output {
        &self * rhs
    }
}

impl<F: Field> Mul<&[Expression<F>]> for ElementMatrix<F> {
    type Output = Vec<Expression<F>>;

    fn mul(self, rhs: &[Expression<F>]) -> Self::Output {
        &self * rhs
    }
}

/// A Maximum Distance Separable matrix.
pub struct MdsMatrix<F: Field> {
    matrix: ElementMatrix<F>,
}

impl<F: Field> MdsMatrix<F> {
    pub fn new(rows: Vec<Vec<Element<F>>>) -> Self {
        // TODO: Verify the MDS diffusion property.
        MdsMatrix { matrix: ElementMatrix::new(rows) }
    }

    pub fn inverse(&self) -> Self {
        unimplemented!("TODO: Implement inverse")
    }
}

impl<F: Field> Clone for MdsMatrix<F> {
    fn clone(&self) -> Self {
        MdsMatrix { matrix: self.matrix.clone() }
    }
}

impl<F: Field> Mul<&[Element<F>]> for &MdsMatrix<F> {
    type Output = Vec<Element<F>>;

    fn mul(self, rhs: &[Element<F>]) -> Self::Output {
        &self.matrix * rhs
    }
}

impl<F: Field> Mul<&[Expression<F>]> for &MdsMatrix<F> {
    type Output = Vec<Expression<F>>;

    fn mul(self, rhs: &[Expression<F>]) -> Self::Output {
        &self.matrix * rhs
    }
}

impl<F: Field> Mul<&[Element<F>]> for MdsMatrix<F> {
    type Output = Vec<Element<F>>;

    fn mul(self, rhs: &[Element<F>]) -> Self::Output {
        self.matrix * rhs
    }
}

impl<F: Field> Mul<&[Expression<F>]> for MdsMatrix<F> {
    type Output = Vec<Expression<F>>;

    fn mul(self, rhs: &[Expression<F>]) -> Self::Output {
        self.matrix * rhs
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn matrix_vector_multiplication() {
        // TODO
    }
}