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

refact(bls):implement gas calculation logic #325

Merged
merged 3 commits into from
Mar 25, 2025

Conversation

trestinlsd
Copy link
Contributor

@trestinlsd trestinlsd commented Mar 17, 2025

Description


Summary by CodeRabbit

Summary by CodeRabbit

  • New Features
    • Introduced dynamic gas cost calculations with updated resource cost parameters for cryptographic operations.
  • Refactor
    • Standardized naming conventions and updated interface declarations for improved clarity and consistency.
    • Added a new file defining gas constants for BLS12-381 elliptic curve operations.
  • Tests
    • Adjusted test cases to validate the updated naming and gas computation logic.
  • Chore
    • Removed legacy configuration parameters to simplify initialization and streamline operation setup.

Copy link
Contributor

coderabbitai bot commented Mar 17, 2025

Walkthrough

The pull request updates the BLS precompile and its associated interfaces by standardizing naming conventions and refining method functionality. Changes include renaming parameters and return types to use camel case consistently, removing the baseGas parameter from precompile initialization, adding dynamic gas calculation logic based on function selectors, and introducing helper methods and new gas constants for BLS12-381 operations. Test files have been updated accordingly to reflect these changes.

Changes

File(s) Change Summary
precompiles/bls/IBLS.sol
precompiles/bls/abi.json
Renamed parameters and return types for consistent camel case usage (e.g., pubkeypubKey, aggregatePubkeysaggregatePubKeys).
precompiles/bls/bls_test.go
precompiles/bls/methods.go
Updated method identifiers and function names to align with revised naming conventions (e.g., MethodAggregatePubkeysMethodAggregatePubKeys).
precompiles/bls/bls.go Removed the baseGas parameter; enhanced RequiredGas to compute dynamic gas costs using function selectors; added helper methods (calculateFastAggregateVerifyGas, calculateAggregationGas); updated Run and simplified IsTransaction.
precompiles/bls/protocol_params.go Introduced new gas constants for various BLS12-381 elliptic curve operations.
precompiles/bls/setup_test.go
x/evm/keeper/precompiles.go
Removed hardcoded baseGas value and updated precompile instantiation to use the parameterless NewPrecompile() function.

Possibly related PRs

Suggested labels

C:Proto, Type: Build, Type: CI, Type: Tests

Suggested reviewers

  • magj2006
  • cloud8little
  • bwhour
  • leonz789
  • MaxMustermann2

Poem

I'm a hopping rabbit, coding neat and swift,
Renaming and recalculating—a magical gift!
Fields of camel case and gas in every line,
My tiny paws rejoice in each update so fine.
With a twitch of my ears and a joyful cheer,
I celebrate clean code, bunny spirit clear!
🐇✨

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, 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.
  • @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.

CodeRabbit Configuration 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: 0

🧹 Nitpick comments (1)
precompiles/bls/bls.go (1)

165-166: Simplified IsTransaction method signature and implementation.

The method has been changed to always return false without requiring a method ID parameter. This is appropriate since none of the BLS methods are transactions (they're stateless precompile calls).

However, the function's documentation still mentions "Available bls transactions are: - MethodVerify" which contradicts the implementation.

// IsTransaction checks if the given methodID corresponds to a transaction or query.
//
-// Available bls transactions are:
-//   - MethodVerify
+// BLS precompiles are stateless and don't modify state, so this always returns false.
func (Precompile) IsTransaction() bool {
  return false
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a322d95 and 3a7e3d2.

📒 Files selected for processing (1)
  • precompiles/bls/bls.go (6 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
precompiles/bls/bls.go (2)
precompiles/bls/methods.go (10) (10)
  • p (25:56)
  • p (59:95)
  • p (97:116)
  • p (118:136)
  • p (138:165)
  • MethodVerify (18:18)
  • MethodFastAggregateVerify (16:16)
  • MethodAggregatePubKeys (19:19)
  • MethodAggregateSignatures (20:20)
  • MethodAddTwoPubKeys (21:21)
precompiles/bls/protocol_params.go (4) (4)
  • Bls12381PairingBaseGas (9:9)
  • Bls12381PairingPerPairGas (10:10)
  • Bls12381G1AddGas (5:5)
  • Bls12381G2AddGas (7:7)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: test-unit-e2e
  • GitHub Check: goreleaser
  • GitHub Check: Analyze (go)
🔇 Additional comments (6)
precompiles/bls/bls.go (6)

7-7: Added required import for gas calculation logic.

The import of math/big package is correctly added to support the new gas calculation functions that use big integers for parsing input data.


32-45: Simplified precompile initialization by removing unused baseGas parameter.

The removal of the baseGas parameter from NewPrecompile is appropriate since gas calculations are now dynamically determined in the RequiredGas method. This change simplifies the interface and eliminates an unused parameter.


55-83: Implemented dynamic gas calculation based on method selectors.

The gas calculation logic now properly determines gas costs based on the specific BLS operation being performed, following a similar pattern to other Ethereum precompiles. This is a significant improvement from the previous fixed gas cost approach.

The implementation correctly uses p.MethodById to identify the method and applies appropriate gas costs for each operation type, addressing the previous review comments.


86-102: Implemented specialized gas calculation for FastAggregateVerify.

The function correctly calculates gas based on the number of public keys being verified:

  1. Extracts the public keys offset from the input data
  2. Determines the number of keys (m)
  3. Calculates gas as: (m-1)*G1AddGas + PairingGas

The defensive input length checks help prevent potential panics when parsing malformed inputs.


105-121: Added generic aggregation gas calculation logic.

This helper function is well-designed to:

  1. Extract the offset of the array from the input
  2. Determine the number of elements (n)
  3. Calculate gas as (n-1) * gasPerOp

The function includes appropriate input length validation and handles the case where n < 1, providing robust error handling for edge cases.


144-145: Standardized method naming convention.

The method names have been updated from AggregatePubkeys to AggregatePubKeys and from AddTwoPubkeys to AddTwoPubKeys, using consistent camelCase with capital "K" in "Keys". This improves naming consistency across the codebase.

Also applies to: 148-149

@trestinlsd trestinlsd enabled auto-merge March 20, 2025 01:38
@trestinlsd trestinlsd added this pull request to the merge queue Mar 25, 2025
Merged via the queue into imua-xyz:develop with commit 336573f Mar 25, 2025
23 checks passed
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.

3 participants