From b6f93db866b209c4afa5965011800d352574200f Mon Sep 17 00:00:00 2001 From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com> Date: Fri, 8 Sep 2023 07:36:59 -0700 Subject: [PATCH] chore: add getter functions to Poseidon spec --- halo2-base/src/poseidon/hasher/mds.rs | 24 +++++++++++++++++++++--- halo2-base/src/poseidon/hasher/spec.rs | 17 +++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/halo2-base/src/poseidon/hasher/mds.rs b/halo2-base/src/poseidon/hasher/mds.rs index 159b031f..91b7d262 100644 --- a/halo2-base/src/poseidon/hasher/mds.rs +++ b/halo2-base/src/poseidon/hasher/mds.rs @@ -1,4 +1,6 @@ #![allow(clippy::needless_range_loop)] +use getset::Getters; + use crate::ff::PrimeField; /// The type used to hold the MDS matrix @@ -7,24 +9,40 @@ pub(crate) type Mds = [[F; T]; T]; /// `MDSMatrices` holds the MDS matrix as well as transition matrix which is /// also called `pre_sparse_mds` and sparse matrices that enables us to reduce /// number of multiplications in apply MDS step -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Getters)] pub struct MDSMatrices { + /// MDS matrix + #[getset(get = "pub")] pub(crate) mds: MDSMatrix, + /// Transition matrix + #[getset(get = "pub")] pub(crate) pre_sparse_mds: MDSMatrix, + /// Sparse matrices + #[getset(get = "pub")] pub(crate) sparse_matrices: Vec>, } /// `SparseMDSMatrix` are in `[row], [hat | identity]` form and used in linear /// layer of partial rounds instead of the original MDS -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Getters)] pub struct SparseMDSMatrix { + /// row + #[getset(get = "pub")] pub(crate) row: [F; T], + /// column transpose + #[getset(get = "pub")] pub(crate) col_hat: [F; RATE], } /// `MDSMatrix` is applied to `State` to achive linear layer of Poseidon #[derive(Clone, Debug)] -pub struct MDSMatrix(pub(crate) Mds); +pub struct MDSMatrix(pub(crate) Mds); + +impl AsRef> for MDSMatrix { + fn as_ref(&self) -> &Mds { + &self.0 + } +} impl MDSMatrix { pub(crate) fn mul_vector(&self, v: &[F; T]) -> [F; T] { diff --git a/halo2-base/src/poseidon/hasher/spec.rs b/halo2-base/src/poseidon/hasher/spec.rs index 1568935b..e0a0d2c9 100644 --- a/halo2-base/src/poseidon/hasher/spec.rs +++ b/halo2-base/src/poseidon/hasher/spec.rs @@ -3,6 +3,7 @@ use crate::{ poseidon::hasher::mds::*, }; +use getset::{CopyGetters, Getters}; use poseidon_rs::poseidon::primitives::Spec as PoseidonSpec; // trait use std::marker::PhantomData; @@ -53,20 +54,32 @@ impl< /// `OptimizedPoseidonSpec` holds construction parameters as well as constants that are used in /// permutation step. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Getters, CopyGetters)] pub struct OptimizedPoseidonSpec { + /// Number of full rounds + #[getset(get_copy = "pub")] pub(crate) r_f: usize, + /// MDS matrices + #[getset(get = "pub")] pub(crate) mds_matrices: MDSMatrices, + /// Round constants + #[getset(get = "pub")] pub(crate) constants: OptimizedConstants, } /// `OptimizedConstants` has round constants that are added each round. While /// full rounds has T sized constants there is a single constant for each /// partial round -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Getters)] pub struct OptimizedConstants { + /// start + #[getset(get = "pub")] pub(crate) start: Vec<[F; T]>, + /// partial + #[getset(get = "pub")] pub(crate) partial: Vec, + /// end + #[getset(get = "pub")] pub(crate) end: Vec<[F; T]>, }