Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add getter functions to Poseidon spec #140

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 21 additions & 3 deletions halo2-base/src/poseidon/hasher/mds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![allow(clippy::needless_range_loop)]
use getset::Getters;

use crate::ff::PrimeField;

/// The type used to hold the MDS matrix
Expand All @@ -7,24 +9,40 @@ pub(crate) type Mds<F, const T: usize> = [[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<F: PrimeField, const T: usize, const RATE: usize> {
/// MDS matrix
#[getset(get = "pub")]
pub(crate) mds: MDSMatrix<F, T, RATE>,
/// Transition matrix
#[getset(get = "pub")]
pub(crate) pre_sparse_mds: MDSMatrix<F, T, RATE>,
/// Sparse matrices
#[getset(get = "pub")]
pub(crate) sparse_matrices: Vec<SparseMDSMatrix<F, T, RATE>>,
}

/// `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<F: PrimeField, const T: usize, const RATE: usize> {
/// 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<F: PrimeField, const T: usize, const RATE: usize>(pub(crate) Mds<F, T>);
pub struct MDSMatrix<F, const T: usize, const RATE: usize>(pub(crate) Mds<F, T>);

impl<F, const T: usize, const RATE: usize> AsRef<Mds<F, T>> for MDSMatrix<F, T, RATE> {
fn as_ref(&self) -> &Mds<F, T> {
&self.0
}
}

impl<F: PrimeField, const T: usize, const RATE: usize> MDSMatrix<F, T, RATE> {
pub(crate) fn mul_vector(&self, v: &[F; T]) -> [F; T] {
Expand Down
17 changes: 15 additions & 2 deletions halo2-base/src/poseidon/hasher/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<F: PrimeField, const T: usize, const RATE: usize> {
/// Number of full rounds
#[getset(get_copy = "pub")]
pub(crate) r_f: usize,
/// MDS matrices
#[getset(get = "pub")]
pub(crate) mds_matrices: MDSMatrices<F, T, RATE>,
/// Round constants
#[getset(get = "pub")]
pub(crate) constants: OptimizedConstants<F, T>,
}

/// `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<F: PrimeField, const T: usize> {
/// start
#[getset(get = "pub")]
pub(crate) start: Vec<[F; T]>,
/// partial
#[getset(get = "pub")]
pub(crate) partial: Vec<F>,
/// end
#[getset(get = "pub")]
pub(crate) end: Vec<[F; T]>,
}

Expand Down