-
Notifications
You must be signed in to change notification settings - Fork 127
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/crypto data hash predicate #474
Conversation
Note Reviews PausedUse the following commands to manage reviews:
WalkthroughThe update introduces a new predicate Changes
Poem
TipsChat with CodeRabbit Bot (
|
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.
Review Status
Actionable comments generated: 9
Configuration used: CodeRabbit UI
Files selected for processing (10)
- docs/predicate/predicates.md (3 hunks)
- x/logic/interpreter/registry.go (2 hunks)
- x/logic/predicate/crypto.go (6 hunks)
- x/logic/predicate/crypto_test.go (4 hunks)
- x/logic/predicate/util.go (3 hunks)
- x/logic/predicate/util_test.go (3 hunks)
- x/logic/util/crypto.go (2 hunks)
- x/logic/util/crypto_enum.go (1 hunks)
- x/logic/util/prolog.go (3 hunks)
- x/logic/util/prolog_test.go (1 hunks)
Additional comments: 24
x/logic/interpreter/registry.go (1)
- 112-112: The new predicate
crypto_data_hash/3
is added to the registry. Ensure that the functionpredicate.CryptoDataHash
is correctly implemented and tested.x/logic/util/crypto_enum.go (1)
- 1-73: The code looks good and follows the best practices for defining and handling enumerated types in Go. The
HashAlg
type is well-defined with constants for MD5, SHA-256, and SHA-512 hash algorithms. The functions for obtaining string representations, checking validity, and parsing strings intoHashAlg
values are correctly implemented. The error handling for invalidHashAlg
values is also appropriate.x/logic/predicate/util_test.go (3)
102-113: The new test case for UTF-8 encoding is a good addition. It ensures that the new encoding option is working as expected.
126-129: The error message for an invalid encoding option has been improved to include a list of possible values. This is a good practice as it provides more information to the user, helping them to correct their mistake.
168-174: The function signature for
TermToBytes
has been updated to include an additional parameter for the encoding type. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.x/logic/util/prolog_test.go (1)
- 33-34: Ensure that the
GetOption
function handles the case where theoption
is an atom and theoptions
is a compound term correctly.x/logic/predicate/util.go (1)
- 18-26: The new encoding options are well defined and documented.
x/logic/util/prolog.go (4)
13-19: The introduction of
AtomEmptyList
is a good way to represent an empty list. Ensure that this new global variable is used consistently throughout the codebase.58-67: The
IsList
function has been updated to handleengine.Atom
types in addition toengine.Compound
types. This change seems to be in line with the goal of handling both types.69-75: The
IsEmptyList
function has been updated to handleengine.Atom
types. This change seems to be in line with the goal of handling both types.100-111: The
GetOption
function now returnsnil
if the resolved term is an empty list. This seems to be a logical change, as an empty list would not contain any options.x/logic/predicate/crypto.go (5)
16-22: The
SHAHash
function is deprecated and replaced byCryptoDataHash
. Ensure all calls toSHAHash
are updated to useCryptoDataHash
instead.52-121: The new
CryptoDataHash
function provides more options for hashing algorithms and data encoding. It's a good improvement over the deprecatedSHAHash
function.211-213: The
EDDSAVerify
function now uses autil.KeyAlg
type instead ofutil.Alg
for specifying cryptographic algorithms. This change should be reflected in all calls toEDDSAVerify
.251-253: The
ECDSAVerify
function now uses autil.KeyAlg
type instead ofutil.Alg
for specifying cryptographic algorithms. This change should be reflected in all calls toECDSAVerify
.307-322: The helper function
getOptionAsAtomWithDefault
is a good addition for handling options and resolving atoms. It improves code readability and maintainability.x/logic/util/crypto.go (5)
1-15: Ensure that all the necessary packages are imported correctly. Also, make sure that the
go-enum
package is installed and working as expected.31-48: The
HashAlg
type andHasher
method are correctly implemented. However, consider adding a default case in the switch statement to handle unsupported hash algorithms.1-54: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [50-66]
The
VerifySignature
function is correctly implemented. However, consider adding a default case in the switch statement to handle unsupported key algorithms.
70-79: The
Hash
function is correctly implemented. It uses theHasher
method to get a hash function for the given algorithm and then uses it to hash the input data.67-83: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [81-96]
The
verifySignatureWithCurve
function is correctly implemented. It verifies the ASN1 signature of the given message with the given public key using the given elliptic curve.x/logic/predicate/crypto_test.go (1)
- 193-193: Ensure that all calls to the deprecated
sha_hash/2
function throughout the codebase have been replaced with the newcrypto_data_hash/3
function.docs/predicate/predicates.md (2)
180-222: The new
crypto_data_hash/3
function is well-documented with clear examples and explanations of the parameters and options. Ensure that all references to the deprecatedsha_hash/2
function in the codebase have been updated to usecrypto_data_hash/3
.432-439: The deprecation notice for
sha_hash/2
is clear and points users to the newcrypto_data_hash/3
function. This is good practice when deprecating functions.
c2ba248
to
b5a8320
Compare
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.
Review Status
Actionable comments generated: 4
Configuration used: CodeRabbit UI
Files selected for processing (11)
- docs/predicate/predicates.md (3 hunks)
- x/logic/interpreter/registry.go (2 hunks)
- x/logic/predicate/chain.go (1 hunks)
- x/logic/predicate/crypto.go (6 hunks)
- x/logic/predicate/crypto_test.go (4 hunks)
- x/logic/predicate/util.go (3 hunks)
- x/logic/predicate/util_test.go (3 hunks)
- x/logic/util/crypto.go (2 hunks)
- x/logic/util/crypto_enum.go (1 hunks)
- x/logic/util/prolog.go (3 hunks)
- x/logic/util/prolog_test.go (1 hunks)
Files skipped from review due to trivial changes (2)
- x/logic/interpreter/registry.go
- x/logic/predicate/chain.go
Additional comments: 24
x/logic/util/crypto_enum.go (1)
- 1-73: The new
HashAlg
enum type and its related functions and variables are well defined. The error handling for invalidHashAlg
values is also properly implemented. The code is clear, maintainable, and follows best practices.x/logic/predicate/util.go (1)
- 18-26: The new atoms for encoding options are well defined and follow the existing pattern.
x/logic/util/prolog_test.go (1)
- 23-31: The test cases where the
options
isnil
or an empty list are redundant as they both represent an empty list. Consider removing one of them to reduce redundancy.- { - option: engine.NewAtom("foo"), - options: nil, - wantResult: nil, - wantError: nil, - },x/logic/util/prolog.go (4)
13-19: The introduction of
AtomEmptyList
is a good way to represent an empty list. This will help in maintaining consistency across the codebase.58-67: The
IsList
function has been updated to handle bothengine.Compound
andengine.Atom
types. This is a good change as it makes the function more versatile.69-75: The
IsEmptyList
function has been updated to handle bothengine.Compound
andengine.Atom
types. This is a good change as it makes the function more versatile.100-105: The
GetOption
function now returnsnil
if the resolved term is an empty list. This is a good change as it makes the function more robust and prevents unnecessary processing of empty lists.x/logic/predicate/util_test.go (3)
102-113: The new test case for UTF-8 encoding is correctly implemented and should cover the new functionality adequately.
126-129: The error message for an invalid encoding option has been updated to include the new "utf8" option. This is a good practice as it provides more information to the user about the possible values.
168-174: The
TermToBytes
function signature has been correctly updated to include the newdefaultValue
parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.x/logic/util/crypto.go (5)
1-1: The
go:generate
directive is used to generate enum string methods. Ensure that thego-enum
tool is installed and used correctly.31-48: The
HashAlg
type andHasher
method are introduced to support multiple hash algorithms. This is a good practice as it allows for easy extension of supported algorithms in the future.1-54: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [50-66]
The
VerifySignature
function now accepts aKeyAlg
type instead ofAlg
. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.
70-79: The
Hash
function is introduced to hash data using the given algorithm. This is a good practice as it allows for easy extension of supported algorithms in the future.67-83: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [81-95]
The
verifySignatureWithCurve
function is used to verify the ASN1 signature of a message with a given public key using a specified elliptic curve. This is a good practice as it allows for easy extension of supported algorithms in the future.docs/predicate/predicates.md (3)
163-178: The signature and example for
chain_id/1
have been updated. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.180-222: The new
crypto_data_hash/3
predicate has been introduced with examples. This function is intended to replace the deprecatedsha_hash/2
function. Ensure that all calls tosha_hash/2
have been replaced withcrypto_data_hash/3
throughout the codebase.432-439: The
sha_hash/2
function has been deprecated. Ensure that there are no remaining calls to this function in the codebase.x/logic/predicate/crypto.go (4)
36-50: The deprecated
SHAHash
function only supportsengine.Atom
type for data. The newCryptoDataHash
function should handle more types for better versatility.209-213: The
EDDSAVerify
function has been updated to use a new typeutil.KeyAlg
instead ofutil.Alg
. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.249-253: The
ECDSAVerify
function has been updated to use a new typeutil.KeyAlg
instead ofutil.Alg
. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.307-322: The helper function
getOptionAsAtomWithDefault
has been added. This function retrieves the value of the first option with the given name in the given options. If the option is not found, it returns a default value. This function improves code readability and reusability.x/logic/predicate/crypto_test.go (2)
23-23: The function name has been updated to reflect the broader scope of the tests. Ensure that all references to this function have been updated accordingly.
193-193: The new
crypto_data_hash
function has been registered for use in the tests. Ensure that the function is correctly implemented and that it behaves as expected in all scenarios.
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.
That's better 😉
Maybe we can directly remove the sha_hash/2
predicate as it is not used at this point?
Oh yes sure. we can remove it. Want to take care of it 😛 |
If it's ok for you you can merge :) |
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.
Review Status
Actionable comments generated: 1
Configuration used: CodeRabbit UI
Files selected for processing (3)
- x/logic/interpreter/registry.go (1 hunks)
- x/logic/predicate/crypto.go (5 hunks)
- x/logic/predicate/crypto_test.go (3 hunks)
Additional comments: 14
x/logic/interpreter/registry.go (1)
- 109-115: The registry map has been updated to include the new
crypto_data_hash/3
predicate and remove the deprecatedsha_hash/2
. Ensure that all references to the oldsha_hash/2
predicate are updated to use the newcrypto_data_hash/3
predicate. Additionally, verify that the new predicate is implemented correctly and that all necessary unit tests are in place to validate its functionality.x/logic/predicate/crypto_test.go (5)
23-24: Renaming the test function to
TestCryptoOperations
reflects the broader scope of the cryptographic operations being tested. This is a good practice as it makes the purpose of the test clearer.30-140: The test cases have been updated to include a
wantSuccess
boolean. This is a good practice as it explicitly states the expected outcome of the test, making the test cases easier to understand and maintain.33-37, 38-43, ...:
The test cases forhex_bytes
predicate are well-structured and cover various scenarios, including success and error cases. This thorough testing ensures that the predicate behaves as expected in different situations.
120-122: The test case for an invalid algorithm in
crypto_data_hash
is important for ensuring that the function handles incorrect inputs gracefully and provides a clear error message.148-153: The registration of the new
crypto_data_hash
predicate in the Prolog interpreter is necessary for the test cases to use it. This change is consistent with the introduction of the new predicate.147-153: It's important to verify that the
Compile
method does not return an error before proceeding with the tests. This ensures that the Prolog program is syntactically correct and can be executed.Convey blocks (nested):
The use of nestedConvey
blocks provides a clear structure for the tests, allowing for setup, execution, and assertions to be grouped logically. This makes the tests easier to follow and maintain.Error handling in Convey blocks:
The error handling within theConvey
blocks is consistent and thorough, checking for both the presence and absence of errors as appropriate, and comparing error messages when errors are expected.Overall, the test cases are well-structured, cover a wide range of scenarios, and follow good practices for error handling and assertions. The changes made in the pull request are consistent with the introduction of the new
crypto_data_hash
predicate and the deprecation ofsha_hash
. The tests are likely to be effective in catching regressions and ensuring the correctness of the cryptographic predicates.x/logic/predicate/crypto.go (8)
60-60: The
CryptoDataHash
function is using aDelay
function to return a promise. This is a common pattern in Prolog implementations to handle computations that should be executed later. Ensure that the delayed execution context is properly managed and that it does not lead to any race conditions or state inconsistencies.61-61: The
getOptionAsAtomWithDefault
function is used to retrieve the algorithm option with a default value of "sha256". This is a good use of a helper function to handle default values and option parsing, which can improve code maintainability.72-72: The
TermToBytes
function is called withAtomUtf8
as a parameter, butAtomUtf8
is not defined within the provided code snippet. Ensure thatAtomUtf8
is properly defined and accessible within this context.175-175: The
EDDSAVerify
function is a wrapper around a more genericxVerify
function, passing in specific parameters for the EdDSA algorithm. This is a good example of code reuse and maintaining a clean API surface.---end hunk 1---
---start hunk 2---
- 215-215: Similar to the
EDDSAVerify
function,ECDSAVerify
is also a wrapper around thexVerify
function. It's good to see consistency in how these verification functions are implemented.---end hunk 2---
---start hunk 3---
235-240: The use of
slices.IndexFunc
and the subsequent error handling is a good way to validate the algorithm type against a list of supported algorithms. This ensures that only valid algorithms are used for the verification process.242-255: The
TermToBytes
function is used multiple times with different encoding options. It's important to ensure that theTermToBytes
function is robust and handles all possible encodings correctly. Additionally, the error messages are clear and provide sufficient information for debugging.---end hunk 3---
---start hunk 4---
- 270-285: The
getOptionAsAtomWithDefault
helper function is well-implemented, providing a clear and reusable way to handle options with default values. The error handling is consistent and will provide useful information in case of failure.---end hunk 4---
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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.
Review Status
Actionable comments generated: 3
Configuration used: CodeRabbit UI
Files selected for processing (1)
- x/logic/predicate/crypto.go (5 hunks)
Additional comments: 8
x/logic/predicate/crypto.go (8)
55-56: The function signature for
CryptoDataHash
has been updated to include anoptions
parameter, which allows for specifying the hashing algorithm and data encoding. This is a significant change that should be clearly documented and communicated to users, as it may require updates to existing code that uses this function.61-70: The error handling within
CryptoDataHash
is robust, providing clear error messages when an invalid algorithm is specified. This is good practice as it aids in debugging and user comprehension when errors occur.72-74: The
TermToBytes
function is used to decode the data term based on the specified encoding options. It's important to ensure that this function handles all possible encodings correctly and that it's been updated to support the new "utf8" encoding option mentioned in the pull request summary.82-82: The
BytesToList
function is used to convert the result of the hashing operation into a Prolog list. It's important to ensure that this conversion is done correctly and efficiently, as improper handling of data types can lead to subtle bugs.---end hunk 0---
---start hunk 1---
- 174-175: The
EDDSAVerify
function now uses a newutil.KeyAlg
type for specifying cryptographic algorithms. This change should be verified for compatibility with existing code and the correct functioning of the verification process.---end hunk 1---
---start hunk 2---
- 214-215: The
ECDSAVerify
function has been updated similarly toEDDSAVerify
, using the newutil.KeyAlg
type. As withEDDSAVerify
, it's important to verify that this change integrates well with the rest of the system and that the verification process works as expected with the new type.---end hunk 2---
---start hunk 3---
242-255: The
TermToBytes
function is used multiple times to decode different terms (key, data, signature) based on the specified encoding options. As with theCryptoDataHash
function, it's important to ensure that this function is robust and handles all specified encodings correctly.257-260: The
util.VerifySignature
function is called to perform the actual signature verification. It's important to ensure that this function is implemented correctly and securely, as flaws in signature verification can lead to serious security vulnerabilities.---end hunk 3---
---start hunk 4---
@coderabbitai pause |
Introduces the
crypto_data_hash/3
predicate, which closely looks like the one provided by swiprolog, with current support forMD5
(for convenience),SHA-256
andSHA-512
. This predicate offers more versatility (in terms of algorithm and encoding) compared to thenow-deprecatednow-removedsha_hash/2
predicate.Additionally, this PR addresses a bug related to parsing options for the empty list (
[]
), correcting the issue where the empty list was treated as a compound term, instead of an atom.--
Note: The code for the
HashAlg
enum type has been generated with abice/go-enum.Summary by CodeRabbit
New Features
crypto_data_hash/3
predicate for enhanced cryptographic hashing with multiple algorithms and encoding options.chain_id/1
predicate signature tochain_id(?ID)
for improved clarity.Improvements
sha_hash/2
predicate in favor of the more versatilecrypto_data_hash/3
.util.KeyAlg
type for specifying cryptographic algorithms.Bug Fixes
Documentation
crypto_data_hash/3
predicate.Refactor
SHAHash
toCryptoDataHash
to align with new functionality.Tests
crypto_data_hash
and UTF-8 encoding.