-
Notifications
You must be signed in to change notification settings - Fork 13
Poseidon hash function #1777
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
Open
OlivierBBB
wants to merge
1
commit into
main
Choose a base branch
from
1744-poseidon-hash-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Poseidon hash function #1777
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Generating data | ||
|
|
||
| The Poseidon hash function requires two pieces of data besides the (nonempty) input byte slice: | ||
|
|
||
| - the mds matrix of size t×t | ||
| - the round constants a matrix of size r×t | ||
| where t ≡ state width t and r ≡ total rounds. | ||
|
|
||
| This data can be generated using the [poseidon-tools](https://github.com/khovratovich/poseidon-tools) library. | ||
|
|
||
| ## Generating an mds matrix | ||
|
|
||
| The following generates the MDS matrix for the koalabear prime with state width 16. | ||
|
|
||
| ```bash | ||
| python3 | ||
| >>> from poseidon.mds_matrix import generate_mds_matrix | ||
| >>> KOALABEAR_P = 2130706433 | ||
| >>> STATE_WIDTH = 16 | ||
| >>> [[f"{k:08x}" for k in l] for l in generate_mds_matrix(STATE_WIDTH, KOALABEAR_P)] | ||
| ``` | ||
|
|
||
| ## Getting round constants | ||
|
|
||
| For the round constants I used `_KB_ROUND_CONSTANTS_16` from the tests of that repo. | ||
|
|
||
| ## Stuff | ||
|
|
||
| ```bash | ||
| python3 | ||
| >>> from poseidon.poseidon import Poseidon | ||
| >>> KOALABEAR_P = 2130706433 | ||
| >>> pos = Poseidon(prime=KOALABEAR_P, alpha=3, t=16, r_f=8, r_p=20) | ||
| # t is the state_width, no rate is specified (it defaults to t-1), r_f is the number of full rounds (which has to be | ||
| # even), r_p that of partial rounds | ||
| >>> pos.sponge_hash(list(range(16)), 1) | ||
| [584229223] | ||
| >>> pos.sponge_hash(list(range(16)), 15) | ||
| [584229223, 1225903167, 435734976, 745693090, 1580884015, 1393870516, 1514786559, 1416327482, 401740899, 305698337, 123847430, 1985271412, 660999169, 1953826170, 1390527262] | ||
| # 1 and 15 are the respectve output_size's, which have to be ≤ rate | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // STATE_WIDTH × STATE_WIDTH matrix | ||
| // M_{a,b} = Mij with ij = 16 × a + b | ||
| static mds_matrix(ij:u8) -> (Mij:𝔽) { | ||
| 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, 0x6fc28f5d, 0x5ccec4ed, 0x41da12f7, 0x7164924a, 0x5bf72c24, 0x47f77778, 0x4dd6b5ae, | ||
| 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, 0x6fc28f5d, 0x5ccec4ed, 0x41da12f7, 0x7164924a, 0x5bf72c24, 0x47f77778, | ||
| 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, 0x6fc28f5d, 0x5ccec4ed, 0x41da12f7, 0x7164924a, 0x5bf72c24, | ||
| 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, 0x6fc28f5d, 0x5ccec4ed, 0x41da12f7, 0x7164924a, | ||
| 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, 0x6fc28f5d, 0x5ccec4ed, 0x41da12f7, | ||
| 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, 0x6fc28f5d, 0x5ccec4ed, | ||
| 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, 0x6fc28f5d, | ||
| 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, 0x59f55556, | ||
| 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, 0x2c2c8591, | ||
| 0x48924925, 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, 0x1cdd1746, | ||
| 0x69d55556, 0x48924925, 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, 0x42861862, | ||
| 0x32cccccd, 0x69d55556, 0x48924925, 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, 0x6bf33334, | ||
| 0x1fc00000, 0x32cccccd, 0x69d55556, 0x48924925, 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, 0x281af287, | ||
| 0x54aaaaab, 0x1fc00000, 0x32cccccd, 0x69d55556, 0x48924925, 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, 0x23471c72, | ||
| 0x3f800000, 0x54aaaaab, 0x1fc00000, 0x32cccccd, 0x69d55556, 0x48924925, 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000, 0x0ef0f0f1, | ||
| 0x7f000000, 0x3f800000, 0x54aaaaab, 0x1fc00000, 0x32cccccd, 0x69d55556, 0x48924925, 0x0fe00000, 0x468e38e4, 0x58e66667, 0x39ba2e8c, 0x34eaaaab, 0x3a9d89d9, 0x63c92493, 0x10eeeeef, 0x07f00000 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.