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

createCommitment & blob to shares #329

Open
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

S1nus
Copy link
Contributor

@S1nus S1nus commented Aug 21, 2024

Overview

solidity implementation of bytesToShares and createCommitment

Summary by CodeRabbit

  • New Features

    • Introduced a new configuration option for file system permissions, enhancing security and operational control.
    • Added a new Solidity file for managing blockchain commitments, facilitating data integrity through Merkle trees.
    • Implemented a testing contract for commitment functionalities, enabling validation of related operations.
    • Introduced a function to check reserved namespaces for improved validation.
  • Bug Fixes

    • Updated visibility of certain functions in libraries, allowing more accessible usage.
  • Chores

    • Minor improvements in code style and organization, including a newline addition for better readability.

@S1nus S1nus requested a review from adlerjohn as a code owner August 21, 2024 02:34
@S1nus S1nus requested a review from rach-id as a code owner August 21, 2024 02:34
Copy link

coderabbitai bot commented Aug 21, 2024

Walkthrough

The recent changes introduce enhancements across multiple files in the project. A new fs_permissions configuration option has been added to foundry.toml, improving file access control. New Solidity functionalities for managing commitments and testing capabilities have been introduced, along with visibility adjustments for certain functions in the BinaryMerkleTree and NamespaceMerkleTree libraries. These updates enhance the framework's capabilities and access management.

Changes

Files Change Summary
foundry.toml Introduced fs_permissions for file system access control (read permission for current directory).
lib/ds-test Updated subproject commit reference. No changes to exported entities.
src/lib/commitment/Commitment.sol Added functions for commitment management using Merkle trees; utility functions for data manipulation.
src/lib/commitment/test/Commitment.t.sol New test contract for commitment functionalities; includes hex conversion functions and test vectors.
src/lib/tree/Types.sol Added isReservedNamespace function to identify reserved namespaces.
src/lib/tree/binary/BinaryMerkleTree.sol Changed function visibility from private to public, enhancing accessibility.
src/lib/tree/binary/TreeHasher.sol Added newline at the end of the file; no functional changes.
src/lib/tree/namespace/NamespaceMerkleTree.sol Changed visibility of several functions, allowing broader access and state modifications.
src/lib/verifier/DAVerifier.sol Modified visibility of verifySharesToDataRootTupleRoot (from view to internal) and adjusted verifyMultiRowRootsToDataRootTupleRootProof to remove pure.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CommitmentSystem
    participant MerkleTree

    User->>CommitmentSystem: Create Commitment
    CommitmentSystem->>MerkleTree: Generate Shares
    MerkleTree-->>CommitmentSystem: Return Shares
    CommitmentSystem-->>User: Commitment Created
Loading

Poem

🐰 In fields of code where bunnies play,
New paths emerge, brightening the day.
With permissions granted, we hop with glee,
Commitments secured, as safe as can be.
So let’s celebrate, with a joyful cheer,
For every change brings us closer, my 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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e88c524 and 4fa90fc.

Files selected for processing (9)
  • foundry.toml (1 hunks)
  • lib/ds-test (1 hunks)
  • src/lib/commitment/Commitment.sol (1 hunks)
  • src/lib/commitment/test/Commitment.t.sol (1 hunks)
  • src/lib/tree/Types.sol (1 hunks)
  • src/lib/tree/binary/BinaryMerkleTree.sol (1 hunks)
  • src/lib/tree/binary/TreeHasher.sol (1 hunks)
  • src/lib/tree/namespace/NamespaceMerkleTree.sol (5 hunks)
  • src/lib/verifier/DAVerifier.sol (2 hunks)
Files skipped from review due to trivial changes (2)
  • lib/ds-test
  • src/lib/tree/binary/TreeHasher.sol
Additional comments not posted (8)
foundry.toml (1)

5-5: LGTM! Addition of fs_permissions is appropriate.

The introduction of file system permissions enhances security and operational control. The configuration is correctly formatted.

src/lib/tree/Types.sol (1)

31-35: LGTM! Verify the constants for reserved namespace range.

The function isReservedNamespace correctly checks if a namespace is reserved using byte comparison.

However, ensure that the constants upper and lower accurately represent the intended reserved range.

Run the following script to verify the constants:

src/lib/tree/binary/BinaryMerkleTree.sol (1)

129-129: Verify the change in function visibility from private to public.

The visibility of the function _computeRootMulti has been changed from private to public. Ensure that this change is intentional and does not expose sensitive functionality.

Run the following script to verify the function usage and ensure the change is intentional:

src/lib/tree/namespace/NamespaceMerkleTree.sol (3)

Line range hint 15-18: Verify necessity of visibility change for verifyProof.

The change from internal pure to internal suggests that the function may now interact with state variables. Verify if this change is necessary and ensure that it aligns with the intended functionality.


Line range hint 20-23: Verify necessity of visibility change for validateProof.

The change from internal pure to internal suggests that the function may now interact with state variables. Verify if this change is necessary and ensure that it aligns with the intended functionality.


253-271: Consider implications of making _computeTreeRoot public.

The change from private pure to public suggests plans for external access. Ensure that this change is necessary and aligns with the intended functionality. Additionally, consider implementing error handling as noted in the TODO comment.

src/lib/verifier/DAVerifier.sol (2)

Line range hint 97-105: Verify necessity of visibility change for verifySharesToDataRootTupleRoot.

The change from view to internal restricts external access and suggests potential state modifications. Verify if this change is necessary and ensure that it aligns with the intended functionality.


Line range hint 124-131: Verify necessity of modifier change for verifyMultiRowRootsToDataRootTupleRootProof.

The change from pure to internal suggests potential state interactions. Verify if this change is necessary and ensure that it aligns with the intended functionality.

Comment on lines 48 to 73
function testBytesToSharesV0() view external {

// test vectors were generated here: https://github.com/S1nus/share-test-vec-gen
string memory path = "./src/lib/commitment/test/testVectors.json";
string memory jsonData = vm.readFile(path);
bytes memory vecsData = vm.parseJson(jsonData);
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));

for (uint i = 0; i < vecs.length; i++) {
bytes29 nsString = bytes29(fromHex(vecs[i].namespace));
Namespace memory ns = toNamespace(nsString);
bytes memory data = fromHex(vecs[i].data);
(bytes[] memory shares, bool err) = _bytesToSharesV0(data, ns);
string memory out = "";
for (uint j = 0; j < shares.length; j++) {
out = string.concat(out, _bytesToHexString(shares[j]));
}
// none of the test vectors should cause an error
//assert(!err);
//assert(compareStrings(out, vecs[i].shares));
if (!compareStrings(out, vecs[i].shares)) {
console.log("expected: ", vecs[i].shares);
console.log("got: ", out);
}
}
}
Copy link

Choose a reason for hiding this comment

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

Uncomment assertions for complete test coverage in testBytesToSharesV0.

The test function testBytesToSharesV0 includes commented-out assertions that should be uncommented to ensure complete test coverage.

Uncomment the following lines to enable assertions:

-    //assert(!err);
-    //assert(compareStrings(out, vecs[i].shares));
+    assert(!err);
+    assert(compareStrings(out, vecs[i].shares));
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function testBytesToSharesV0() view external {
// test vectors were generated here: https://github.com/S1nus/share-test-vec-gen
string memory path = "./src/lib/commitment/test/testVectors.json";
string memory jsonData = vm.readFile(path);
bytes memory vecsData = vm.parseJson(jsonData);
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));
for (uint i = 0; i < vecs.length; i++) {
bytes29 nsString = bytes29(fromHex(vecs[i].namespace));
Namespace memory ns = toNamespace(nsString);
bytes memory data = fromHex(vecs[i].data);
(bytes[] memory shares, bool err) = _bytesToSharesV0(data, ns);
string memory out = "";
for (uint j = 0; j < shares.length; j++) {
out = string.concat(out, _bytesToHexString(shares[j]));
}
// none of the test vectors should cause an error
//assert(!err);
//assert(compareStrings(out, vecs[i].shares));
if (!compareStrings(out, vecs[i].shares)) {
console.log("expected: ", vecs[i].shares);
console.log("got: ", out);
}
}
}
function testBytesToSharesV0() view external {
// test vectors were generated here: https://github.com/S1nus/share-test-vec-gen
string memory path = "./src/lib/commitment/test/testVectors.json";
string memory jsonData = vm.readFile(path);
bytes memory vecsData = vm.parseJson(jsonData);
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));
for (uint i = 0; i < vecs.length; i++) {
bytes29 nsString = bytes29(fromHex(vecs[i].namespace));
Namespace memory ns = toNamespace(nsString);
bytes memory data = fromHex(vecs[i].data);
(bytes[] memory shares, bool err) = _bytesToSharesV0(data, ns);
string memory out = "";
for (uint j = 0; j < shares.length; j++) {
out = string.concat(out, _bytesToHexString(shares[j]));
}
// none of the test vectors should cause an error
assert(!err);
assert(compareStrings(out, vecs[i].shares));
if (!compareStrings(out, vecs[i].shares)) {
console.log("expected: ", vecs[i].shares);
console.log("got: ", out);
}
}
}

Comment on lines 75 to 96
function testCreateCommitmentV0() external {
string memory path = "./src/lib/commitment/test/testVectors2.json";
string memory jsonData = vm.readFile(path);
bytes memory vecsData = vm.parseJson(jsonData);
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));


//for (uint i = 0; i < vecs.length; i++) {
bytes29 nsString = bytes29(fromHex(vecs[0].namespace));
Namespace memory ns = toNamespace(nsString);
bytes memory data = fromHex(vecs[0].data);
(bytes[] memory shares, bool err) = _bytesToSharesV0(data, ns);
bytes32 commitment = _createCommitment(shares, ns);
if (!compareStrings(_bytesToHexString(abi.encodePacked(commitment)), vecs[0].commitment)) {
console.log("Commitment mismatch for vector ", 0);
console.log("expected: ", vecs[0].commitment);
console.log("got: ", _bytesToHexString(abi.encodePacked(commitment)));
return;
}
//}
console.log("All good :)");

Copy link

Choose a reason for hiding this comment

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

Uncomment assertions for complete test coverage in testCreateCommitmentV0.

The test function testCreateCommitmentV0 includes commented-out code that should be uncommented to ensure complete test coverage.

Uncomment the following lines to enable assertions:

-    //for (uint i = 0; i < vecs.length; i++) {
+    for (uint i = 0; i < vecs.length; i++) {
-    //}
+    }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function testCreateCommitmentV0() external {
string memory path = "./src/lib/commitment/test/testVectors2.json";
string memory jsonData = vm.readFile(path);
bytes memory vecsData = vm.parseJson(jsonData);
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));
//for (uint i = 0; i < vecs.length; i++) {
bytes29 nsString = bytes29(fromHex(vecs[0].namespace));
Namespace memory ns = toNamespace(nsString);
bytes memory data = fromHex(vecs[0].data);
(bytes[] memory shares, bool err) = _bytesToSharesV0(data, ns);
bytes32 commitment = _createCommitment(shares, ns);
if (!compareStrings(_bytesToHexString(abi.encodePacked(commitment)), vecs[0].commitment)) {
console.log("Commitment mismatch for vector ", 0);
console.log("expected: ", vecs[0].commitment);
console.log("got: ", _bytesToHexString(abi.encodePacked(commitment)));
return;
}
//}
console.log("All good :)");
function testCreateCommitmentV0() external {
string memory path = "./src/lib/commitment/test/testVectors2.json";
string memory jsonData = vm.readFile(path);
bytes memory vecsData = vm.parseJson(jsonData);
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));
for (uint i = 0; i < vecs.length; i++) {
bytes29 nsString = bytes29(fromHex(vecs[i].namespace));
Namespace memory ns = toNamespace(nsString);
bytes memory data = fromHex(vecs[i].data);
(bytes[] memory shares, bool err) = _bytesToSharesV0(data, ns);
bytes32 commitment = _createCommitment(shares, ns);
if (!compareStrings(_bytesToHexString(abi.encodePacked(commitment)), vecs[i].commitment)) {
console.log("Commitment mismatch for vector ", i);
console.log("expected: ", vecs[i].commitment);
console.log("got: ", _bytesToHexString(abi.encodePacked(commitment)));
return;
}
}
console.log("All good :)");

src/lib/commitment/Commitment.sol Outdated Show resolved Hide resolved
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4fa90fc and 2f40a19.

Files selected for processing (2)
  • src/lib/commitment/test/Commitment.t.sol (1 hunks)
  • src/lib/tree/namespace/NamespaceMerkleTree.sol (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/lib/commitment/test/Commitment.t.sol
  • src/lib/tree/namespace/NamespaceMerkleTree.sol

@S1nus S1nus added the enhancement New feature or request label Aug 23, 2024
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2f40a19 and 6305985.

Files selected for processing (3)
  • src/lib/commitment/Commitment.sol (1 hunks)
  • src/lib/commitment/test/Commitment.t.sol (1 hunks)
  • src/lib/tree/namespace/NamespaceMerkleTree.sol (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • src/lib/commitment/Commitment.sol
  • src/lib/commitment/test/Commitment.t.sol
  • src/lib/tree/namespace/NamespaceMerkleTree.sol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant