From 71bc59bf802c6c7e5d6849c55fa964f48e15893a Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Wed, 3 Jul 2024 15:13:16 -0400 Subject: [PATCH] fix: `poseidon-consistency` crate with benchmarks --- Cargo.toml | 2 +- poseidon-consistency/Cargo.toml | 6 ++- poseidon-consistency/benches/permutation.rs | 16 +++--- poseidon-consistency/src/lib.rs | 55 +++++++++++---------- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d3a0085..edbd602 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = [ "poseidon377", # "poseidon-paramgen" # Temporarily excluded until refactored "poseidon-permutation", - # "poseidon-consistency", # Temporarily excluded until paramgen is refactored + "poseidon-consistency", "poseidon-parameters", "poseidon-tests", ] diff --git a/poseidon-consistency/Cargo.toml b/poseidon-consistency/Cargo.toml index a55fca3..b8b0e52 100644 --- a/poseidon-consistency/Cargo.toml +++ b/poseidon-consistency/Cargo.toml @@ -10,6 +10,7 @@ repository = "https://github.com/penumbra-zone/poseidon377" [dependencies] poseidon-parameters = { path = "../poseidon-parameters", default-features = false } #poseidon-paramgen = { path = "../poseidon-paramgen", default-features = false } +poseidon377 = { path = "../poseidon377", default-features = false } poseidon-permutation = { path="../poseidon-permutation", default-features = false } decaf377 = { version="0.10.1", default-features = false } @@ -30,7 +31,8 @@ harness = false [features] default = ["std"] std = [ - "poseidon-paramgen/std", "poseidon-permutation/std", ] - +arkworks = [ + "decaf377/arkworks", +] diff --git a/poseidon-consistency/benches/permutation.rs b/poseidon-consistency/benches/permutation.rs index a667589..12a8e9d 100644 --- a/poseidon-consistency/benches/permutation.rs +++ b/poseidon-consistency/benches/permutation.rs @@ -1,23 +1,19 @@ -use ark_ed_on_bls12_377::{Fq, FqConfig}; -use ark_ff::{MontConfig, PrimeField}; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; -use once_cell::sync::Lazy; +use decaf377::Fq; use rand_chacha::ChaChaRng; use rand_core::{RngCore, SeedableRng}; +use poseidon377::RATE_4_PARAMS; use poseidon_permutation::Instance; -static PARAMS_4_TO_1: Lazy> = - Lazy::new(|| poseidon_paramgen::v1::generate(128, 5, FqConfig::MODULUS, true)); - fn hash_4_1_our_impl(i: &Fq, j: &Fq, k: &Fq, l: &Fq, m: &Fq) -> Fq { - let mut our_instance = Instance::new(&PARAMS_4_TO_1); - our_instance.n_to_1_fixed_hash(vec![*i, *j, *k, *l, *m]) + let mut our_instance = Instance::new(&RATE_4_PARAMS); + our_instance.n_to_1_fixed_hash(&[*i, *j, *k, *l, *m]) } fn hash_4_1_our_impl_unoptimized(i: &Fq, j: &Fq, k: &Fq, l: &Fq, m: &Fq) -> Fq { - let mut our_instance = Instance::new(&PARAMS_4_TO_1); - our_instance.unoptimized_n_to_1_fixed_hash(vec![*i, *j, *k, *l, *m]) + let mut our_instance = Instance::new(&RATE_4_PARAMS); + our_instance.unoptimized_n_to_1_fixed_hash([*i, *j, *k, *l, *m]) } pub fn bench_unoptimized_vs_optimized(c: &mut Criterion) { diff --git a/poseidon-consistency/src/lib.rs b/poseidon-consistency/src/lib.rs index bf07519..eab4ac9 100644 --- a/poseidon-consistency/src/lib.rs +++ b/poseidon-consistency/src/lib.rs @@ -1,31 +1,37 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(test)] +#[cfg(all(test, feature = "arkworks"))] mod tests { - use ark_ed_on_bls12_377::{Fq, FqConfig}; - use ark_ff::{MontConfig, PrimeField, Zero}; - use poseidon_paramgen::v1::generate; + use ark_ed_on_bls12_377::Fq as ArkFq; + use ark_ff::BigInteger256; + + use decaf377::Fq; + use poseidon377::{RATE_2_PARAMS, RATE_4_PARAMS}; use poseidon_permutation::Instance; use proptest::prelude::*; + pub(crate) fn from_ark_fq(x: ArkFq) -> Fq { + BigInteger256::from(x).into() + } + #[test] fn check_optimized_impl_vs_sage() { - let params_2_to_1 = generate(128, 3, FqConfig::MODULUS, true); + let params_2_to_1 = RATE_2_PARAMS; let mut our_instance = Instance::new(¶ms_2_to_1); let hash_output = - our_instance.n_to_1_fixed_hash(vec![Fq::zero(), Fq::from(1u64), Fq::from(2u64)]); + our_instance.n_to_1_fixed_hash(&[Fq::from(0u64), Fq::from(1u64), Fq::from(2u64)]); let output_words = our_instance.output_words(); assert_eq!(hash_output, output_words[1]); let expected_output_words = [ - ark_ff::MontFp!( + from_ark_fq(ark_ff::MontFp!( "6368779772888548211318735707249600947486536081021109980085678920065117075165" - ), - ark_ff::MontFp!( + )), + from_ark_fq(ark_ff::MontFp!( "546637332213889354237126997303352456465330747031466737868777261691321847212" - ), - ark_ff::MontFp!( + )), + from_ark_fq(ark_ff::MontFp!( "1488544471679348337017344865262529731114801536476862121626711131361325263279" - ), + )), ]; for (a, b) in expected_output_words.iter().zip(output_words.iter()) { assert_eq!(*a, *b); @@ -34,25 +40,25 @@ mod tests { #[test] fn check_unoptimized_impl_vs_sage() { - let params_2_to_1 = generate(128, 3, FqConfig::MODULUS, true); + let params_2_to_1 = RATE_2_PARAMS; let mut our_instance = Instance::new(¶ms_2_to_1); - let hash_output = our_instance.unoptimized_n_to_1_fixed_hash(vec![ - Fq::zero(), + let hash_output = our_instance.unoptimized_n_to_1_fixed_hash([ + Fq::from(0u64), Fq::from(1u64), Fq::from(2u64), ]); let output_words = our_instance.output_words(); assert_eq!(hash_output, output_words[1]); let expected_output_words = [ - ark_ff::MontFp!( + from_ark_fq(ark_ff::MontFp!( "6368779772888548211318735707249600947486536081021109980085678920065117075165" - ), - ark_ff::MontFp!( + )), + from_ark_fq(ark_ff::MontFp!( "546637332213889354237126997303352456465330747031466737868777261691321847212" - ), - ark_ff::MontFp!( + )), + from_ark_fq(ark_ff::MontFp!( "1488544471679348337017344865262529731114801536476862121626711131361325263279" - ), + )), ]; for (a, b) in expected_output_words.iter().zip(output_words.iter()) { assert_eq!(*a, *b); @@ -68,15 +74,14 @@ mod tests { proptest! { #[test] fn optimized_and_unoptimized_permutation_consistent(elem_1 in fq_strategy(), elem_2 in fq_strategy(), elem_3 in fq_strategy(), elem_4 in fq_strategy(), elem_5 in fq_strategy()) { - let t = 5; - let params_4_to_1 = generate(128, t, FqConfig::MODULUS, true); + let params_4_to_1 = RATE_4_PARAMS; let mut our_instance = Instance::new(¶ms_4_to_1); - let our_result = our_instance.n_to_1_fixed_hash(vec![elem_1, elem_2, elem_3, elem_4, elem_5]); + let our_result = our_instance.n_to_1_fixed_hash(&[elem_1, elem_2, elem_3, elem_4, elem_5]); let mut unoptimized_instance = Instance::new(¶ms_4_to_1); let unoptimized_result = - unoptimized_instance.unoptimized_n_to_1_fixed_hash(vec![elem_1, elem_2, elem_3, elem_4, elem_5]); + unoptimized_instance.unoptimized_n_to_1_fixed_hash([elem_1, elem_2, elem_3, elem_4, elem_5]); assert_eq!(unoptimized_result, our_result); }