Skip to content

Commit

Permalink
Implement serde for PoseidonConstants (#165)
Browse files Browse the repository at this point in the history
* (WIP) Implement serde for PoseidonConstants

* Compress struct field names, format

* Formatting, clippy

* Add serde hashing tests

* Remove pasta patch

* Remove comments
  • Loading branch information
samuelburnham authored and storojs72 committed Jan 12, 2023
1 parent 30ca8b0 commit deb0ea5
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ ff = "0.12.0"
generic-array = "0.14.4"
itertools = { version = "0.8.0" }
log = "0.4.8"
#<<<<<<< HEAD
halo2_proofs = "0.2.0"
rust-gpu-tools = { version = "0.5.0", default-features = false, optional = true }
pasta_curves = { version = "0.5.1", optional = true, package = "fil_pasta_curves" }
#pasta_curves = { version = "0.5.1", optional = true, package = "fil_pasta_curves" }
#=======
pasta_curves = { version = "0.5.1", features = ["serde"], package = "fil_pasta_curves" }
#>>>>>>> 640d350 (Implement serde for PoseidonConstants (#165))
trait-set = "0.3.0"
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
blstrs = "0.6.1"
Expand Down
10 changes: 10 additions & 0 deletions src/halo2_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ where
let width = A::to_usize() + 1;
consts
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(round * width)
.take(width)
Expand Down Expand Up @@ -415,6 +417,8 @@ where

let round_consts = consts
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(round * width)
.take(width);
Expand Down Expand Up @@ -459,6 +463,8 @@ where

let round_consts = consts
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(round * width)
.take(width);
Expand Down Expand Up @@ -528,12 +534,16 @@ where

let round_consts_a = consts
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(round_a * width)
.take(width);

let round_consts_b = consts
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(round_b * width)
.take(width);
Expand Down
6 changes: 4 additions & 2 deletions src/hash_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
/// may still express the full range of hash function types.
use crate::{Arity, Strength};
use ff::PrimeField;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum HashType<F: PrimeField, A: Arity<F>> {
MerkleTree,
MerkleTreeSparse(u64),
VariableLength,
ConstantLength(usize),
Encryption,
#[serde(skip)]
Custom(CType<F, A>),
Sponge,
}
Expand Down Expand Up @@ -64,7 +66,7 @@ impl<F: PrimeField, A: Arity<F>> HashType<F, A> {
}
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum CType<F: PrimeField, A: Arity<F>> {
Arbitrary(u64),
_Phantom((F, A)),
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use blstrs::Scalar as Fr;
pub use error::Error;
use ff::PrimeField;
use generic_array::GenericArray;
use serde::{Deserialize, Serialize};
use std::fmt;
use trait_set::trait_set;

Expand Down Expand Up @@ -95,11 +96,13 @@ trait_set! {
pub trait NeptuneField = PrimeField + ec_gpu::GpuName;
}

mod serde_impl;

pub(crate) const TEST_SEED: [u8; 16] = [
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5,
];

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Strength {
Standard,
Strengthened,
Expand Down
5 changes: 3 additions & 2 deletions src/mds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#![allow(clippy::ptr_arg)]

use ff::PrimeField;
use serde::{Deserialize, Serialize};

use crate::matrix;
use crate::matrix::{
apply_matrix, invert, is_identity, is_invertible, is_square, mat_mul, minor, transpose, Matrix,
};

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MdsMatrices<F: PrimeField> {
pub m: Matrix<F>,
pub m_inv: Matrix<F>,
Expand Down Expand Up @@ -44,7 +45,7 @@ pub fn derive_mds_matrices<F: PrimeField>(m: Matrix<F>) -> MdsMatrices<F> {
/// This means its first row and column are each dense, and the interior matrix
/// (minor to the element in both the row and column) is the identity.
/// We will pluralize this compact structure `sparse_matrixes` to distinguish from `sparse_matrices` from which they are created.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SparseMatrix<F: PrimeField> {
/// `w_hat` is the first column of the M'' matrix. It will be directly multiplied (scalar product) with a row of state elements.
pub w_hat: Vec<F>,
Expand Down
7 changes: 4 additions & 3 deletions src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{matrix, quintic_s_box, BatchHasher, Strength, DEFAULT_STRENGTH};
use crate::{round_constants, round_numbers, Error};
use ff::PrimeField;
use generic_array::{sequence::GenericSequence, typenum, ArrayLength, GenericArray};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use typenum::marker_traits::Unsigned;
use typenum::*;
Expand Down Expand Up @@ -105,7 +106,7 @@ where
A: Arity<F>,
{
pub mds_matrices: MdsMatrices<F>,
pub round_constants: Vec<F>,
pub round_constants: Option<Vec<F>>,
pub compressed_round_constants: Vec<F>,
pub pre_sparse_matrix: Matrix<F>,
pub sparse_matrixes: Vec<SparseMatrix<F>>,
Expand All @@ -117,7 +118,7 @@ where
pub half_full_rounds: usize,
pub partial_rounds: usize,
pub hash_type: HashType<F, A>,
_a: PhantomData<A>,
pub(crate) _a: PhantomData<A>,
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -206,7 +207,7 @@ where

Self {
mds_matrices,
round_constants,
round_constants: Some(round_constants),
compressed_round_constants,
pre_sparse_matrix,
sparse_matrixes,
Expand Down
19 changes: 14 additions & 5 deletions src/poseidon_alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ where
let pre_round_keys = p
.constants
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(p.constants_offset)
.map(Some);
Expand Down Expand Up @@ -132,6 +134,8 @@ pub fn full_round_dynamic<F, A>(
let pre_round_keys = p
.constants
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(p.constants_offset)
.map(|x| {
Expand All @@ -148,6 +152,8 @@ pub fn full_round_dynamic<F, A>(
let post_vec = p
.constants
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(
p.constants_offset
Expand Down Expand Up @@ -227,11 +233,14 @@ where
F: PrimeField,
A: Arity<F>,
{
for (element, round_constant) in p
.elements
.iter_mut()
.zip(p.constants.round_constants.iter().skip(p.constants_offset))
{
for (element, round_constant) in p.elements.iter_mut().zip(
p.constants
.round_constants
.as_ref()
.unwrap()
.iter()
.skip(p.constants_offset),
) {
element.add_assign(round_constant);
}

Expand Down
Loading

0 comments on commit deb0ea5

Please sign in to comment.