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

chore: Encapsulate all kzg functionality for PeerDAS into the kzg package #14136

Merged
merged 12 commits into from
Jul 3, 2024

Conversation

kevaundray
Copy link
Contributor

@kevaundray kevaundray commented Jun 24, 2024

What type of PR is this?

Other (refactor)

What does this PR do? Why is it needed?

This PR removes ckzg4844 from the public API and puts it behind a kzg API. It means that the rest of the code becomes more modular as it is not tied to the implementation details of a particular cryptographic library.

This could also be done for Go4844, though I left it as is since that would need to be done on the develop branch.

Status

This PR still needs a bit of cleanup, in some places we accept Bytes48 and in others Commitment for example. Putting it up to get thoughts on this direction. I've also put everything in validation.go for now

@CLAassistant
Copy link

CLAassistant commented Jun 24, 2024

CLA assistant check
All committers have signed the CLA.

Comment on lines 69 to 74
recoveredCells, err := cKzg4844.RecoverAllCells(cellsId, cKzgCells)
recoveredCells, err := kzg.RecoverAllCells(cellsId, cKzgCells)
if err != nil {
return nil, errors.Wrapf(err, "recover all cells for blob %d", blobIndex)
}

recoveredBlob, err := cKzg4844.CellsToBlob(recoveredCells)
recoveredBlob, err := kzg.CellsToBlob(recoveredCells)
Copy link
Contributor Author

@kevaundray kevaundray Jun 24, 2024

Choose a reason for hiding this comment

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

The usecase for calling these two methods is to eventually compute the CellsAndKZGProofs -- I would recommend adding a RecoverCellsAndComputeKZGProofs method which does this, so that when libraries are updated there is not much that needs to be modified.

Pseudocode to clarify

func RecoverCellsAndKZGProofs(cells []Cell) ([]Cells, []Proofs, error) {
  recoveredCells, err := ckzg.RecoverAllCells(cellsIds, cells) // This is no longer called directly in beacon-chain
  recoveredBlob, err := ckzg.CellsToBlob(recoveredCells) // This is no longer called directly in beacon-chain
  cells, proofs, err := ckzg.ComputeCellsAndProofs(recoveredBlob)
  return cells, proofs, err
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is addressed in #14160

@kevaundray kevaundray marked this pull request as ready for review June 25, 2024 13:32
@kevaundray kevaundray requested a review from a team as a code owner June 25, 2024 13:32
@kevaundray kevaundray requested review from kasey, nalepae and rauljordan and removed request for a team June 25, 2024 13:32
@kevaundray
Copy link
Contributor Author

Setting it as ready to get feedback, before polishing it

@nalepae
Copy link
Contributor

nalepae commented Jun 27, 2024

In general, blob, cells etc... are quite heavy.
Instead of returning a Blob or Cell object, it's better to return a pointer to these objects.
This way, no copy of these objects are creted.

@nalepae
Copy link
Contributor

nalepae commented Jun 27, 2024

Setting it as ready to get feedback, before polishing it
Thanks a lot for your PR! Please go ahead with polishing :)

@nalepae
Copy link
Contributor

nalepae commented Jun 27, 2024

You need to run gazelle:

bazel run //:gazelle -- fix

@kevaundray
Copy link
Contributor Author

Thanks for the review! I'll clean up the PR since it seems you are fine with the general PR.

RE having pointers to large objects, I agree -- In this PR, I was aiming to solely apply the abstraction layer with little modifications to the other code, and then apply modify the abstraction layer to do things like not pass around large objects and sloely deprecate some functions in another PR. Let me know what you think of this.

@kevaundray
Copy link
Contributor Author

Note: Had to manually modify the build.Bazel in fdedf2d

Comment on lines 124 to 139
func RecoverCellsAndKZGProofs(cellIds []uint64, _cells []Cell) ([ckzg4844.CellsPerExtBlob]Cell, [ckzg4844.CellsPerExtBlob]Proof, error) {
// First recover all of the cells
recoveredCells, err := RecoverAllCells(cellIds, _cells)
if err != nil {
return [ckzg4844.CellsPerExtBlob]Cell{}, [ckzg4844.CellsPerExtBlob]Proof{}, err
}

// Extract the Blob from all of the Cells
blob, err := CellsToBlob(recoveredCells)
if err != nil {
return [ckzg4844.CellsPerExtBlob]Cell{}, [ckzg4844.CellsPerExtBlob]Proof{}, err
}

// Compute all of the cells and KZG proofs
return ComputeCellsAndKZGProofs(&blob)
}
Copy link
Contributor Author

@kevaundray kevaundray Jun 28, 2024

Choose a reason for hiding this comment

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

This method is not being used -- the most recent commit of the crypto libraries will no longer expose RecoverAllCells and CellsToBlob, so I've added this method tentatively, so that we can modify the codebase preemptively in anticipation for that breaking change

@kevaundray kevaundray force-pushed the kw/crypto-refactor branch from f87b994 to d49d0e6 Compare June 28, 2024 19:03
@kevaundray
Copy link
Contributor Author

I have pulled out the last deepsource error into #14162 since it is not related to any changes to kzg/peerdas

@nalepae
Copy link
Contributor

nalepae commented Jul 3, 2024

bazel run //:gazelle -- fix

needs to be run.

EDIT: I did not pull.

@nalepae
Copy link
Contributor

nalepae commented Jul 3, 2024

After running gazelle, there is some build errors:

bazel build //cmd/beacon-chain

...
beacon-chain/blockchain/kzg/validation.go:93:12: nil dereference in index operation (nilness)
beacon-chain/blockchain/kzg/validation.go:103:12: nil dereference in index operation (nilness)
beacon-chain/blockchain/kzg/validation.go:106:59: Expression is already a slice. (slicedirect)

EDIT: I did not pull.

@nalepae nalepae merged commit b8f43c0 into prysmaticlabs:peerDAS Jul 3, 2024
13 of 16 checks passed
nalepae pushed a commit that referenced this pull request Jul 17, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Jul 17, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nisdas pushed a commit that referenced this pull request Jul 18, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Jul 18, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Jul 29, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nisdas pushed a commit that referenced this pull request Aug 14, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nisdas pushed a commit that referenced this pull request Aug 15, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nisdas pushed a commit that referenced this pull request Aug 20, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Aug 27, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Aug 28, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Aug 28, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Oct 7, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Oct 7, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Oct 8, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Oct 23, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Oct 24, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Nov 20, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Nov 22, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Nov 22, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Nov 25, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Nov 25, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
nalepae pushed a commit that referenced this pull request Nov 27, 2024
…kage (#14136)

* chore: move all ckzg related functionality into kzg package

* refactor code to match

* run: bazel run //:gazelle -- fix

* chore: add some docs and stop copying large objects when converting between types

* fixes

* manually add kzg.go dep to Build.Hazel

* move kzg methods to kzg.go

* chore: add RecoverCellsAndProofs method

* bazel run //:gazelle -- fix

* use BytesPerBlob constant

* chore: fix some deepsource issues

* one declaration for commans and blobs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants