Skip to content

fix: codecv7 challenge digest #50

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

Merged
merged 5 commits into from
Apr 1, 2025
Merged

Conversation

colinlyguo
Copy link
Member

@colinlyguo colinlyguo commented Mar 27, 2025

Purpose or design rationale of this PR

PR title

Your PR title must follow conventional commits (as we are doing squash merge for each PR), so it must start with one of the following types:

  • fix: A bug fix

Breaking change label

Does this PR have the breaking-change label?

  • No, this PR is not a breaking change

Summary by CodeRabbit

  • New Features

    • Introduced a cryptographic challenge digest into data batch creation and blob payload generation, enhancing integrity checks and data proof validation.
  • Refactor

    • Streamlined methods and interfaces across multiple encoding versions to consistently incorporate and expose the new challenge digest for improved reliability.
  • Bug Fixes

    • Updated error handling to ensure consistent return values, including the challenge digest, across various methods.

Copy link

coderabbitai bot commented Mar 27, 2025

Walkthrough

The changes update multiple codec versions to include a new challengeDigest in blob construction and batch creation procedures. Method signatures for functions such as constructBlob, constructBlobPayload, and corresponding test cases across codec versions (v0, v2, v3, v4, and v7) have been updated. In addition, the changes add a new field and related method (ChallengeDigest) to several batch structs and the DABatch interface, ensuring that the challenge digest is generated, passed, and retrievable throughout the creation and validation processes.

Changes

File(s) Summary
encoding/codecv7.go, encoding/codecv7_test.go, encoding/codecv7_types.go Updated DACodecV7 methods to have constructBlob return an additional challengeDigest. Modified NewDABatch to use the new value and adjusted error handling. Added a challengeDigest field and a ChallengeDigest method to daBatchV7.
encoding/interfaces.go, encoding/codecv0_types.go Added a new method ChallengeDigest() common.Hash to the DABatch interface and implemented it in daBatchV0 (returning an empty hash), expanding the interface for challenge digest retrieval.
encoding/codecv2.go, encoding/codecv2_test.go Revised constructBlobPayload in DACodecV2 to include and return a challengeDigest. Adjusted corresponding calls and tests to capture the new extra return value without altering the existing error handling.
encoding/codecv3.go, encoding/codecv3_test.go, encoding/codecv3_types.go Enhanced DACodecV3 methods (NewDABatch and NewDABatchFromBytes) to incorporate the challengeDigest from blob payload construction. Updated constructors for batch objects (including daBatchV3) to require and assign the challenge digest, and added a corresponding retrieval method.
encoding/codecv4.go, encoding/codecv4_test.go Modified DACodecV4 methods including constructBlobPayload, NewDABatch, and NewDABatchFromBytes to handle a challengeDigest parameter and return value. Adjusted error return values in error cases to consistently output common.Hash{} and updated tests accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Codec as DACodec (vX)
    participant BlobFunc as constructBlob/constructBlobPayload
    participant BatchMgr as newDABatch / Batch Constructor

    Client->>Codec: NewDABatch(batch)
    Codec->>BlobFunc: Construct blob (includes challengeDigest)
    BlobFunc-->>Codec: (blob, blobVersionedHash, ..., challengeDigest)
    Codec->>BatchMgr: Create new DABatch(..., challengeDigest)
    BatchMgr-->>Codec: DABatch instance with challengeDigest field
    Codec-->>Client: Return DABatch object
Loading

Possibly related PRs

Suggested reviewers

  • Thegaram
  • jonastheis

Poem

I'm a rabbit, quick on my feet,
Hopping through code with a rhythmic beat.
ChallengeDigest now joins the dance,
In every blob, it finds its chance.
With hashes and bytes, our work's complete!
🐇💻


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4d1233d and 5f1aec7.

📒 Files selected for processing (2)
  • encoding/codecv7.go (4 hunks)
  • encoding/da.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • encoding/da.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • encoding/codecv7.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: tests

🪧 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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: 0

🧹 Nitpick comments (2)
encoding/codecv1.go (1)

460-475: Implementation of ChallengeDigestFromBlobBytes in DACodecV1

The implementation correctly:

  1. Converts blob bytes to canonical form
  2. Creates a commitment from the blob
  3. Calculates a versioned hash
  4. Computes and returns the challenge digest

Unlike V7, this implementation doesn't perform length validation or padding before processing. Consider adding similar validation for consistency across versions.

func (d *DACodecV1) ChallengeDigestFromBlobBytes(blobBytes []byte) (common.Hash, error) {
+	if len(blobBytes) > maxEffectiveBlobBytes {
+		return common.Hash{}, fmt.Errorf("blobBytes length exceeds %d bytes, got %d bytes", maxEffectiveBlobBytes, len(blobBytes))
+	}
+
+	paddedBlobBytes := make([]byte, maxEffectiveBlobBytes)
+	copy(paddedBlobBytes, blobBytes)
+
	blob, err := makeBlobCanonical(blobBytes)
	if err != nil {
		return common.Hash{}, fmt.Errorf("failed to convert blobBytes to canonical form: %w", err)
	}

	c, err := kzg4844.BlobToCommitment(blob)
	if err != nil {
		return common.Hash{}, fmt.Errorf("failed to create blob commitment: %w", err)
	}
	blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &c)

-	challengeDigest := crypto.Keccak256Hash(crypto.Keccak256(blobBytes), blobVersionedHash[:])
+	challengeDigest := crypto.Keccak256Hash(crypto.Keccak256(paddedBlobBytes), blobVersionedHash[:])
	return challengeDigest, nil
}
encoding/codecv7.go (1)

368-390: Added ChallengeDigestFromBlobBytes implementation.

The new method computes a challenge digest from blob bytes by:

  1. Validating the blob size
  2. Converting the bytes to canonical form
  3. Generating a KZG commitment
  4. Calculating a versioned hash
  5. Applying padding to standardize the input size
  6. Computing the final digest with Keccak256 hashing

This implementation properly handles size validation, canonical conversions, and padding, ensuring consistent digest calculation.

Consider adding a more detailed comment explaining the purpose of this method and how it relates to the overall data availability system. A brief explanation of why padding is necessary and how the challenge digest is used would improve maintainability.

-// ChallengeDigestFromBlobBytes calculates the challenge digest from the given blob bytes.
+// ChallengeDigestFromBlobBytes calculates the challenge digest from the given blob bytes.
+// This method:
+// 1. Validates that the blob doesn't exceed the maximum allowed size
+// 2. Converts the blob bytes to canonical form for KZG operations
+// 3. Generates a KZG commitment and versioned hash
+// 4. Pads the blob to a fixed size to ensure consistent hashing
+// 5. Returns the Keccak256 hash of the padded blob and versioned hash
+//
+// The challenge digest is used for verifying data availability proofs and
+// ensuring the integrity of blob data across the system.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 344f2d5 and 69be2d2.

📒 Files selected for processing (6)
  • encoding/codecv0.go (1 hunks)
  • encoding/codecv1.go (1 hunks)
  • encoding/codecv7.go (2 hunks)
  • encoding/codecv7_test.go (6 hunks)
  • encoding/codecv7_types.go (1 hunks)
  • encoding/interfaces.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: tests
🔇 Additional comments (10)
encoding/codecv0.go (1)

433-437: Implementation of ChallengeDigestFromBlobBytes in DACodecV0

The function is correctly stubbed out in this legacy codec version, returning an empty hash and nil error. This maintains interface compatibility while acknowledging that V0 doesn't need to implement actual challenge digest calculation.

encoding/interfaces.go (1)

77-78: Interface extension with ChallengeDigestFromBlobBytes

Good addition of the ChallengeDigestFromBlobBytes method to the Codec interface. This method enables blob data processing across all codec versions, with a clean signature that matches the pattern of other methods in the interface.

encoding/codecv7_types.go (1)

141-149: Improved error handling and padding for blob data processing

The changes enhance robustness by:

  1. Adding length validation to prevent processing oversized blobs
  2. Creating a padded byte slice of fixed size (maxEffectiveBlobBytes)
  3. Ensuring consistent input size for hash computation

This is a good improvement that helps prevent potential issues with variable-sized inputs.

encoding/codecv7_test.go (6)

825-825: Updated test expectations for blob data proof.

The expected blob data proof for this test case has been updated to reflect the fix in the challenge digest calculation in the DACodecV7 implementation.


836-836: Updated test expectations for blob data proof.

The expected blob data proof for this test case has been updated to match the corrected challenge digest calculation.


847-847: Updated test expectations for blob data proof.

The expected blob data proof has been correctly updated to align with the fixed challenge digest calculation logic.


858-858: Updated test expectations for blob data proof.

The expected blob data proof for this test case has been properly updated to reflect the challenge digest fix.


869-869: Updated test expectations for blob data proof.

The expected blob data proof for the multi-block test case has been updated to match the fixed challenge digest calculation.


880-880: Updated test expectations for blob data proof.

The expected blob data proof for this complex multi-block test case has been updated to reflect the corrected challenge digest algorithm.

encoding/codecv7.go (1)

13-13: Added necessary crypto package import.

Added the crypto package import, which is required for the Keccak256Hash and Keccak256 functions used in the new ChallengeDigestFromBlobBytes method.

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

🧹 Nitpick comments (3)
encoding/codecv0.go (1)

434-437: Add parameter name for documentation clarity

The parameter name is missing, which makes it unclear what the expected input is. Consider naming the parameter for better code documentation and readability.

-func (d *DACodecV0) BlobDataProofFromBlobBytes([]byte) (common.Hash, kzg4844.Commitment, kzg4844.Proof, error) {
+func (d *DACodecV0) BlobDataProofFromBlobBytes(blobBytes []byte) (common.Hash, kzg4844.Commitment, kzg4844.Proof, error) {
	return common.Hash{}, kzg4844.Commitment{}, kzg4844.Proof{}, nil
}
encoding/codecv1.go (2)

462-494: Add input validation for blob bytes

The function doesn't validate the input blob bytes before processing, which could lead to unexpected behavior if the data doesn't meet size or format requirements.

func (d *DACodecV1) BlobDataProofFromBlobBytes(blobBytes []byte) (common.Hash, kzg4844.Commitment, kzg4844.Proof, error) {
+	// Validate input
+	if len(blobBytes) == 0 {
+		return common.Hash{}, kzg4844.Commitment{}, kzg4844.Proof{}, fmt.Errorf("empty blob bytes")
+	}
+	
	blob, err := makeBlobCanonical(blobBytes)
	if err != nil {
		return common.Hash{}, kzg4844.Commitment{}, kzg4844.Proof{}, fmt.Errorf("failed to convert blobBytes to canonical form: %w", err)
	}

490-490: Simplify error message formatting

The error message includes hex encoding of the z value which adds an import dependency only used in this error message. Unless this is critical for debugging, consider simplifying.

-		return common.Hash{}, kzg4844.Commitment{}, kzg4844.Proof{}, fmt.Errorf("failed to create KZG proof at point, err: %w, z: %v", err, hex.EncodeToString(z[:]))
+		return common.Hash{}, kzg4844.Commitment{}, kzg4844.Proof{}, fmt.Errorf("failed to create KZG proof at point: %w", err)

If the hex encoding is truly needed for debugging, consider using a logging approach rather than including it in the error message.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 69be2d2 and cd3e572.

📒 Files selected for processing (4)
  • encoding/codecv0.go (1 hunks)
  • encoding/codecv1.go (2 hunks)
  • encoding/codecv7.go (2 hunks)
  • encoding/interfaces.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • encoding/interfaces.go
  • encoding/codecv7.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: tests

roynalnaruto
roynalnaruto previously approved these changes Mar 27, 2025
@roynalnaruto roynalnaruto self-requested a review March 28, 2025 08:09
georgehao
georgehao previously approved these changes Mar 31, 2025
Copy link
Contributor

@jonastheis jonastheis left a comment

Choose a reason for hiding this comment

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

would also be good to add a check for the challengeDigest to the tests at least for CodecV7

@colinlyguo
Copy link
Member Author

would also be good to add a check for the challengeDigest to the tests at least for CodecV7

good suggestion. @roynalnaruto could you provide a test case here?

@colinlyguo colinlyguo merged commit 9f9f538 into main Apr 1, 2025
4 checks passed
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.

4 participants