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

feat(katana): classes trie hash #2876

Merged
merged 3 commits into from
Jan 8, 2025
Merged

feat(katana): classes trie hash #2876

merged 3 commits into from
Jan 8, 2025

Conversation

kariy
Copy link
Member

@kariy kariy commented Jan 7, 2025

It was set to Pedersen based on the Starknet docs but Pathfinder is using Poseidon. Based on my experience, it's better to just follow what Pathfinder is doing.

Summary by CodeRabbit

  • New Features

    • None
  • Bug Fixes

    • None
  • Refactor

    • Updated hashing algorithm from Pedersen to Poseidon in proof verification processes across multiple components.
    • Modified trie and proof verification implementations to utilize Poseidon hash function.
  • Tests

    • Updated test cases to reflect the new hashing algorithm in proof verification.

Copy link

coderabbitai bot commented Jan 7, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Ohayo, sensei! This PR introduces a modification in the hashing algorithm for proof verification within the Katana RPC and trie components. The main change involves transitioning from the Pedersen hash function to the Poseidon hash function across several files. This update affects the verification processes for both class and contract proofs, ensuring consistency in the cryptographic approach used in multi-proof verification and trie structures.

Changes

File Change Summary
crates/katana/rpc/rpc/tests/proofs.rs Replaced hash::Pedersen with hash::Poseidon in proof verification test functions.
crates/katana/trie/src/classes.rs Updated hash function from Pedersen to Poseidon in ClassesMultiProof and ClassesTrie structures, including import modifications.
crates/katana/storage/db/src/trie/mod.rs Changed verify_proof function calls from Pedersen to Poseidon in the testing module.

Possibly related PRs


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ed6ed02 and 95a7c12.

📒 Files selected for processing (1)
  • crates/katana/storage/db/src/trie/mod.rs (4 hunks)

🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

@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: 2

🧹 Nitpick comments (2)
crates/katana/trie/src/classes.rs (1)

29-29: Ohayo! Important architectural change detected, sensei!

The transition from Pedersen to Poseidon in BonsaiTrie is a fundamental change that affects the entire trie structure. This change:

  1. Modifies the hash computation for all trie nodes
  2. Will result in different trie roots for the same data
  3. Requires careful migration planning for existing data

Consider documenting:

  • Migration strategy for existing tries
  • Performance implications of Poseidon vs Pedersen
  • Backward compatibility considerations
crates/katana/rpc/rpc/tests/proofs.rs (1)

125-128: Enhance test coverage for hash function transition, sensei!

While the existing tests verify the proof functionality, consider adding specific test cases to:

  1. Verify that old Pedersen proofs are rejected
  2. Test the performance implications of Poseidon vs Pedersen
  3. Add edge cases specific to Poseidon hash characteristics
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 23614ea and 60d5231.

📒 Files selected for processing (2)
  • crates/katana/rpc/rpc/tests/proofs.rs (1 hunks)
  • crates/katana/trie/src/classes.rs (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: fmt
🔇 Additional comments (1)
crates/katana/trie/src/classes.rs (1)

4-4: Ohayo! Import changes look good, sensei!

The transition from Pedersen to Poseidon hash is properly reflected in the imports.

@@ -15,7 +14,7 @@ impl ClassesMultiProof {
// TODO: maybe perform results check in this method as well. make it accept the compiled class
// hashes
pub fn verify(&self, root: Felt, class_hashes: Vec<ClassHash>) -> Vec<Felt> {
crate::verify_proof::<Pedersen>(&self.0, root, class_hashes)
crate::verify_proof::<Poseidon>(&self.0, root, class_hashes)
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Ohayo! Test files need updating, sensei!

The switch to Poseidon affects these test files:

  • crates/katana/storage/db/src/trie/mod.rs (test cases)
  • crates/katana/rpc/rpc/tests/proofs.rs

Please update the test cases to use Poseidon instead of Pedersen to maintain consistency with the production code.

🔗 Analysis chain

Verify compatibility with existing callers, sensei!

The switch from Pedersen to Poseidon in verify_proof is a breaking change. Let's verify all callers are updated.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all calls to verify_proof to ensure they're updated
rg -l "verify_proof::<.*Pedersen>" 

Length of output: 115


Script:

#!/bin/bash
# Check the actual usage in both files
echo "=== storage/db/src/trie/mod.rs ==="
rg "verify_proof::<.*Pedersen>" crates/katana/storage/db/src/trie/mod.rs -B 2 -A 2

echo -e "\n=== rpc/rpc/tests/proofs.rs ==="
rg "verify_proof::<.*Pedersen>" crates/katana/rpc/rpc/tests/proofs.rs -B 2 -A 2

Length of output: 1531

@@ -122,7 +122,7 @@ async fn genesis_states() {

let classes_proof = MultiProof::from(proofs.classes_proof.nodes);
let classes_tree_root = proofs.global_roots.classes_tree_root;
let classes_verification_result = katana_trie::verify_proof::<hash::Pedersen>(
let classes_verification_result = katana_trie::verify_proof::<hash::Poseidon>(
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Inconsistent hash function usage detected, sensei!

While classes verification has been updated to use Poseidon, contract and storage proofs are still using Pedersen. This inconsistency could lead to:

  1. Confusion in the codebase
  2. Potential verification issues
  3. Maintenance challenges

Consider:

  1. Updating all proof verifications to use Poseidon consistently
  2. Or documenting why different hash functions are used for different proofs

Copy link

@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: 0

🧹 Nitpick comments (1)
crates/katana/storage/db/src/trie/mod.rs (1)

391-391: Ohayo! Both hash functions are still imported but only Poseidon is used.

While the code now uses Poseidon exclusively, the Pedersen import remains. Consider removing the unused import to maintain code cleanliness.

-    use katana_primitives::hash::{Pedersen, Poseidon, StarkHash};
+    use katana_primitives::hash::{Poseidon, StarkHash};
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 60d5231 and ed6ed02.

📒 Files selected for processing (1)
  • crates/katana/storage/db/src/trie/mod.rs (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: fmt
🔇 Additional comments (2)
crates/katana/storage/db/src/trie/mod.rs (2)

450-450: LGTM! Consistent transition to Poseidon hash for proof verification.

The changes correctly implement the transition from Pedersen to Poseidon hash across all proof verification scenarios:

  • Single key proof for snapshot 0
  • Single key proof for snapshot 1
  • Multi-key proof for current state

Also applies to: 467-467, 478-478


Line range hint 391-486: Verify the impact of the hash function transition.

The switch from Pedersen to Poseidon hash is a significant architectural change that could affect:

  1. Proof verification compatibility with existing data
  2. Performance characteristics
  3. Security properties

Sensei, let's verify the broader impact:

✅ Verification successful

Ohayo sensei! The hash function transition has been properly implemented and verified.

The test demonstrates that the Poseidon hash implementation works correctly for:

  • Root calculation
  • Proof verification
  • Hash value computation

No compatibility issues or remaining Pedersen dependencies were found in the proof verification system.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining Pedersen hash usage in proof verification
# and potential compatibility issues.

echo "Checking for remaining Pedersen hash usage in proof verification..."
rg -l "verify_proof::<Pedersen>" 

echo "Checking for potential compatibility handling..."
ast-grep --pattern 'fn verify_proof<H: $_>($_) -> $_'

Length of output: 342


Script:

#!/bin/bash
# Search for broader Pedersen hash usage and hash implementations

echo "Checking for Pedersen hash usage..."
rg -l "Pedersen" 

echo "Looking for hash trait implementations..."
ast-grep --pattern 'impl $_ for Pedersen'

echo "Checking test files for hash function usage..."
rg -l "Pedersen|Poseidon" "**/test*.rs"

echo "Looking for hash-related configuration..."
rg -l "hash.*config|hash.*const"

Length of output: 2001


Script:

#!/bin/bash
# Examine hash function usage context and compatibility

echo "Checking hash function usage in trie files..."
rg -A 5 "Pedersen|Poseidon" "crates/katana/trie/src/*.rs"

echo "Checking test files with correct pattern..."
fd -t f "test.*\.rs$" -x rg -l "Pedersen|Poseidon" {}

echo "Looking for hash type parameters and trait bounds..."
ast-grep --pattern 'H: Hash'

echo "Checking for migration or compatibility code..."
rg -A 5 "migration|compatibility|legacy.*hash"

Length of output: 74712

Copy link

codecov bot commented Jan 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.76%. Comparing base (23614ea) to head (95a7c12).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2876      +/-   ##
==========================================
- Coverage   55.77%   55.76%   -0.01%     
==========================================
  Files         447      447              
  Lines       57794    57794              
==========================================
- Hits        32233    32230       -3     
- Misses      25561    25564       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

1 participant