Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion poly-commit/src/linear_codes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ where
H::Output: Into<C::Leaf>,
C::Leaf: Default + Clone + Send,
{
let ext_mat_cols = ext_mat.cols();
let ext_mat_cols = ext_mat.cols().clone();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we should avoid cloning and rearranging as much as we can. This PR is trying to solve that!

Huh?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is inevitable, unfortunately. H::evaluate consumes the vector as far as I understood. Notice that we were cloning it previously too, but it was not explicit here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, if we mange to derive Copy for Vec<F> we can avoid cloning here too, I guess

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@autquis I figured out that you can actually just call .borrow() and it works like a charm. See e20f67e

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmagician Nice!
Also, let me know if I can still help with this PR!


let mut col_hashes: Vec<C::Leaf> = cfg_into_iter!(ext_mat_cols)
.map(|col| hash_column::<F, C, H>(col, &col_hash_params).unwrap())
Expand Down
54 changes: 31 additions & 23 deletions poly-commit/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub(crate) fn ceil_div(x: usize, y: usize) -> usize {
pub struct Matrix<F: Field> {
pub(crate) n: usize,
pub(crate) m: usize,
entries: Vec<Vec<F>>,
row_major: Vec<Vec<F>>,
col_major: Vec<Vec<F>>,
}

impl<F: Field> Matrix<F> {
Expand All @@ -74,32 +75,48 @@ impl<F: Field> Matrix<F> {
);

// TODO more efficient to run linearly?
let entries: Vec<Vec<F>> = (0..n)
let row_major: Vec<Vec<F>> = (0..n)
.map(|row| (0..m).map(|col| entry_list[m * row + col]).collect())
.collect();
let col_major = (0..m)
.map(|col| (0..n).map(|row| row_major[row][col]).collect())
.collect();

Self { n, m, entries }
Self {
n,
m,
row_major,
col_major,
}
}

/// Returns a Matrix given a list of its rows, each in turn represented as a list of field elements.
///
/// # Panics
/// Panics if the sub-lists do not all have the same length.
pub(crate) fn new_from_rows(row_list: Vec<Vec<F>>) -> Self {
let m = row_list[0].len();
pub(crate) fn new_from_rows(row_major: Vec<Vec<F>>) -> Self {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used in testing now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed / refactor tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that RowMajorMatrix is used in Hyrax and Ligero too. We need this method for them. So I would say let's keep it.

let m = row_major[0].len();

for row in row_list.iter().skip(1) {
for row in row_major.iter().skip(1) {
assert_eq!(
row.len(),
m,
"Invalid matrix construction: not all rows have the same length"
);
}
let col_major = (0..m)
.map(|col| {
(0..row_major.len())
.map(|row| row_major[row][col])
.collect()
})
.collect();

Self {
n: row_list.len(),
n: row_major.len(),
m,
entries: row_list,
row_major,
col_major,
}
}

Expand All @@ -110,19 +127,17 @@ impl<F: Field> Matrix<F> {
/// Index bound checks are waived for efficiency and behaviour under invalid indexing is undefined
#[cfg(test)]
pub(crate) fn entry(&self, i: usize, j: usize) -> F {
self.entries[i][j]
self.row_major[i][j]
}

/// Returns self as a list of rows
pub(crate) fn rows(&self) -> Vec<Vec<F>> {
self.entries.clone()
pub(crate) fn rows(&self) -> &Vec<Vec<F>> {
&self.row_major
}

/// Returns self as a list of columns
pub(crate) fn cols(&self) -> Vec<Vec<F>> {
(0..self.m)
.map(|col| (0..self.n).map(|row| self.entries[row][col]).collect())
.collect()
pub(crate) fn cols(&self) -> &Vec<Vec<F>> {
&self.col_major
}

/// Returns the product v * self, where v is interpreted as a row vector. In other words,
Expand All @@ -139,14 +154,7 @@ impl<F: Field> Matrix<F> {
);

(0..self.m)
.map(|col| {
inner_product(
v,
&(0..self.n)
.map(|row| self.entries[row][col])
.collect::<Vec<F>>(),
)
})
.map(|col| inner_product(v, &self.col_major[col]))
.collect()
}
}
Expand Down