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

Propagate new backend changes through poseidon377 #53

Merged
merged 54 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
58ff8e9
deps: use decaf377 0.8.0
redshiftzero Feb 5, 2024
b1959de
deps: add `arkworks` feature to `poseidon377`
redshiftzero Feb 5, 2024
f7d1760
ci: add build without global allocator
redshiftzero Feb 5, 2024
448ce37
deps: add `alloc` feature to `poseidon377`
redshiftzero Feb 5, 2024
4154b96
test: fix r1cs tests in poseidon377
redshiftzero Feb 5, 2024
4969f90
deps: rearrange features in poseidon-parameters
redshiftzero Feb 5, 2024
5723ca5
poseidon-parameters: replace use of anyhow
redshiftzero Feb 5, 2024
074009e
chore: bump versions everywhere
redshiftzero Feb 5, 2024
bdcbbf2
poseidon-parameters: remove `ark_ff::PrimeField` usage
redshiftzero Feb 5, 2024
392b4cb
hack: impl Fq::pow which needs to exist upstream
redshiftzero Feb 7, 2024
584513b
poseidon-parameters: remove num-integer dependency
redshiftzero Feb 7, 2024
8bade81
poseidon-parameters: replace all vec operations
redshiftzero Feb 7, 2024
956e04e
ci: ledger uses thumbv8m.main-none-eabi
redshiftzero Feb 8, 2024
d5acc20
poseidon-parameters: get tests running using heapless
redshiftzero Feb 8, 2024
e03561c
fix: 2x2 matrix inverse
redshiftzero Feb 8, 2024
0bd5f2d
poseidon-parameters: heapless Vec operations are fallible
redshiftzero Feb 8, 2024
318bcb2
wip: Vec interface
redshiftzero Feb 20, 2024
12b0266
poseidon-parameters: add round_numbers
redshiftzero Feb 22, 2024
9e6d2a0
matrix_ops (except mat_mul)
redshiftzero Feb 22, 2024
db10d98
poseidon-parameters: `Matrix` Hadamard product
redshiftzero Feb 22, 2024
2bb0b3d
poseidon-parameters: scalar mult, const generic `SquareMatrix`
redshiftzero Feb 22, 2024
615c48a
poseidon-parameters: `Matrix`, `SquareMatrix` helper methods
redshiftzero Feb 22, 2024
7f0dbb1
poseidon-parameters: `SquareMatrix` inverse, cofactors, determinant
redshiftzero Feb 22, 2024
4a3416d
poseidon-parameters: `ArcMatrix`, `OptimizedArcMatrix`
redshiftzero Feb 22, 2024
894b510
poseidon-parameters: `MdsMatrix`
redshiftzero Feb 23, 2024
0c8dcec
poseidon-parameters: v2
redshiftzero Feb 23, 2024
604b6b9
poseidon-parameters: v1
redshiftzero Feb 23, 2024
cee2e75
poseidon-parameters: re-enable identity, cofactor, transpose tests
redshiftzero Feb 23, 2024
cb941ba
poseidon-parameters: move transpose out from trait, add mat_mul
redshiftzero Feb 26, 2024
1edbd2c
poseidon-parameters: square mat_mul
redshiftzero Feb 26, 2024
a662faf
poseidon-parameters: this check is now done at compile time
redshiftzero Feb 26, 2024
cea3926
poseidon-parameters: impl Mhat matrix
redshiftzero Feb 26, 2024
8019903
poseidon-parameters: matrix determinant for fixed sizes
redshiftzero Feb 26, 2024
946cbb3
poseidon-parameters: renable rest of tests
redshiftzero Feb 27, 2024
cda7617
poseidon-parameters: matrix minors
redshiftzero Feb 27, 2024
e6633bd
poseidon-parameters: clean up matrix determinant calculation
redshiftzero Feb 27, 2024
a7810be
poseidon-parameters: remove `MatrixOperations::rows` and last Vecs
redshiftzero Feb 27, 2024
f45ba1a
poseidon-permutation: rewrite for embedded environments
redshiftzero Feb 29, 2024
445df76
poseidon-paramgen: begin to use latest poseidon-parameters
redshiftzero Apr 1, 2024
645976f
poseidon377: rate 1 hashing working
redshiftzero Apr 1, 2024
0e50735
use for-poseidon377 branch of decaf377
redshiftzero Apr 1, 2024
ee82471
remove temporary stuff that should go in decaf377
redshiftzero Apr 2, 2024
7f88c32
use const fns to instantiate parameters
redshiftzero Apr 2, 2024
d9fffe5
poseidon377 compiling on thumbv8m.main-none-eabi
redshiftzero Apr 2, 2024
31ac64d
poseidon377: rate 2
redshiftzero Apr 2, 2024
cef2d5e
poseidon377: rate 3
redshiftzero Apr 2, 2024
5ec426b
poseidon377: rate 4
redshiftzero Apr 2, 2024
33d972c
poseidon377: rate 5
redshiftzero Apr 2, 2024
f7358f9
poseidon377: rate 6
redshiftzero Apr 2, 2024
f9116fe
poseidon377: rate 7
redshiftzero Apr 2, 2024
9b6eddf
poseidon377: r1cs
redshiftzero Apr 2, 2024
0afde62
use released version of decaf377
redshiftzero Apr 3, 2024
37b37d8
rustfmt
redshiftzero Apr 3, 2024
6b25ac1
test: migrate tests to dedicated crate
redshiftzero Apr 4, 2024
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
17 changes: 17 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ jobs:
popd
done

build_no_alloc:
name: build without alloc
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup target add thumbv8m.main-none-eabi
- uses: Swatinem/rust-cache@v1
- uses: actions-rs/cargo@v1
with:
command: build
args: --package poseidon377 --target thumbv8m.main-none-eabi --no-default-features

# clippy:
# name: Clippy
# runs-on: ubuntu-latest
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

members = [
"poseidon377",
"poseidon-paramgen",
# "poseidon-paramgen" # Temporarily excluded until refactored
"poseidon-permutation",
"poseidon-consistency",
# "poseidon-consistency", # Temporarily excluded until paramgen is refactored
"poseidon-parameters",
"poseidon-tests",
]

[profile.release]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This repository contains:
parameters
* [`poseidon-permutation`](../main/poseidon-permutation): an independent implementation of the Poseidon permutation
* [`poseidon-consistency`](../main/poseidon-consistency): property-based tests for consistency between Poseidon implementations
* [`poseidon-tests`](../main/poseidon-tests): test vectors for `poseidon-parameters` and `poseidon377`

## Audits

Expand Down
8 changes: 4 additions & 4 deletions poseidon-consistency/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/penumbra-zone/poseidon377"

[dependencies]
poseidon-parameters = { path = "../poseidon-parameters" }
poseidon-paramgen = { path = "../poseidon-paramgen", default-features = false }
poseidon-parameters = { path = "../poseidon-parameters", default-features = false }
#poseidon-paramgen = { path = "../poseidon-paramgen", default-features = false }
poseidon-permutation = { path="../poseidon-permutation", default-features = false }
decaf377 = { version = "0.4", default-features = false }
decaf377 = { version="0.9", default-features = false }

[dev-dependencies]
criterion = { version = "0.4", features=["html_reports"] }
ark-ff = "0.4"
ark-ed-on-bls12-377 = "0.4"
num-bigint = "0.4"
proptest = "1"
rand_core = { version = "0.6.3", features = ["getrandom"] }
rand_core = { version = "0.6.3", default-features=false }
rand_chacha = "0.3"
once_cell = "1.8"

Expand Down
6 changes: 5 additions & 1 deletion poseidon-parameters/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@

# 0.4.0

* Add `v1` and `v2` APIs.
* Add `v1` and `v2` APIs.

# 1.0.0

* Remove Arkworks requirement
18 changes: 7 additions & 11 deletions poseidon-parameters/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "poseidon-parameters"
version = "0.4.0"
version = "1.0.0"
edition = "2018"
authors = []
description = "A crate for Poseidon parameters"
Expand All @@ -9,17 +9,13 @@ keywords = ["no_std"]
repository = "https://github.com/penumbra-zone/poseidon377"

[dependencies]
anyhow = { version = "1", default-features = false }
ark-ff = { version = "^0.4.0", default-features = false }
num-integer = { version = "0.1.45", default-features = false }

[dev-dependencies]
ark-ed-on-bls12-377 = "0.4"
proptest = "1"
decaf377 = { version="0.9", default-features = false }

[features]
default = ["std"]
alloc = [
"decaf377/alloc"
]
std = [
"anyhow/std",
"ark-ff/std",
"num-integer/std",
"decaf377/std",
]
108 changes: 56 additions & 52 deletions poseidon-parameters/src/arc_matrix.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
use anyhow::Result;
use ark_ff::{vec::Vec, PrimeField};

use crate::{matrix::Matrix, matrix_ops::MatrixOperations};
use crate::{error::PoseidonParameterError, matrix::Matrix, matrix_ops::MatrixOperations};
use decaf377::Fq;

/// Represents an matrix of round constants.
///
/// Arc stands for `AddRoundConstant` which is the
/// step in the permutation where this matrix is used.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArcMatrix<F: PrimeField>(pub Matrix<F>);
pub struct ArcMatrix<const N_ROWS: usize, const N_COLS: usize, const N_ELEMENTS: usize>(
pub Matrix<N_ROWS, N_COLS, N_ELEMENTS>,
);

impl<const N_ROWS: usize, const N_COLS: usize, const N_ELEMENTS: usize>
ArcMatrix<N_ROWS, N_COLS, N_ELEMENTS>
{
pub fn transpose(&self) -> ArcMatrix<N_COLS, N_ROWS, N_ELEMENTS> {
ArcMatrix(self.0.transpose())
}

impl<F: PrimeField> MatrixOperations<F> for ArcMatrix<F> {
fn new(n_rows: usize, n_cols: usize, elements: Vec<F>) -> Self {
Self(Matrix::new(n_rows, n_cols, elements))
pub fn inner_elements(&self) -> [Fq; N_ELEMENTS] {
self.0.elements
}

fn elements(&self) -> &Vec<F> {
/// Create a new matrix from a slice of elements.
pub const fn new_from_known(elements: [Fq; N_ELEMENTS]) -> Self {
Self(Matrix::new_from_known(elements))
}
}

impl<const N_ROWS: usize, const N_COLS: usize, const N_ELEMENTS: usize> MatrixOperations
for ArcMatrix<N_ROWS, N_COLS, N_ELEMENTS>
{
fn new(elements: &[Fq]) -> Self {
Self(Matrix::new(elements))
}

fn elements(&self) -> &[Fq] {
self.0.elements()
}

fn get_element(&self, i: usize, j: usize) -> F {
fn get_element(&self, i: usize, j: usize) -> Fq {
self.0.get_element(i, j)
}

fn set_element(&mut self, i: usize, j: usize, val: F) {
fn set_element(&mut self, i: usize, j: usize, val: Fq) {
self.0.set_element(i, j, val)
}

fn rows(&self) -> Vec<&[F]> {
self.0.rows()
}

fn n_rows(&self) -> usize {
self.0.n_rows()
}
Expand All @@ -39,34 +54,14 @@ impl<F: PrimeField> MatrixOperations<F> for ArcMatrix<F> {
self.0.n_cols()
}

fn transpose(&self) -> Self {
Self(self.0.transpose())
}

fn hadamard_product(&self, rhs: &Self) -> Result<Self>
fn hadamard_product(&self, rhs: &Self) -> Result<Self, PoseidonParameterError>
where
Self: Sized,
{
Ok(Self(self.0.hadamard_product(&rhs.0)?))
}
}

impl<F: PrimeField> From<ArcMatrix<F>> for Vec<Vec<F>> {
fn from(arc: ArcMatrix<F>) -> Self {
let mut rows = Vec::<Vec<F>>::new();
let m = &arc.0;

for i in 0..arc.n_rows() {
let mut row = Vec::new();
for j in 0..arc.n_cols() {
row.push(m.get_element(i, j));
}
rows.push(row);
}
rows
}
}

/// Represents an optimized matrix of round constants.
///
/// This modifies the partial rounds in the middle of the permutation,
Expand All @@ -76,30 +71,43 @@ impl<F: PrimeField> From<ArcMatrix<F>> for Vec<Vec<F>> {
/// This method follows `calc_equivalent_constants` from Appendix B's
/// `poseidonperm_x3_64_24_optimized.sage`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OptimizedArcMatrix<F: PrimeField>(pub ArcMatrix<F>);
pub struct OptimizedArcMatrix<const N_ROWS: usize, const N_COLS: usize, const N_ELEMENTS: usize>(
pub ArcMatrix<N_ROWS, N_COLS, N_ELEMENTS>,
);

impl<const N_ROWS: usize, const N_COLS: usize, const N_ELEMENTS: usize>
OptimizedArcMatrix<N_ROWS, N_COLS, N_ELEMENTS>
{
pub fn transpose(&self) -> OptimizedArcMatrix<N_COLS, N_ROWS, N_ELEMENTS> {
OptimizedArcMatrix(self.0.transpose())
}

/// Create a new matrix from a slice of elements.
pub const fn new_from_known(elements: [Fq; N_ELEMENTS]) -> Self {
Self(ArcMatrix::new_from_known(elements))
}
}

impl<F: PrimeField> MatrixOperations<F> for OptimizedArcMatrix<F> {
impl<const N_ROWS: usize, const N_COLS: usize, const N_ELEMENTS: usize> MatrixOperations
for OptimizedArcMatrix<N_ROWS, N_COLS, N_ELEMENTS>
{
/// Create a `OptimizedArcMatrix` from its elements.
fn new(n_rows: usize, n_cols: usize, elements: Vec<F>) -> Self {
Self(ArcMatrix::new(n_rows, n_cols, elements))
fn new(elements: &[Fq]) -> Self {
Self(ArcMatrix::new(elements))
}

fn elements(&self) -> &Vec<F> {
fn elements(&self) -> &[Fq] {
self.0.elements()
}

fn get_element(&self, i: usize, j: usize) -> F {
fn get_element(&self, i: usize, j: usize) -> Fq {
self.0.get_element(i, j)
}

fn set_element(&mut self, i: usize, j: usize, val: F) {
fn set_element(&mut self, i: usize, j: usize, val: Fq) {
self.0.set_element(i, j, val)
}

fn rows(&self) -> Vec<&[F]> {
self.0.rows()
}

fn n_rows(&self) -> usize {
self.0.n_rows()
}
Expand All @@ -108,11 +116,7 @@ impl<F: PrimeField> MatrixOperations<F> for OptimizedArcMatrix<F> {
self.0.n_cols()
}

fn transpose(&self) -> Self {
Self(self.0.transpose())
}

fn hadamard_product(&self, rhs: &Self) -> Result<Self>
fn hadamard_product(&self, rhs: &Self) -> Result<Self, PoseidonParameterError>
where
Self: Sized,
{
Expand Down
16 changes: 16 additions & 0 deletions poseidon-parameters/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[derive(Debug)]
pub enum PoseidonParameterError {
InvalidMatrixDimensions,
NoMatrixInverse,
}

impl core::fmt::Display for PoseidonParameterError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let msg = match self {
Self::InvalidMatrixDimensions => "Invalid matrix dimensions",
Self::NoMatrixInverse => "No matrix inverse",
};

msg.fmt(f)
}
}
4 changes: 1 addition & 3 deletions poseidon-parameters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

mod alpha;
mod arc_matrix;
mod error;
mod matrix;
mod matrix_ops;
mod mds_matrix;
mod round_numbers;

#[cfg(test)]
mod tests;

/// Structures related to Poseidon version 1 parameters.
pub mod v1;

Expand Down
Loading
Loading