Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Traits for Default Poseidon Parameters + Poseidon cleanup #22

Merged
merged 23 commits into from
Jul 15, 2021

Conversation

weikengchen
Copy link
Member

@weikengchen weikengchen commented Jul 6, 2021

Description

This PR is from a branch named add-rescue, although this PR did not add Rescue.

It consists of two parts:

  1. Cleanup of the Poseidon implementation to better structure the Parameters and the Sponge struct.
  2. Add necessary traits and derivations for default Poseidon parameters for each field, which includes Grain LFSR and an aligned parameter generation for Poseidon.

This marks the first step toward the handling of Poseidon parameter generation.

More changes may show up in separate PRs. This one is long enough and should be concluded.


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (master)
  • Linked to Github issue with discussion and accepted design OR have an explanation in the PR that describes this work.
  • Wrote unit tests
  • Updated relevant documentation in the code
  • Added a relevant changelog entry to the Pending section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer

@weikengchen weikengchen changed the title Trais for Default Poseidon Parameters + Poseidon cleanup Traits for Default Poseidon Parameters + Poseidon cleanup Jul 6, 2021
@weikengchen
Copy link
Member Author

weikengchen commented Jul 6, 2021

Note that ideally, the interface should use optimization_goal instead of optimized_for_constraints: bool. This is intentionally avoided because OptimizationGoal is inside R1CS and requires "std", so the implementation can be "no-std".

@weikengchen weikengchen requested a review from tsunrise July 6, 2021 09:58
@weikengchen
Copy link
Member Author

I will make more cleanup to this repo---the reference implementation puts the capacity at the beginning, while we put it at the end. Since it is better to avoid such differences, I will make a PR to change so, as well as adding some test numbers.

Copy link
Member

@tsunrise tsunrise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! This PR is quite useful. Left a few comments

src/poseidon/traits.rs Outdated Show resolved Hide resolved
src/poseidon/traits.rs Outdated Show resolved Hide resolved
src/poseidon/traits.rs Show resolved Hide resolved
src/poseidon/grain_lfsr.rs Outdated Show resolved Hide resolved
src/poseidon/grain_lfsr.rs Outdated Show resolved Hide resolved
src/poseidon/mod.rs Outdated Show resolved Hide resolved
src/poseidon/mod.rs Outdated Show resolved Hide resolved
src/poseidon/mod.rs Outdated Show resolved Hide resolved
src/poseidon/traits.rs Outdated Show resolved Hide resolved
Comment on lines +142 to +163
impl Fp256Parameters for TestFrParameters {}
impl FftParameters for TestFrParameters {
type BigInt = <FrParameters as FftParameters>::BigInt;
const TWO_ADICITY: u32 = FrParameters::TWO_ADICITY;
const TWO_ADIC_ROOT_OF_UNITY: Self::BigInt = FrParameters::TWO_ADIC_ROOT_OF_UNITY;
}

// This TestFrParameters is the same as the BLS12-381's Fr.
// MODULUS = 52435875175126190479447740508185965837690552500527637822603658699938581184513
impl FpParameters for TestFrParameters {
const MODULUS: BigInteger256 = FrParameters::MODULUS;
const MODULUS_BITS: u32 = FrParameters::MODULUS_BITS;
const CAPACITY: u32 = FrParameters::CAPACITY;
const REPR_SHAVE_BITS: u32 = FrParameters::REPR_SHAVE_BITS;
const R: BigInteger256 = FrParameters::R;
const R2: BigInteger256 = FrParameters::R2;
const INV: u64 = FrParameters::INV;
const GENERATOR: BigInteger256 = FrParameters::GENERATOR;
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger256 = FrParameters::MODULUS_MINUS_ONE_DIV_TWO;
const T: BigInteger256 = FrParameters::T;
const T_MINUS_ONE_DIV_TWO: BigInteger256 = FrParameters::T_MINUS_ONE_DIV_TWO;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just set TestFrParameters to be bls12_381::FrParameters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this TestFrParameters would be slightly different in that it has:
impl PoseidonDefaultParameters for TestFrParameters

and I cannot add this to the existing struct bls12_381::FrParameters that is in another crate (as this is the test mod).

}

/// A field with Poseidon parameters associated
pub trait PoseidonDefaultParametersField: PrimeField {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm the issue with this design is that we can only add an impl for PoseidonDefaultParametersField in two places: the ark-curves crate corresponding to that field, or in this crate. Do you think there's a better way to do this?

Also, what does it mean for Parameters to be "default" here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider this for a future PR. This PR probably will just provide a "default parameter retriever".

I think we need to make sure that a user can freely choose a different set of Poseidon parameters for, e.g., Marlin.

@Pratyush
Copy link
Member

Pratyush commented Jul 7, 2021

Thanks for the PR @weikengchen

It might also be worth it to look at zcash/orchard#41 (or the more recent version of the code here) to see if we can port anything from their design (and if there are any incompatibilities)

@weikengchen
Copy link
Member Author

weikengchen commented Jul 7, 2021

The PR is ready for a follow-up review. And I would consider this to be the preliminary API which we will make further improvements over.

For example, we may need more optimization goals, as there can be a "balanced" version which takes into account that (1) one may use this same sponge in both Groth16 and Marlin, (2) Marlin has a different metrics, and (3) Groth16 is faster than Marlin.

@weikengchen weikengchen merged commit 55401d5 into master Jul 15, 2021
@weikengchen weikengchen deleted the add-rescue branch July 15, 2021 06:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Poseidon default parameter
3 participants