From 9a0fc44f51993156f390d5bb13c6199202b4c0f6 Mon Sep 17 00:00:00 2001 From: incertia Date: Mon, 23 Sep 2024 17:02:25 -0400 Subject: [PATCH 1/2] add serde for some matrix types --- matrix/Cargo.toml | 4 ++++ matrix/src/dense.rs | 3 +++ matrix/src/lib.rs | 6 +++++- matrix/src/sparse.rs | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/matrix/Cargo.toml b/matrix/Cargo.toml index b926833a4..3e7836f13 100644 --- a/matrix/Cargo.toml +++ b/matrix/Cargo.toml @@ -4,11 +4,15 @@ version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" +[features] +serde = ["dep:serde"] + [dependencies] p3-field = { path = "../field" } p3-maybe-rayon = { path = "../maybe-rayon" } p3-util = { path = "../util" } rand = "0.8.5" +serde = { version = "1.0.210", optional = true } [dev-dependencies] criterion = "0.5.1" diff --git a/matrix/src/dense.rs b/matrix/src/dense.rs index cea0d7c2a..ed6d471fc 100644 --- a/matrix/src/dense.rs +++ b/matrix/src/dense.rs @@ -8,6 +8,8 @@ use p3_field::{ExtensionField, Field, PackedField}; use p3_maybe_rayon::prelude::*; use rand::distributions::{Distribution, Standard}; use rand::Rng; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; use crate::{Matrix, MatrixGet, MatrixRowSlices, MatrixRowSlicesMut, MatrixRows, MatrixTranspose}; @@ -16,6 +18,7 @@ const TRANSPOSE_BLOCK_SIZE: usize = 64; /// A dense matrix stored in row-major form. #[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct RowMajorMatrix { /// All values, stored in row-major order. pub values: Vec, diff --git a/matrix/src/lib.rs b/matrix/src/lib.rs index 83ab699fd..2f7b765df 100644 --- a/matrix/src/lib.rs +++ b/matrix/src/lib.rs @@ -7,6 +7,9 @@ extern crate alloc; use alloc::vec::Vec; use core::fmt::{Debug, Display, Formatter}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use crate::dense::RowMajorMatrix; use crate::strided::VerticallyStridedMatrixView; @@ -31,7 +34,8 @@ pub trait Matrix { } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct Dimensions { pub width: usize, pub height: usize, diff --git a/matrix/src/sparse.rs b/matrix/src/sparse.rs index 625d6297d..f2b0884ca 100644 --- a/matrix/src/sparse.rs +++ b/matrix/src/sparse.rs @@ -4,10 +4,14 @@ use core::ops::Range; use rand::distributions::{Distribution, Standard}; use rand::Rng; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; use crate::Matrix; /// A sparse matrix stored in the compressed sparse row format. +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct CsrMatrix { width: usize, From fd256e93fbc447696a24a7c7d2365913e326a4af Mon Sep 17 00:00:00 2001 From: incertia Date: Wed, 25 Sep 2024 11:46:33 -0400 Subject: [PATCH 2/2] cargo +nightly fmt with new formatter --- air/src/two_row_matrix.rs | 6 +++++- commit/src/adapters/extension_mmcs.rs | 10 ++++++++-- fri/src/two_adic_pcs.rs | 5 ++++- matrix/src/bitrev.rs | 5 ++++- matrix/src/dense.rs | 17 ++++++++++++++--- matrix/src/stack.rs | 5 ++++- matrix/src/strided.rs | 5 ++++- merkle-tree/src/mmcs.rs | 6 +++++- tensor-pcs/src/wrapped_matrix.rs | 6 +++++- uni-stark/src/public.rs | 6 +++++- 10 files changed, 58 insertions(+), 13 deletions(-) diff --git a/air/src/two_row_matrix.rs b/air/src/two_row_matrix.rs index 2c91b7454..4bc2d351a 100644 --- a/air/src/two_row_matrix.rs +++ b/air/src/two_row_matrix.rs @@ -26,7 +26,11 @@ impl<'a, T> Matrix for TwoRowMatrixView<'a, T> { } impl MatrixRows for TwoRowMatrixView<'_, T> { - type Row<'a> = Cloned> where Self: 'a, T: 'a; + type Row<'a> + = Cloned> + where + Self: 'a, + T: 'a; fn row(&self, r: usize) -> Self::Row<'_> { let slice = match r { diff --git a/commit/src/adapters/extension_mmcs.rs b/commit/src/adapters/extension_mmcs.rs index 9377586a5..c671d91a0 100644 --- a/commit/src/adapters/extension_mmcs.rs +++ b/commit/src/adapters/extension_mmcs.rs @@ -32,7 +32,10 @@ where type Commitment = InnerMmcs::Commitment; type Proof = InnerMmcs::Proof; type Error = InnerMmcs::Error; - type Mat<'a> = ExtensionMatrix> where Self: 'a; + type Mat<'a> + = ExtensionMatrix> + where + Self: 'a; fn open_batch( &self, @@ -131,7 +134,10 @@ where EF: ExtensionField, InnerMat: MatrixRows, { - type Row<'a> = ExtensionRow>::Row<'a> as IntoIterator>::IntoIter> where Self: 'a; + type Row<'a> + = ExtensionRow>::Row<'a> as IntoIterator>::IntoIter> + where + Self: 'a; fn row(&self, r: usize) -> Self::Row<'_> { ExtensionRow { diff --git a/fri/src/two_adic_pcs.rs b/fri/src/two_adic_pcs.rs index 596eb5139..b7a0097f2 100644 --- a/fri/src/two_adic_pcs.rs +++ b/fri/src/two_adic_pcs.rs @@ -140,7 +140,10 @@ where >::ProverData: Send + Sync, >::ProverData: Send + Sync + Sized, { - type Lde<'a> = BitReversedMatrixView<>::Mat<'a>> where Self: 'a; + type Lde<'a> + = BitReversedMatrixView<>::Mat<'a>> + where + Self: 'a; fn coset_shift(&self) -> C::Val { C::Val::generator() diff --git a/matrix/src/bitrev.rs b/matrix/src/bitrev.rs index 692e4f89f..60f2fafe2 100644 --- a/matrix/src/bitrev.rs +++ b/matrix/src/bitrev.rs @@ -47,7 +47,10 @@ impl> MatrixGet for BitReversedMatrixView { } impl> MatrixRows for BitReversedMatrixView { - type Row<'a> = Inner::Row<'a> where Self: 'a; + type Row<'a> + = Inner::Row<'a> + where + Self: 'a; fn row(&self, r: usize) -> Self::Row<'_> { self.inner.row(reverse_bits_len(r, self.log_height)) diff --git a/matrix/src/dense.rs b/matrix/src/dense.rs index ed6d471fc..9184cd5ba 100644 --- a/matrix/src/dense.rs +++ b/matrix/src/dense.rs @@ -204,7 +204,10 @@ impl MatrixGet for RowMajorMatrix { } impl MatrixRows for RowMajorMatrix { - type Row<'a> = Cloned> where T: 'a; + type Row<'a> + = Cloned> + where + T: 'a; fn row(&self, r: usize) -> Self::Row<'_> { self.row_slice(r).iter().cloned() @@ -287,7 +290,11 @@ impl MatrixGet for RowMajorMatrixView<'_, T> { } impl MatrixRows for RowMajorMatrixView<'_, T> { - type Row<'a> = Cloned> where Self: 'a, T: 'a; + type Row<'a> + = Cloned> + where + Self: 'a, + T: 'a; fn row(&self, r: usize) -> Self::Row<'_> { self.row_slice(r).iter().cloned() @@ -423,7 +430,11 @@ impl MatrixGet for RowMajorMatrixViewMut<'_, T> { } impl MatrixRows for RowMajorMatrixViewMut<'_, T> { - type Row<'a> = Cloned> where Self: 'a, T: 'a; + type Row<'a> + = Cloned> + where + Self: 'a, + T: 'a; fn row(&self, r: usize) -> Self::Row<'_> { self.row_slice(r).iter().cloned() diff --git a/matrix/src/stack.rs b/matrix/src/stack.rs index df525150f..a522a1030 100644 --- a/matrix/src/stack.rs +++ b/matrix/src/stack.rs @@ -36,7 +36,10 @@ where Second: MatrixRows, T: core::fmt::Debug, { - type Row<'a> = EitherIterable, Second::Row<'a>> where Self: 'a; + type Row<'a> + = EitherIterable, Second::Row<'a>> + where + Self: 'a; fn row(&self, r: usize) -> Self::Row<'_> { if r < self.first.height() { diff --git a/matrix/src/strided.rs b/matrix/src/strided.rs index 6b409b9e3..79db61d89 100644 --- a/matrix/src/strided.rs +++ b/matrix/src/strided.rs @@ -29,7 +29,10 @@ impl> MatrixGet for VerticallyStridedMatrixView impl> MatrixRows for VerticallyStridedMatrixView { - type Row<'a> = Inner::Row<'a> where Self: 'a; + type Row<'a> + = Inner::Row<'a> + where + Self: 'a; fn row(&self, r: usize) -> Self::Row<'_> { self.inner.row(r * self.stride + self.offset) diff --git a/merkle-tree/src/mmcs.rs b/merkle-tree/src/mmcs.rs index f7268cd5b..1a5b17d03 100644 --- a/merkle-tree/src/mmcs.rs +++ b/merkle-tree/src/mmcs.rs @@ -52,7 +52,11 @@ where type Commitment = [P::Scalar; DIGEST_ELEMS]; type Proof = Vec<[P::Scalar; DIGEST_ELEMS]>; type Error = (); - type Mat<'a> = RowMajorMatrixView<'a, P::Scalar> where H: 'a, C: 'a; + type Mat<'a> + = RowMajorMatrixView<'a, P::Scalar> + where + H: 'a, + C: 'a; fn open_batch( &self, diff --git a/tensor-pcs/src/wrapped_matrix.rs b/tensor-pcs/src/wrapped_matrix.rs index d3e4d03b1..97cef8459 100644 --- a/tensor-pcs/src/wrapped_matrix.rs +++ b/tensor-pcs/src/wrapped_matrix.rs @@ -40,7 +40,11 @@ where M: MatrixRows, T: core::fmt::Debug, { - type Row<'a> = WrappedMatrixRow<'a, T, M> where T: 'a, M: 'a; + type Row<'a> + = WrappedMatrixRow<'a, T, M> + where + T: 'a, + M: 'a; fn row(&self, r: usize) -> Self::Row<'_> { WrappedMatrixRow { diff --git a/uni-stark/src/public.rs b/uni-stark/src/public.rs index 98067e2e5..c569e1061 100644 --- a/uni-stark/src/public.rs +++ b/uni-stark/src/public.rs @@ -70,7 +70,11 @@ impl Matrix for PublicRow { } impl MatrixRows for PublicRow { - type Row<'a> = Cloned> where T: 'a, Self: 'a; + type Row<'a> + = Cloned> + where + T: 'a, + Self: 'a; fn row(&self, _r: usize) -> Self::Row<'_> { self.0.iter().cloned()