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

🧠 Logic: 🔑 implement verify_signature #462

Merged
merged 31 commits into from
Oct 27, 2023
Merged

🧠 Logic: 🔑 implement verify_signature #462

merged 31 commits into from
Oct 27, 2023

Conversation

bdeneux
Copy link
Contributor

@bdeneux bdeneux commented Oct 12, 2023

Implements #458.

Summary by CodeRabbit

  • New Feature: Added cryptographic verification functions eddsa_verify/4 and ecdsa_verify/4 to enhance security.
  • Improvement: Enhanced error messages in TestBech32 for better clarity.
  • Refactor: Reorganized global variables in atom.go for improved readability.
  • New Feature: Introduced TermToBytes function in util.go for flexible term-to-byte conversions.
  • Test: Added new test functions TestXVerify and TestTermToBytes to ensure the reliability of new features.
  • Improvement: Updated DIDComponents function in did.go for more secure and consistent DID string construction.
  • New Feature: Added utility functions in prolog.go for list checking and option retrieval.
  • Test: Added TestGetOption function in prolog_test.go to validate the new utility functions.
  • New Feature: Introduced Map function in slice.go for flexible slice mapping.
  • Improvement: Enhanced did_components function and added relevant test cases for better error handling.
  • New Feature: Introduced util package with cryptographic operations, including signature verification.

@codecov
Copy link

codecov bot commented Oct 12, 2023

Codecov Report

Merging #462 (88afbfc) into main (c5ddb85) will increase coverage by 0.98%.
Report is 11 commits behind head on main.
The diff coverage is 87.80%.

❗ Current head 88afbfc differs from pull request most recent head e046dbf. Consider uploading reports for the commit e046dbf to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #462      +/-   ##
==========================================
+ Coverage   64.94%   65.92%   +0.98%     
==========================================
  Files          62       62              
  Lines        2707     2829     +122     
==========================================
+ Hits         1758     1865     +107     
- Misses        870      881      +11     
- Partials       79       83       +4     
Files Coverage Δ
x/logic/predicate/atom.go 100.00% <ø> (ø)
x/logic/predicate/util.go 100.00% <100.00%> (ø)
x/logic/predicate/crypto.go 76.41% <76.19%> (-0.86%) ⬇️

@bdeneux bdeneux force-pushed the feat/crypto-verify branch from 88afbfc to e046dbf Compare October 12, 2023 09:30
@ccamel ccamel force-pushed the feat/crypto-verify branch from e046dbf to a730bf3 Compare October 18, 2023 09:20
@ccamel ccamel self-assigned this Oct 18, 2023
@ccamel ccamel self-requested a review October 18, 2023 09:58
@ccamel ccamel force-pushed the feat/crypto-verify branch from 69c43bf to 78f8869 Compare October 18, 2023 15:29
@ccamel ccamel force-pushed the feat/crypto-verify branch 3 times, most recently from 0a63358 to 1fb2dba Compare October 21, 2023 08:29
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 21, 2023

Note

Reviews Paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

This pull request introduces significant enhancements to the cryptographic operations, error handling, and test coverage. It also improves code readability and maintainability by reorganizing import statements and defining global variables in a var block. The changes span across multiple files, focusing on the x/logic and starship/tests directories.

Changes

File(s) Summary
starship/tests/setup_test.go, starship/tests/suite.go Reordered and grouped import statements for better readability.
x/logic/interpreter/registry.go Added two new functions, eddsa_verify/4 and ecdsa_verify/4, to the existing registry map.
x/logic/predicate/address_test.go, x/logic/predicate/crypto_test.go, x/logic/predicate/util_test.go, x/logic/util/prolog_test.go Enhanced test coverage with new test functions and cases. Improved error messages for better clarity.
x/logic/predicate/atom.go, x/logic/util/prolog.go Improved readability by defining global variables in a var block and adding new global variables.
x/logic/predicate/crypto.go, x/logic/util/crypto.go Introduced significant enhancements to cryptographic operations, including new functions and a new Alg type.
x/logic/predicate/did.go Improved the construction of the DID string and handled different segment types correctly.
x/logic/predicate/util.go Added new functionality for converting terms to bytes with different encoding options.
x/logic/util/slice.go Introduced a new Map function in the util package.

"In the land of code, where logic is king, 🤴
A rabbit hopped in, with changes to bring. 🐇
Tests were enhanced, imports were neat, 📚
Crypto functions danced to a new beat. 💃
With every hop, the code became bright, 🌞
Celebrate the changes, brought by rabbit's flight!" 🎉


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.json

@ccamel ccamel marked this pull request as ready for review October 21, 2023 08:33
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.

Review Status

Actionable comments generated: 6

Commits Files that changed from the base of the PR and between 14c0114 and 1fb2dba.
Files ignored due to filter (2)
  • go.mod
  • go.sum
Files selected for processing (14)
  • starship/tests/setup_test.go (1 hunks)
  • starship/tests/suite.go (1 hunks)
  • x/logic/interpreter/registry.go (1 hunks)
  • x/logic/predicate/address_test.go (1 hunks)
  • x/logic/predicate/atom.go (1 hunks)
  • x/logic/predicate/crypto.go (3 hunks)
  • x/logic/predicate/crypto_test.go (2 hunks)
  • x/logic/predicate/did.go (1 hunks)
  • x/logic/predicate/util.go (3 hunks)
  • x/logic/predicate/util_test.go (2 hunks)
  • x/logic/util/crypto.go (1 hunks)
  • x/logic/util/prolog.go (2 hunks)
  • x/logic/util/prolog_test.go (1 hunks)
  • x/logic/util/slice.go (1 hunks)
Files skipped from review due to trivial changes (5)
  • starship/tests/setup_test.go
  • starship/tests/suite.go
  • x/logic/interpreter/registry.go
  • x/logic/predicate/address_test.go
  • x/logic/predicate/atom.go
Additional comments (Suppressed): 18
x/logic/util/slice.go (1)
  • 1-10: The Map function is a generic function that applies a given function to each element of a slice and returns a new slice with the results. This is a common utility function that can be used in various scenarios where a transformation needs to be applied to each element of a slice. The function is well-written and follows Go's best practices for writing generic functions. It uses the any type constraint introduced in Go 1.18, which allows it to work with any type that satisfies the empty interface. The function is also efficient as it preallocates the slice that will hold the results, avoiding unnecessary memory allocations.
x/logic/predicate/util_test.go (1)
  • 84-188: The new test function TestTermToBytes is well-structured and covers a wide range of scenarios for the TermToBytes function. It tests different types of terms and options, and checks for both successful conversions and expected errors. The use of table-driven tests makes the code clean and easy to understand. The error messages are clear and informative, which will be helpful for debugging if a test fails. Good job on writing comprehensive tests.
x/logic/predicate/crypto_test.go (2)
  • 103-114: The test case has been updated to reflect the new error message when an integer value in the list is out of byte range. This is a more specific error message than the previous one, which is a good improvement. However, it's important to ensure that the function handling this error can properly handle this new error message.

  • 175-377: The new test function TestXVerify is introduced to test the eddsa_verify and ecdsa_verify functions. The test cases cover various scenarios, including successful verification, wrong message, wrong public key, wrong signature, and unsupported algorithm. This is a comprehensive set of tests that should cover most edge cases. However, it's important to ensure that these tests are run and pass successfully before merging the changes.

x/logic/predicate/util.go (4)
  • 1-7: The import of "encoding/hex" is new in this hunk. It's used for hexadecimal encoding and decoding in the TermToBytes function. This change is consistent with the PR summary.

  • 10-25: The import of "github.com/okp4/okp4d/x/logic/util" is new and is used for the GetOptionWithDefault function in the TermToBytes function. The global variables AtomEncoding, AtomHex, and AtomOctet are also new and are used in the TermToBytes function to specify encoding options. This change is consistent with the PR summary.

  • 80-119: The TermToBytes function is new and is used to convert a term to a byte slice with different encoding options. The function uses the GetOptionWithDefault function from the util package to get the encoding option from the options term. It then checks the type of the encoding and the term and converts the term to a byte slice based on the encoding option. This function is well-structured and handles errors properly. However, it might be a good idea to break this function into smaller functions for better readability and maintainability.

  • 121-142: The ListToBytes function has been updated to include an index variable and error messages that include the position of the invalid term in the list. This change improves error handling and debugging.

x/logic/util/prolog.go (4)
  • 10-16: The introduction of AtomDot and AtomEmpty as global variables is a good practice as it avoids creating new instances of these common atoms every time they are needed. This can improve performance and memory usage.

  • 23-33: The ResolveToAtom function has been modified to return an error instead of a boolean. This is a good practice as it provides more information about what went wrong when the function fails. However, ensure that all calls to this function throughout the codebase have been updated to handle the error.

- func Resolve(env *engine.Env, t engine.Term) (engine.Atom, bool) {
+ func ResolveToAtom(env *engine.Env, t engine.Term) (engine.Atom, error) {
  • 54-57: The IsList function is a useful utility for checking if a compound is a list. It improves readability by encapsulating the logic for checking if a compound is a list.

  • 107-116: The GetOptionWithDefault function is a useful utility for getting an option with a default value. It improves code readability by encapsulating the logic for getting an option and providing a default value if the option is not found.

x/logic/predicate/crypto.go (5)
  • 38-38: The package name for the crypto library has been changed from "crypto" to "cometcrypto". Ensure that this change is intentional and that the new package provides the same functionality as the old one.

  • 99-154: The new functions EdDSAVerify and ECDSAVerify have been added. They provide signature verification functionality for the EdDSA and ECDSA algorithms respectively. Ensure that these functions are tested thoroughly, especially since they deal with cryptographic operations.

  • 156-206: The function xVerify has been added. It provides a generic implementation for signature verification. This function is used by both EdDSAVerify and ECDSAVerify, which pass specific parameters to it. This is a good example of code reuse and modularity.

  • 173-178: The code checks if the provided algorithm is supported. If it's not, an error is returned. This is a good practice as it ensures that only supported algorithms are used.

  • 195-202: The function util.VerifySignature is used to verify the signature. If the verification fails, the function returns engine.Bool(false). This is a good practice as it allows the caller to handle the failure case.

x/logic/util/crypto.go (1)
  • 28-32: The use of recover in a defer block is a good practice to catch any panic that might occur during the execution of VerifySignature. However, it's important to note that this will only catch panics that occur within the same goroutine. If VerifySignature is called in a new goroutine, panics in that goroutine will not be caught by this recover. Ensure that this behavior is intended and understood.

x/logic/predicate/did.go Outdated Show resolved Hide resolved
x/logic/util/prolog_test.go Outdated Show resolved Hide resolved
x/logic/util/prolog.go Outdated Show resolved Hide resolved
x/logic/predicate/crypto.go Show resolved Hide resolved
x/logic/util/crypto.go Outdated Show resolved Hide resolved
x/logic/util/crypto.go Outdated Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 2

Commits Files that changed from the base of the PR and between 1fb2dba and 0f59f1c.
Files selected for processing (2)
  • x/logic/predicate/did.go (3 hunks)
  • x/logic/predicate/did_test.go (1 hunks)
Additional comments (Suppressed): 4
x/logic/predicate/did.go (4)
  • 14-18: The new constant DIDPrefix is introduced to represent the prefix for a DID. This is a good practice as it improves readability and maintainability of the code. If the prefix changes in the future, it can be updated in one place.

  • 37-37: The example for reconstructing a DID from its components has been updated. The null value has been replaced with an underscore (_). Ensure that this change is intentional and aligns with the expected behavior of the did_components function.

  • 71-98: The code for constructing a DID string from its components has been refactored to use a slice of functions (processors). Each function in the slice corresponds to a specific segment of the DID and handles the processing of that segment. This is a good practice as it improves the modularity and readability of the code. However, ensure that the order of the functions in the slice matches the order of the segments in the DID.

  • 112-126: The processSegment function has been introduced to process a segment of a DID. This function takes a function as an argument and applies it to the segment. This is a good practice as it improves the modularity of the code and allows for more flexible processing of the segments. However, ensure that the function passed as an argument is always safe to call and handles all possible values of the segment.

x/logic/predicate/did_test.go Show resolved Hide resolved
x/logic/predicate/did_test.go Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 1

Commits Files that changed from the base of the PR and between 0f59f1c and 3bf1725.
Files selected for processing (2)
  • x/logic/util/prolog.go (2 hunks)
  • x/logic/util/prolog_test.go (1 hunks)
Additional comments (Suppressed): 5
x/logic/util/prolog.go (5)
  • 10-16: The introduction of AtomDot and AtomEmpty as global variables is a good practice as it avoids creating new instances of these common atoms every time they are needed. This can improve performance and memory usage.

  • 23-33: The ResolveToAtom function has been modified to return an error instead of a boolean. This is a good practice as it provides more information about what went wrong when the function fails. However, ensure that all calls to this function throughout the codebase have been updated to handle the error.

  • 54-57: The IsList function is a useful utility for checking if a given compound is a list. It's simple and straightforward.

  • 59-105: The GetOption function is well-implemented. It handles both single options and lists of options, and it provides detailed error messages when things go wrong. However, it might be more efficient to break this function into smaller functions to improve readability and maintainability.

  • 107-116: The GetOptionWithDefault function is a useful utility for getting an option with a default value. It's simple and straightforward.

x/logic/util/prolog_test.go Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 1

Commits Files that changed from the base of the PR and between 3bf1725 and f306246.
Files selected for processing (1)
  • x/logic/util/crypto.go (1 hunks)
Additional comments (Suppressed): 4
x/logic/util/crypto.go (4)
  • 1-10: The import section looks clean and organized. All the necessary packages for cryptographic operations and error handling are imported.

  • 12-18: The Alg type and its String method are well implemented. This provides a clear way to represent the algorithm used for cryptographic operations.

  • 20-24: The constants for the supported algorithms are well defined. This makes it easy to use these algorithms in the code.

  • 48-64: The verifySignatureWithCurve function is well implemented. It correctly handles the unmarshalling of the public key and the verification of the signature. The error handling is also well done, with a clear error message when the public key cannot be parsed.

x/logic/util/crypto.go Outdated Show resolved Hide resolved
@ccamel ccamel force-pushed the feat/crypto-verify branch from f306246 to d74056c Compare October 21, 2023 11:14
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.

Review Status

Actionable comments generated: 2

Commits Files that changed from the base of the PR and between 3bf1725 and d74056c.
Files selected for processing (1)
  • x/logic/util/crypto.go (1 hunks)
Additional comments (Suppressed): 3
x/logic/util/crypto.go (3)
  • 1-10: The import statements are well-organized and only necessary packages are imported. Good job on maintaining clean and efficient code.

  • 12-18: The Alg type and its string representation method are well implemented. This provides a clear way to handle different algorithm types.

  • 20-24: The constants for the different algorithms are well defined and clear. This makes it easy to understand which algorithms are supported.

x/logic/util/crypto.go Show resolved Hide resolved
x/logic/util/crypto.go Outdated Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 1

Commits Files that changed from the base of the PR and between d74056c and 0157f9a.
Files selected for processing (2)
  • x/logic/predicate/crypto_test.go (2 hunks)
  • x/logic/util/crypto.go (1 hunks)
Additional comments (Suppressed): 3
x/logic/util/crypto.go (1)
  • 26-44: The VerifySignature function has been improved to handle the case where the length of the public key is not [PublicKeySize]. This is a good practice as it prevents unexpected panics from occurring due to an invalid public key size. The function now checks the public key size before it's used in the ed25519.Verify function. This change enhances the robustness and reliability of the function.
x/logic/predicate/crypto_test.go (2)
  • 175-377: The new test suite TestXVerify is a comprehensive set of tests for the eddsa_verify and ecdsa_verify functions. It covers various scenarios including valid and invalid inputs, different encoding types, and different cryptographic algorithms. The tests are well-structured and provide clear error messages for failure cases. However, it would be beneficial to add a few more test cases to cover edge cases, such as empty or null inputs, and extremely large inputs. This will ensure the robustness of the functions under test.

  • 103-114: The test cases for the hex_bytes function have been updated to include more detailed error messages. This is a good improvement as it provides more context when a test fails. However, the error message in line 113 seems to be inconsistent with the input data. The error message indicates an issue with the integer value 345 at position 1, but the input data has the string 'hey' at position 2. Please verify if the error message is correct.

x/logic/util/crypto.go Show resolved Hide resolved
@ccamel ccamel requested a review from amimart October 21, 2023 14:06
Copy link
Member

@ccamel ccamel left a comment

Choose a reason for hiding this comment

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

🚀

@bdeneux
Copy link
Contributor Author

bdeneux commented Oct 22, 2023

Thanks @ccamel (and @coderabbitai 😛) for finishing this work 😊.

Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

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

Well done guys! 😉

@axone-protocol axone-protocol deleted a comment from coderabbitai bot Oct 27, 2023
@axone-protocol axone-protocol deleted a comment from coderabbitai bot Oct 27, 2023
@ccamel
Copy link
Member

ccamel commented Oct 27, 2023

@coderabbitai pause

bdeneux and others added 27 commits October 27, 2023 17:23
…mponents

Aligned with Swi-Prolog's behavior for the `uri_components` predicate, `DIDComponents`
now strictly mandates components to be of type atom, introducing a breaking change.
@ccamel ccamel force-pushed the feat/crypto-verify branch from 0157f9a to e84d018 Compare October 27, 2023 15:48
@ccamel ccamel merged commit 7b8bea0 into main Oct 27, 2023
20 checks passed
@ccamel ccamel deleted the feat/crypto-verify branch October 27, 2023 16:14
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