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

ripemd160 & bech32 #150

Merged
merged 6 commits into from
Oct 23, 2024
Merged

ripemd160 & bech32 #150

merged 6 commits into from
Oct 23, 2024

Conversation

sh-cha
Copy link
Contributor

@sh-cha sh-cha commented Oct 22, 2024

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features
    • Introduced Bech32 encoding and decoding functionality with new native functions.
    • Added RIPEMD-160 hashing capability, including a new native function for hashing.
  • Bug Fixes
    • Enhanced gas parameter definitions for Bech32 and RIPEMD-160 operations.
  • Documentation
    • Added test functions for Bech32 and RIPEMD-160 to validate encoding, decoding, and hashing processes.
  • Chores
    • Updated dependency management to include the ripemd library and adjusted existing dependency constraints.

@sh-cha sh-cha self-assigned this Oct 22, 2024
@sh-cha sh-cha requested a review from a team as a code owner October 22, 2024 07:50
Copy link

coderabbitai bot commented Oct 22, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request introduces several changes across multiple files, primarily focusing on dependency management, gas parameter definitions, and the addition of new native functions for Bech32 encoding/decoding and RIPEMD-160 hashing. The Cargo.toml files have been updated to include a new dependency on ripemd, and new gas parameters for Bech32 and RIPEMD-160 have been defined in the respective gas parameter files. Additionally, new modules and functions for encoding and decoding Bech32 addresses have been implemented, along with corresponding test functions.

Changes

File Change Summary
Cargo.toml Added dependency ripemd = "0.1.1"; updated bigdecimal version constraint to >=0.4.5.
crates/gas/src/initia_stdlib.rs Added gas parameters for Bech32 encoding/decoding: bech32_encode_base, bech32_encode_unit, bech32_decode_base, bech32_decode_unit.
crates/gas/src/move_stdlib.rs Added gas parameters for hash.ripemd160: hash_ripemd160_base, hash_ripemd160_per_byte.
crates/natives/Cargo.toml Added dependency ripemd = { workspace = true }.
crates/natives/src/bech32.rs Introduced functions for Bech32 encoding/decoding: native_encode, native_decode, and a registration function make_all.
crates/natives/src/lib.rs Added new module bech32 and updated native functions registration.
crates/natives/src/move_stdlib/hash.rs Added native function native_ripemd160 for RIPEMD-160 hashing and updated registration in make_all.
precompile/modules/initia_stdlib/sources/bech32.move Added module initia_std::bech32 with functions encode and decode, including test functions.
precompile/modules/minitia_stdlib/sources/bech32.move Added module minitia_std::bech32 with functions encode and decode, including test functions.
precompile/modules/move_stdlib/sources/hash.move Added function ripemd160 for hashing and test function ripemd160_test.

Possibly related PRs

  • feat: update gas meter #117: This PR updates the Cargo.toml file, which is relevant as the main PR also modifies the Cargo.toml file for dependency management, specifically adding the ripemd dependency.
  • fix: audit #126: This PR adds new gas parameters in initia_stdlib.rs, which may relate to the changes in the main PR that involve dependency updates for the ripemd hashing algorithm.
  • fix: metadata to denom #135: This PR modifies the metadata_to_denom function in the coin module, which may indirectly relate to the overall changes in the project structure and dependencies, although it does not directly connect to the Cargo.toml changes.
  • fix: remove unused api #138: This PR removes an unused API, which may not directly relate to the main PR but indicates ongoing maintenance and cleanup in the codebase that could affect dependency management.
  • fix: restrict struct creation from the other modules #149: This PR restricts struct creation from other modules, enhancing encapsulation, which aligns with the overall goal of maintaining a clean and manageable codebase as seen in the main PR's focus on dependency management.

Suggested reviewers

  • sh-cha

🐇 In the meadow, changes bloom,
With Bech32 and hashes, there's plenty of room.
New functions frolic, gas costs align,
In the world of code, everything's fine!
Let's hop along, with joy we cheer,
For every new feature, we hold so dear! 🌼


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>, 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 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

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

🧹 Outside diff range and nitpick comments (8)
precompile/modules/move_stdlib/sources/hash.move (2)

10-33: LGTM: Test function for RIPEMD-160 implemented with suggestions.

The test function ripemd160_test is well-structured and covers basic cases. However, consider the following improvements:

  1. Use a more specific error code or message in the assertion for better debugging.
  2. Add more test cases, including longer inputs, to ensure thorough testing.
  3. Consider parameterizing the test function to allow for easier addition of test cases.

Here's a suggested improvement for the assertion:

-            assert!(hash_expected == hash, 1);
+            assert!(hash_expected == hash, 0x1000); // RIPEMD-160 hash mismatch

Would you like assistance in implementing these suggestions?


8-33: Summary: RIPEMD-160 hash function and test successfully implemented.

The additions to the std::hash module are well-implemented and align with the existing structure. The new ripemd160 function expands the module's capabilities, and the corresponding test function ensures its correctness.

Consider the following for future improvements:

  1. Expand test cases to cover a wider range of inputs.
  2. Implement property-based testing for more thorough validation.
  3. Add documentation comments for the new function to maintain consistency with other module functions.

Overall, these changes enhance the functionality of the hash module without introducing any apparent issues.

crates/natives/src/move_stdlib/hash.rs (2)

76-105: LGTM! Consider a minor improvement for consistency.

The native_ripemd160 function is well-implemented and follows the structure of existing hash functions. It correctly handles gas costs, input validation, and hash computation using the ripemd crate.

For consistency with the sha2_256 and sha3_256 functions, consider using the Digest trait method directly:

-    let mut hasher = ripemd::Ripemd160::new();
-    hasher.update(&hash_arg);
-    let hash_vec = hasher.finalize().to_vec();
+    let hash_vec = ripemd::Ripemd160::digest(hash_arg.as_slice()).to_vec();

This change would make the implementation more concise and align it with the other hash functions in this file.


Line range hint 76-116: Summary: RIPEMD-160 hash function successfully implemented

The changes in this file successfully introduce the RIPEMD-160 hash function to the Move standard library. The implementation follows the established patterns for hash functions in this module, including proper gas cost calculation and error handling. The make_all function is correctly updated to expose the new native function.

These changes enhance the cryptographic capabilities of the Move language, allowing developers to use RIPEMD-160 in their smart contracts. This addition is particularly useful for applications that require compatibility with systems or protocols that use RIPEMD-160, such as certain blockchain address derivation schemes.

Consider documenting the use cases for RIPEMD-160 in the Move ecosystem, particularly in comparison to the existing SHA-2 and SHA-3 functions, to guide developers in choosing the appropriate hash function for their needs.

crates/gas/src/initia_stdlib.rs (1)

42-45: LGTM! Consider adding a brief comment for clarity.

The new gas parameters for Bech32 encoding and decoding are consistent with existing patterns and values in the file. The base and per-byte costs are appropriate and align well with similar operations like base64 encoding/decoding.

Consider adding a brief comment above these new parameters to explain their purpose and relation to blockchain address handling. This would improve code documentation and maintainability.

+    // Gas parameters for Bech32 address encoding and decoding operations
     [bech32_encode_base: InternalGas, "bech32.encode.base", 1102],
     [bech32_encode_unit: InternalGasPerByte, "bech32.encode.unit", 18],
     [bech32_decode_base: InternalGas, "bech32.decode.base", 1102],
     [bech32_decode_unit: InternalGasPerByte, "bech32.decode.unit", 18],
precompile/modules/minitia_stdlib/sources/bech32.move (2)

7-10: Review the usage of #[test_only] with use statements

The #[test_only] attribute is applied to the use statements for minitia_std::string and minitia_std::hex. Ensure that this attribute correctly limits the scope of these imports to test code. Alternatively, consider moving these imports inside the test functions to clearly indicate they are used only in tests.


31-32: Avoid variable shadowing for data in test_decode

In the test_decode function, the variable data is reassigned after encoding it to a hex string (lines 31 and 37). This shadows the original data variable obtained from the decode function, which can lead to confusion. Consider using a different variable name, such as hex_data, to improve code readability.

Also applies to: 37-38

crates/natives/src/bech32.rs (1)

28-33: Implement to_words and from_words functions

The comments mention native public fun to_words and from_words, but these functions are not implemented in the code. If these functions are intended to be part of the module, please provide their implementations to ensure full functionality.

Would you like assistance in implementing these functions?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1e55ec9 and 43e5b19.

📒 Files selected for processing (10)
  • Cargo.toml (1 hunks)
  • crates/gas/src/initia_stdlib.rs (1 hunks)
  • crates/gas/src/move_stdlib.rs (1 hunks)
  • crates/natives/Cargo.toml (1 hunks)
  • crates/natives/src/bech32.rs (1 hunks)
  • crates/natives/src/lib.rs (2 hunks)
  • crates/natives/src/move_stdlib/hash.rs (2 hunks)
  • precompile/modules/initia_stdlib/sources/bech32.move (1 hunks)
  • precompile/modules/minitia_stdlib/sources/bech32.move (1 hunks)
  • precompile/modules/move_stdlib/sources/hash.move (1 hunks)
🧰 Additional context used
🔇 Additional comments (12)
precompile/modules/move_stdlib/sources/hash.move (1)

8-8: LGTM: New RIPEMD-160 hash function added.

The new native function ripemd160 is a good addition to the hash module. Its signature is consistent with other hash functions, and the native declaration is appropriate for cryptographic implementations.

crates/natives/Cargo.toml (1)

28-28: New dependency added: Verify necessity and usage

The addition of the ripemd dependency aligns with the PR objectives related to implementing RIPEMD-160 hashing. This is a commonly used cryptographic hash function, often used in blockchain applications.

A few points to consider:

  1. Ensure that RIPEMD-160 is indeed required for your project's cryptographic needs.
  2. Verify that the ripemd crate is actively maintained and has no known security vulnerabilities.
  3. Confirm that the workspace-level version specification (workspace = true) is intentional and consistent with other dependencies.

To ensure the ripemd crate is being used in the project:

✅ Verification successful
<!-- [monologue]
[task review_file_1/1]
Review the usage of the `ripemd` dependency in the codebase.
[observations]
- The shell script searched for usages of `ripemd` and found a reference in `crates/natives/src/move_stdlib/hash.rs`:
  ```rust
  let mut hasher = ripemd::Ripemd160::new();
  • This indicates that the ripemd crate is actively used in the project.
    [analysis]
  • The presence of ripemd::Ripemd160::new() confirms that the newly added dependency is necessary for the project's functionality.
  • No conflicting dependencies or security issues are immediately apparent from the provided information.
  • The usage aligns with the project's objective to implement RIPEMD-160 hashing.
    [actions]
  • Conclude that the addition of the ripemd dependency is appropriate and necessary.
  • No further shell scripts are required as the dependency usage has been verified.
    -->

**Dependency `ripemd` is properly utilized**

The `ripemd` crate is actively used in `crates/natives/src/move_stdlib/hash.rs`, confirming that its addition is necessary for implementing RIPEMD-160 hashing.



</details>

<details>
<summary>🏁 Scripts executed</summary>

The following scripts were executed for the analysis:

Script:

```shell
#!/bin/bash
# Search for usage of ripemd in the codebase
rg -i 'use.*ripemd|ripemd::'

Length of output: 116

crates/gas/src/move_stdlib.rs (1)

19-20: Verify gas cost values for RIPEMD-160.

The addition of gas parameters for RIPEMD-160 is appropriate and consistent with other hash functions. However, the values are identical to SHA2-256, which may not accurately reflect the computational cost of RIPEMD-160.

Please confirm:

  1. Are these values based on benchmarks specific to RIPEMD-160, or are they initial estimates?
  2. If they are estimates, consider running separate benchmarks for RIPEMD-160 to ensure accurate gas costs.

To assist in verifying the relative performance of RIPEMD-160 compared to SHA2-256, you could run the following benchmark:

This script will help you compare the performance of RIPEMD-160 and SHA2-256, which can inform more accurate gas cost settings.

crates/natives/src/lib.rs (2)

11-11: LGTM: New bech32 module added correctly.

The bech32 module is added to the list of public modules, maintaining the alphabetical order. This is consistent with the existing structure and improves code organization.


66-66: LGTM: bech32 module correctly added to native functions.

The bech32 module is properly integrated into the initia_move_natives function, maintaining alphabetical order and following the established pattern for adding native functions.

To ensure the bech32 module is correctly implemented, please run the following verification script:

This script will help verify the existence and basic structure of the bech32 module implementation.

crates/natives/src/move_stdlib/hash.rs (1)

116-116: LGTM! The make_all function is correctly updated.

The new ripemd160 function is properly added to the list of native functions, maintaining consistency with the existing entries.

Cargo.toml (1)

110-110: New dependency added: ripemd

The addition of the ripemd = "0.1.1" dependency aligns with the PR objectives of introducing changes related to the ripemd160 hashing algorithm. This library provides an implementation of the RIPEMD (RACE Integrity Primitives Evaluation Message Digest) hash function family, which includes RIPEMD-160.

A few points to consider:

  1. The version "0.1.1" is relatively new. Ensure that this version is stable and suitable for production use.
  2. Verify that this dependency is actually used in the codebase, particularly in the implementation of the new native functions for RIPEMD-160 hashing mentioned in the PR summary.

To confirm the usage of this new dependency, please run the following script:

This will help ensure that the dependency is being utilized and not unnecessarily added.

✅ Verification successful

Dependency ripemd is properly utilized

The ripemd = "0.1.1" dependency is used in crates/natives/src/move_stdlib/hash.rs as shown below:

let mut hasher = ripemd::Ripemd160::new();

This confirms that the dependency is necessary for the implementation of the RIPEMD-160 hashing functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of ripemd in the codebase
rg -i 'use.*ripemd|ripemd::'

Length of output: 116

precompile/modules/initia_stdlib/sources/bech32.move (3)

4-5: LGTM: Native function declarations

The native function declarations for encode and decode are appropriately defined and match the expected signatures.


12-24: LGTM: test_encode function

The test_encode function correctly tests the encode function with different prefixes and data inputs. The assertions accurately verify the expected outcomes.


27-39: LGTM: test_decode function

The test_decode function effectively tests the decode function. The validation of the prefix and data ensures that the decoding process is functioning as intended.

precompile/modules/minitia_stdlib/sources/bech32.move (1)

4-5: Ensure consistent error handling in native functions

The signatures for encode and decode do not specify how errors are handled if the input is invalid. Verify that these native functions appropriately handle errors and consider updating the function signatures to include error types if necessary.

crates/natives/src/bech32.rs (1)

46-46: Verify the gas parameter reference

The gas parameters are accessed using initia_stdlib. Please confirm that this is the correct module name for gas parameters.

Run the following script to verify the existence of initia_stdlib:

precompile/modules/initia_stdlib/sources/bech32.move Outdated Show resolved Hide resolved
precompile/modules/minitia_stdlib/sources/bech32.move Outdated Show resolved Hide resolved
precompile/modules/minitia_stdlib/sources/bech32.move Outdated Show resolved Hide resolved
precompile/modules/minitia_stdlib/sources/bech32.move Outdated Show resolved Hide resolved
crates/natives/src/bech32.rs Show resolved Hide resolved
crates/natives/src/bech32.rs Outdated Show resolved Hide resolved
Copy link
Member

@beer-1 beer-1 left a comment

Choose a reason for hiding this comment

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

LGTM

@beer-1 beer-1 merged commit fdaff32 into main Oct 23, 2024
3 of 5 checks passed
@beer-1 beer-1 deleted the feat/ripemd branch October 23, 2024 05:44
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