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

Poseidon 2 #28

Merged
merged 28 commits into from
Jun 6, 2024
Merged

Poseidon 2 #28

merged 28 commits into from
Jun 6, 2024

Conversation

mpenciak
Copy link
Contributor

@mpenciak mpenciak commented May 20, 2024

Poseidon 2 chip for Loam.

Generic in width using hybrid-array

The structure of the API around the chip is in a good spot I think.
The tests don't currently pass, I'm debugging!

Please take a look to make sure everything is as one would expect from the precompile

Note 1:

In order for this to work I had to fork Plonky3 and make a couple small changes

  1. (uncontroversial) Add a few extra supported arities.
  2. (potentially controversial) Change the visibility of the Permutation trait so I could implement a new version to support fixed round constants, and not those generated by the rng in Plonky3.

Note 2:

I added the AlignedBorrow in loam-macros pretty early on in the process. I don't think it's strictly necessary anymore but it could help in other spots...

Note 3:

I'm not sure what to do about the sage script... It doesn't make much sense to include it in the repository, but I also don't know if it's good practice for people to take us on our word that we generated the constants correctly.

@adr1anh
Copy link
Contributor

adr1anh commented May 30, 2024

  • Made some suggestions to make it more "Air-like" and avoid allocations in the eval function at ah/poseidon-air.
  • I think you were missing the initial linear layer.
  • We should find a way to return the constants without allocating a vector.
  • The config could have default methods for returning the ranges of the different rounds. fn external_first_range() -> Range<usize> etc.

Copy link
Contributor

@adr1anh adr1anh left a comment

Choose a reason for hiding this comment

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

Comments about Air

  • It's common for the eval function to define all the flags and bools at the start, so it's clear when later constraints are being used.
  • The is_internal and is_external can be derived from the sum of the rounds flags.
  • The application of the constants to the rounds cannot have an if. Instead, you need to conditionally select the constants using local.rounds, and use another selector to conditionally add the constants (is_external, is_init, is_internal)

src/poseidon/air.rs Outdated Show resolved Hide resolved
src/poseidon/air.rs Outdated Show resolved Hide resolved
src/poseidon/config.rs Outdated Show resolved Hide resolved
src/poseidon/mod.rs Show resolved Hide resolved
src/poseidon/config.rs Show resolved Hide resolved
src/poseidon/trace.rs Outdated Show resolved Hide resolved
src/poseidon/config.rs Outdated Show resolved Hide resolved
src/poseidon/columns.rs Outdated Show resolved Hide resolved
src/poseidon/air.rs Outdated Show resolved Hide resolved
src/poseidon/constants.rs Outdated Show resolved Hide resolved
src/poseidon/air.rs Outdated Show resolved Hide resolved
src/poseidon/eval.rs Outdated Show resolved Hide resolved
src/poseidon/util.rs Show resolved Hide resolved
src/poseidon/util.rs Outdated Show resolved Hide resolved
src/poseidon/util.rs Outdated Show resolved Hide resolved
@mpenciak mpenciak marked this pull request as ready for review May 31, 2024 23:41
Copy link
Member

@arthurpaulino arthurpaulino left a comment

Choose a reason for hiding this comment

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

Great work 🎉
Left some higher level comments related to maintenance

Cargo.toml Outdated Show resolved Hide resolved
loam-macros/src/lib.rs Outdated Show resolved Hide resolved
poseidon2_rust_params.sage Outdated Show resolved Hide resolved
src/poseidon/trace.rs Outdated Show resolved Hide resolved
src/poseidon/util.rs Outdated Show resolved Hide resolved
loam-macros/Cargo.toml Outdated Show resolved Hide resolved
@mpenciak
Copy link
Contributor Author

mpenciak commented Jun 3, 2024

Updates: I applied the suggests in the comments, and fixed a small error in the internal matrix action to fix the tests.

The only couple outstanding issues to address before I think this is ready to approve:

  1. What to do with the sage script? I don't think it really fits in this repo, but we could add a public lurk-lab gist with the script so people can verify/audit it and link to it in the constants.rs file.

  2. What to do about the mp/poseidon_fix branch on lurk-lab/Plonky3? I could create a PR and merge into our Plonky3 repo, but I don't know if that adds a lot of friction to keeping our version of Plonky3 in sync with the upstream one.

Copy link
Member

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

Swooping in to block on the personal fork branch.

Cargo.toml Outdated Show resolved Hide resolved
Copy link
Member

@wwared wwared left a comment

Choose a reason for hiding this comment

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

This is great! I have a tiny suggestion below (feel free to ignore). Regarding the other two points mentioned, here's my two cents:

  1. I think the .sage script would not necessarily be out of place in this repo in some additional folder in the repo (assets/ or scripts/ or even docs/) and would vote for that, but I think the public-gist solution is also perfectly acceptable.
  2. We definitely want the changes of mp/poseidon_fix to at least be merged into our fork, they are very minor and are unlikely to be a maintenance burden. I think upstream would be open to them as well, but we can do that later.

src/poseidon/config.rs Show resolved Hide resolved
src/poseidon/util.rs Outdated Show resolved Hide resolved
@mpenciak mpenciak force-pushed the mp/poseidon branch 3 times, most recently from 427a506 to e206c61 Compare June 5, 2024 17:42
Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated
@@ -51,9 +69,11 @@ p3-maybe-rayon = { workspace = true }
p3-poseidon2 = { workspace = true }
p3-symmetric = { workspace = true }
p3-util = { workspace = true }
hybrid-array = "0.2.0-rc"
Copy link
Member

Choose a reason for hiding this comment

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

Please use workspace dependencies. To do this, you can use the cargo autoinherit tool.

Copy link
Contributor

@adr1anh adr1anh left a comment

Choose a reason for hiding this comment

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

Leaving some comments to be converted to issues

src/poseidon/config.rs Outdated Show resolved Hide resolved
src/poseidon/constants.rs Outdated Show resolved Hide resolved
src/poseidon/constants.rs Outdated Show resolved Hide resolved
mod util;

/// A chip that implements addition for the Poseidon2 permutation
pub struct Poseidon2Chip<C>
Copy link
Contributor

Choose a reason for hiding this comment

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

![derive(Default)]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because C: PoseidonConfig doesn't have a blanket Default implementation (and it shouldn't, because the round constants need to be provided by hand) this doesn't work and I had to do it by hand.

huitseeker
huitseeker previously approved these changes Jun 5, 2024
Copy link
Member

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

Good for me!

wwared
wwared previously approved these changes Jun 6, 2024
Copy link
Member

@wwared wwared left a comment

Choose a reason for hiding this comment

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

Looks good! Needs a final rebase/conflict resolve before merge

@mpenciak
Copy link
Contributor Author

mpenciak commented Jun 6, 2024

Rebased!

@mpenciak mpenciak merged commit b307aca into main Jun 6, 2024
2 checks passed
@mpenciak mpenciak deleted the mp/poseidon branch June 6, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants