Skip to content

Conversation

@supragya
Copy link
Owner

@supragya supragya commented Apr 27, 2024

Summary by CodeRabbit

  • Documentation
    • Updated documentation with insights into the Poseidon Hasher, HadesMiMC construction, and licensing terms.
    • Included scripts and reference implementations for Poseidon and Starkad, with tools for computations and bug fixes.
    • Added functions for calculating round numbers, costs, and generating parameters for the Grain stream cipher.
    • Introduced Sage scripts for generating parameters for the Poseidon hash function with security criteria.
    • Defined parameters and implementation details for Poseidon and Starkad permutation functions.
    • Included test vectors for various permutation functions and a Poseidon hash function implementation using ark_bn254 and ark_std crates.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 27, 2024

Walkthrough

The project has enriched its documentation with the Poseidon Hasher, a sophisticated hashing mechanism leveraging sponge construction and HadesMiMC. Updates include enhanced scripts and implementations for Poseidon and Starkad, addressing bugs and improving calculations, along with new tools for computations and testing.

Changes

File Change Summary
README.md Added a reference to Tho09 for the Poseidon Hasher in the documentation.
poseidon-hash/reference/hadeshash/README.md Updated scripts and reference implementations for Poseidon and Starkad, fixing bugs and calculations.
poseidon-hash/reference/hadeshash/code/ Added scripts for calculating round numbers, generating parameters for Poseidon and Grain cipher.
poseidon-hash/reference/hadeshash/code/poseidonperm_x5_254_3.sage
poseidon-hash/reference/hadeshash/code/poseidonperm_x5_255_3.sage
Implemented permutation functions using the Poseidon hash algorithm with specific parameters.
poseidon-hash/reference/hadeshash/code/starkadperm_x5_256_3.sage Introduced permutation function using the Starkad block cipher.
poseidon-hash/src/lib.rs Added functionality for Poseidon hash function implementation using ark_bn254 and ark_std crates.

🐇✨

In the realm of algorithms deep and wide,

Poseidon and Starkad dance with pride.

Hashes and ciphers, secrets untold,

Through code and math, their stories unfold.

Rabbit's wisdom in scripts is fed,

New paths revealed, where knowledge is spread.


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 54cc5ad and c88e373.
Files selected for processing (4)
  • README.md (1 hunks)
  • [Tho09]poseidon-hash/README.md (1 hunks)
  • [Tho09]poseidon-hash/reference/hadeshash/code/generate_params_poseidon.sage (1 hunks)
  • [Tho09]poseidon-hash/src/lib.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • [Tho09]poseidon-hash/src/lib.rs
Additional Context Used
LanguageTool (41)
[Tho09]poseidon-hash/README.md (41)

Near line 1: This phrase is duplicated. You should probably use “Poseidon Hasher” only once.
Context: # Poseidon Hasher Poseidon Hasher is a mapping over strings of $F_p$ (for...


Near line 2: Possible spelling mistake found.
Context: ...don Hasher is a mapping over strings of $F_p$ (for prime $p > 2^{31}$) such that i...


Near line 2: Possible spelling mistake found.
Context: ... Hasher is a mapping over strings of $F_p$ (for prime $p > 2^{31}$) such that it m...


Near line 2: Possible spelling mistake found.
Context: ...apping over strings of $F_p$ (for prime $p > 2^{31}$) such that it maps $F_p^* \to...


Near line 2: Possible spelling mistake found.
Context: ...r prime $p > 2^{31}$) such that it maps $F_p^* \to F_p^o$ where $o$ is the number ...


Near line 2: Possible spelling mistake found.
Context: ...{31}$) such that it maps $F_p^* \to F_p^o$ where $o$ is the number of output eleme...


Near line 2: Possible spelling mistake found.
Context: ...ch that it maps $F_p^* \to F_p^o$ where $o$ is the number of output elements (often...


Near line 2: Possible spelling mistake found.
Context: ... output elements (often chosen value is $o = 1$). Poseidon is said to be a varian...


Near line 2: The currency mark is usually put at the beginning of the number.
Context: ...ut elements (often chosen value is $o = 1$). Poseidon is said to be a variant of ...


Near line 4: Possible spelling mistake found.
Context: .... Poseidon is said to be a variant of HadesMiMC construction however with a fixed and ...


Near line 6: This phrase is duplicated. You should probably use “sponge construction” only once.
Context: ... a fixed and known key. ## A primer on sponge construction Sponge construction looks as follows: ![sponge_construction...


Near line 9: Possible spelling mistake found.
Context: ...](sponge_construction.png) In this, the $I$ is maintained state that changes over t...


Near line 9: Possible missing article found.
Context: ...e_construction.png) In this, the $I$ is maintained state that changes over time, $m_x$ are...


Near line 9: Possible spelling mistake found.
Context: ...aintained state that changes over time, $m_x$ are injected values to be hashed and...


Near line 9: Possible spelling mistake found.
Context: ...tained state that changes over time, $m_x$ are injected values to be hashed and $h...


Near line 9: Possible spelling mistake found.
Context: ...x$ are injected values to be hashed and $h_y$ are the output elements. General co...


Near line 9: Possible spelling mistake found.
Context: ...are injected values to be hashed and $h_y$ are the output elements. General const...


Near line 13: Possible spelling mistake found.
Context: ... the obtained input into chunks of size $r$. - Apply the permutation to the capacit...


Near line 16: Possible spelling mistake found.
Context: ...ate and apply the permutation. - Output $o$ output elements out of the rate part of...


Near line 21: Possible spelling mistake found.
Context: ...S design strategy consists of: - First, $R_f$ rounds in the beginning, in which S-...


Near line 21: Possible spelling mistake found.
Context: ...esign strategy consists of: - First, $R_f$ rounds in the beginning, in which S-box...


Near line 23: Possible spelling mistake found.
Context: ...are applied to the full state. - Next, $R_p$ rounds in the middle contain single ...


Near line 23: Possible spelling mistake found.
Context: ... applied to the full state. - Next, $R_p$ rounds in the middle contain single S-b...


Near line 23: Possible missing article found.
Context: ...ext, $R_p$ rounds in the middle contain single S-box application. Rest of the state go...


Near line 23: A determiner appears to be missing. Consider inserting it.
Context: ...iddle contain single S-box application. Rest of the state goes through this phase un...


Near line 24: Possible spelling mistake found.
Context: ...through this phase unchanged - Finally, $R_f$ rounds in the end, in which S-boxes ...


Near line 24: Possible spelling mistake found.
Context: ...ough this phase unchanged - Finally, $R_f$ rounds in the end, in which S-boxes are...


Near line 24: The preposition “at” seems more likely in this position than the preposition “in”.
Context: ...phase unchanged - Finally, $R_f$ rounds in the end, in which S-boxes are applied t...


Near line 30: Possible spelling mistake found.
Context: ...ts of the following three sub-steps: 1. $ARC$: Add round constants 2. $SBOX$: Applica...


Near line 31: Possible spelling mistake found.
Context: ...steps: 1. $ARC$: Add round constants 2. $SBOX$: Application of S-Boxes 3. $MIX$: Mix l...


Near line 32: Possible spelling mistake found.
Context: ...ts 2. $SBOX$: Application of S-Boxes 3. $MIX$: Mix layers ## Reference implementatio...


Near line 35: Possible spelling mistake found.
Context: ...plementation for magic values Directory reference/hadeshash includes `generate_params_poseidong.sa...


Near line 35: Possible spelling mistake found.
Context: ...irectory reference/hadeshash includes generate_params_poseidong.sage for generating values u...


Near line 35: Possible spelling mistake found.
Context: ...sage` for generating values used inside poseidon hasher. We build values for BLS12-381's...


Near line 35: Possible spelling mistake found.
Context: ... generating values used inside poseidon hasher. We build values for BLS12-381's $F_q$ ...


Near line 35: Possible spelling mistake found.
Context: ...hasher. We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use: ...


Near line 35: Possible spelling mistake found.
Context: ...her. We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use: ```...


Near line 35: The abbreviation/initialism is missing a period after the last letter.
Context: .... We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use: ```sh # F...


Near line 52: Possible spelling mistake found.
Context: ...5BFEFFFFFFFF00000001 ``` This generates poseidon_params_n255_t3_alpha3_M128.txt file. U...


Near line 56: Possible spelling mistake found.
Context: ...sted in the rust code. ## References [ICAR 2019/458](https://eprint.iacr.org/2019/...


Near line 56: Unpaired symbol: ‘[’ seems to be missing
Context: ...ust code. ## References ICAR 2019/458 [...

Additional comments not posted (1)
[Tho09]poseidon-hash/reference/hadeshash/code/generate_params_poseidon.sage (1)

55-78: The function sat_inequiv_alpha is logically correct and well-implemented.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Out of diff range and nitpick comments (3)
README.md (3)

Line range hint 4-4: Consider revising the phrase "research of:" to avoid grammatical issues.

- A single repository that hosts my "in-code" research of: protocols, ZK, cryptography, rust etc.
+ A single repository that hosts my "in-code" research on protocols, ZK, cryptography, rust, etc.

Line range hint 6-6: Add a blank line after the heading "Browsing the codebase" for better readability and to adhere to Markdown best practices.

## Browsing the codebase

+ 
The codebase is mostly split into different directories depending on the specific ideas that they tackle.

Line range hint 13-13: Add a blank line after the heading "References" for better readability and to adhere to Markdown best practices.

## References

+ 
- **\[Sha97\]**: Shamir's secret sharing https://apps.dtic.mil/sti/pdfs/ADA069397.pdf.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 25

Comment on lines +25 to +33
def print_words_to_hex(words):
hex_length = int(ceil(float(n) / 4)) + 2 # +2 for "0x"
print(["{0:#0{1}x}".format(entry.integer_representation(), hex_length) for entry in words])

def print_concat_words_to_large(words):
hex_length = int(ceil(float(n) / 4))
nums = ["{0:0{1}x}".format(entry.integer_representation(), hex_length) for entry in words]
final_string = "0x" + ''.join(nums)
print(final_string)
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider refactoring the list comprehensions in print_words_to_hex and print_concat_words_to_large for better readability and maintainability.

Comment on lines 5 to 9
if len(sys.argv) < 8:
print("Usage: <script> <field> <s_box> <field_size> <num_cells> <alpha> <security_level> <modulus_hex>")
print("field = 1 for GF(p)")
print("s_box = 0 for x^alpha, s_box = 1 for x^(-1)")
exit()
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper error handling for incorrect command-line arguments.

- exit()
+ sys.exit("Error: Incorrect usage of the script.")

This change ensures that the script exits with an appropriate error message, improving the user experience by providing clear feedback on what went wrong.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if len(sys.argv) < 8:
print("Usage: <script> <field> <s_box> <field_size> <num_cells> <alpha> <security_level> <modulus_hex>")
print("field = 1 for GF(p)")
print("s_box = 0 for x^alpha, s_box = 1 for x^(-1)")
exit()
if len(sys.argv) < 8:
print("Usage: <script> <field> <s_box> <field_size> <num_cells> <alpha> <security_level> <modulus_hex>")
print("field = 1 for GF(p)")
print("s_box = 0 for x^alpha, s_box = 1 for x^(-1)")
sys.exit("Error: Incorrect usage of the script.")

# In Hex that is `0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001`
# So, the following command should work:
# sage generate_params_poseidon.sage 1 0 255 3 3 128 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001
write_file = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider making the write_file flag configurable via command-line arguments.

This would enhance the flexibility of the script, allowing users to decide at runtime whether they want to write the output to a file or not.

Comment on lines +55 to +78
def sat_inequiv_alpha(p, t, R_F, R_P, alpha, M):
N = int(FIELD_SIZE * NUM_CELLS)

if alpha > 0:
R_F_1 = 6 if M <= ((floor(log(p, 2) - ((alpha-1)/2.0))) * (t + 1)) else 10 # Statistical
R_F_2 = 1 + ceil(log(2, alpha) * min(M, FIELD_SIZE)) + ceil(log(t, alpha)) - R_P # Interpolation
R_F_3 = (log(2, alpha) * min(M, log(p, 2))) - R_P # Groebner 1
R_F_4 = t - 1 + log(2, alpha) * min(M / float(t + 1), log(p, 2) / float(2)) - R_P # Groebner 2
R_F_5 = (t - 2 + (M / float(2 * log(alpha, 2))) - R_P) / float(t - 1) # Groebner 3
R_F_max = max(ceil(R_F_1), ceil(R_F_2), ceil(R_F_3), ceil(R_F_4), ceil(R_F_5))

# Addition due to https://eprint.iacr.org/2023/537.pdf
r_temp = floor(t / 3.0)
over = (R_F - 1) * t + R_P + r_temp + r_temp * (R_F / 2.0) + R_P + alpha
under = r_temp * (R_F / 2.0) + R_P + alpha
binom_log = log(binomial(over, under), 2)
if binom_log == inf:
binom_log = M + 1
cost_gb4 = ceil(2 * binom_log) # Paper uses 2.3727, we are more conservative here

return ((R_F >= R_F_max) and (cost_gb4 >= M))
else:
print("Invalid value for alpha!")
exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor the sat_inequiv_alpha function to improve readability and maintainability.

- if alpha > 0:
+ if alpha <= 0:
+     print("Invalid value for alpha!")
+     sys.exit(1)
+ # Rest of the code remains unchanged

This change makes the function's flow easier to follow by handling the error case first and reducing the nesting of the main logic.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def sat_inequiv_alpha(p, t, R_F, R_P, alpha, M):
N = int(FIELD_SIZE * NUM_CELLS)
if alpha > 0:
R_F_1 = 6 if M <= ((floor(log(p, 2) - ((alpha-1)/2.0))) * (t + 1)) else 10 # Statistical
R_F_2 = 1 + ceil(log(2, alpha) * min(M, FIELD_SIZE)) + ceil(log(t, alpha)) - R_P # Interpolation
R_F_3 = (log(2, alpha) * min(M, log(p, 2))) - R_P # Groebner 1
R_F_4 = t - 1 + log(2, alpha) * min(M / float(t + 1), log(p, 2) / float(2)) - R_P # Groebner 2
R_F_5 = (t - 2 + (M / float(2 * log(alpha, 2))) - R_P) / float(t - 1) # Groebner 3
R_F_max = max(ceil(R_F_1), ceil(R_F_2), ceil(R_F_3), ceil(R_F_4), ceil(R_F_5))
# Addition due to https://eprint.iacr.org/2023/537.pdf
r_temp = floor(t / 3.0)
over = (R_F - 1) * t + R_P + r_temp + r_temp * (R_F / 2.0) + R_P + alpha
under = r_temp * (R_F / 2.0) + R_P + alpha
binom_log = log(binomial(over, under), 2)
if binom_log == inf:
binom_log = M + 1
cost_gb4 = ceil(2 * binom_log) # Paper uses 2.3727, we are more conservative here
return ((R_F >= R_F_max) and (cost_gb4 >= M))
else:
print("Invalid value for alpha!")
exit(1)
def sat_inequiv_alpha(p, t, R_F, R_P, alpha, M):
N = int(FIELD_SIZE * NUM_CELLS)
if alpha <= 0:
print("Invalid value for alpha!")
sys.exit(1)
R_F_1 = 6 if M <= ((floor(log(p, 2) - ((alpha-1)/2.0))) * (t + 1)) else 10 # Statistical
R_F_2 = 1 + ceil(log(2, alpha) * min(M, FIELD_SIZE)) + ceil(log(t, alpha)) - R_P # Interpolation
R_F_3 = (log(2, alpha) * min(M, log(p, 2))) - R_P # Groebner 1
R_F_4 = t - 1 + log(2, alpha) * min(M / float(t + 1), log(p, 2) / float(2)) - R_P # Groebner 2
R_F_5 = (t - 2 + (M / float(2 * log(alpha, 2))) - R_P) / float(t - 1) # Groebner 3
R_F_max = max(ceil(R_F_1), ceil(R_F_2), ceil(R_F_3), ceil(R_F_4), ceil(R_F_5))
# Addition due to https://eprint.iacr.org/2023/537.pdf
r_temp = floor(t / 3.0)
over = (R_F - 1) * t + R_P + r_temp + r_temp * (R_F / 2.0) + R_P + alpha
under = r_temp * (R_F / 2.0) + R_P + alpha
binom_log = log(binomial(over, under), 2)
if binom_log == inf:
binom_log = M + 1
cost_gb4 = ceil(2 * binom_log) # Paper uses 2.3727, we are more conservative here
return ((R_F >= R_F_max) and (cost_gb4 >= M))



# This file was *autogenerated* from the file generate_params_poseidon.sage
from sage.all_cmdline import * # import sage library
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using specific imports instead of wildcard imports to improve code clarity and avoid namespace pollution.

- from sage.all_cmdline import *   # import sage library
+ from sage.all_cmdline import Integer, RealNumber, GF, matrix, VectorSpace, floor, ceil, log, binomial

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
from sage.all_cmdline import * # import sage library
from sage.all_cmdline import Integer, RealNumber, GF, matrix, VectorSpace, floor, ceil, log, binomial

@@ -0,0 +1,145 @@
from math import *
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid using from math import * as it can lead to namespace pollution and make it harder to identify where functions come from.

- from math import *
+ import math

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
from math import *
import math

@@ -0,0 +1,145 @@
from math import *
import sys
Copy link
Contributor

Choose a reason for hiding this comment

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

The sys module is imported but not used in this file. Consider removing the import if it's unnecessary.

- import sys

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import sys

for R_P_t in range(1, 500):
for R_F_t in range(4, 100):
if R_F_t % 2 == 0:
if (sat_inequiv(p, t, R_F_t, R_P_t, alpha, M) == True):
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoid comparing boolean expressions to True. Use the expression directly in the if statement.

- if (sat_inequiv(p, t, R_F_t, R_P_t, alpha, M) == True):
+ if sat_inequiv(p, t, R_F_t, R_P_t, alpha, M):

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (sat_inequiv(p, t, R_F_t, R_P_t, alpha, M) == True):
if sat_inequiv(p, t, R_F_t, R_P_t, alpha, M):

@@ -0,0 +1,373 @@
# Remark: This script contains functionality for GF(2^n), but currently works only over GF(p)! A few small adaptations are needed for GF(2^n).
Copy link
Contributor

Choose a reason for hiding this comment

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

Clarify the remark about the script's functionality and the needed adaptations for GF(2^n). This helps future developers understand the limitations and required changes.

Would you like me to help document these adaptations or open a GitHub issue to track this task?

Comment on lines +16 to +20
if len(sys.argv) < 7:
print("Usage: <script> <field> <s_box> <field_size> <num_cells> <R_F> <R_P> (<prime_number_hex>)")
print("field = 1 for GF(p)")
print("s_box = 0 for x^alpha, s_box = 1 for x^(-1)")
exit()
Copy link
Contributor

Choose a reason for hiding this comment

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

The script exits without providing detailed usage instructions if the required arguments are not provided. Consider enhancing the error message to include an example of correct usage.

-    print("Usage: <script> <field> <s_box> <field_size> <num_cells> <R_F> <R_P> (<prime_number_hex>)")
+    print("Usage: <script> <field> <s_box> <field_size> <num_cells> <R_F> <R_P> <prime_number_hex>")
+    print("Example: <script> 1 0 255 5 8 60 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if len(sys.argv) < 7:
print("Usage: <script> <field> <s_box> <field_size> <num_cells> <R_F> <R_P> (<prime_number_hex>)")
print("field = 1 for GF(p)")
print("s_box = 0 for x^alpha, s_box = 1 for x^(-1)")
exit()
if len(sys.argv) < 7:
print("Usage: <script> <field> <s_box> <field_size> <num_cells> <R_F> <R_P> <prime_number_hex>")
print("Example: <script> 1 0 255 5 8 60 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001")
print("field = 1 for GF(p)")
print("s_box = 0 for x^alpha, s_box = 1 for x^(-1)")
exit()

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

supragya and others added 2 commits April 27, 2024 23:31
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 9

@@ -0,0 +1,57 @@
# Poseidon Hasher
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure headings are surrounded by blank lines for proper Markdown formatting.

+#
# Poseidon Hasher
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
# Poseidon Hasher
#
# Poseidon Hasher
#


Poseidon is said to be a variant of *HadesMiMC* construction however with a fixed and known key.

## A primer on sponge construction
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure headings are surrounded by blank lines for proper Markdown formatting.

+#
## A primer on sponge construction
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## A primer on sponge construction
#
## A primer on sponge construction
#

- Output $o$ output elements out of the rate part of the state.
If needed, iterate the permutation more times.

## The HADES design strategy
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure headings are surrounded by blank lines for proper Markdown formatting.

+#
## The HADES design strategy
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## The HADES design strategy
##
## The HADES design strategy
##

2. $SBOX$: Application of S-Boxes
3. $MIX$: Mix layers

## Reference implementation for magic values
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure headings are surrounded by blank lines for proper Markdown formatting.

+#
## Reference implementation for magic values
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## Reference implementation for magic values
##
## Reference implementation for magic values
##

This generates `poseidon_params_n255_t3_alpha3_M128.txt` file. Using values in this file, we generate values ingested in the rust code.


## References
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure headings are surrounded by blank lines for proper Markdown formatting.

+#
## References
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## References
#
## References
#

3 # alpha \
128 # security level \
0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure fenced code blocks are surrounded by blank lines for proper Markdown formatting.

+#
sage generate_params_poseidon.sage \

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
```
#
sage generate_params_poseidon.sage \

In this, the $I$ is maintained state that changes over time, $m_x$ are injected values to be hashed and $h_y$ are the output elements.

General construction looks as follows:
- Depending on the use case, determine the capacity element value and the input padding if needed.
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure lists are surrounded by blank lines for proper Markdown formatting.

+#
- Depending on the use case, determine the capacity element value and the input padding if needed.
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- Depending on the use case, determine the capacity element value and the input padding if needed.
#
#


## The HADES design strategy
The HADES design strategy consists of:
- First, $R_f$ rounds in the beginning, in which S-boxes
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure lists are surrounded by blank lines for proper Markdown formatting.

+#
- First, $R_f$ rounds in the beginning, in which S-boxes
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- First, $R_f$ rounds in the beginning, in which S-boxes
#
- First, $R_f$ rounds in the beginning, in which S-boxes
#

![hades_construction](hades_construction.png)

Each such round consists of the following three sub-steps:
1. $ARC$: Add round constants
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure lists are surrounded by blank lines for proper Markdown formatting.

+#
1. $ARC$: Add round constants
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
1. $ARC$: Add round constants
#
1. $ARC$: Add round constants
#

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 8

Out of diff range and nitpick comments (1)
README.md (1)

Line range hint 20-20: Remove extra blank lines to maintain clean formatting.

-
-

Comment on lines +14 to +19

- **\[Sha97\]**: Shamir's secret sharing https://apps.dtic.mil/sti/pdfs/ADA069397.pdf.
- **\[Fel87\]**: Feldman's verifiable secret sharing https://www.zkdocs.com/docs/zkdocs/protocol-primitives/verifiable-secret-sharing/
- **\[Sch91\]**: Schnorr's DLog PoK https://www.zkdocs.com/docs/zkdocs/zero-knowledge-protocols/schnorr/
- **\[Tho09\]**: Poseidon Hasher https://eprint.iacr.org/2009/226.pdf

Copy link
Contributor

Choose a reason for hiding this comment

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

Add blank lines around the references section for better readability and Markdown formatting.

+#
- **\[Sha97\]**: Shamir's secret sharing https://apps.dtic.mil/sti/pdfs/ADA069397.pdf.
- **\[Fel87\]**: Feldman's verifiable secret sharing https://www.zkdocs.com/docs/zkdocs/protocol-primitives/verifiable-secret-sharing/
- **\[Sch91\]**: Schnorr's DLog PoK https://www.zkdocs.com/docs/zkdocs/zero-knowledge-protocols/schnorr/
- **\[Tho09\]**: Poseidon Hasher https://eprint.iacr.org/2009/226.pdf
+#

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- **\[Sha97\]**: Shamir's secret sharing https://apps.dtic.mil/sti/pdfs/ADA069397.pdf.
- **\[Fel87\]**: Feldman's verifiable secret sharing https://www.zkdocs.com/docs/zkdocs/protocol-primitives/verifiable-secret-sharing/
- **\[Sch91\]**: Schnorr's DLog PoK https://www.zkdocs.com/docs/zkdocs/zero-knowledge-protocols/schnorr/
- **\[Tho09\]**: Poseidon Hasher https://eprint.iacr.org/2009/226.pdf
- **\[Sha97\]**: Shamir's secret sharing https://apps.dtic.mil/sti/pdfs/ADA069397.pdf.
- **\[Fel87\]**: Feldman's verifiable secret sharing https://www.zkdocs.com/docs/zkdocs/protocol-primitives/verifiable-secret-sharing/
- **\[Sch91\]**: Schnorr's DLog PoK https://www.zkdocs.com/docs/zkdocs/zero-knowledge-protocols/schnorr/
- **\[Tho09\]**: Poseidon Hasher https://eprint.iacr.org/2009/226.pdf

Comment on lines +1 to +2
# Poseidon Hasher
Poseidon Hasher is a mapping over strings of $F_p$ (for prime $p > 2^{31}$) such that it maps $F_p^* \to F_p^o$ where $o$ is the number of output elements (often chosen value is $o = 1$).
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the repeated phrase "Poseidon Hasher" for clarity.

-# Poseidon Hasher Poseidon Hasher is a mapping over strings of $F_p$ (for prime $p > 2^{31}$) such that it maps $F_p^* \to F_p^o$ where $o$ is the number of output elements (often chosen value is $o = 1$).
+# Poseidon Hasher is a mapping over strings of $F_p$ (for prime $p > 2^{31}$) such that it maps $F_p^* \to F_p^o$ where $o$ is the number of output elements (often chosen value is $o = 1$).

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
# Poseidon Hasher
Poseidon Hasher is a mapping over strings of $F_p$ (for prime $p > 2^{31}$) such that it maps $F_p^* \to F_p^o$ where $o$ is the number of output elements (often chosen value is $o = 1$).
# Poseidon Hasher is a mapping over strings of $F_p$ (for prime $p > 2^{31}$) such that it maps $F_p^* \to F_p^o$ where $o$ is the number of output elements (often chosen value is $o = 1$).

Comment on lines +6 to +7
## A primer on sponge construction
Sponge construction looks as follows:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the repeated phrase and add a blank line after the heading for clarity and proper formatting.

-## A primer on sponge construction Sponge construction looks as follows:
+## A primer on sponge construction
+
+Sponge construction looks as follows:

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## A primer on sponge construction
Sponge construction looks as follows:
## A primer on sponge construction
Sponge construction looks as follows:

Comment on lines +19 to +20
## The HADES design strategy
The HADES design strategy consists of:
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line after the heading for proper Markdown formatting.

-## The HADES design strategy The HADES design strategy consists of:
+## The HADES design strategy
+
+The HADES design strategy consists of:

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## The HADES design strategy
The HADES design strategy consists of:
## The HADES design strategy
The HADES design strategy consists of:

Comment on lines +34 to +35
## Reference implementation for magic values
Directory `reference/hadeshash` includes `generate_params_poseidong.sage` for generating values used inside poseidon hasher. We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use:
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line after the heading for proper Markdown formatting.

-## Reference implementation for magic values Directory `reference/hadeshash` includes `generate_params_poseidong.sage` for generating values used inside poseidon hasher. We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use:
+## Reference implementation for magic values
+
+Directory `reference/hadeshash` includes `generate_params_poseidong.sage` for generating values used inside poseidon hasher. We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use:

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## Reference implementation for magic values
Directory `reference/hadeshash` includes `generate_params_poseidong.sage` for generating values used inside poseidon hasher. We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use:
## Reference implementation for magic values
Directory `reference/hadeshash` includes `generate_params_poseidong.sage` for generating values used inside poseidon hasher. We build values for BLS12-381's $F_q$ a.k.a Scalar Field. For this, use:

Comment on lines +55 to +56
## References
[ICAR 2019/458](https://eprint.iacr.org/2019/458.pdf)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line after the heading for proper Markdown formatting.

-## References [ICAR 2019/458](https://eprint.iacr.org/2019/458.pdf)
+## References
+
+[ICAR 2019/458](https://eprint.iacr.org/2019/458.pdf)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
## References
[ICAR 2019/458](https://eprint.iacr.org/2019/458.pdf)
## References
[ICAR 2019/458](https://eprint.iacr.org/2019/458.pdf)

print("Usage: <script> <field> <s_box> <field_size> <num_cells> <alpha> <security_level> <modulus_hex>")
print("field = 1 for GF(p)")
print("s_box = 0 for x^alpha, s_box = 1 for x^(-1)")
exit()
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace exit() with sys.exit() to provide an exit status code, which is useful for debugging and integration with other software.

- exit()
+ sys.exit(1)  # Exit with a status code indicating failure

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
exit()
sys.exit(1) # Exit with a status code indicating failure

Comment on lines +170 to +191
def grain_sr_generator():
bit_sequence = INIT_SEQUENCE
for _ in range(0, 160):
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)

while True:
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
while new_bit == 0:
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
yield new_bit
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor the grain_sr_generator function to reduce code duplication and improve clarity.

+ def update_bit_sequence():
+     new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
+     bit_sequence.pop(0)
+     bit_sequence.append(new_bit)
+     return new_bit
+
- for _ in range(0, 160):
-     new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
-     bit_sequence.pop(0)
-     bit_sequence.append(new_bit)
- while True:
-     new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
-     bit_sequence.pop(0)
-     bit_sequence.append(new_bit)
+ for _ in range(160):
+     update_bit_sequence()
+ while True:
+     new_bit = update_bit_sequence()

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def grain_sr_generator():
bit_sequence = INIT_SEQUENCE
for _ in range(0, 160):
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
while True:
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
while new_bit == 0:
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
yield new_bit
def grain_sr_generator():
bit_sequence = INIT_SEQUENCE
def update_bit_sequence():
new_bit = bit_sequence[62] ^^ bit_sequence[51] ^^ bit_sequence[38] ^^ bit_sequence[23] ^^ bit_sequence[13] ^^ bit_sequence[0]
bit_sequence.pop(0)
bit_sequence.append(new_bit)
return new_bit
for _ in range(160):
update_bit_sequence()
while True:
new_bit = update_bit_sequence()
while new_bit == 0:
new_bit = update_bit_sequence()
yield new_bit

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.

2 participants