Matrix moves to array

This commit is contained in:
Ashashin 2021-08-05 18:44:19 +02:00
parent 5b11d8aa70
commit 0cfd5937eb
2 changed files with 10 additions and 16 deletions

View File

@ -72,7 +72,7 @@ pub fn kyber_cpapke_key_gen(params: KyberParams) -> (ByteArray, ByteArray) {
let d = ByteArray::random(32);
let (rho, sigma) = g(&d);
let mut a = PolyMatrix3329::init_matrix(k, k);
let mut a = PolyMatrix3329::init();
for i in 0..k {
for j in 0..k {
@ -111,7 +111,7 @@ pub fn kyber_cpapke_enc(
let (t, rho) = pk.split_at(offset);
let t_hat = decode_to_polyvec(t, 12);
let mut a_t = PolyMatrix3329::init_matrix(params.k, params.k);
let mut a_t = PolyMatrix3329::init();
for i in 0..params.k {
for j in 0..params.k {

View File

@ -8,12 +8,13 @@ use crate::polyvec::structures::{FiniteRing, RingModule};
use std::fmt::{self, Debug};
/// A `Matrix` is a collection of `Vector`s
#[derive(Clone, Copy)]
pub struct Matrix<K, const X: usize, const Y: usize>
where
K: FiniteRing + Clone + Default,
{
/// Internal representation as a list of elements of type `T`
coefficients: Vec<K>,
coefficients: [[K; X];Y],
}
impl<K, const X: usize, const Y: usize> Matrix<K, X, Y>
@ -23,9 +24,9 @@ where
/// Initialise an empty `Matrix`
/// - `col_num`: number of columns
/// - `col_dim`: number of rows
pub fn init_matrix(col_num: usize, col_dim: usize) -> Self {
pub fn init() -> Self {
Self {
coefficients: vec![Default::default(); col_num * col_dim],
coefficients: [[Default::default(); X];Y],
}
}
@ -36,13 +37,7 @@ where
/// Return a specific row
pub fn row(&self, index: usize) -> PolyVec<K, X> {
let mut t = PolyVec::<K, X>::init();
for i in 0..X {
t.set(i, self.coefficients[index * Y + i].clone());
}
t
PolyVec::<K, X>::from_vec(self.coefficients[index])
}
/// Return a specific column
@ -50,7 +45,7 @@ where
let mut t = PolyVec::<K, Y>::init();
for i in 0..Y {
t.set(i, self.coefficients[index * i + X].clone());
t.set(i, self.coefficients[index * X][i].clone());
}
t
@ -59,14 +54,13 @@ where
/// Set a coefficient
pub fn set(&mut self, row: usize, column: usize, value: K) {
assert!((column < X) && (row < Y));
self.coefficients[row * X + column] = value;
self.coefficients[row][column] = value;
}
/// Get a coefficient
pub fn get(&self, row: usize, column: usize) -> K {
assert!((column < X) && (row < Y));
self.coefficients[row * X + column].clone()
self.coefficients[row][column]
}
/// Perform a matrix vector multiplication