Skip to content

feat(crypto): Upgrade all usage of crypto/sha256 to minio/sha256-simd#2909

Closed
itsdevbear wants to merge 1 commit intocometbft:mainfrom
berachain:upgrade-sha256
Closed

feat(crypto): Upgrade all usage of crypto/sha256 to minio/sha256-simd#2909
itsdevbear wants to merge 1 commit intocometbft:mainfrom
berachain:upgrade-sha256

Conversation

@itsdevbear
Copy link

@itsdevbear itsdevbear commented Apr 27, 2024

Free performance boost, used in hundreds of production projects.


PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

@itsdevbear itsdevbear requested a review from a team as a code owner April 27, 2024 03:29
@itsdevbear itsdevbear requested a review from a team April 27, 2024 03:29
@itsdevbear itsdevbear changed the title feat(crypto): Upgrae all usage of crypto/sha256 to simd-256 feat(crypto): Upgrae all usage of crypto/sha256 to minio/sha256-simd Apr 27, 2024
@adizere adizere added this to the 2024-Q2 milestone Apr 29, 2024
@sergio-mena
Copy link
Collaborator

Thanks for this @itsdevbear. We will shortly be running large scale tests with (and without) this PR. If we see visible performance gain, we'll adopt this.

@itsdevbear
Copy link
Author

Will be CPU architecture dependant, ensure test environment is setup correctly

@andynog andynog changed the title feat(crypto): Upgrae all usage of crypto/sha256 to minio/sha256-simd feat(crypto): Upgrade all usage of crypto/sha256 to minio/sha256-simd Apr 29, 2024
@andynog andynog added the crypto Cryptography-related label Apr 29, 2024
@andynog
Copy link
Collaborator

andynog commented Apr 29, 2024

Thanks @itsdevbear.

@sergio-mena do you know if we have a mixed environment in our tests ? Do we test on Intel and ARM ?

Looks like the benefits are dependent on message size according to the repo. Might be also interesting to see if this could be tested with different input sizes.

@andynog
Copy link
Collaborator

andynog commented Apr 29, 2024

One concern that I have though is that this library seems to be a bit stale, last commit was February 2023. Maybe it's stable enough. But since we're replacing it everywhere where a Sha256 is needed, changing it on hash, merkletree, keys, etc,

It is critical that this library is vetted. I understand this is used in production in many projects, but the concern is if there's a vulnerability how fast it can be addressed as opposed to using the official one that would be patched possibly faster.

@sergio-mena
Copy link
Collaborator

sergio-mena commented May 1, 2024

We are in the process of having an expert in golang crypto libraries vet this library change.

@itsdevbear
Copy link
Author

itsdevbear commented May 6, 2024

I can also hide behind an optional build flag if desired.

@itsdevbear
Copy link
Author

@andynog yeah intel and arm, but notably will see the greatest improvement on cpus with AVX-512 support

@itsdevbear
Copy link
Author

This being said, we want to optimize some places in the code to use the AVX-512 scheduler directly. But that's a bigger change

@cason
Copy link

cason commented May 8, 2024

This package was added by #2765. Not sure why they decided to use this precise implementation.

@cason
Copy link

cason commented May 8, 2024

Overall the code now is kind of confusing with the use of:

  • crypto/sha256: standard lib
  • minio/sha256-simd: possibly improved lib
  • crypto/tmhash: our own lib, which is a wrapper to crypto/sha256

We might consider simplifying this setup.

@itsdevbear
Copy link
Author

Personally, I like having everywhere in the codebase important from tmhash where tmhash is an alias it whatever library we like. This alias could be changed via build flags for instance.

many to one to few relationship

@cason
Copy link

cason commented May 8, 2024

Free performance boost, used in hundreds of production projects.

Can you add a Go test/benchmark so we can evaluate this boost?

@sergio-mena
Copy link
Collaborator

sergio-mena commented May 13, 2024

We discussed about this in our internal meeting today. These are the conclusions:

  1. We currently don't have performance data that shows an obvious gain in the production setups where CometBFT is used
  2. We could gather such performance data ourselves as a due-diligence activity, but we don't have cycles ATM
  3. There is an ongoing longer term solution, described in docs(ADR): #113 Modular transaction hashing #2241, whereby an app can have CometBFT use custom hashing logic.
    • This longer term solution will render our efforts in point 2 unnecessary

Therefore, we will:

@cason
Copy link

cason commented May 13, 2024

Use this issue to move all imports of minio/sha256-simd (BLS-related code) to crypto/sha256

Opened #3071.

@faddat
Copy link
Contributor

faddat commented May 13, 2024

We currently don't have performance data that shows an obvious gain in the production setups where CometBFT is used

IDK how much realworldish gain there is, but:

Support for Intel SHA Extensions

hehe this would make it a lot faster on my homebrew routers which use the J3455 cpu

Support for AVX512

That's basically "every validator"

Agree with @andynog's concern about maintenance though, that is an easy way to get burned. PR into crypto/sha256? The performance gains are pretty undeniable, and while the SHA extensions are (bizarrely) on just one intel cpu + AMD ryzen, a lot of validators use Ryzen.

@melekes
Copy link
Collaborator

melekes commented May 14, 2024

IDK how much realworldish gain there is, but:

That's the culprit of this PR 😅

@faddat
Copy link
Contributor

faddat commented May 14, 2024

https://twitter.com/phoronix/status/1790016231614849318

What scenario would allow us to see real world results here?

@github-actions
Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale For use by stalebot label May 25, 2024
@hvanz hvanz removed the stale For use by stalebot label May 27, 2024
@melekes
Copy link
Collaborator

melekes commented Jun 5, 2024

Closing as per #2909 (comment).

@melekes melekes closed this Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crypto Cryptography-related

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

8 participants