-
Notifications
You must be signed in to change notification settings - Fork 16
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
ripemd160 & bech32 #150
Conversation
Caution Review failedThe pull request is closed. WalkthroughThe 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 Changes
Possibly related PRs
Suggested reviewers
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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:
- Use a more specific error code or message in the assertion for better debugging.
- Add more test cases, including longer inputs, to ensure thorough testing.
- 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 mismatchWould 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 newripemd160
function expands the module's capabilities, and the corresponding test function ensures its correctness.Consider the following for future improvements:
- Expand test cases to cover a wider range of inputs.
- Implement property-based testing for more thorough validation.
- 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 theripemd
crate.For consistency with the
sha2_256
andsha3_256
functions, consider using theDigest
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 implementedThe 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]
withuse
statementsThe
#[test_only]
attribute is applied to theuse
statements forminitia_std::string
andminitia_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 fordata
intest_decode
In the
test_decode
function, the variabledata
is reassigned after encoding it to a hex string (lines 31 and 37). This shadows the originaldata
variable obtained from thedecode
function, which can lead to confusion. Consider using a different variable name, such ashex_data
, to improve code readability.Also applies to: 37-38
crates/natives/src/bech32.rs (1)
28-33
: Implementto_words
andfrom_words
functionsThe comments mention
native public fun to_words
andfrom_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
📒 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 usageThe 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:
- Ensure that RIPEMD-160 is indeed required for your project's cryptographic needs.
- Verify that the
ripemd
crate is actively maintained and has no known security vulnerabilities.- 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:
- Are these values based on benchmarks specific to RIPEMD-160, or are they initial estimates?
- 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: Newbech32
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 theinitia_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! Themake_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: ripemdThe 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:
- The version "0.1.1" is relatively new. Ensure that this version is stable and suitable for production use.
- 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 utilizedThe
ripemd = "0.1.1"
dependency is used incrates/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 declarationsThe native function declarations for
encode
anddecode
are appropriately defined and match the expected signatures.
12-24
: LGTM:test_encode
functionThe
test_encode
function correctly tests theencode
function with different prefixes and data inputs. The assertions accurately verify the expected outcomes.
27-39
: LGTM:test_decode
functionThe
test_decode
function effectively tests thedecode
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 functionsThe signatures for
encode
anddecode
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 referenceThe 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
:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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...
!
in the type prefix if API or client breaking changeReviewers 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...
Summary by CodeRabbit
ripemd
library and adjusted existing dependency constraints.